yolov5不能检测长宽比超过20的目标的解决方法

最近在使用YOLOv5代码的时候,发现yolov5对长宽比很大,如超过100+的目标检测,完全检测不出来,
之前一直怀疑是anchors设置有问题,验证了很多次,证明不是anchors的问题,

最后经组内小伙伴提醒,yolov5在数据增强的时候,默认设置了目标的长宽比最大为20,需要修改的文件(utils/datasets.py)

def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, special_classes=0):  # box1(4,n), box2(4,n)
    # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
    w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
    w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
    ar = np.maximum(w2 / (h2 + 1e-16), h2 / (w2 + 1e-16))  # aspect ratio
    return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + 1e-16) > area_thr) & (ar < ar_thr)  # candidates

修改为:

def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, special_classes=0):  # box1(4,n), box2(4,n)
    # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
    w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
    w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
    ar = np.maximum(w2 / (h2 + 1e-16), h2 / (w2 + 1e-16))  # aspect ratio
    return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + 1e-16) > area_thr) & ((ar < ar_thr) | ((special_classes==0) and (ar < 120)))  #candidates

其中 special_classes为长宽比最大的类别,因为长宽比最大为 105,所以设置了ar<120