Unable to load Yolov7 pretrained pt file

Hi there.
I am trying to load a .pt file from Official Yolov7 GITHUB via:

import torch

model = torch.load('yolov7.pt').to('cpu')
print('DONE')

When doing it I get as follows:


My pytorch version is ‘1.12.1+cu113’

How can I make it work?

Hi @Albert1404,

The .pt file you have will most likely only contain the weights of your model, it does not contain the model itself. So, you need to instantiate your model, then load your weights into that instance from the checkpoint file.

For example,

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).

Now, you’ll need to check what keys/items are inside your loaded object, which should be a dict. Can you print what keys it has via print(loaded.keys())?

1 Like

Hello @AlphaBetaGamma96,
Thank you for your fast reply.
I cannot create loaded variable as I get the exact same error as shown before; therefore, I can’t print it.

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 this tutorial.
Line 316 here seems to have confused me. Line 316 throws the error depicted above as well.

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

It seem to be solved by first loading the model by hardcoding the file as follows:

_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
3 Likes