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

PedroA: <p>I’ve been trying to reproduce to results found on this paper, but i can’t get to code to work.<br>
<a href="https://github.com/Tangsenghenshou/Detecting-Generated-Images-by-Real-Images" rel="noopener nofollow ugc">https://github.com/Tangsenghenshou/Detecting-Generated-Images-by-Real-Images</a><br>
Uses code from:<a href="https://github.com/PeterWang512/CNNDetection" rel="noopener nofollow ugc">https://github.com/PeterWang512/CNNDetection</a></p>
<p>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.</p>
<p>Part of traceback:</p>
<pre><code class="lang-auto">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></pre>
<p>Code used to copy the weights:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>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?</p>

ptrblck: <aside class="quote no-group" data-username="PedroA" data-post="1" data-topic="175851">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/p/cab0a1/48.png" class="avatar"> PedroA:</div>
<blockquote>
<p>Do i have to modify the copy code to only copy to matching weights or is there a easier option?</p>
</blockquote>
</aside>
<p>Yes, I would recommend to check the parameters of both models and to clarify why these differ and if it’s expected.<br>
Based on the posted error message it seems you are mixing the <code>weight</code> parameters with the <code>bias</code> parameters which causes the shape mismatches.<br>
I would also recommend to use <code>strict=True</code> whenever possible to avoid silent errors unless you explicitly know why incompatible keys should be skipped.</p>

Yue_Wan: <p>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!</p>

PedroA: <p>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.</p>
<p>You can do it by going on the resnet.py file and removing these lines:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>And replacing them with something simpler. You just need to load the weights, <code>pop('conv1.weight')</code> and then use load_state_dict with the new weights and <code>strict = False</code>.</p>

Yue_Wan: <p>Thank you so much! It worked!</p>