shenyi0220 / CP-Cluster

45 stars 3 forks source link

question in yolov5 #5

Closed 535484159 closed 1 year ago

535484159 commented 2 years ago

How to replace the default torchvision.nms with CP-Cluster from mmcv? `i = torchvision.ops.nms(boxes, scores, iou_thres) # NMS

    if i.shape[0] > max_det:  # limit detections
        i = i[:max_det]
    if merge and (1 < n < 3E3):  # Merge NMS (boxes merged using weighted mean)
        # update boxes as boxes(i,4) = weights(i,n) * boxes(n,4)
        iou = box_iou(boxes[i], boxes) > iou_thres  # iou matrix
        weights = iou * scores[None]  # box weights
        x[i, :4] = torch.mm(weights, x[:, :4]).float() / weights.sum(1, keepdim=True)  # merged boxes
        if redundant:
            i = i[iou.sum(1) > 1]  # require redundancy

    output[xi] = x[i]
    if (time.time() - t) > time_limit:
        LOGGER.warning(f'WARNING: NMS time limit {time_limit}s exceeded')
        break  # time limit exceeded`

I found above in utils/general.py where i think should be modified,but i have difficulty in how to do this because the return type is not the same.Would you please provide a detail code?thanks very much.

shenyi0220 commented 2 years ago

You can check below commit: https://github.com/shenyi0220/yolov5/commit/ded82ccc57ed9f29f9cd6f5d4f1812dd86714f16

Actually in my previously uploaded yolov5 repo(https://github.com/shenyi0220/yolov5), this is the only modification.

535484159 commented 2 years ago

非常感谢您的回答!我也认为这是一个非常好的算法,但我成功将CP-Cluster运用在yolov5上训练自定义数据集(train:600/val:180)时,mAP_0.5:0.95和mAP_0.5均出现1%~1.5%的下降,请问这是否是一个正常现象呢,也许这个聚类不适合我的数据集?

shenyi0220 commented 2 years ago

你的val data和model如果可以share的话,我抽空可以刷一把。除了coco数据集,我在公司产品里自动驾驶2D目标检测的模型里也验证过是有效的。 如果要调参的话,可以试试调整nms.cpp下面加粗的参数(1-3),但一般变化不会太大: return cp_cluster_impl(boxes, scores, dets, iou_threshold, min_score, offset, 0.8f, 0, 3);

shenyi0220 commented 2 years ago

此外还可以试一试用mmcv原版的softnms是不是也有类似accuracy regression,有些模型对这种调confidence的后处理不是很match,CP的代码中把这行注释改回来替换CP的实现就行 https://github.com/shenyi0220/CP-Cluster/blob/main/src/mmcv/ops/csrc/pytorch/nms.cpp#L25

535484159 commented 2 years ago

抱歉,数据暂时还不方便分享,具体是用于番茄植株的果实检测中。我用softnms尝试了一下,发现的确有类似的accuracy regression

               mAP_0.5       mAP_0.5:0.95
nms            0.93242          0.74637
softnms        0.92659          0.72433 
CP             0.92087          0.73015
shenyi0220 commented 2 years ago

嗯,那看上去是有可能不适配。 另外我刚刚有以下更新,也许你可以试一试 @535484159 : --最近在yolov5中fix了一个可能对CP造成影响的issue,已上传倒我自己的yolov5 repo, 可以试试看会不会影响结果: commit --默认启用了CP中box coordinate update的选项,会对AP50稍微友好一些,更新代码重新编译下mmcv看看accuracy有没有变化 --相对于NMS的IOU threshold,尝试在CP中用更低的IOU阈值。比如Yolov5中默认IOU是0.65,在CP中用0.6或者0.55。一般IOU阈值越低对AP50越友好。

535484159 commented 2 years ago

感谢回复,我用以上方法改进后发现AP50确实较之前有显著提升,约1%。

River-Cold commented 2 years ago

此外还可以试一试用mmcv原版的softnms是不是也有类似accuracy regression,有些模型对这种调confidence的后处理不是很match,CP的代码中把这行注释改回来替换CP的实现就行 https://github.com/shenyi0220/CP-Cluster/blob/main/src/mmcv/ops/csrc/pytorch/nms.cpp#L25

@shenyi0220 大佬您好,我clone了您最新的yolov5仓库,并在官方指南中推荐的coco128数据集上使用自带的模型yolov5s.pt进行了测试,发现仍然存在accuracy regression

环境版本

pytorch版本:1.9.1 cuda版本:10.2

测试指令

python val.py --iou 0.6 --data coco128.yaml --weights yolov5s.pt

测试结果

NMS | model | mAP50 -- | -- | -- torchvision.ops.nms | yolov5s.pt | 0.732 CP-cluster | yolov5s.pt | 0.731

麻烦大佬帮忙看一下可能是什么原因造成的吗?

River-Cold commented 2 years ago

感谢回复,我用以上方法改进后发现AP50确实较之前有显著提升,约1%。

@535484159 楼主您好,请问一下您还记得使用了哪些方法进行改进的吗?我在自己的数据集上也遇到了accuracy regression的相同问题

shenyi0220 commented 2 years ago

coco128我暂时没验证过, 之前说的对AP50有影响的参数如下: ----相对于NMS的IOU threshold,尝试在CP中用更低的IOU阈值。比如Yolov5中默认IOU是0.65,在CP中用0.6或者0.55。一般IOU阈值越低对AP50越友好。