I am trying to create a custom dataset to pass into torch.utils.data.DataLoader for training: https://pytorch.org/docs/stable/torchvision/models.html#faster-r-cnn
The instructions indicate they want a list of images and a list of (list[Dict[Tensor]]) for the targets with ‘boxes’ and ‘labels’. Now I create this in my dataset getitem which returns the image as a tensor and a dictionary with keys “boxes” and “labels” and a list of 4 floats for “boxes” and an int for “labels”.
When I try training I run into the following issues:
File "/home/mschiappa/Desktop/VisualRelationshipsDetection/run_exp.py", line 118, in run_model
outputs = model(inputs, targets)
File "/home/mschiappa/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "/usr/lib/python3/dist-packages/torchvision/models/detection/generalized_rcnn.py", line 47, in forward
images, targets = self.transform(images, targets)
File "/home/mschiappa/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
result = self.forward(*input, **kwargs)
File "/usr/lib/python3/dist-packages/torchvision/models/detection/transform.py", line 36, in forward
target = targets[i] if targets is not None else targets
KeyError: 0
and when I print what the targets look like:
{'boxes': tensor([[0.6547, 1.0000, 0.4703, 0.4938],
[0.7802, 0.9403, 0.0472, 0.0796],
[0.9897, 0.9823, 0.0000, 0.4879],
[1.0000, 0.4943, 0.8626, 0.3522],
[1.0000, 1.0000, 0.0000, 0.0000],
[1.0000, 0.2750, 0.8687, 0.0000],
[0.9956, 0.7544, 0.8100, 0.5774],
[1.0000, 0.9531, 0.0000, 0.1137]]), 'labels': tensor([227, 445, 432, 307, 390, 281, 419, 333])}
When I look into the GeneralizedRCNN code which is inherited by the fastrcnn, it tries to transform the targets: https://github.com/pytorch/vision/blob/master/torchvision/models/detection/generalized_rcnn.py#L64
Which shouldn’t work since it is a dictionary, and it appears to be where it is failing.
I am unsure how to fix this without modifying the PyTorch fastrcnn code.