facebookresearch / facebookresearch/Detic

Custom dataset, dataloader returns list[list[dict]]

Open
#33 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2k
Forks
228
PR merge metrics
No merged PRs in 30d

Description

I am having a curious problem with a custom dataset, registered with a custom dataset function.

The Detectron2 setup and `cfg` is copied from [Detic Colab tutorial](https://colab.research.google.com/drive/1QtTW9-ukX2HKZGvt0QvVGqjuqEykoZKI)

A snippet of my code:
```
from detectron2.modeling import build_model
from detectron2.data import build_detection_test_loader
import torch

def my_dataset_generator():
data_list = []
for index, observation in df.iterrows():
data_dict = {}
data_dict["file_name"] = observation['file_path']
data_dict['image_id'] = observation['image']
data_list.append(data_dict)
return data_list

DatasetCatalog.register("my_whales3", my_dataset_generator)

model = build_model(cfg) # returns a torch.nn.Module
test_loader = build_detection_test_loader(cfg, 'my_whales3')
```
which returns
```
[03/01 12:05:54 d2.data.dataset_mapper]: [DatasetMapper] Augmentations used in inference: [ResizeShortestEdge(short_edge_length=(800, 800), max_size=1333, sample_style='choice')]
[03/01 12:05:54 d2.data.common]: Serializing 51033 elements to byte tensors and concatenating them all ...
[03/01 12:05:54 d2.data.common]: Serialized dataset takes 6.42 MiB
```
Note that the output format of ` my_dataset_generator()` is `list[dict]`. Also, the `df` in the function is the actual image metadata Pandas dataframe.

Running
```
model.eval()
with torch.no_grad():
outputs = model(test_loader)
```
produces this traceback:
```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_3448/2278559883.py in
1 model.eval()
2 with torch.no_grad():
----> 3 outputs = model(test_loader)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []

~/Detic/detic/modeling/meta_arch/custom_rcnn.py in forward(self, batched_inputs)
113 """
114 if not self.training:
--> 115 return self.inference(batched_inputs)
116
117 images = self.preprocess_image(batched_inputs)

~/Detic/detic/modeling/meta_arch/custom_rcnn.py in inference(self, batched_inputs, detected_instances, do_postprocess)
94 assert detected_instances is None
95
---> 96 images = self.preprocess_image(batched_inputs)
97 features = self.backbone(images.tensor)
98 proposals, _ = self.proposal_generator(images, features, None)

/opt/conda/lib/python3.7/site-packages/detectron2/modeling/meta_arch/rcnn.py in preprocess_image(self, batched_inputs)
222 Normalize, pad and batch the input images.
223 """
--> 224 images = [x["image"].to(self.device) for x in batched_inputs]
225 images = [(x - self.pixel_mean) / self.pixel_std for x in images]
226 images = ImageList.from_tensors(images, self.backbone.size_divisibility)

/opt/conda/lib/python3.7/site-packages/detectron2/modeling/meta_arch/rcnn.py in (.0)
222 Normalize, pad and batch the input images.
223 """
--> 224 images = [x["image"].to(self.device) for x in batched_inputs]
225 images = [(x - self.pixel_mean) / self.pixel_std for x in images]
226 images = ImageList.from_tensors(images, self.backbone.size_divisibility)

TypeError: list indices must be integers or slices, not str
```

So the `x` in `batched_inputs` is a list, not a dict as expected. When I try
```
data_iter = iter(test_loader)
i = 0
for x in data_iter:
if i % 1000 == 0:
print(type(x))
i = i + 1
```
the output is
```

(...)

```
In other words, my data loader seems to return `list[list[dict]]`, instead of `list[dict]`. Have I goofed, or is this error due to something else?

Contributor guide

Open the contributing guide

Research direction

Start by running the shown custom dataset registration and comparing one batch from build_detection_test_loader with the input expected by detic/modeling/meta_arch/custom_rcnn.py. Read detectron2/modeling/meta_arch/rcnn.py around preprocess_image and the loader entry point; done means the documented inference path uses the expected batch structure without the traceback.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.