KeyError while running the model on Custom Dataset
See original GitHub issueHi! Thanks for this repo. I am trying to run this model on a custom dataset and I face this error. Here is the stack trace:
Traceback (most recent call last):
File "train.py", line 386, in <module>
train()
File "train.py", line 215, in train
for datum in data_loader:
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 582, in __next__
return self._process_next_batch(batch)
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 606, in _process_next_batch
raise Exception("KeyError:" + batch.exc_msg)
Exception: KeyError:Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 99, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 99, in <listcomp>
samples = collate_fn([dataset[i] for i in batch_indices])
File "/home/user/yolact/data/coco.py", line 87, in __getitem__
im, gt, masks, h, w, num_crowds = self.pull_item(index)
File "/home/user/yolact/data/coco.py", line 126, in pull_item
file_name = self.coco.loadImgs(img_id)[0]['file_name']
File "/usr/local/lib/python3.6/dist-packages/pycocotools/coco.py", line 229, in loadImgs
return [self.imgs[id] for id in ids]
File "/usr/local/lib/python3.6/dist-packages/pycocotools/coco.py", line 229, in <listcomp>
return [self.imgs[id] for id in ids]
KeyError: 7
The value changes every time. I have shown an example where the KeyError threw was 7. I followed the answer from issue #40 , but still, the error persists. My class id’s start from 1 and end in 10 (inclusive). Here is my config.py
:
my_dataset= dataset_base.copy({
'name': 'my dataset',
'train_images': '/home/user/dataset/train/',
'train_info': '/home/user/dataset/train/via_region_data.json',
'valid_images':'/home/user/dataset/val/',
'valid_info': '/home/user/dataset/val/via_region_data.json',
'has_gt': True,
'class_names': ('BWK12', 'LWK1', 'LWK2', 'LWK3', 'LWK4', 'LWK5', 'SWK1/2', 'Cage', 'Schraube', 'Stab'),
'label_map': {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:10}
})
Here is the configuration:
yolact_base_config = coco_base_config.copy({
'name': 'yolact_base',
# Dataset stuff
'dataset': my_dataset,
'num_classes': len(spine_dataset.class_names)+1,
.........
)}
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (7 by maintainers)
Top Results From Across the Web
Getting a 'KeyError" while trying to access a Custom Dataset
However, when I run the code I get a 'KeyError'. import glob import cv2 import numpy as np import torch from torch.utils.data ...
Read more >KeyError: 'loss' when fine-tuning a Transformer model
I am trying to fine tune a transformer model on my own unlabeled corpus of text. My code for doing this is: from...
Read more >KeyError: 'fasterrcnn_resnet50' - W&B Help - WandB community
I am trying to run this GitHub repository faster rcnn-pytorch-custom-dataset but I got this error. Building model from scratch.
Read more >Getting KeyError: 'id' even when 'id' is present in the json file
Context: I am trying to train the VOC dataset on this few shot object detection library called FewX and in order to do...
Read more >Key Error: Pytorch Custom Dataset class : r/learnpython - Reddit
This seems to be an issue with how you are trying to access values from your pandas dataframe when returning values in __getitem__...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
OH, your last observation is actually critical.
Notice something wrong with your annotations? Specifically on this line:
That’s a string not an integer!
It’s executing the array branch because it’s operating on each character of the string, and it can’t find the strings
'2'
and'7'
, because of course your IDs are integers! I’m not sure why your first stack trace hasKeyError: 7
, but your second hasKeyError: '2'
, which indicates that you passed in a string instead of an integer.So, the solution would be to make sure all the image ids and annotation image_ids are integers (since your first error message had
KeyError: 7
, it might be that image id 7 has a string for its id field, while all annotations for image id 2 have a string for their image_id fields).i also meet the first question. And i change the type to int,it is OK. But i alse meet the second question as you.
File “pycocotools/_mask.pyx”, line 292, in pycocotools._mask.frPyObjects TypeError: object of type ‘int’ has no len()