Detecting Generated Images by Real Images, problems trying to reproduce paper

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?

Yes, I would recommend to check the parameters of both models and to clarify why these differ and if it’s expected.
Based on the posted error message it seems you are mixing the weight parameters with the bias parameters which causes the shape mismatches.
I would also recommend to use strict=True whenever possible to avoid silent errors unless you explicitly know why incompatible keys should be skipped.

1 Like

We encountered exactly the same problem and error when reproducing this paper, I wonder whether you solved the problem and how to solve it? Thank you so much!

1 Like

After downloading the pretrained model, i removed the weights of the first layer and then loaded them into the network that was going to be fine tuned.

You can do it by going on the resnet.py file and removing these lines:

dict_trained = torch.load('resnet50.pth')
dict_new = model.state_dict().copy()
new_list = list(model.state_dict().keys())
print(new_list)
trained_list = list(dict_trained.keys())
print(trained_list)
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)

And replacing them with something simpler. You just need to load the weights, pop('conv1.weight') and then use load_state_dict with the new weights and strict = False.

Thank you so much! It worked!