How to transfer weight/bias parameters from one (architecture A) to another (architecture A')?

There is a network that has completed training with VGG16. It was saved using torch.save.
e.g.

        state = {
            'net': net.module if use_cuda else net,
            'acc': acc,
        }
        if not os.path.isdir('checkpoint'):
            os.mkdir('checkpoint')
        torch.save(state, './checkpoint/ckpt_20190312_cifar10_vgg16.t7')
        best_acc = acc

I would like to implement VGG16_v2 with the same structure as VGG16 and using other convolution operations.

At this time, I want to import the weight / bias values of the pretrained VGG16.

Once I have implemented the following:

checkpoint = torch.load('./checkpoint/ckpt_20190312_cifar10_vgg16.t7') # Load your own neural network models
net = checkpoint['net']

model = VGG16_v2.VGG()

model.load_state_dict(net, strict=False)

When I implemented this, I got the following error.

Traceback (most recent call last):
File “main_20190325_cifar10_vgg16_alpha_training_v1.py”, line 75, in
model.load_state_dict(net, strict=False)
File “/home/mhha/.conda/envs/pytorchmh/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 695, in load_state_dict
state_dict = state_dict.copy()
File “/home/mhha/.conda/envs/pytorchmh/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 532, in getattr
type(self).name, name))
AttributeError: ‘VGG’ object has no attribute ‘copy’

How can I solve this problem?

This seems like it’s related to the way you saved the model, rather than the changes in the architecture.

Instead of doing model.load_state_dict(net), try model.load_state_dict(net.state_dict)! The state_dict of the model is the weights and biases of the network, whereas the model itself is the entire object, with the architecture, weights and other elements included.

Unfortunately, it doesn’t work…

Is there any method that is not using torch.load??

Haha, using state_dict(), it works!

1 Like

Sorry about that, I forgot the parentheses :sweat_smile: glad it works!

Your solution helps a lot!!

Thanks!