cocodataset / cocoapi

COCO API - Dataset @ http://cocodataset.org/
Other
6.1k stars 3.76k forks source link

A mistake in the implementation of `catId = coco.getCatIds(catNms=cat_name)` #291

Open JingyunLiang opened 5 years ago

JingyunLiang commented 5 years ago

When using catId = coco.getCatIds(catNms=cat_name) to get category id from category name, the function returns two category ids ([3,57]) for the name carrot, because it shares same characters with car.

Just run the simple demo:

from pycocotools.coco import COCO

dataDir='/home/daisy/Downloads'
dataType='val2017'
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)

# initialize COCO api for instance annotations
coco=COCO(annFile)

# display COCO categories and supercategories
cats = coco.loadCats(coco.getCatIds())
cat_nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(cat_nms)))

for cat_name in cat_nms:
    catId = coco.getCatIds(catNms=cat_name)
    if cat_name == "carrot":
        print(catId)
    imgId = coco.getImgIds(catIds=catId)
    annId = coco.getAnnIds(imgIds=imgId, catIds=catId, iscrowd=False)

    # print("{:<15} {:<6d}     {:<10d}".format(cat_name, len(imgId), len(annId)))

It prints

loading annotations into memory...
Done (t=0.46s)
creating index...
index created!
COCO categories: 
person bicycle car motorcycle airplane bus train truck boat traffic light fire hydrant stop sign parking meter bench bird cat dog horse sheep cow elephant bear zebra giraffe backpack umbrella handbag tie suitcase frisbee skis snowboard sports ball kite baseball bat baseball glove skateboard surfboard tennis racket bottle wine glass cup fork knife spoon bowl banana apple sandwich orange broccoli carrot hot dog pizza donut cake chair couch potted plant bed dining table toilet tv laptop mouse remote keyboard cell phone microwave oven toaster sink refrigerator book clock vase scissors teddy bear hair drier toothbrush

[3, 57]

The same mistake happens to bear & teddy beardog & hot dog.

It's really tricky when conducting statistical analysis.

ZwwWayne commented 4 years ago

Hi @JingyunLiang , This is because you directly send the string cat_name into the function, and it makes something like the following code:

if 'car' in 'carrot':
    print(True)

Changing catId = coco.getCatIds(catNms=cat_name) to catId = coco.getCatIds(catNms=[cat_name]) could avoid this problem.