关于cocoapi评估时显示特定类别的一些操作
指定一类输出的时候
在cocoapi-master/PythonAPI中构建evaluate.py
构建evaluate.py的内容如下:
"""
COCO-Style Evaluations
"""
import json
import os
import argparse
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
def eval(coco_gt, image_ids, pred_json_path):
# load results in COCO evaluation tool
coco_pred = coco_gt.loadRes(pred_json_path)
# run COCO evaluation
print('BBox')
coco_eval = COCOeval(coco_gt, coco_pred, 'bbox')
coco_eval.params.imgIds = image_ids
coco_eval.params.catIds = [2] # 重点在这句!!!!! !
#2代表的是该类对应的coco数据集上的标签序号,根据现实情况修改即可
coco_eval.evaluate()
coco_eval.accumulate()
coco_eval.summarize()
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('--gt-json', type=str, default='instances.json', help='coco val2017 annotations json files')
ap.add_argument('--pred-json', type=str, default='coco_results.json', help='pred coco val2017 annotations json files')
args = ap.parse_args()
print(args)
pred_json_path = args.pred_json
MAX_IMAGES = 1000
coco_gt = COCO(args.gt_json)
image_ids = coco_gt.getImgIds()[:MAX_IMAGES]
eval(coco_gt, image_ids, pred_json_path)
运行时输入如下命令:
python3 evaluate.py --gt-json [真实标签json路径] --pred-json [预测标签json路径]
指定多类输出的时候
在pycocotools/coco.py更改init部分,具体见:
with open(annotation_file, 'r') as f:
dataset = json.load(f)
assert type(dataset)==dict, 'annotation file format {} not supported'.format(type(dataset))
print('Done (t={:0.2f}s)'.format(time.time()- tic))
print(
"category names: {}".format([e["name"] for e in sorted(dataset["categories"], key=lambda x: x["id"])])) # 重点是这句!!!
然后修改pycocotools/cocoeval.py,具体修改内容为:
#print(iStr.format(titleStr, typeStr, iouStr, areaRng, maxDets, mean_s))
category_dimension = 1 + int(ap)
if s.shape[category_dimension] > 1:
iStr += ", per category = {}"
mean_axis = (0,)
if ap == 1:
mean_axis = (0, 1)
per_category_mean_s = np.mean(s, axis=mean_axis).flatten()
with np.printoptions(precision=3, suppress=True, sign=" ", floatmode="fixed"):
print(iStr.format(titleStr, typeStr, iouStr, areaRng, maxDets, mean_s, per_category_mean_s))
else:
print(iStr.format(titleStr, typeStr, iouStr, areaRng, maxDets, mean_s, ""))
通过这个修改之后,运行evaluate.py,会按照[类别1,类别2,…,类别n]的形式打印出各AP和AR值。