Skip to content

Instantly share code, notes, and snippets.

@botcs
Created December 19, 2019 23:56
Show Gist options
  • Select an option

  • Save botcs/bd54d5c78274435ec4e1721c11b2ca97 to your computer and use it in GitHub Desktop.

Select an option

Save botcs/bd54d5c78274435ec4e1721c11b2ca97 to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from pycocotools.coco import COCO\n",
"from pycocotools.cocoeval import COCOeval\n",
"from copy import deepcopy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"orig_gt_path = \"/home/botoscs/data/MS-COCO/annotations/instances_minival2014.json\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"gt = json.load(open(orig_gt_path))\n",
"\n",
"gt[\"annotations\"][:] = gt[\"annotations\"][:1]\n",
"del gt[\"annotations\"][0][\"segmentation\"]\n",
"\n",
"pred = deepcopy(gt)\n",
"pred[\"annotations\"][0][\"score\"] = 1.0\n",
"\n",
"# Duplicate the detections for the very same object\n",
"pred[\"annotations\"] += pred[\"annotations\"]\n",
"pred = pred[\"annotations\"]\n",
"\n",
"json.dump(gt, open(\"gt.json\", \"w\"))\n",
"json.dump(pred, open(\"pred.json\", \"w\"))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'area': 702.10575,\n",
" 'iscrowd': 0,\n",
" 'image_id': 289343,\n",
" 'bbox': [473.07, 395.93, 38.65, 28.67],\n",
" 'category_id': 18,\n",
" 'id': 1768}]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gt[\"annotations\"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'area': 702.10575,\n",
" 'iscrowd': 0,\n",
" 'image_id': 289343,\n",
" 'bbox': [473.07, 395.93, 38.65, 28.67],\n",
" 'category_id': 18,\n",
" 'id': 1768,\n",
" 'score': 1.0},\n",
" {'area': 702.10575,\n",
" 'iscrowd': 0,\n",
" 'image_id': 289343,\n",
" 'bbox': [473.07, 395.93, 38.65, 28.67],\n",
" 'category_id': 18,\n",
" 'id': 1768,\n",
" 'score': 1.0}]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pred"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loading annotations into memory...\n",
"Done (t=0.02s)\n",
"creating index...\n",
"index created!\n",
"Loading and preparing results...\n",
"DONE (t=0.00s)\n",
"creating index...\n",
"index created!\n"
]
}
],
"source": [
"cocoGt = COCO(\"gt.json\")\n",
"cocoDt = cocoGt.loadRes(\"pred.json\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running per image evaluation...\n",
"Evaluate annotation type *bbox*\n",
"DONE (t=3.14s).\n",
"Accumulating evaluation results...\n",
"DONE (t=0.52s).\n",
" Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 1.000\n",
" Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 1.000\n",
" Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 1.000\n",
" Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 1.000\n",
" Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000\n",
" Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000\n",
" Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 1.000\n",
" Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 1.000\n",
" Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 1.000\n",
" Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 1.000\n",
" Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = -1.000\n",
" Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = -1.000\n"
]
}
],
"source": [
"cocoEval = COCOeval(cocoGt,cocoDt,\"bbox\")\n",
"cocoEval.evaluate()\n",
"cocoEval.accumulate()\n",
"cocoEval.summarize()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment