KeyError: 'Traceback (most recent call last):\

When I use pytorch to fine-tune a VGG model in the Stanford Dogs dataset, a mistake accur:
Traceback (most recent call last):
File “/home/zyd/PycharmProjects/Dog Breed Identification/train.py”, line 237, in
train_stanford()
File “/home/zyd/PycharmProjects/Dog Breed Identification/train.py”, line 148, in train_stanford
for idx, (images, labels) in enumerate(data_loader):
File “/home/zyd/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 286, in next
return self._process_next_batch(batch)
File “/home/zyd/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 307, in _process_next_batch
raise batch.exc_type(batch.exc_msg)
KeyError: ‘Traceback (most recent call last):\n File “/home/zyd/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 57, in _worker_loop\n samples = collate_fn([dataset[i] for i in batch_indices])\n File “/home/zyd/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 57, in \n samples = collate_fn([dataset[i] for i in batch_indices])\n File “/home/zyd/PycharmProjects/Dog Breed Identification/dataset.py”, line 157, in getitem\n label_idx = label_map[image_name]\nKeyError: ‘shih’\n’

and I have no idea how it comes

It looks like it’s looking for image named shih in your label map for you dataset but it does not exits.
You should check your dataset code to see why this happens.

1 Like