yolov8计算iou=0.5下的APs、APm、APl

  • Post author:
  • Post category:其他


计算v8的APs、APm、APl,是直接下载安装pycocotools工具包,然后直接导入运行,然后得到结果,但此时的APs、APm、APl的iou是

IoU=0.50:0.95

,要想更改

IoU=0.50:0.5

,就得下载pycocotools的源码

1、直接下载安装pycocotools工具包,然后直接导入运行的脚本

conda activate mmrota

pycocotools工具包是安装在

mmrota

的环境下

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval


if __name__ == '__main__':

    anno_json = '/F/nyj/dataset/wuliao-5k/val/val_json-one/wuliao-5k-val-annotations.json'
    pred_json = '/F/nyj/ultralytics-main/detect-wuliao-val/v8m-densenet-p2-pt-wuliao/predictions.json'


    anno = COCO(anno_json)  		# init annotations api
    pred = anno.loadRes(pred_json)  # init predictions api
    eval = COCOeval(anno, pred, 'bbox')

    eval.evaluate()
    eval.accumulate()
    eval.summarize()
    map, map50 = eval.stats[:2]  # update results (mAP@0.5:0.95, mAP@0.5)
    print(eval.stats)

2、更改

IoU=0.50:0.5

,首先下载pycocotools的源码

地址

https://github.com/philferriere/cocoapi


更改脚本为:


import sys
sys.path.append('/F/nyj/ultralytics-main/cocoapi/PythonAPI/pycocotools/')
from coco import COCO
from  cocoeval import COCOeval

if __name__ == '__main__':

    anno_json = '/F/nyj/dataset/wuliao-5k/val/val_json-one/wuliao-5k-val-annotations.json'
    pred_json = '/F/nyj/ultralytics-main/detect-wuliao-val/v8m-densenet-p2-pt-wuliao/predictions.json'


    anno = COCO(anno_json)  		# init annotations api
    pred = anno.loadRes(pred_json)  # init predictions api
    eval = COCOeval(anno, pred, 'bbox')

    eval.evaluate()
    eval.accumulate()
    eval.summarize()
    map, map50 = eval.stats[:2]  # update results (mAP@0.5:0.95, mAP@0.5)
    print(eval.stats)

运行上述脚本后会报错

TypeError: 'numpy.float64' object cannot be interpreted as an integer

网上说是numpy的版本过高,要降低其版本,但更改numpy的版本可能会影响已经建立好的环境,所以网上有个大佬给出了另一个方法,有效:

参考:

https://blog.csdn.net/bieliwuguiqi/article/details/123376558


修改

/F/nyj/ultralytics-main/cocoapi/PythonAPI/pycocotools/cocoeval.py

中的部分代码,如下:

将原始的:

self.iouThrs = np.linspace(.5, 0.95, np.round((0.95 - .5) / .05) + 1, endpoint=True)
self.recThrs = np.linspace(.0, 1.00, np.round((1.00 - .0) / .01) + 1, endpoint=True)

修改为:

self.iouThrs = np.linspace(.5, 0.95, 10, endpoint=True)
self.recThrs = np.linspace(.0, 1.00, 101, endpoint=True)

一共要修改2个地方,修改好后就可以运行了,但得到的APs、APm、APl的iou还是

IoU=0.50:0.95

,所以将

self.iouThrs = np.linspace(.5, 0.95, 10, endpoint=True)

修改成:

self.iouThrs = np.linspace(.5, 0.5, 10, endpoint=True)

即把0.95改成0.5,这样就可以得到

IoU=0.50:0.5

的APs、APm、APl了

以上在6063服务器上运行可以,但在418服务器上(windows系统)还会出现一个问题,如下:

(yolov5--wr) F:\UserData2\nyj\yolov5-master>python eval_coco.py
Traceback (most recent call last):
  File "eval_coco.py", line 5, in <module>
    from  cocoeval import COCOeval
  File "F:/UserData2/nyj/cocoapi/PythonAPI/pycocotools\cocoeval.py", line 7, in <module>
    from . import mask as maskUtils
ImportError: attempted relative import with no known parent package

原因和修改方法参考:

https://www.freesion.com/article/2470865013/

修改方法如下:



F:/UserData2/nyj/cocoapi/PythonAPI/pycocotools\cocoeval.py



F:/UserData2/nyj/cocoapi/PythonAPI/pycocotools\cocoeval.py

两个文件中的语句

 from . import mask as maskUtils

修改成:

from pycocotools import mask as maskUtils

然后即可运行



版权声明:本文为nyj_ouc原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。