facebookresearch / Detectron

FAIR's research platform for object detection research, implementing popular algorithms like Mask R-CNN and RetinaNet.
Apache License 2.0
26.21k stars 5.45k forks source link

Convert Cityscapes to COCO format: How to convert to other classes (ex: traffic light) #1033

Open MarceloContreras opened 1 year ago

MarceloContreras commented 1 year ago

Has somebody successfully converted cityscapes dataset filtering classes such as traffic lights, poles, etc? Indeed, I did the change of desired classes in _categoryinstancesonly and also commented on the invalid contour warning. However, even if I did that, I still get .json output files with 0 annotations after loading all the images.

Aryan-Mishra24 commented 1 year ago

To successfully convert the Cityscapes dataset while filtering specific classes such as traffic lights and poles, you can follow the steps below:

First, clone the Cityscapes scripts repository: bash

git clone https://github.com/mcordts/cityscapesScripts.git cd cityscapesScripts/cityscapesscripts/preparation Modify the createTrainIdInstanceImgs.py script to filter out specific classes. In the createTrainIdInstanceImgs.py file, find the following lines: python

Load the annotation

objects = csHelpers.loadAnnotaton(labelImgPath) Add a filtering function after the lines mentioned above: python

def filter_objects(objects, classes_to_keep): filtered_objects = [] for obj in objects: if obj.label.name in classes_to_keep: filtered_objects.append(obj) return filtered_objects

classes_to_keep = ['traffic light', 'pole'] objects = filter_objects(objects, classes_to_keep) Replace the classes_to_keep list with the classes you want to keep in the final annotations.

In the json2instanceImg.py script, find the following line: python

if not anno['regions']: Replace the above line with the following: python

if len(anno['regions']) == 0: Now you can run the createTrainIdInstanceImgs.py script to generate the filtered dataset: bash

python createTrainIdInstanceImgs.py --cityscapesPath path/to/cityscapes --outputPath path/to/output Finally, run the json2instanceImg.py script to convert the modified annotations to JSON files: bash python json2instanceImg.py --cityscapesPath path/to/cityscapes --outputPath path/to/output After following these steps, the output JSON files should contain only the filtered classes (e.g., traffic lights and poles). Make sure to replace path/to/cityscapes with the path to your Cityscapes dataset and path/to/output with your desired output path.