1. 目标检测 YOLOv5的loss权重
YOLOv5中有三个损失分别是 box, obj, cls

(图片来源网络,侵删)
box: 0.05 cls: 0.5 obj: 1

(图片来源网络,侵删)
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 = 2.0).any(1)] # filter > 2 pixels
2. obj损失与图像大小的关系
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