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.

KeyError while trying to retrain on Pascal

See original GitHub issue

Hello

I am facing a little issue. I am trying to retrain the model on Pascal Voc 2012 dataset. I took the coco like annotations from this source: https://github.com/facebookresearch/multipathnet

Then I follow the instruction concerning the modification to do in the file config.py

But when I call : python train.py --config=yolact_base_config

I receive the following error:

KeyError: 'Traceback (most recent call last):\n File "/home/smile/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop\n samples = collate_fn([dataset[i] for i in batch_indices])\n File "/home/smile/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>\n samples = collate_fn([dataset[i] for i in batch_indices])\n File "/hdd1/prog/yolact/data/coco.py", line 88, in __getitem__\n im, gt, masks, h, w, num_crowds = self.pull_item(index)\n File "/hdd1/prog/yolact/data/coco.py", line 145, in pull_item\n target = self.target_transform(target, width, height)\n File "/hdd1/prog/yolact/data/coco.py", line 39, in __call__\n label_idx = self.label_map[obj[\'category_id\']] - 1\nKeyError: 12\n'

The error is quite not clear to me.

So what I did is create a new dataset:

PASCAL_VOC_CLASSES = ("aeroplane", "bicycle", "bird", "boat", "bottle",
		      "bus", "car", "cat", "chair", "cow", "diningtable",
        	      "dog", "horse", "motorbike", "person", "pottedplant",
		      "sheep", "sofa", "train", "tvmonitor")


PASCAL_VOC_LABEL_MAP = { 1:  1,  2:  2,  3:  3,  4:  4,  5:  5,  6:  6,  7:  7,  8:  8,
                   9:  9, 10: 10, 11: 11, 13: 12, 14: 13, 15: 14, 16: 15, 17: 16,
                  18: 17, 19: 18, 20: 19, 21: 20}

pascalvoc2012_dataset = dataset_base.copy({
    'name': 'PASCAL VOC 2012',
    
    'train_images':'/media/smile/45C142AD782A7053/Datasets/PASCAL_VOC/VOC2012/VOCdevkit/VOC2012/JPEGImages/',
    'train_info':'/home/smile/multipathnet/data/annotations/pascal_train2012.json',

    'valid_images':'/media/smile/45C142AD782A7053/Datasets/PASCAL_VOC/VOC2012/VOCdevkit/VOC2012/JPEGImages/',
    'valid_info':'/home/smile/multipathnet/data/annotations/pascal_val2012.json',

    'label_map': PASCAL_VOC_LABEL_MAP
})

I created a new base_config that only which call the dataset I previously created with the proper number of classes:

pascalvoc_base_config = Config({
    'dataset': pascalvoc2012_dataset,
    'num_classes': 21, # This should include the background class
...

All the other fields are let untouch.

Finally I adapted yolact_base_config:

#yolact_base_config = coco_base_config.copy({
yolact_base_config = pascalvoc_base_config.copy({
    'name': 'yolact_base',

    # Dataset stuff
#    'dataset': coco2017_dataset,
#    'num_classes': len(coco2017_dataset.class_names) + 1,

    'dataset': pascalvoc2012_dataset,
    'num_classes': len(pascalvoc2012_dataset.class_names) + 1,

Here also all the other fields are let untouch.

EDIT

After applying the modifications discussed here the dataset configuration in order to train Pascal Voc is:

MEANS_PV = (103.17, 111.70, 116.69)
STD_PV = (61.11, 59.89, 61.00)

PASCAL_VOC_CLASSES = ("aeroplane", "bicycle", "bird", "boat", "bottle",
		      "bus", "car", "cat", "chair", "cow", "diningtable",
        	      "dog", "horse", "motorbike", "person", "pottedplant",
		      "sheep", "sofa", "train", "tvmonitor")


PASCAL_VOC_LABEL_MAP = { 1:  1,  2:  2,  3:  3,  4:  4,  5:  5,  6:  6,  7:  7,  8:  8,
                   9:  9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16,
                  17: 17, 18: 18, 19: 19, 20: 20}

pascalvoc2012_dataset = dataset_base.copy({
    'name': 'PASCAL VOC 2012',
    
    'train_images':'/media/smile/45C142AD782A7053/Datasets/PASCAL_VOC/VOC2012/VOCdevkit/VOC2012/JPEGImages/',
    'train_info':'/home/smile/multipathnet/data/annotations/pascal_train2012.json',

    'valid_images':'/media/smile/45C142AD782A7053/Datasets/PASCAL_VOC/VOC2012/VOCdevkit/VOC2012/JPEGImages/',
    'valid_info':'/home/smile/multipathnet/data/annotations/pascal_val2012.json',

    'label_map': PASCAL_VOC_LABEL_MAP,
    'class_names': PASCAL_VOC_CLASSES,
})

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
dbolyacommented, May 13, 2019

The reason for COCO_LABEL_MAP to exist is because the category ids in COCO aren’t sequential. During development they removed some classes (there were originally 90) but kept the IDs the same, so while for instance toothbrush is the 80th element in COCO_CLASSES, in the annotations it’s actually category_id 90. Thus for COCO, that map is necessary to get from category_id to index in the class_names list. For most datasets without that kind of issue, it can be omitted (set to None, which will load the identity map; i.e., x maps to x).

So I don’t think you need a label_map at all. Just set label_map to None and it should work.

2reactions
dbolyacommented, May 13, 2019

Glad to hear that! Feel free to let me know if you have any other issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

There is "KeyError:face" when i convert pascal dataset to tfrecord
You can convert the widerface dataset to TFRecords using the below methods. 1.You need to create a config.py file.
Read more >
Cannot read Pascal 2007 into a Fastai2 dataloader - fastai dev
I am trying to port a Pascal 2007-based object detection application to fastai2, but it fails reading the Pascal data set into the...
Read more >
How to Fix KeyError Exceptions in Python - Rollbar
The Python KeyError is an exception that occurs when an attempt is made to access an item in a dictionary that does not...
Read more >
Most Common Python Error Types in Data Science
When you try to perform an operation in the wrong type of object, ... KeyError is when a key is not found in...
Read more >
How to Train YOLO v5 on a Custom Dataset - Paperspace Blog
While you may want to train with a larger dataset (like the LISA Dataset) to fully ... Annotations for the dataset we downloaded...
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