再运行YOLO V5原程序过程中,晕倒几个需要解决的问题,具体如下:
一、paycharm运行train.py需要添加参数,具体如下:
python train.py --img 640 --batch 16 --epochs 5 --data ./data/BCC.yaml --cfg ./models/yolov5s.yaml --weights ''
如果对源码中的参数部分进行了相应的改进,或是默认设置,可以不用再次设置。
二、yolov5报错:RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place
具体可以参看博文,主要是修改在yolov5-master\models\yolo.py文件中的_initialize_biases函数,具体如下:
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)
三、yolov5报错:TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy…错误显示位于yolov5\utils\general.py文件中。
这个部分费了一些时间,在网上找了一些资料,主要是将数据转成cpu的,如targets.cpu().numpy(),但是均没有效果。
根据错误提示找到YOLOV5的运行环境,如D:\Program Files\Miniconda3\envs\torch\lib\site-packages\torch\tensor.py文件的第643行(可能不同版本的pytorch位置会有不同,可以直接搜索如下内容更改)
return self.numpy()
改为:
return self.cpu().detach().numpy()
主要参考:
https://blog.csdn.net/weixin_44612221/article/details/115359866
https://blog.csdn.net/qq_44703886/article/details/117231542