How to test a pytorch model to processing images and videos

username: Sgomes14843
cooked: <p>hello, i already have a retrained model in pytorch, i used mobilenet-v1-ssd-mp-0_675.pth to retrain with my own image dataset.<br>
After doing this I converted the model to the onnx format to be able to work on the jetson nano, and I managed to process images and videos giving the respective results very well.<br>
My question is how can I process images and videos using the pytorch model (“mb1-ssd-Epoch-20-Loss-1.2040329179303213.pth”), in order to compare the performance between the model on my computer and in my jetson.<br>
I used the jetson-inference repo to do that.<br>
Can someone explain then how do I use the .pth file to get results from images and videos?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/4/c42e1ea6aa0f466cae33617ea19b50c001d6bf22.png" data-download-href="https://discuss.pytorch.org/uploads/default/c42e1ea6aa0f466cae33617ea19b50c001d6bf22" title="Screenshot from 2020-07-31 02-53-29"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/4/c42e1ea6aa0f466cae33617ea19b50c001d6bf22.png" alt="Screenshot from 2020-07-31 02-53-29" data-base62-sha1="rZuovr7JCSa4BZzbKBVo6hTH9qW" width="641" height="500" data-dominant-color="E5E1DC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2020-07-31 02-53-29</span><span class="informations">658×513 31.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Hmrishav_Bandyopadhy
cooked: <p>Hi,</p>
<p>You have saved your model as a .pth file. The next step is to load the trained model. For this, you do something like:</p>
<pre><code class="lang-auto">model=Net() ##Your model here
pre_trained_path="path.pth"   ##Your saved model path here
state_dict = torch.load(pre_trained_path)
model.load_state_dict(state_dict)
print(f'model {pre_trained_path} loaded')
</code></pre>
<p>Now, pass image as tensor to the model and get results:</p>
<pre><code class="lang-auto">with torch.no_grad():
       model.eval()
       result=model(input_image)
</code></pre>
<p>Hope that solves it <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: Sgomes14843
cooked: <p>Hi, First thanks for responding.<br>
What am I supposed to put on the model=Net() line? is the path to the model folder, I put the path and got this error message.<code>raise RuntimeError("{} is a zip archive (did you mean to use torch.load()?)".format(f.name)) RuntimeError: /home/sergio/Documents/jetson-inference/python/training/detection/ssd/models/cars/mb1-ssd-Epoch-49-Loss-0.7622953280806541.pth is a zip archive (did you mean to use torch.load()?)</code><br>
And sorry for not realizing much of this, I’m still entering the pytorch now.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/9/895ebb67073cd4bd4fcefae535cc4cce6fae06f1.png" data-download-href="https://discuss.pytorch.org/uploads/default/895ebb67073cd4bd4fcefae535cc4cce6fae06f1" title="Screenshot from 2020-07-31 17-35-21"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/9/895ebb67073cd4bd4fcefae535cc4cce6fae06f1_2_690x392.png" alt="Screenshot from 2020-07-31 17-35-21" data-base62-sha1="jBenj0AcBThmGETjWH2yZchEqwV" width="690" height="392" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/9/895ebb67073cd4bd4fcefae535cc4cce6fae06f1_2_690x392.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/9/895ebb67073cd4bd4fcefae535cc4cce6fae06f1_2_1035x588.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/9/895ebb67073cd4bd4fcefae535cc4cce6fae06f1_2_1380x784.png 2x" data-dominant-color="323233"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot from 2020-07-31 17-35-21</span><span class="informations">1846×1051 106 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Hmrishav_Bandyopadhy
cooked: <p>Hi,</p>
<p>I think I was not clear. No you dont need to use the model path in <code>model</code>.<br>
You must have defined model somewhere. Use that. For perspective, it should look something like this:</p>
<pre><code class="lang-auto">model=nn.Sequential(
                     nn.Conv2d(...),
                     nn.BatchNorm(..),
                     nn.ReLU()
                     ...
                    )
</code></pre>
<p>or something like this</p>
<pre><code class="lang-auto">class Net(nn.Module):
    
    def __init__(self):
        super(Net, self).__init__()

        self.layer1=nn.Linear(...)
        self.activate=nn.ReLU()

    def forward(self,x):
        return self.activate(self.layer1(x))

if __name__=='__main__':
    model=Net()
</code></pre>
<p>Or maybe if you are using models saved in torchvision, something like:</p>
<pre><code class="lang-auto">import torchvision.models as M
model=M.mobilenet_v2()
</code></pre>
<p>It is not the saved model, but a version of the model which has just been initialized. This is needed as only the weights of the model are saved and in order to restore the model, the actual model skeleton is needed.</p>

username: Hmrishav_Bandyopadhy
cooked: <p>On second introspection of the error you are getting, it seems that your .pth file is a zip file and not a weight file. It is highly probable that you didn’t save the model weights in the correct format.</p>

username: Sgomes14843
cooked: <p>Hi, first thanks for explaining the other problem. Regarding this one I am recording in .pth as you can see here. What will be the problem then? Why does it come in a zip file?<img src="https://discuss.pytorch.org/uploads/default/original/3X/2/e/2eb9eb5a4dad0fabb4a4c4d6c2827a4b3f19debf.jpeg" alt="Anotação 2020-08-01 150508" data-base62-sha1="6Fmie9ldaYQpyoBVwua3DkcxUuz" width="611" height="57"></p>

username: Hmrishav_Bandyopadhy
cooked: <p>I think you do something like this:</p>
<pre><code class="lang-auto">torch.save(net.state_dict(),model_path)
</code></pre>
<p>in place of</p>
<pre><code class="lang-auto">net.save(model_path)
</code></pre>

username: Sgomes14843
cooked: <p>Thank you very much, I changed the line and it worked very well<br>
Thanks again for your help!!</p>