Did any one know how to load the pth pre-trained model from fastai to pytorch?

username: JonathanSum
cooked: <p>Did any one know how to load the pth pre-trained model from fastai to pytorch?</p>

username: ptrblck
cooked: <p>You would need the model definition, create an instance and use <code>model.load_state_dict</code>.<br>
I’m not sure, which model you would like to load and how it is defined, so feel free to share some more information. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: JonathanSum
cooked: <p>Thank You for replying, I was using the resnet 34 from fastai export a pre-trained model:</p>
<p><a href="https://drive.google.com/drive/u/0/folders/1vGgwkDCDMb5L8z6SgE1LDor6PhBeGmuW" rel="nofollow noopener">pth trained file</a></p>
<p><a href="https://colab.research.google.com/github/JonathanSum/Pytorch_Projects/blob/master/character_identifier.ipynb#scrollTo=RcOi0BuCkBgx" rel="nofollow noopener">The notebook I trained and created the "stage-2.pth file’</a><br>
learn = cnn_learner(data, models.resnet34, metrics=error_rate)<br>
learn.save(‘stage-2’, return_path= True)</p>
<p>I want to load this pre-trained pth file for feature extraction for gan<br>
How do I defind a model definition that the model I used was from fastai that I don’t even know it?</p>

username: ptrblck
cooked: <p>I’m not deeply familiar with the <code>cnn_learner</code> class, but based on <a href="https://github.com/fastai/fastai/blob/ef79ccb63e7466a6704f53cf78ba19f9d03f9bfa/old/fastai/learner.py#L54">this line of code</a> it should contain the <code>model</code> instance as an attribute, which might be used to save the <code>state_dict</code> directly.</p>
<aside class="quote no-group" data-username="JonathanSum" data-post="3" data-topic="69298">
<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/jonathansum/48/54067_2.png" class="avatar"> JonathanSum:</div>
<blockquote>
<p>How do I defind a model definition that the model I used was from fastai that I don’t even know it?</p>
</blockquote>
</aside>
<p>Based on your code the actual model might just be <code>torchvision.models.resnet34</code>.<br>
You could try to create an instance of this model and load the <code>state_dict</code>.</p>

