open-mmlab / mmrotate

OpenMMLab Rotated Object Detection Toolbox and Benchmark
https://mmrotate.readthedocs.io/en/latest/
Apache License 2.0
1.84k stars 543 forks source link

[Bug] image_demo.py empty using different device #856

Open Luo-Z13 opened 1 year ago

Luo-Z13 commented 1 year ago

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

I followed the tutorial (https://mmrotate.readthedocs.io/en/1.x/get_started.html) step by step to perform testing. During the inference validation, I modified the line parser.add_argument( '--device', default='cuda:0', help='Device used for inference') to parser.add_argument( '--device', default='cuda:3', help='Device used for inference') . While the former provided normal prediction results for demo.jpg, the modified device setting resulted in empty output. What could be the reason for this?

Reproduces the problem - code sample

# Copyright (c) OpenMMLab. All rights reserved.
from argparse import ArgumentParser

import mmcv
from mmdet.apis import inference_detector, init_detector

from mmrotate.registry import VISUALIZERS
from mmrotate.utils import register_all_modules

def parse_args():
    parser = ArgumentParser()
    parser.add_argument('img', help='Image file')
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file')
    parser.add_argument('--out-file', default=None, help='Path to output file')
    parser.add_argument(
        # '--device', default='cpu', help='Device used for inference')
        '--device', default='cuda:3', help='Device used for inference')
    parser.add_argument(
        '--palette',
        default='dota',
        choices=['dota', 'sar', 'hrsc', 'random'],
        help='Color palette used for visualization')
    parser.add_argument(
        '--score-thr', type=float, default=0.3, help='bbox score threshold')
    args = parser.parse_args()
    return args

def main(args):
    # register all modules in mmrotate into the registries
    register_all_modules()

    # build the model from a config file and a checkpoint file
    model = init_detector(
        args.config, args.checkpoint, palette=args.palette, device=args.device)

    # init visualizer
    visualizer = VISUALIZERS.build(model.cfg.visualizer)
    # the dataset_meta is loaded from the checkpoint and
    # then pass to the model in init_detector
    visualizer.dataset_meta = model.dataset_meta

    # test a single image
    result = inference_detector(model, args.img)

    # show the results
    img = mmcv.imread(args.img)
    img = mmcv.imconvert(img, 'bgr', 'rgb')
    print('result:',result)
    visualizer.add_datasample(
        'result',
        img,
        data_sample=result,
        draw_gt=False,
        show=args.out_file is None,
        wait_time=0,
        out_file=args.out_file,
        pred_score_thr=args.score_thr)

    print('done!')

if __name__ == '__main__':
    args = parse_args()
    main(args)

Reproduces the problem - command or script

python demo/image_demo.py\ demo/demo.jpg\ oriented-rcnn-le90_r50_fpn_1x_dota.py\ oriented_rcnn_r50_fpn_1x_dota_le90-6d2b2ce0.pth\ --out-file result.jpg > demo/demo_img_cuda3.txt

Reproduces the problem - error message

context of demo_img_cuda3.txt:

Loads checkpoint by local backend from path: oriented_rcnn_r50_fpn_1x_dota_le90-6d2b2ce0.pth 05/26 20:44:19 - mmengine - WARNING - Visualizer backend is not initialized because save_dir is None. result: <DetDataSample(

META INFORMATION
pad_shape: (1024, 1024)
batch_input_shape: (1024, 1024)
ori_shape: (1024, 1024)
img_id: 0
scale_factor: (1.0, 1.0)
img_path: 'demo/demo.jpg'
img_shape: (1024, 1024)

DATA FIELDS
ignored_instances: <InstanceData(

        META INFORMATION

        DATA FIELDS
        labels: tensor([], device='cuda:3', dtype=torch.int64)
        bboxes: tensor([], device='cuda:3', size=(0, 5))
    ) at 0x7f2d328267f0>
pred_instances: <InstanceData(

        META INFORMATION

        DATA FIELDS
        scores: tensor([], device='cuda:3')
        labels: tensor([], device='cuda:3', dtype=torch.int64)
        bboxes: tensor([], device='cuda:3', size=(0, 5))
    ) at 0x7f2d328267c0>
gt_instances: <InstanceData(

        META INFORMATION

        DATA FIELDS
        labels: tensor([], device='cuda:3', dtype=torch.int64)
        bboxes: tensor([], device='cuda:3', size=(0, 5))
    ) at 0x7f2d32826820>

) at 0x7f2d32826790> done!

Additional information

I just use image_demo.py to inference, and I just change the device form 'cuda:0' to 'cuda:3' then the result is empty, I can get normal result when use 'cpu'.

Luo-Z13 commented 1 year ago

@zytx121

zytx121 commented 1 year ago

Hi @Luo-Z13, Thank you for providing this bug. It may have been caused by an unsuccessful call to the GPU.

You can try CUDA_VISIBLE_DEVICES=3 python demo/image_demo.py to specify the 4th GPU.

Luo-Z13 commented 1 year ago

Hi @Luo-Z13, Thank you for providing this bug. It may have been caused by an unsuccessful call to the GPU.

You can try CUDA_VISIBLE_DEVICES=3 python demo/image_demo.py to specify the 4th GPU.

Thanks, it works.