I’m trying to make a currency recognition model and I did so using a dataset on kaggle and colab using yolov5 and I exactly carried out the steps explained on yolov5 github. At the end, I downloaded a .pt file which has the weights of the model and now I want to use it in python file to detect and recognize currency . How to do this?
I am a beginner in computer vision and I am totally confused about what to do. I am searching over and over but I don’t reach anything.
JavaScript
x
18
18
1
import torch
2
3
# Model
4
model=torch.load('E:_best.pt')
5
6
# Images
7
imgs=['E:Studycurrency.jpg']
8
9
# Inference
10
results = model(imgs)
11
12
# Results
13
results.print()
14
results.save() # or .show()
15
results.show()
16
results.xyxy[0] # img1 predictions (tensor)
17
results.pandas().xyxy[0]
18
Advertisement
Answer
If you want to read trained parameters from .pt file and load it into your model, you could do the following.
JavaScript
1
6
1
file = "model.pt"
2
model = your_model()
3
model.load_state_dict(torch.load(file))
4
# this will automatically load the file and load the parameters into the model.
5
6
before calling load_state_dict(), be sure that the .pt file contains only model parameters, otherwise, error occurs. This can be checked by print(torch.load(file)).