Single Gpu parameters to Multi Gpu

I want to load the model trained on single GPU previously to multi-Gpu now, but got an error:

KeyError: ‘unexpected key “conv1.weight” in state_dict’

what could be the problem?

this is my code:

net = Net()
#using Gpus
if torch.cuda.is_available():
    print("cuda is available...")
    if torch.cuda.device_count() > 1:
        print('using %d Gpus'%torch.cuda.device_count())
        net=nn.DataParallel(net)
    net.cuda()
else:
    print('cuda disabled')
print(net)
save_path='/gdata/0510nnoptim_withTV_0.1.pt'
param_path='/gdata/0507nnoptim_withTV_1.pt'
if param_path is None:
    net.apply(weights_init)
    print('random initialize parameters')
else:
    net.load_state_dict(torch.load(param_path))
    print('load parameters from: ', param_path)

you are trying to load a state_dict() of a model that trained on a single gpu to a multi-gpu with nn.DataParallel() i see.
Every model that is trained with nn.DataParallel() a .module is added to it so the weights also contains .module
for example if you have fc layer in your model, a model that is trained with nn.DataParallel you will be able to access it using model.module.fc and a model that is not trained with nn.DataParallel you will be able to access it using model.fc

simply load to weights of the model before wrapping it with nn.DataParallel

1 Like