I’ve been trying to reproduce to results found on this paper, but i can’t get to code to work.
https://github.com/Tangsenghenshou/Detecting-Generated-Images-by-Real-Images
Uses code from:https://github.com/PeterWang512/CNNDetection
I need to copy the weights of a pre-trained ResNet50, and put them into a modified ResNet50 with 5 channels in order to do fine tuning. The code comes with a section made just to do that, but when i try to train the model i get a huge size mismatch error on most weights.
Part of traceback:
RuntimeError: Error(s) in loading state_dict for ResNet:
size mismatch for bn1.num_batches_tracked: copying a param with shape torch.Size([64, 64, 1, 1]) from checkpoint, the shape in current model is torch.Size([]).
size mismatch for layer1.0.conv1.weight: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([64, 64, 1, 1]).
size mismatch for layer1.0.bn1.running_var: copying a param with shape torch.Size([64, 64, 3, 3]) from checkpoint, the shape in current model is torch.Size([64]).
size mismatch for layer1.0.conv2.weight: copying a param with shape torch.Size([64]) from checkpoint, the shape in current model is torch.Size([64, 64, 3, 3]).
size mismatch for layer1.0.bn2.running_mean: copying a param with shape torch.Size([256, 64, 1, 1]) from checkpoint, the shape in current model is torch.Size([64]).
size mismatch for layer1.0.bn2.running_var: copying a param with shape torch.Size([256]) from checkpoint, the shape in current model is torch.Size([64]).
size mismatch for layer1.0.conv3.weight: copying a param with shape torch.Size([256]) from checkpoint, the shape in current model is torch.Size([256, 64, 1, 1]).
size mismatch for layer1.0.bn3.bias: copying a param with shape torch.Size([256, 64, 1, 1]) from checkpoint, the shape in current model is torch.Size([256]).
size mismatch for layer1.0.downsample.0.weight: copying a param with shape torch.Size([256]) from checkpoint, the shape in current model is torch.Size([256, 64, 1, 1]).
size mismatch for layer1.0.downsample.1.weight: copying a param with shape torch.Size([64, 256, 1, 1]) from checkpoint, the shape in current model is torch.Size([256]).
Code used to copy the weights:
dict_trained = torch.load('resnet50.pth')
dict_new = model.state_dict().copy()
new_list = list(model.state_dict().keys())
trained_list = list(dict_trained.keys())
for i in range(1, len(trained_list), 1):
dict_new[new_list[i]] = dict_trained[trained_list[i]]
model.load_state_dict(dict_new, strict=False)
I’ve printed the keys and it seems that they’re all out of order and there are more keys in the new model than the pre-trained one. Do i have to modify the copy code to only copy to matching weights or is there a easier option?