【发布时间】:2021-08-12 06:19:41
【问题描述】:
我在这个项目中使用 Yolov5
这是我的代码
import numpy as np
import cv2
import torch
import torch.backends.cudnn as cudnn
from models.experimental import attempt_load
from utils.general import non_max_suppression
weights = '/Users/nidhi/Desktop/yolov5/best.pt'
device = torch.device('cpu')
model = attempt_load(weights, map_location=device) # load FP32 model
stride = int(model.stride.max()) # model stride
cudnn.benchmark = True
# Capture with opencv and detect object
cap = cv2.VideoCapture('Pothole testing.mp4')
width, height = (352, 352) # quality
cap.set(3, width) # width
cap.set(4, height) # height
while(cap.isOpened()):
time.sleep(0.2) # wait for 0.2 second
ret, frame = cap.read()
if ret ==True:
now = time.time()
img = torch.from_numpy(frame).float().to(device).permute(2, 0, 1)
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
pred = model(img, augment=False)[0]
pred = non_max_suppression(pred, 0.39, 0.45, classes=0, agnostic=True) # img, conf, iou, classes, ...
print('time -> ', time.time()-now)
else:
break
cap.release()
我得到的错误:
File "run.py", line 38, in <module>
pred = model(img, augment=False)[0]
File "/Users/nidhi/Library/Python/3.8/lib/python/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "/Users/nidhi/Desktop/yolov5/models/yolo.py", line 118, in forward
return self.forward_once(x, profile) # single-scale inference, train
File "/Users/nidhi/Desktop/yolov5/models/yolo.py", line 134, in forward_once
x = m(x) # run
File "/Users/nidhi/Library/Python/3.8/lib/python/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "/Users/nidhi/Desktop/yolov5/models/common.py", line 152, in forward
return torch.cat(x, self.d)
RuntimeError: Sizes of tensors must match except in dimension 1. Got 108 and 107 in dimension 3 (The offending index is 1)
操作系统:macOS Big Sur 11.2.3
Python 版本:3.8.2
模型使用的是我在Google Colab上训练的best.pt,我使用yolov5l模型训练数据集。
【问题讨论】:
标签: python opencv deep-learning pytorch object-detection