open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.06k stars 9.37k forks source link

single_gpu_test() error (mmdetection 2.9) #10455

Open unique-chan opened 1 year ago

unique-chan commented 1 year ago
from mmdet.apis import single_gpu_test
from mmdet.datasets import build_dataloader

test_dataloader_default_args = dict(
    samples_per_gpu=1,
    workers_per_gpu=2,
    shuffle=False,
    persistent_workers=False)

test_dataloader_args = {
    **test_dataloader_default_args,
    **cfg.data.get('test_dataloader', {})
}

test_dataset = build_dataset(cfg.data.test, dict(test_mode=True))
test_dataloader = build_dataloader(test_dataset, **test_dataloader_args)
results = single_gpu_test(model, test_dataloader)
eval_res = test_dataset.evaluate(results)

I got error msg as follows... why?? (when training/validation... no error msg encountered...):

[                                                  ] 0[/1144](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f61676974617465645f686f64676b696e227d.vscode-resource.vscode-cdn.net/1144), elapsed: 0s, ETA:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
----> 1 results = single_gpu_test(model, test_dataloader)
      2 eval_res = test_dataset.evaluate(results)

[/mmdetection/mmdet/apis/test.py](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f61676974617465645f686f64676b696e227d.vscode-resource.vscode-cdn.net/mmdetection/mmdet/apis/test.py) in single_gpu_test(model, data_loader, show, out_dir, show_score_thr)
     27     for i, data in enumerate(data_loader):
     28         with torch.no_grad():
---> 29             result = model(return_loss=False, rescale=True, **data)
     30 
     31         batch_size = len(result)

[/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f61676974617465645f686f64676b696e227d.vscode-resource.vscode-cdn.net/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py) in _call_impl(self, *input, **kwargs)
   1049         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1050                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051             return forward_call(*input, **kwargs)
   1052         # Do not call functions when jit is used
   1053         full_backward_hooks, non_full_backward_hooks = [], []

[/opt/conda/lib/python3.7/site-packages/mmcv/runner/fp16_utils.py](https://vscode-remote+attached-002dcontainer-002b7b22636f6e7461696e65724e616d65223a222f61676974617465645f686f64676b696e227d.vscode-resource.vscode-cdn.net/opt/conda/lib/python3.7/site-packages/mmcv/runner/fp16_utils.py) in new_func(*args, **kwargs)
    117                                 f'method of those classes {supported_types}')
    118             if not (hasattr(args[0], 'fp16_enabled') and args[0].fp16_enabled):
--> 119                 return old_func(*args, **kwargs)
    120 
...
--> 137                 img_meta[img_id]['batch_input_shape'] = tuple(img.size()[-2:])
    138 
    139         if num_augs == 1:

TypeError: 'DataContainer' object is not subscriptable
Output is truncated. View as a [scrollable element](command:cellOutput.enableScrolling?cc872dd1-023c-48f0-8898-46c7f86dcf98) or open in a [text editor](command:workbench.action.openLargeOutput?cc872dd1-023c-48f0-8898-46c7f86dcf98). Adjust cell output [settings](command:workbench.action.openSettings?%5B%22%40tag%3AnotebookOutputLayout%22%5D)...
CaMi1le commented 7 months ago

I faced similar problem too. I solved by adding a judgement in detectors/base.py function forward_test, judging the type of img_meta. If it is Datacontainer, replace img_meta[img_id]['batch_input_shape'] with img_meta.data[img_id][0]['batch_input_shape']