由于最近沉迷写论文导致课外学习不多(不写毕不了业啊!!!),所以也没有新知识来很更新啊,刚好今天使用COCO API来评估自己的结果,就学习和调试过程中遇到的问题进行记录。
目前绝大多数目标检测算法都使用coco数据集进行模型评估。那么如何验证自己的实验结果,或者自己的数据増广策略、训练调参等是否能提升模型的AP?COCO_API提供了便捷的接口,安装使用及评估过程如下
一、COCO API的下载和安装
官方使用说明
COCO API - Common Objects in Context
COCO is a large image dataset designed for object detection, segmentation, person keypoints detection, stuff segmentation, and caption generation. This package provides Matlab, Python, and Lua APIs that assists in loading, parsing, and visualizing the annotations in COCO. Please visit Common Objects in Context for more information on COCO, including for the data, paper, and tutorials. The exact format of the annotations is also described on the COCO website. The Matlab and Python APIs are complete, the Lua API provides only basic functionality.
In addition to this API, please download both the COCO images and annotations in order to run the demos and use the API. Both are available on the project website.
-Please download, unzip, and place the images in: coco/images/
-Please download and place the annotations in: coco/annotations/
For substantially more details on the API please see Common Objects in Context.
After downloading the images and annotations, run the Matlab, Python, or Lua demos for example usage.
To install:
-For Matlab, add coco/MatlabApi to the Matlab path (OSX/Linux binaries provided)
-For Python, run "make" under coco/PythonAPI
-For Lua, run “luarocks make LuaAPI/rocks/coco-scm-1.rockspec” under coco/
1、从GitHub上下载源码包进行安装,或者使用git clone命令获取
url为:https://github.com/cocodataset/cocoapi
2、我们较为常用的为PythonAPI,进入该目录,在Linux系统该终端输入make进行编译(即在cocoapi/PythonAPI目录下编译)
二、将检测结果保存成标准的json格式
实验的结果应该保存为指定的形式,该格式在COCO_API的result文件夹中存在,以下这篇文章就COCO的主要任务的结果保存形式进行粗略的介绍:
HUST小菜鸡:用于COCO API测试的结果的文件格式zhuanlan.zhihu.com三、使用COCO_API进行评估
在Python_API文件目录建立脚本evaluation.py进行结果评估
评估代码如下:
from
cocoEval = COCOeval(cocoGt, cocoDt, "keypoints")中第三个参数为iouType参数
’segm‘表示分割,‘bbox’表示目标检测,‘keypoints’表示人体关键点检测
在使用过程中遇到的问题进行解析
1、安装scikit-image总是报错
我在使用pip3和pip安装scikit-image的时候总会报错,我的确尝试了很多方法,但是都没有解决,有知道如何解决的小伙伴欢迎评论区和我交流,万分感谢。
对于这种问题,我直接删除了import http://skimage.io as io,不导入这个模块也是可以正常评估的
2、Results do not correspond to current coco set
这种问题经常出现在用自己的数据集的结果进行验证的结果
我们去pycocotools/coco.py这个文件里面去一探究竟
这个断言的作用是:判断你的result.json文件内,所有image的id是否和你annotations中出现的图片id有对应,当有不对应的时候就会出现错误。
由于我的测试的图片有些不在这个trainval2017中,所以导致了这样的错误;或者有些文件没有标注也会出现这种情况,对于这种问题的解决要么就是用官方的数据集进行测试,要么就是把没有标注的文件给删除。
由于COCO val2017数据集共有5000张图片,但是统计xxx_val2017.json文件发现只有4952张有标注,所以再验证集上验证但会出现数据结果不匹配的错误,但是删除些图片的确有点着迷。
3、object of type xxx cannot be safely interpreted as an integer
咱们再到pycocotools/cocoeval.py这个文件中去一探究竟
之前的报错的部分在这里,报错的原因是linespace第三个步数应该是一个int型的证书,但是round函数是一个四舍五入的数字,四舍五入后还是会保留意味小数,例如4.0,所以强制将其转成int型会报错,网上的其他的博客给出的解释是Python2中使用/代表整除,但是Python3就得使用//代表整除,我按照这个方式去改了但是好像没有效果
知道了是转换成int型除了问题,所以我直接用int型对其转换
注意这里不能直接用int去替换round,因为int是向下取整,round是四舍五入,所以只能在外面加上int
4、每次测试改代码中标注文件和结果文件位置好麻烦啊
我上面的那个简单的evaluation.py细心的小伙伴可以发现我的GrounTruth文件位置和结果文件位置是需要在代码中改的,程序的鲁棒性并不是很好,所以可以用命令行参数选项解析的方式进行改进,有关argparse的使用可以参照这篇文章:
HUST小菜鸡:argparse 命令行选项、参数和子命令解析器zhuanlan.zhihu.com改进后的代码
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
import numpy as np
import skimage.io as io
import pylab,json
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-g", "--gt", type=str, help="Assign the groud true path.", default=None)
parser.add_argument("-d", "--dt", type=str, help="Assign the detection result path.", default=None)
args = parser.parse_args()
cocoGt = COCO(args.gt)
cocoDt = cocoGt.loadRes(args.dt)
cocoEval = COCOeval(cocoGt, cocoDt, "keypoints")
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()
解决了上述问题我随便测试了一个结果,仅供参考