cv516Buaa / tph-yolov5

GNU General Public License v3.0
707 stars 170 forks source link

`torch.load('yolov5l-xs-1.pt')` --> `No module named 'models'` #29

Open martinbaerwolff opened 2 years ago

martinbaerwolff commented 2 years ago

Dear authors,

we are trying to use your weights in our own python application for inference with yolov5.

When we try to load your weights with torch.load('yolov5l-xs-1.pt'), we receive the error No module named 'models'.

Is the missing module the folder "models" in your repo?

Is it possible (and intended) to use your weights out of the box with torch.load()?

cv516Buaa commented 2 years ago

You can use torch.load() by the following code. Before use please set the right path of the .pt file and the img file, you will get the result at ./runs/detect/exp

%cd D:/study/DeepLearning/workspace/yolov5/yolov5/
import torch
from utils.autobatch import autobatch
import cv2
from PIL import Image
import os

img_size = 1920
weight_path = 'D:/study/DeepLearning/workspace/VisDrone/my_model/paper/kaiyuan/train/yolov5x-xs-ms-fintune/weights/best.pt'

model = torch.hub.load('D:/study/DeepLearning/workspace/yolov5/yolov5/', 'custom', 
                       path=weight_path, source='local', force_reload=True)
model.amp = False 

# detect
path = 'D:/study/DeepLearning/workspace/datasets/VisDrone/VisDrone2019-DET-test-challenge/images/'
img1_path = os.path.join('D:/study/DeepLearning/workspace/yolov5/yolov5/data/images/bus.jpg')
img2_path = os.path.join(path, '0000231_02500_d_0000006.jpg')

img1 = Image.open(img1_path)  # PIL image
img2 = cv2.imread(img2_path)[..., ::-1]  # OpenCV image (BGR to RGB)
imgs = [img2]  # batch of images

# Inference
results = model(imgs, size=img_size)  # includes NMS

# Results
results.print()  
results.save()  # or .show()

results.xyxy[0]  # img1 predictions (tensor)
results.pandas().xyxy[0]  # img1 predictions (pandas)
martinbaerwolff commented 2 years ago

Hi @cv516Buaa, thanks for your response. Unfortunately it took me a while to try this out.

Instead of D:/study/DeepLearning/workspace/yolov5/yolov5/ I tried with ultralytics/yolov5 (and without source='local').

We ran into the following error:

AttributeError: Can't get attribute 'NonDynamicallyQuantizableLinear' on <module 'torch.nn.modules.linear' from 'C:\Users(...)venv\lib\site-packages\torch\nn\modules\linear.py'>

Did you change something in D:/study/DeepLearning/workspace/yolov5/yolov5/ in comparison to the yolo master? Or do you have any other idea on how to fix this?

Thx and best, Martin