How to create a MS COCO style dataset to use with TensorFlow? Does anyone have an experience with this? I have images, and annotations, as well as ground truth masks. I need to convert them to be compatible with MS COCO and any help is appreciated. I can't find any open source tool to create COCO style JSON annotations.
TensorFlow MS COCO reads JSON files which I'm not very experienced with.
I'm working on a python library which has many useful classes and functions for doing this. It's called Image Semantics.
Here is an example of adding masks and exporting them in COCO format:
from imantics import Mask, Image, Category
image = Image.from_path('path/to/image.png')
mask = Mask(mask_array)
image.add(mask, category=Category("Category Name"))
# dict of coco
coco_json = image.export(style='coco')
# Saves to file
image.save('coco/annotation.json', style='coco')
CREATING COCO STYLE DATASETS AND USING ITS API TO EVALUATE METRICS
Let's assume that we want to create annotations and results files for an object detection task (So, we are interested in just bounding boxes). Here is a simple and light-weight example which shows how one can create annoatation and result files appropriately formatted for using COCO API metrics.
Annotation file: ann.json
{"images":[{"id": 73}],"annotations":[{"image_id":73,"category_id":1,"bbox":[10,10,50,100],"id":1,"iscrowd": 0,"area": 10}],"categories": [{"id": 1, "name": "person"}, {"id": 2, "name": "bicycle"}, {"id": 3, "name": "car"}]}
Result file: res.json
[{"image_id":73,"category_id":1,"bbox":[10,10,50,100],"score":0.9}]
Now, you can simply use the following script to evaluate the COCO metrics:
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
annFile = './ann.json'
resFile='./res.json'
cocoGt=COCO(annFile)
cocoDt=cocoGt.loadRes(resFile)
annType = 'bbox'
cocoEval = COCOeval(cocoGt,cocoDt,annType)
cocoEval.evaluate()
cocoEval.accumulate()
cocoEval.summarize()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With