mmrotate icon indicating copy to clipboard operation
mmrotate copied to clipboard

[Bug] v1.x image_demo bboxes AttributeError: 'Tensor' object has no attribute 'convert_to'

Open austinmw opened this issue 3 years ago • 5 comments

Prerequisite

Task

I'm using the official example scripts/configs for the officially supported tasks/models/datasets.

Branch

1.x branch https://github.com/open-mmlab/mmrotate/tree/1.x

Environment

Not found in dev-1.x: python: can't open file 'mmrotate/utils/collect_env.py': [Errno 2] No such file or directory

Reproduces the problem - code sample

I trained the Oriented R-CNN model on the tiny SSDD example dataset for 3 epochs. I'm now attempting to run demo/image_demo.py on an image from the dataset (ssdd_tiny/images/000631.png), but get an error with the bboxes.

Reproduces the problem - command or script

python demo/image_demo.py ssdd_tiny/images/000631.png <config path> <checkpoint path> --out-file result.jpg

Reproduces the problem - error message

local loads checkpoint from path: /opt/ml/model/epoch_12.pth
The model and loaded state dict do not match exactly

size mismatch for roi_head.bbox_head.fc_cls.weight: copying a param with shape torch.Size([2, 1024]) from checkpoint, the shape in current model is torch.Size([16, 1024]).
size mismatch for roi_head.bbox_head.fc_cls.bias: copying a param with shape torch.Size([2]) from checkpoint, the shape in current model is torch.Size([16]).
/opt/conda/lib/python3.8/site-packages/mmengine/visualization/visualizer.py:170: UserWarning: `Visualizer` backend is not initialized because save_dir is None.
  warnings.warn('`Visualizer` backend is not initialized '
Traceback (most recent call last):
  File "/opt/mmrotate/demo/image_demo.py", line 63, in <module>
    main(args)
  File "/opt/mmrotate/demo/image_demo.py", line 50, in main
    visualizer.add_datasample(
  File "/opt/conda/lib/python3.8/site-packages/mmengine/dist/utils.py", line 346, in wrapper
    return func(*args, **kwargs)
  File "/opt/mmdetection/mmdet/visualization/local_visualizer.py", line 368, in add_datasample
    pred_img_data = self._draw_instances(image, pred_instances,
  File "/opt/mmrotate/mmrotate/visualization/local_visualizer.py", line 73, in _draw_instances
    polygons = bboxes.convert_to('qbox').tensor
AttributeError: 'Tensor' object has no attribute 'convert_to'

Additional information

No response

austinmw avatar Nov 11 '22 05:11 austinmw

I encountered the same problem. Someone explained that mmrotate has rewritten the previous bbox writing method in order to access mmdet. It used to be a tensor, but now it continues to use the box list in mmdet. But I don't know how to solve it

jsago avatar Nov 11 '22 12:11 jsago

In 1.x, the model output should be RotatedBoxes, maybe in local_visualizer we should add a type check.

maybe you try replace this line to:

polygon = rbox2qbox(bboxes)

@zytx121 should we add a check to handle tensor bboxes?

liuyanyi avatar Nov 11 '22 13:11 liuyanyi

@liuyanyi Ah okay, yeah I'm using 1.x and it seems to be type Tensor for me 🤔

austinmw avatar Nov 11 '22 15:11 austinmw

@liuyanyi Thank you for your help. Yes, I did. But there are new problems here. I may need to understand them comprehensively File "G:/Scientific_Research/mmrotate-1.0.0rc0/tools/my_test.py", line 130, in main() File "G:/Scientific_Research/mmrotate-1.0.0rc0/tools/my_test.py", line 126, in main runner.test() File "D:\Software Area\Anaconda\envs\my_openmmlab\lib\site-packages\mmengine\runner\runner.py", line 1704, in test metrics = self.test_loop.run() # type: ignore File "D:\Software Area\Anaconda\envs\my_openmmlab\lib\site-packages\mmengine\runner\loops.py", line 417, in run self.run_iter(idx, data_batch) File "D:\Software Area\Anaconda\envs\my_openmmlab\lib\site-packages\torch\autograd\grad_mode.py", line 27, in decorate_context return func(*args, **kwargs) File "D:\Software Area\Anaconda\envs\my_openmmlab\lib\site-packages\mmengine\runner\loops.py", line 438, in run_iter self.runner.call_hook( File "D:\Software Area\Anaconda\envs\my_openmmlab\lib\site-packages\mmengine\runner\runner.py", line 1720, in call_hook getattr(hook, fn_name)(self, **kwargs) File "D:\Software Area\Anaconda\envs\my_openmmlab\lib\site-packages\mmdet\engine\hooks\visualization_hook.py", line 147, in after_test_iter self._visualizer.add_datasample( File "D:\Software Area\Anaconda\envs\my_openmmlab\lib\site-packages\mmengine\dist\utils.py", line 346, in wrapper return func(*args, **kwargs) File "D:\Software Area\Anaconda\envs\my_openmmlab\lib\site-packages\mmdet\visualization\local_visualizer.py", line 350, in add_datasample gt_img_data = self._draw_instances(image, File "G:\Scientific_Research\mmrotate-1.0.0rc0\mmrotate\visualization\local_visualizer.py", line 85, in _draw_instances positions = bboxes.centers + self.line_width AttributeError: 'Tensor' object has no attribute 'centers'

jsago avatar Nov 12 '22 03:11 jsago

Hi @austinmw @jsago Sorry, it is a bug. and you can replace https://github.com/open-mmlab/mmrotate/blob/766185ed317f99379cb14035a6f9e5cf8a5340ad/mmrotate/visualization/local_visualizer.py#L72 with

from mmrotate.structures.bbox import RotatedBoxes
bboxes  = RotatedBoxes(bboxes)
polygons = bboxes.convert_to('qbox').tensor 

temporarily, or refer to #613.

In mmrotate 1.x, we use the BoxType to replace Torch.Tensor. We will provide detailed documentation about it later.

image

Now, you can refer unit test to learn how to use it. https://github.com/open-mmlab/mmrotate/tree/dev-1.x/tests/test_structures/test_bbox

Thank you very much for your feedback, which is very important to us ❤️❤️

zytx121 avatar Nov 13 '22 01:11 zytx121

Closing since this bug fixed, although when running demo I end up with a subsequent bug referenced in above merged PR

austinmw avatar Nov 15 '22 21:11 austinmw