参考网址:https://zhuanlan.zhihu.com/p/501798155
源码下载及使用
release下载source及pt文件(yolov5s.pt)
https://github.com/ultralytics/yolov5/tags
https://github.com/ultralytics/yolov5/releases/tag/v5.0
安装yolov5训练所需的第三方库
1. 安装anaconda,版本conda -V 进入yolo5目录下 cd [path_to_yolov5]
2. 新建虚拟环境 conda create -n yolov5 python=3.7 -y
3. 激活虚拟环境 conda activate yolov5
4. pip install -r requirements.txt
5. 将xx.pt复制在yolov5文件夹下
6. PyCharm打开yolov5源码,File->Settings->Project:yolov5-5.0->Python Interpreter->Add->Conda Enviroment->Existing environment (D:\ProgramData\Anaconda3\envs\yolov5\python.exe),保存后,右下角显示Python3.7(yolo5)
7. PyCharm运行detect.py,提示错误 AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
进入 D:\ProgramData\Anaconda3\envs\yolov5\lib\site-packages\torch\nn\modules\upsampling.py 行号:155 编辑并保存
def forward(self, input: Tensor) -> Tensor:
#return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners,
# recompute_scale_factor=self.recompute_scale_factor)
return F.interpolate(input, self.size, self.scale_factor, self.mode, self.align_corners)
train时,提示错误
问题1:AssertionError: Label class 1 exceeds nc=1 in data/fire.yaml. Possible class labels are 0-0
解决方法:注释掉train.py中的一行代码 #assert mlc < nc, 'Label class %g exceeds nc=%g in %s. Possible class labels are 0-%g' % (mlc, nc, opt.data, nc - 1)
问题2:
Traceback (most recent call last):
File "G:/yolov/yolov5-5.0/train.py", line 543, in <module>
train(hyp, opt, device, tb_writer)
File "G:/yolov/yolov5-5.0/train.py", line 304, in train
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
File "G:\yolov\yolov5-5.0\utils\loss.py", line 117, in __call__
tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets
File "G:\yolov\yolov5-5.0\utils\loss.py", line 211, in build_targets
indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices
RuntimeError: result type Float can't be cast to the desired output type __int64
解决方法:loss.py 行号:1136
for i in range(self.nl): anchors, shape = self.anchors[i], p[i].shape gain[2:6] = torch.tensor(shape)[[3, 2, 3, 2]] # xyxy gain # Match targets to anchors t = targets * gain # shape(3,n,7) if nt: # Matches r = t[..., 4:6] / anchors[:, None] # wh ratio j = torch.max(r, 1 / r).max(2)[0] < self.hyp['anchor_t'] # compare # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2)) t = t[j] # filter # Offsets gxy = t[:, 2:4] # grid xy gxi = gain[[2, 3]] - gxy # inverse j, k = ((gxy % 1 < g) & (gxy > 1)).T l, m = ((gxi % 1 < g) & (gxi > 1)).T j = torch.stack((torch.ones_like(j), j, k, l, m)) t = t.repeat((5, 1, 1))[j] offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j] else: t = targets[0] offsets = 0 # Define bc, gxy, gwh, a = t.chunk(4, 1) # (image, class), grid xy, grid wh, anchors a, (b, c) = a.long().view(-1), bc.long().T # anchors, image, class gij = (gxy - offsets).long() gi, gj = gij.T # grid indices # Append indices.append((b, a, gj.clamp_(0, shape[2] - 1), gi.clamp_(0, shape[3] - 1))) # image, anchor, grid tbox.append(torch.cat((gxy - gij, gwh), 1)) # box anch.append(anchors[a]) # anchors tcls.append(c) # class return tcls, tbox, indices, anch
测试:
conda activate yolov5
python detect.py --weights yolov5s.pt --source data/images/bus.jpg # 测试结果保存在runs/detect
conda activate yolov5
python detect.py --weights yolov5s.pt --source test.mp4
自己训练模型
1.收集数据集(image/train/001.jpg)
2.标注数据集(label/train/001.txt),半自动化标注
3.训练数据集(weight/best.pt)
4.模型测试,目标检测(objname=people;potion=box_l, box_t, box_r, box_b;confidence=0.6;name=29)
标签:yolov5,anchors,self,py,gxy,targets From: https://www.cnblogs.com/chen1880/p/17054949.html