我自己训练的模型在自己的代码里面能进行预测,但是在运行yolov5_deepsort的时候报错AttributeError: 'Detect' object has no attribute 'm'
报错详情:
Traceback (most recent call last):
File "L:/chen/yolov5_material/unbox_yolov5_deepsort_counting-main/main.py", line 83, in
bboxes = detector.detect(im)
File "L:\chen\yolov5_material\unbox_yolov5_deepsort_counting-main\detector.py", line 49, in detect
pred = self.m(img, augment=False)[0]
File "C:\Users\n406\Anaconda3\envs\pytorch-chen\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "L:\chen\yolov5_material\unbox_yolov5_deepsort_counting-main\models\yolo.py", line 110, in forward
return self.forward_once(x, profile) # single-scale inference, train
File "L:\chen\yolov5_material\unbox_yolov5_deepsort_counting-main\models\yolo.py", line 126, in forward_once
x = m(x) # run
File "C:\Users\n406\Anaconda3\envs\pytorch-chen\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "L:\chen\yolov5_material\unbox_yolov5_deepsort_counting-main\models\yolo.py", line 39, in forward
x[i] = self.m[i](x[i]) # conv
File "C:\Users\n406\Anaconda3\envs\pytorch-chen\lib\site-packages\torch\nn\modules\module.py", line 948, in __getattr__
type(self).__name__, name))
AttributeError: 'Detect' object has no attribute 'm'
解决办法:把自己训练模型里面可以用来预测的yolo.py里面的函数替换掉yolov5_deepsort里面的函数就可以了
def forward(self, x):
# x = x.copy() # for profiling
z = [] # inference output
self.training |= self.export
for i in range(self.nl):
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
if not self.training: # inference
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
self.grid[i] = self._make_grid(nx, ny).to(x[i].device)
y = x[i].sigmoid()
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i].to(x[i].device)) * self.stride[i] # xy
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
z.append(y.view(bs, -1, self.no))
return x if self.training else (torch.cat(z, 1), x)