question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving data dict to COCO format

See original GitHub issue

Thanks for the excellent work! I’ve encountered some issues around saving my own annotations into COCO format.

How To Reproduce the Issue

from detectron2.data.datasets.coco import convert_to_coco_json
from detectron2.data import MetadataCatalog, DatasetCatalog
def data_dict():
    return [{
        'file_name': 'image.png',
        'image_id': 'image',
        'height': 100,
        'width': 100,
        'annotations': [{
            'bbox': [70, 30, 100, 70],
            ## area should be 30*40=1200
            'bbox_mode': BoxMode.XYXY_ABS,
            'category_id': 0,
            'iscrowd': 0,
        }]
    }]

def to_json():
    DatasetCatalog.register('test', data_dict)
    MetadataCatalog.get('test').set(thing_classes=["first"])
    convert_to_coco_json('test', output_folder='./output', allow_cached=False)

if __name__ == '__main__':
    to_json()

After running the script above, in ./output/test_coco_format.json I get: (formatted)

{
    "info": {
        "date_created": "2019-11-19 23:23:08.401029",
        "description": "Automatically generated COCO json file for Detectron2."
    },
    "images": [
        {
            "id": "image",
            "width": 100,
            "height": 100,
            "file_name": "image.png"
        }
    ],
    "annotations": [
        {
            "id": 1,
            "image_id": "image",
            "bbox": [
                70.0,
                30.0,
                30.0,
                40.0
            ],
            "area": -400.0,
            "category_id": 0,
            "iscrowd": 0
        }
    ],
    "categories": [
        {
            "id": 0,
            "name": "first"
        }
    ],
    "licenses": null
}

Expected behavior

The area in annotation should be positive (1200) I think the problem is here, but I’m not sure how to correct it. The .area() function isn’t considering its BoxMode.
BTW, Is saving my own annotation into COCO format and registering it by register_coco_instances the best practice when using COCO_Evaluator?

Environment

------------------------  --------------------------------------------------
sys.platform              linux
Python                    3.7.3 (default, Mar 27 2019, 22:11:17) [GCC 7.3.0]
Numpy                     1.17.4
Detectron2 Compiler       GCC 7.4
Detectron2 CUDA Compiler  10.0
DETECTRON2_ENV_MODULE     <not set>
PyTorch                   1.3.1
PyTorch Debug Build       False
torchvision               0.4.2
CUDA available            True
GPU 0,1,2,3               GeForce RTX 2080 Ti
CUDA_HOME                 /usr/local/cuda-10.0
NVCC                      Cuda compilation tools, release 10.0, V10.0.130
Pillow                    6.2.1
cv2                       4.1.1
------------------------  --------------------------------------------------
PyTorch built with:
  - GCC 7.3
  - Intel(R) Math Kernel Library Version 2019.0.4 Product Build 20190411 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v0.20.5 (Git Hash 0125f28c61c1f822fd48570b4c1066f96fcb9b2e)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - NNPACK is enabled
  - CUDA Runtime 10.1
  - NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_37,code=compute_37
  - CuDNN 7.6.3
  - Magma 2.5.1
  - Build settings: BLAS=MKL, BUILD_NAMEDTENSOR=OFF, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Wno-stringop-overflow, DISABLE_NUMA=1, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=True, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF, 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
botcscommented, Nov 19, 2019

Hi, nice catch!

@ppwwyyxx BoxMode should be considered in https://github.com/facebookresearch/detectron2/blob/master/detectron2/structures/boxes.py#L118 not in convert_to_coco_json

@donnydonny123 if you do not use external tools that require your data to be in COCO format it is indeed quite pointless.

0reactions
ppwwyyxxcommented, Nov 22, 2019

Yes, the Boxes class is defined to be in XYXY_ABS mode only. So a conversion before constructing Boxes is needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to work with object detection datasets in COCO format
The “COCO format” is a specific JSON structure dictating how labels and metadata are saved for an image dataset. Many blog posts exist...
Read more >
Create COCO Annotations From Scratch - Immersive Limit
This tutorial will teach you how to create a simple COCO-like dataset from scratch. It gives example code and example JSON annotations.
Read more >
Source code for detectron2.data.datasets.coco
Returns: list[dict]: a list of dicts in Detectron2 standard dataset dicts ... allow_cached=True): """ Converts dataset into COCO format and saves it to...
Read more >
Transform Object Detection dataset… | Apple Developer Forums
COCO has set a standard in Object Detection task dataset format. Is there a tool for translating this dataset format to CreateML format...
Read more >
Build Custom COCO Dataset 512x512 Tiled - Kaggle
Convert Annotations to COCO Format ... Image.fromarray(img_array) im_out.save(out_img_file) data['images'].append(dict( license=0, url=None, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found