【问题标题】:What and where am I going wrong in this code for pytorch based object detection?在基于 pytorch 的对象检测的这段代码中,我在哪里出错了?
【发布时间】: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


    【解决方案1】:

    您是否在以下行中收到错误?

    pred = model(img, augment=False)[0]
    

    这可能是因为 YOLO 期望输入的图像尺寸是 32 的倍数。所以 320×320、352×352 等。但你是 352x288。您要么必须调整它的大小,要么用白色/黑色像素填充 288 尺寸以使其达到 352。

    如果您不确定从哪里得到错误,可以附上整个错误吗?

    【讨论】:

    • 嘿,我进行了建议的更改,但我仍然收到此错误,我已经稍微编辑了我的问题并提出了整个错误,如果你能找到这个错误,请告诉我并感谢你的回复:)
    • 所以错误在我提到的那一行。你能告诉我们你做了什么改变吗?
    • 我把图片尺寸改成352x352
    • 能确认一下img维度是不是(1, 3, h, w)?
    • 是的,我实际上为此使用了 yolov5 的 detect.py 脚本,它确实有效。谢谢!
    最近更新 更多