yolov5用官方权重文件测试detect.py检测无结果
原因很简单,就是你的cuda版本错误了。英伟达的显卡从30系开始,就不支持cuda11.0以下的cuda版本了,你这个40系的需要安装cuda11以上的cuda版本才行。
所以解决方案就是换cpu,或者重新安装cuda,然后torch等和cuda相关的都要换成cuda11.x的版本。一般来说,cuda11.x只要大于torch要求的11.y(x>=y)是没啥问题的,所以你可以安装cuda11.8,然后torch和torchvision等安装就看你要多少版本了。另外,请注意,cuda目前最新的是12.x的版本,目前阶段不是很建议用12.x版本的cuda
python
import torch
from PIL import Image
from torch.utils.data import DataLoader
from models.experimental import attempt_load
from utils.autoanchor import check_anchors
from utils.datasets import LoadImages
from utils.general import non_max_suppression
from utils.torch_utils import select_device
def yolov5_detect(img_path, weight_file_path):
device = select_device('')
model = attempt_load(weight_file_path, map_location=device)
img = Image.open(img_path)
img_size = img.size
img = img.resize((640, 640))
img = torch.from_numpy(img).float().to(device)
img /= 255.0
img = img.permute(2, 0, 1).unsqueeze(0)
conf_thres, iou_thres = 0.25, 0.45
pred = model(img)[0]
pred = non_max_suppression(pred, conf_thres, iou_thres, fast=True, classes=None, agnostic=False)
pred = pred[0]
if pred is not None:
for det in pred:
x1, y1, x2, y2, conf, cls = det
x1, y1, x2, y2 = int(x1 * img_size[0]), int(y1 * img_size[1]), \
int(x2 * img_size[0]), int(y2 * img_size[1])
print(f'class: {cls}, confidence: {conf:.4f}, box: [{x1}, {y1}, {x2}, {y2}]')
else:
print('No object detected')
if __name__ == '__main__':
img_path = 'test.jpg' # 完整路径或相对路径
weight_file_path = 'yolov5s.pt' # 完整路径或相对路径,与detect.py中的--weights参数相同
yolov5_detect(img_path, weight_file_path)
如果yolov5用官方权重文件测试detect.py检测无响应,可能是由于代码有问题,比如权重文件的使用方式不对,死循环,或者计算量太大,程序还在执行中,还没有出结果等。所以首先检查下代码。其次有可能是你的电脑cpu或gpu性能不足,可以打开任务管理器,看下你的CPU使用率是否太高。
我遇到过这个问题,但我不清楚你的跟我之前遇到的是否相同,我之前在参数--source 处填写的是网络摄像头rtsp或者0 ,填写后发现程序一直运行但就是打不开,后来一检查,才发现是摄像头异常了,或者是连接rtsp网络摄像头时没有使用同一个网段WiFi,所以导致程序运行却不出结果,也不报错,你可以查看一下--source参数是否是摄像头,如果是,检查一下连接摄像头是否正常。
不知道你这个问题是否已经解决, 如果还没有解决的话: