最近技术员把服务器重装了,本以为重新配了环境就可以像从前一样跑yolov5,然而,配完环境,以前可以跑起来的yolov5出现了各种毛病。现在开始填坑日记。
1.TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
解决方案:在报错的地方.numpy()前面加个.cpu()
比如:原代码为return self.numpy()应该改为return self.cup().numpy()
参考https://blog.csdn.net/weixin_45033788/article/details/115160288(ps:各种yolov5的坑几乎都有)
2.yolov5报错:RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place
解决方案:https://blog.csdn.net/weixin_44612221/article/details/115359866
将yolo.py第145行代码修改如下
def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
m = self.model[-1] # Detect() module
for mi, s in zip(m.m, m.stride): # from
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
with torch.no_grad():
b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)