Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using generated .json with CocoDetection dataset class from PyTorch #14

Open
aitor-martinez-seras opened this issue Jan 10, 2023 · 0 comments

Comments

@aitor-martinez-seras
Copy link

Hello everyone,

First of all, thanks for sharing, this repo has been so useful for me. I will explain a possible change to the script for those who want to use the CocoDetection class from PyTorch to load the dataset, just in case anyone faces the same issues as me.

I'm using PyTorch to train a Faster RCNN object detector, and all the model expects the data in the form defined by CocoDetection dataset class from PyTorch. The .json created with the script has one issue when using it with the mentioned dataset class. It has to do with image ids. The problem is that image ids are created as strings inside the .json, what then causes problems in CocoDetection class when loading images, as it uses the image id to lod the image and checks internally several times if the id is an integer, following a different loading path if not and causing exceptions.

In order to solve this, the easiest solution I've found is to just define the image ids as integers in the script, rather than strings. As images are labeled year_xxxxxxx.jpg, the id will be year_xxxxxxx. If this string is converted to integer, when converting the ids to a integers it just will convert them to yearxxxxxxx, therefore preserving the uniqueness of the id and not causing error when loading images with CocoDetection class.

The solution is to modify the voc2coco.py in the lines 51 to 56 of the script:
Current version:

image_info = {
        'file_name': filename,
        'height': height,
        'width': width,
        'id': img_id
    }

Modification:

image_info = {
        'file_name': filename,
        'height': height,
        'width': width,
        'id': int(img_id)
    }

And that's it, just casting the img_id variable to integer.

I hope this will be useful for someone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant