赞
踩
YOLOv5中有三个损失分别是 box, obj, cls
在超参数配置文件hyp.*.yaml中可以设置基础值,例如
- box: 0.05
- cls: 0.5
- obj: 1
训练使用时,在train.py进行更新
- hyp['box'] *= 3 / nl # scale to layers
- hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers
- hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers
可以看到损失与nl(number of detection layers,检测层的层数,这里是3)和图像尺寸相关,与layers相关这个好理解,是因为损失多个layers的加和。与图像尺寸相关则需要进一步探讨。
nl = model.model[-1].nl # number of detection layers (used for scaling hyp['obj'])
然后,在loss计算时会乘上各自的权重
loss.py
- lbox *= self.hyp['box']
- lobj *= self.hyp['obj']
- lcls *= self.hyp['cls']
训练时会过滤掉小于2像素的框
- # Filter
- i = (wh0 < 3.0).any(1).sum()
- if i:
- print(f'{prefix}WARNING: Extremely small objects found. {i} of {len(wh0)} labels are < 3 pixels in size.')
- wh = wh0[(wh0 >= 2.0).any(1)] # filter > 2 pixels
obj损失通过图像大小进行调整
hyp['obj'] *= (imgsz / 640) ** 2 * 3. / nl
看一下图像大小分别是1280,640,320,224的时候,各自权重分别是多少
Image sizes 1280
- nl: 3
- hyp['box']: 0.05
- hyp['obj']: 4.0
- hyp['cls']: 0.5
Image sizes 640
- nl: 3
- hyp['box']: 0.05
- hyp['obj']: 1.0
- hyp['cls']: 0.5
Image sizes 320
- nl: 3
- hyp['box']: 0.05
- hyp['obj']: 0.25
- hyp['cls']: 0.5
Image sizes 224
- nl: 3
- hyp['box']: 0.05
- hyp['obj']: 0.12249999999999998
- hyp['cls']: 0.5
参考:
https://blog.csdn.net/flyfish1986/article/details/116832354
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。