Previously, we have trained a mmdetection model with custom annotated dataset in Pascal VOC data format. You are out of luck if your object detection training pipeline require COCO data format since the labeling tool we use does not support COCO annotation format. If you still want to stick with the tool for annotation and later convert your annotation to COCO format, this post is for you.
We will start with a brief introduction to the two annotation formats, followed with an introduction to the conversion script to convert VOC to COCO format, finally, we will validate the converted result by plotting the bounding boxes and class labels.
8 Skills You Need to Become a Data Scientist | Data Driven Investor
Numbers do not scare you? There is nothing more satisfying than a beautiful excel sheet? You speak several languages…
Pascal VOC and COCO annotations
Pascal VOC annotations are saved as XML files, one XML file per image. For an XML file generated by the labeling tool. It contains the path to the image in the <path> element. Each bounding box is stored in an <object> element, an example can look like below.
As you can see the bounding box is defined by two points, the upper left and bottom right corners.
For the COCO data format, first of all, there is only a single JSON file for all the annotation in a dataset or one for each split of datasets(Train/Val/Test).
The bounding box is express as the upper left starting coordinate and the box width and height, like
Here is an example for the COCO data format JSON file which just contains one image as seen the top-level “images” element, 3 unique categories/classes in total seen in top-level “categories” element and 2 annotated bounding boxes for the image seen in top-level “annotations” element.
Convert Pascal VOC to COCO annotation
Once you have some annotated XML and images files, put them in the following folder structures similar the one below,
│ ├── 0.xml
│ ├── ...
│ └── 9.xml
Then you can run the voc2coco.py script from my GitHub like this which will generate a COCO data formatted JSON file for you.
python voc2coco.py ./data/VOC/Annotations ./data/coco/output.json
Once we have the JSON file, we can visualize the COCO annotation by drawing bounding box and class labels as an overlay over the image. Open the COCO_Image_Viewer.ipynb in Jupyter notebook. Find the following cell inside the notebook which calls the
display_image method to generate an SVG graph right inside the notebook.
html = coco_dataset.display_image(0, use_url=False)
The first argument is the image id, for our demo datasets, there are totally 18 images, so you can try setting it from 0 to 17.
Conclusion and further reading
In this quick tutorial, you have learned how you can stick with the popular labeling for custom dataset annotation and later convert the Pascal VOC to COCO dataset to train an object detection model pipeline requires COCO format datasets.
You might find the following links useful
How to train an object detection model with mmdetection — my previous post about creating custom Pascal VOC annotation files and train an object detection model with PyTorch mmdetection framework.
Download labelImg for the bounding box annotation.
Get the source code for this post, check out my GitHub repo.
Originally published at www.dlology.com.