username: JonathanSum
cooked: <p>Hi, thank you for answering me. But, your way does not work. It looks like once it is using learn.module, it will fail to correctly predict the true value, even though the original fastai model has high accuracy.</p>
<p>[<a href="https://drive.google.com/drive/u/0/folders/1vGgwkDCDMb5L8z6SgE1LDor6PhBeGmuW" rel="nofollow noopener">https://drive.google.com/drive/u/0/folders/1vGgwkDCDMb5L8z6SgE1LDor6PhBeGmuW</a>](<a href="http://pre-trained" rel="nofollow noopener">http://pre-trained</a> model)<br>
<a href="https://drive.google.com/drive/u/0/folders/1o9z60T2C7uWPp_KPq_h7PT8HkhptM0Xr" rel="nofollow noopener">data_set</a></p>
<pre><code class="lang-auto">In [304]:
print(np.argmax((model_pkl(intput.cuda() )).cpu().detach().numpy()   ))
print(((model_pkl(intput.cuda() )).cpu().detach().numpy()   ))
0
[[ 2.419829 -0.988511 -0.085516 -0.889875]]
In [0]:
learn_pth = learn.load('/content/drive/My Drive/fastai-v3/data/Trained Happy Sugar Life/stage-2').model;
learn_pth.eval()
In [303]:
print(np.argmax((learn_pth(intput.cuda() )).cpu().detach().numpy()   ))
print(((learn_pth(intput.cuda() )).cpu().detach().numpy()   ))
0
[[ 2.419829 -0.988511 -0.085516 -0.889875]]
In [301]:
pred_class,pred_idx,outputs = learn_pkl.predict(img_it)
print(pred_class)
print(pred_idx)
print(outputs)
shouko
tensor(3)
tensor([2.0889e-05, 3.8629e-11, 2.9075e-07, 9.9998e-01])
In [305]:
pred_class,pred_idx,outputs = learn.predict(img_it)
print(pred_class)
print(pred_idx)
print(outputs)
shouko
tensor(3)
tensor([2.0889e-05, 3.8629e-11, 2.9075e-07, 9.9998e-01])
</code></pre>
<p>The last two tests are testing loading pth and pkl and their output value. Their output value are same, but they are so different when they are using the <code>model</code>  instance attribute. As a result, they made an wrong prediction(correct index is 3).  I don’t know. The Resnet that fastai using is totally same as the Pytorch offical’s.</p>
<p><a href="https://github.com/JonathanSum/Pytorch_Projects/blob/master/character_identifier.ipynb" rel="nofollow noopener">Link to notebook</a></p>

username: ptrblck
cooked: <p>I would recommend to check, what <code>learn.predict</code> is doing and try to match the behavior in your “manual approach” . I.e. is it normalizing the inputs or processing in some other way?</p>
<p>Also, did you compare the parameters and made sure they are equal after loading the model?</p>

username: JonathanSum
cooked: <p>Thank You for replying.  I don’t think the parameters arechanged after the learner loaded the pth file because its accuarcy is high after loaded the pth file.</p>
<p>I just hope that you are right that the learn.model has the learned weight and it is the model itself. Thus, how I check the learn.model’s parameters and layer dict key to compare the learn object’s own parameters and layer dict key? Please take your time if you are busy.</p>

username: ptrblck
cooked: <p>Are you loading a <code>state_dict</code> object or a custom checkpoint implementation of fastai?<br>
I’m not deeply familiar with the source code of fastai, so <a class="mention" href="/u/jphoward">@jphoward</a> might give some insight on how to get the underlying model.</p>

username: JonathanSum
cooked: <p>I won’t brother him because I think he is busy.</p>
<p>In addition, I think you have helped me enough.<br>
Thank You very much.</p>
<p>I will try to re-implement the learn.predict function because I guess there it has a magic or whatever.</p>

username: jphoward
cooked: <p>You can see the source code for <code>save</code> here:</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/fastai/fastai/blob/master/fastai/basic_train.py#L246" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/fastai/fastai/blob/master/fastai/basic_train.py#L246" target="_blank" rel="nofollow noopener">fastai/fastai/blob/master/fastai/basic_train.py#L246</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="236" style="counter-reset: li-counter 235 ;">
<li>    #layer_groups -&gt; need to find a way</li>
<li>    #TO SEE: do we save model structure and weights separately?</li>
<li>    with ModelOnCPU(self.model) as m:</li>
<li>        state['model'] = m</li>
<li>        xtra = dict(normalize=self.data.norm.keywords) if getattr(self.data, 'norm', False) else {}</li>
<li>        state['data'] = self.data.valid_ds.get_state(**xtra)</li>
<li>        state['cls'] = self.__class__</li>
<li>        try_save(state, self.path, file)</li>
<li>    if destroy: self.destroy()</li>
<li>
</li>
<li class="selected">def save(self, file:PathLikeOrBinaryStream=None, return_path:bool=False, with_opt:bool=True):</li>
<li>    "Save model and optimizer state (if `with_opt`) with `file` to `self.model_dir`. `file` can be file-like (file or buffer)"</li>
<li>    if is_pathlike(file): self._test_writeable_path()</li>
<li>    if rank_distrib(): return # don't save if slave proc</li>
<li>    target = self.path/self.model_dir/f'{file}.pth' if is_pathlike(file) else file</li>
<li>    if not hasattr(self, 'opt'): with_opt=False</li>
<li>    if not with_opt: state = get_model(self.model).state_dict()</li>
<li>    else: state = {'model': get_model(self.model).state_dict(), 'opt':self.opt.state_dict()}</li>
<li>    torch.save(state, target)</li>
<li>    if return_path: return target</li>
<li>
</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>(<a class="mention" href="/u/ptrblck">@ptrblck</a> you linked to v0.7 - you can see the ‘old’ in the path to it; v1 is in ‘fastai’, not ‘old’)</p>
<p>As you can see from the source, if <code>with_opt</code> is false, then the saved file is just the model state_dict; if it’s true, it’s a dictionary with the state_dict and the optimizer state.</p>

username: JonathanSum
cooked: <p>Hi jemery.  The reason why learn.module(input_img) can not predict as well as learn.prrdict was because I didn’t load the opt state dict? Or after I set eval() and require grad is false just like lesson7 notebook, learn.module(input_img) will predict as well as learn.predict? Thank you</p>

username: nofreewill
cooked: <p>Thank you, this made it work for me:</p>
<hr>
<p>get_model_structure() gives you an untrained model</p>
<hr>
<h2>Train it</h2>
<p>model = get_model_structure()<br>
learn = Learner(data, model)<br>
learn.fit_one_cycle(64)<br>
learn.save(‘name’, with_opt=False)</p>
<hr>
<h2>Load it for evaluation</h2>
<p>model = get_model_structure()<br>
state = torch.load(‘path to saved/name.pth’)<br>
model.load_state_dict(state)<br>
model.eval()</p>