Unable to load Yolov7 pretrained pt file

Albert1404: <p>Hi there.<br>
I am trying to load a .pt file from <a href="https://github.com/WongKinYiu/yolov7" rel="noopener nofollow ugc">Official Yolov7 GITHUB</a> via:</p>
<pre data-code-wrap="python"><code class="lang-python">import torch

model = torch.load('yolov7.pt').to('cpu')
print('DONE')
</code></pre>
<p>When doing it I get as follows:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/d/2da506053bd1449aa65d7b462166d0524e263d8d.png" data-download-href="https://discuss.pytorch.org/uploads/default/2da506053bd1449aa65d7b462166d0524e263d8d" title="error"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/d/2da506053bd1449aa65d7b462166d0524e263d8d.png" alt="error" data-base62-sha1="6vN2WiPKNyntuYDKU0POMzSXCbz" width="690" height="116" data-dominant-color="2C2D2D"><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">error</span><span class="informations">1066×180 32.3 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><br>
My pytorch version is <strong>‘1.12.1+cu113’</strong></p>
<p>How can I make it work?</p>

AlphaBetaGamma96: <p>Hi <a class="mention" href="/u/albert1404">@Albert1404</a>,</p>
<p>The <code>.pt</code> file you have will most likely only contain the weights of your model, it does <strong>not</strong> contain the model itself. So, you need to instantiate your model, then load your weights into that instance from the checkpoint file.</p>
<p>For example,</p>
<pre><code class="lang-auto">model = Model(*args, **kwargs) #your Yolo model here...
loaded = torch.load("yolov7.pt", map_location='cpu') # don't use .to("cpu"), this is a dict not Tensor.

model.load_state_dict(loaded['model_state_dict']) #load state dict to our model (same applies to optimizer).
</code></pre>
<p>Now, you’ll need to check what keys/items are inside your <code>loaded</code> object, which should be a dict. Can you print what keys it has via <code>print(loaded.keys())</code>?</p>

Albert1404: <p>Hello <a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a>,<br>
Thank you for your fast reply.<br>
I cannot create <code>loaded</code> variable as I get the exact same error as shown before; therefore, I can’t print it.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/5/55cd37bfaa8baa94b105c8a39a92c422a58329ad.png" data-download-href="https://discuss.pytorch.org/uploads/default/55cd37bfaa8baa94b105c8a39a92c422a58329ad" title="error_same"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/5/55cd37bfaa8baa94b105c8a39a92c422a58329ad.png" alt="error_same" data-base62-sha1="cf2gfeRH74lAly6yLRm3Mg6KXGB" width="690" height="102" data-dominant-color="2E2E2E"><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">error_same</span><span class="informations">1070×159 27.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>
<p>Perhaps it is easier for you to see why I asked that. It is because I want to implement Yolov7 in a Nvidia Jetson Nano via Deepstream. I was managing to follow <a href="https://github.com/marcoslucianops/DeepStream-Yolo/blob/master/docs/YOLOv7.md" rel="noopener nofollow ugc">this tutorial</a>.<br>
<a href="https://github.com/marcoslucianops/DeepStream-Yolo/blob/d58daef720ca0e3157b7a6bc9ba227f9dbedcfd9/utils/gen_wts_yoloV7.py#L316" rel="noopener nofollow ugc">Line 316 here</a> seems to have confused me. Line 316 throws the error depicted above as well.</p>

AlphaBetaGamma96: <p>Given this is with Jetson Nano via Deepstream, I have no idea how it works with <code>.pt</code> in this context. Are you sure your <code>yolov7.pt</code> is the correctly formatted file?</p>

Albert1404: <p>It seem to be solved by first loading the model by hardcoding the file as follows:</p>
<pre><code class="lang-python">_model = torch.hub.load('repo_name', 'model_name') # _model is the model itself
model = torch.load("yolov7.pt", map_location='cpu') # Weights, wrongly named as model. 
Apart from that, it works
</code></pre>