AI Research/Object Detection

[Object Detection] mmdetection의 input image size 변경하는 방법 (Faster r-cnn)

wawawaaw 2022. 12. 28. 16:30
반응형

mmdetection 은 다양한 object detection, object segmentation등을 실행해볼 수 있는 pytorch 기반의 툴이다. 

https://github.com/open-mmlab/mmdetection

 

GitHub - open-mmlab/mmdetection: OpenMMLab Detection Toolbox and Benchmark

OpenMMLab Detection Toolbox and Benchmark. Contribute to open-mmlab/mmdetection development by creating an account on GitHub.

github.com

 

mmdetection을 이용하여 faster rcnn을 실행하였는데 입력 이미지의 사이즈를 자동으로 resizing을 하고 있다는 것을 확인했다. 따라서 내가 준 입력 이미지에 대해서 (1333,800)의 사이즈로 detection을 진행하고 있었다. 

본 포스팅에서는 input 이미지의 사이즈를 원하는 사이즈로 변경하는 방법에 대해서 다루겠다. 

먼저, image_demo.py의 main을 살펴보자. 

def main(args):
    # build the model from a config file and a checkpoint file
    model = init_detector(args.config, args.checkpoint, device=args.device)
    # test a single image
    result = inference_detector(model, args.img)
    # show the results
    show_result_pyplot(
        model,
        args.img,
        result,
        palette=args.palette,
        score_thr=args.score_thr,
        out_file=args.out_file)

inference_detector로 이미지가 경로를 넘겨준다. (mmdet>apis>inference.py)

이 inference_detector 함수에서 이미지를 tensor로 변경해주는 작업을 한다. 

다음으로 mmdetection에서 data를 어떠한 형태로 저장하고 있는지 살펴보아야한다. 

ㅜmmdet>datasets>utils.py의 replace_ImageToTensor

변경을 원하는 img_scale이 dictionary의 key로 저장되어 있다. 

#기존의 이미지를 tensor로 변경해주는 코드 
cfg.data.test.pipeline = replace_ImageToTensor(cfg.data.test.pipeline)


#이미지를 원하는 w,h로 변경해주는 부분
#cfg.data.test.pipeline[0]은 type 부분이고 
#cfg.data.test.pipelline[1]이 img_scale, flip등등 transforms와 관련된 부분이다. 
cfg.data.test.pipeline[1]['img_scale'] = 320,320

원하는 값으로 이미지의 w,h를 수정해주었다. 

이미지 tensor도 이와 같이 줄어서 실행시간이 주는 것을 확인할 수 있다. 

반응형