evaluate函数使用无效_使用COCO API评估模型在COCO数据集上的结果

4b98c2f8436019d1fadd1f8c4663fcd7.png
由于最近沉迷写论文导致课外学习不多(不写毕不了业啊!!!),所以也没有新知识来很更新啊,刚好今天使用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

c80880738da85b6b67d7324b3d66e829.png

2、我们较为常用的为PythonAPI,进入该目录,在Linux系统该终端输入make进行编译(即在cocoapi/PythonAPI目录下编译)

c2c37501f9b73cfa601e2187e79d984f.png

二、将检测结果保存成标准的json格式

实验的结果应该保存为指定的形式,该格式在COCO_API的result文件夹中存在,以下这篇文章就COCO的主要任务的结果保存形式进行粗略的介绍:

HUST小菜鸡:用于COCO API测试的结果的文件格式​zhuanlan.zhihu.com
3df7d8a2a8c91bd07b4dd42d0c23d3bd.png

三、使用COCO_API进行评估

在Python_API文件目录建立脚本evaluation.py进行结果评估

评估代码如下:

from 

cocoEval = COCOeval(cocoGt, cocoDt, "keypoints")中第三个参数为iouType参数

a13818bbd0f6c708107cde9add8fc28a.png

’segm‘表示分割,‘bbox’表示目标检测,‘keypoints’表示人体关键点检测

在使用过程中遇到的问题进行解析

1、安装scikit-image总是报错

我在使用pip3和pip安装scikit-image的时候总会报错,我的确尝试了很多方法,但是都没有解决,有知道如何解决的小伙伴欢迎评论区和我交流,万分感谢。

4ff94a256f624ce60bf7f9add6a9a582.png

对于这种问题,我直接删除了import http://skimage.io as io,不导入这个模块也是可以正常评估的

2、Results do not correspond to current coco set

这种问题经常出现在用自己的数据集的结果进行验证的结果

855ca228d2b2ae807ce18ed59e679477.png

我们去pycocotools/coco.py这个文件里面去一探究竟

24c7f2ff0546109c00c47fa1de923fde.png

这个断言的作用是:判断你的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

4bb51b6b66378d91602c3e36bc2241d3.png

咱们再到pycocotools/cocoeval.py这个文件中去一探究竟

之前的报错的部分在这里,报错的原因是linespace第三个步数应该是一个int型的证书,但是round函数是一个四舍五入的数字,四舍五入后还是会保留意味小数,例如4.0,所以强制将其转成int型会报错,网上的其他的博客给出的解释是Python2中使用/代表整除,但是Python3就得使用//代表整除,我按照这个方式去改了但是好像没有效果

78f55562903c9087c6baa6f68df2f4c9.png

知道了是转换成int型除了问题,所以我直接用int型对其转换

a5974089fb7e0ffa4c89bbfb59a27b15.png

注意这里不能直接用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()

解决了上述问题我随便测试了一个结果,仅供参考

7494382ed58cb512e09becdcba3a4050.png

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值