AttributeError: 'Conv2d' object has no attribute 'padding_mode'

Fahman: <p>I save resnet model using old pytorch and I load it using Pytorch1.1 but I get error:<br>
AttributeError: ‘Conv2d’ object has no attribute ‘padding_mode’</p>

ptrblck: <p>How did you store and try to load the model?<br>
<code>padding_mode</code> was introduced in PyTorch <code>1.1.0</code> ( <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d" rel="nofollow noopener">docs</a> ), so this argument shouldn’t throw an error while trying to load the model in <code>1.1.0</code>.</p>

Fahman: <p>I save the model using old Pytorch version</p>

ptrblck: <p>How did you store the resnet in the older PyTorch version?<br>
Did you store the <code>state_dict</code> or the complete model?<br>
In the latter case, you might encounter some issues, if the source code of the used classes has changed, which is the case in <code>nn.Conv2d</code>.<br>
Could you save and load the <code>state_dict</code> instead as described in the <a href="https://pytorch.org/docs/stable/notes/serialization.html" rel="nofollow noopener">Serialization Docs</a>?</p>

xvdp: <p>Im here because of the same “error”.<br>
I think that there is somethin fundamentally incomplete in the serialization strategy in pytorch. You could call it user error - but it pops up more often than not when loading saved models from githubs. I dont do this often enough to have a full view on the matter, but Ive had several times to load a pth, wipe it clean and save it again, or save its parts, or… I figure that to write a fault tolerant torch load ought to be a quite simple task.  And while the software is “maturing”,  deep learning is far from ripe yet. Itd be a useful tool.</p>
<p>thanks.</p>

ptrblck: <aside class="quote no-group" data-username="xvdp" data-post="5" data-topic="49903">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xvdp/48/11617_2.png" class="avatar"> xvdp:</div>
<blockquote>
<p>but Ive had several times to load a pth, wipe it clean and save it again, or save its parts, or…</p>
</blockquote>
</aside>
<p>I haven’t seen these issues yet, so could you point me to some examples?<br>
If I’m understanding it correctly, it seems the writing/saving process was somehow interrupted, which resulted in an invalid file?</p>
<aside class="quote no-group" data-username="xvdp" data-post="5" data-topic="49903">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xvdp/48/11617_2.png" class="avatar"> xvdp:</div>
<blockquote>
<p>Im here because of the same “error”.</p>
</blockquote>
</aside>
<p>Storing the <code>state_dict</code> is backward compatible and thus the recommended way.<br>
If you store the model completely, you might need to use <code>dump_patches=True</code> to fix the code.</p>

xvdp: <p>Sure, I was looking at super resolution githubs with pytorch.<br>
One of the ones I grabbed was <a href="https://github.com/BUPTLdy/Pytorch-LapSRN" rel="nofollow noopener">https://github.com/BUPTLdy/Pytorch-LapSRN</a>.</p>
<p>Download it and run the python test.py on torch ‘1.0.1.post2’ (version im currently working with) this error will occur.<br>
Clearly, the author should have saved a state_dict, but they saved the entire model instead.</p>
<p>The fix is quite simple, for this one, on a separate script load the entire model as in test.py then save state state dict as per the serialization docs, then change test.py to load the model class then load the state dict you just saved.</p>
<p>So, if one follows the recommended serialization proc then everything works, but in many cases this isn’t done, because its in human nature to take the perceived simpler path. All Im just arguing that pytorch could have a more comprehensive error tolerant serialization.</p>
<p>btw. not related; a much better written superresolution github is <a href="https://github.com/xinntao/ESRGAN" rel="nofollow noopener">ESRGAN</a> - curiously it isnt in modelzoo.</p>