ultralytics / JSON2YOLO

Convert JSON annotations into YOLO format.
https://docs.ultralytics.com
GNU Affero General Public License v3.0
860 stars 232 forks source link

Empty and Missing labels after conversion #44

Open henrikuayezee opened 1 year ago

henrikuayezee commented 1 year ago

The number of files after conversion is less than the number of files in json. Also some of the converted files have empty labels.

ryouchinsa commented 1 year ago

Thanks for writing the issue. We check it works using this script global_json2yolo.py.

Looking at the global_json2yolo code, there are some flags. Converting the COCO bbox format to YOLO bbox format.

use_segments=False,
use_keypoints=False,

Converting the COCO segmentation format to YOLO segmentation format.

use_segments=True,
use_keypoints=False,

Converting the COCO keypoints format to YOLO keypoints format.

use_segments=False,
use_keypoints=True,

To convert the COCO segmentation format to YOLO segmentation format.

if __name__ == '__main__':
    source = 'COCO'

    if source == 'COCO':
        convert_coco_json('../datasets/coco/annotations',  # directory with *.json
                          use_segments=True,
                          use_keypoints=False,
                          cls91to80=False)

This is the folder structure when we run the script.

スクリーンショット 2023-10-25 20 51 05

Please let us know your opinion.

glenn-jocher commented 12 months ago

Thanks for reaching out and providing the details. From the script and folder structure you've shared, it looks like the issue could be related to the conversion script and its configuration when converting COCO format to YOLO format. Ensure that the conversion flags (use_segments and use_keypoints) are set appropriately for your dataset, ensuring that the conversion is accurately representing your data. In addition, verify that the source path for the JSON files is correctly specified. If you continue to encounter issues, please feel free to provide more information or reach out for further assistance.

Mary14-design commented 7 months ago

I keep running into the following error,

Annotations /tmp/workspace/coco/annotations/captions_train2017.json: 0%| | 0/118287 [00:00<?, ?it/s] Traceback (most recent call last): File "/tmp/workspace/codes/JSON2YOLO/general_json2yolo.py", line 393, in <module> convert_coco_json( File "/tmp/workspace/codes/JSON2YOLO/general_json2yolo.py", line 283, in convert_coco_json if ann["iscrowd"]: KeyError: 'iscrowd'

The directory annotations contains all the json files that are mentioned on the coco dataset website.

glenn-jocher commented 7 months ago

@Mary14-design it seems like the script encounters a KeyError because it expects every annotation to have the iscrowd field, which might not be present in every annotation in your JSON file. A simple fix can be to check for the presence of iscrowd before accessing it. You can modify the problematic line to:

if ann.get("iscrowd", 0):

This way, if iscrowd doesn't exist, it defaults to 0 (assuming non-crowd), and your script should continue without crashing. Give this a try and let us know how it goes! 😊

ryouchinsa commented 7 months ago

Hi @Mary14-design, thanks for writing the issue.

In the captions dataset, "category_id" , "bbox", "segmentation", and "keypoints" do not exist. If "category_id" does not exist, we cannot save the class id in the YOLO format. So that at this moment, we do not correspond to converting the caption datasets to the YOLO format.

If "bbox", "segmentation", and "keypoints" do not exist, we can assume that this dataset might be for the classification task. But even in the classification task, we need the class id to convert to the YOLO format.

We updated this script global_json2yolo.py , so that when converting the caption dataset, there are no errors, but each text file is empty.

Please let us know your opinion.

Caption dataset.

"annotations": [
  {
      "image_id": 179765,
      "id": 38,
      "caption": "A black Honda motorcycle parked in front of a garage."
  },

Detection, Segmentation, and Keypoints dataset.

"annotations": [
  {
      "segmentation": [
          [125.12, 539.69, 140.94, 522.43, 100.67, 496.54, 84.85, 469.21, 73.35, 450.52, 104.99, 342.65, 168.27, 290.88, 179.78, 288, 189.84, 286.56, 191.28, 260.67, 202.79, 240.54, 221.48, 237.66, 248.81, 243.42, 257.44, 256.36, 253.12, 262.11, 253.12, 275.06, 299.15, 233.35, 329.35, 207.46, 355.24, 206.02, 363.87, 206.02, 365.3, 210.34, 373.93, 221.84, 363.87, 226.16, 363.87, 237.66, 350.92, 237.66, 332.22, 234.79, 314.97, 249.17, 271.82, 313.89, 253.12, 326.83, 227.24, 352.72, 214.29, 357.03, 212.85, 372.85, 208.54, 395.87, 228.67, 414.56, 245.93, 421.75, 266.07, 424.63, 276.13, 437.57, 266.07, 450.52, 284.76, 464.9, 286.2, 479.28, 291.96, 489.35, 310.65, 512.36, 284.76, 549.75, 244.49, 522.43, 215.73, 546.88, 199.91, 558.38, 204.22, 565.57, 189.84, 568.45, 184.09, 575.64, 172.58, 578.52, 145.26, 567.01, 117.93, 551.19, 133.75, 532.49]
      ],
      "num_keypoints": 10,
      "area": 47803.27955,
      "iscrowd": 0,
      "keypoints": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 142, 309, 1, 177, 320, 2, 191, 398, 2, 237, 317, 2, 233, 426, 2, 306, 233, 2, 92, 452, 2, 123, 468, 2, 0, 0, 0, 251, 469, 2, 0, 0, 0, 162, 551, 2],
      "image_id": 425226,
      "bbox": [73.35, 206.02, 300.58, 372.5],
      "category_id": 1,
      "id": 183126
  },
pderrenger commented 7 months ago

Hi @ryouchinsa! Thanks for the update and for diving deep into the conversion of caption datasets to YOLO format. As you've observed, without "category_id", "bbox", "segmentation", and "keypoints", the dataset is not directly compatible with typical YOLO use cases, which generally focus on object detection tasks.

Your approach of allowing the script to run without errors and producing empty text files for the caption dataset is sensible under the circumstances. It ensures that users are aware of the limitations when attempting to apply object detection frameworks to datasets intended for different tasks like captioning.

For anyone looking to work with such datasets in the future, it's important to note that YOLO requires specific annotation formats to train on detection, segmentation, and keypoints tasks effectively. Converting non-compatible datasets might require creative preprocessing or a reconsideration of the dataset's suitability for the intended task.

Thanks for sharing your insights, and feel free to reach out if you have more questions or ideas! 😊