Torch.save cannot find source of modules

Rahul_pillai: <p>Whenever i tried to save the entire model the warning shows, it saves without any errors.</p>
<pre><code class="lang-auto">checkpoint = {'model': ResNet9(), 'state_dict': model.state_dict()}
torch.save(checkpoint, 'model.pth')
</code></pre>
<p>But when i try loading it , it gives me this error.</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-1-29ed9644f643&gt; in &lt;module&gt;
     10     return model
     11 
---&gt; 12 model = load_checkpoint('face.pth')

&lt;ipython-input-1-29ed9644f643&gt; in load_checkpoint(filepath)
      1 import torch
      2 def load_checkpoint(filepath):
----&gt; 3     checkpoint = torch.load(filepath)
      4     model = checkpoint['model']
      5     model.load_state_dict(checkpoint['state_dict'])

~/anaconda3/lib/python3.7/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
    578                     return torch.jit.load(f)
    579                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--&gt; 580         return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
    581 
    582 

~/anaconda3/lib/python3.7/site-packages/torch/serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
    758     unpickler = pickle_module.Unpickler(f, **pickle_load_args)
    759     unpickler.persistent_load = persistent_load
--&gt; 760     result = unpickler.load()
    761 
    762     deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

AttributeError: Can't get attribute 'ResNet9' on &lt;module '__main__'&gt;
</code></pre>
<p>my torch version is 1.5.1 and im using colab</p>

ptrblck: <p>This is a common disadvantage of storing the model directly, as you would need to restore the exact same file structure.<br>
Based on the error message it seems that the definition of <code>ResNet9</code> is missing.</p>
<p>The recommended way it to only store the <code>state_dict</code> and recreate the model directly using:</p>
<pre><code class="lang-python">model = ResNet9() # you would need the definition as an import or in the current script
model.load_state_dict(checkpoint['state_dict'])
</code></pre>

Rahul_pillai: <p>So i have to create the model architecture from scratch?.<br>
Is there any other way to use a model architecture in a another python file just like<br>
we import models from torchvision.</p>

ptrblck: <p>You would have to load the model definition. It can be imported from another file or should be defined in the current script.<br>
If you use the recommended approach, you are flexible where the model definition is stored (in another file, from a lib, in the current script), while loading the model directly would force you to replicate the exact file structure, which was used when you stored the model.</p>

Rahul_pillai: <p>What do you mean by ‘should be defined in the current script’.<br>
Im sorry , i’m new to all this , alot of what you’ve just said is confusing.<br>
If im not asking too much could you explain it with an example.</p>

ptrblck: <p>You could either import the model from another script via:</p>
<pre><code class="lang-python">from other_file import ResNet9

model = ResNet9()
</code></pre>
<p>or define the model in the same script:</p>
<pre><code class="lang-python">def ResNet9(nn.Module):
    def __init__(self):
        ...

model = ResNet9()
</code></pre>

Rahul_pillai: <p>So if i want to do training, data preprocessing in one file and using the trained model<br>
for prediction in another file , my best bet would be to import the model.</p>
<p>Thank you so much for helping <a class="mention" href="/u/ptrblck">@ptrblck</a></p>