์ด ํฌ์ŠคํŠธ๋Š” ์ œ๊ฐ€ ๊ฐœ์ธ์ ์ธ ์šฉ๋„๋กœ ์ •๋ฆฌํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.

3 minute read

์ด ํฌ์ŠคํŠธ๋Š” ์ œ๊ฐ€ ๊ฐœ์ธ์ ์ธ ์šฉ๋„๋กœ ์ •๋ฆฌํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.

mmdetection์„ CPU์—์„œ ํ…Œ์ŠคํŠธํ•ด ๋ณผ ์ผ์ด ์žˆ๋Š”๋ฐ, mmdetection ๊ณต์‹ doc์— ์„ค๋ช…์ด ๋ถ€์กฑํ•ด์„œ ์ด๊ณณ์— ๋ณ„๋„๋กœ ์ •๋ฆฌํ•ด๋‘ก๋‹ˆ๋‹ค!

  • open-mmlab/mmdetection - link



mmdetection์ด๋ž€?

pytorch๋กœ ์ž‘์„ฑ๋œ Object Detection๊ณผ Instance Segmentation์˜ ๋‹ค์–‘ํ•œ ๋ชจ๋ธ๋“ค์„ ์ •๋ฆฌํ•œ ํ”Œ๋žซํผ์ด๋‹ค. ์ •๋ง ๋‹ค์–‘ํ•œ ๋ชจ๋ธ๋“ค์„ ์ง€์›ํ•˜๊ณ  ์žˆ๋‹ค!!


Get Start only with โ€˜CPUโ€™

๋”ฅ๋Ÿฌ๋‹์„ ํ•˜์‹œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๋ถ„๋“ค์„ GPU๊ฐ€ ์žˆ๊ณ , ๋ณธ์ธ ์—ญ์‹œ GPU๋ฅผ ์†Œ์œ ํ•˜๊ณ  ์žˆ์ง€๋งŒ, ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” CPU๋กœ ๋ชจ๋ธ์„ ํ…Œ์ŠคํŠธํ•˜๊ณ  ์‹ถ์„ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ ๋ณธ์ธ์€ mmdetection ๋ชจ๋ธ์„ CPU๋กœ ์‹œ์ž‘ํ•˜๊ณ ์ž ํ–ˆ๋‹ค.

์ด ๋งํฌ๋ฅผ ํ†ตํ•ด ๋“ค์–ด๊ฐ€๋ฉด mmdetection์„ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์„ค๋ช…๋˜์–ด ์žˆ๋‹ค.

CPU๋กœ mmdetection์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋Œ€๋ถ€๋ถ„์˜ ๊ณผ์ •์€ ๋™์ผํ•˜๋‚˜, ๋ช‡๋ช‡ ๋ถ€๋ถ„์—์„œ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค.


Installation

์•„๋ž˜์˜ ๊ณผ์ •์„ ๋”ฐ๋ผ๊ฐ€์ž.

1. Create a conda virtual environment and activate it.

conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab

2. Install PyTorch and torchvision

conda install pytorch torchvision -c pytorch

p.s. ์—ฌ๊ธฐ์„œ -c ์˜ต์…˜์€ channel์„ ์˜๋ฏธํ•˜๋Š”๋ฐ, pytorch์— ๋งž์ถฐ์ง„ ์ฑ„๋„์„ ์‚ฌ์šฉํ•ด ์„ค์น˜ํ•˜๋Š” ์˜ต์…˜์ด๋‹ค.

3. mmcv-full ์„ค์น˜

pip install mmcv-full

4. Clone the MMDetection repository.

git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection

5. Install build requirements and then install MMDetection.

pip install -r requirements/build.txt
pip install -v -e .

์œ„์˜ ๊ณผ์ •์„ ์™„๋ฃŒํ•˜๋ฉด, mmdetection์„ ์„ค์น˜ํ•ด CPU๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!

๋‹จ, CPU๋งŒ์œผ๋กœ mmdetection์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, ์ตœ์‹  ํ…Œํฌ๋‹‰์„ ์“ฐ๋Š” ์š”์ฆ˜ ๋ชจ๋ธ์„ ์“ฐ๋Š”๊ฒŒ ๋ถˆ๊ฐ€๋Šฅํ•  ์ˆ˜ ์žˆ๋‹ค!! mmdetection์˜ ๋ฌธ์„œ์— ํ•ด๋‹น ๋‚ด์šฉ์ด ๋‚˜์™€ ์žˆ์œผ๋‹ˆ CPU ๋ฒ„์ „์œผ๋กœ ๋ณธ์ธ์ด ์“ธ ๋ชจ๋ธ์ด ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜์ž.


Verification

์„ค์น˜๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

mmdetection์˜ ๊ณต์‹ ๋ฌธ์„œ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒ€์ฆ ์ฝ”๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

from mmdet.apis import init_detector, inference_detector

config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
# download the checkpoint from model zoo and put it in `checkpoints/`
# url: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
# device = 'cuda:0'
device = 'cpu' # cpu๋กœ ํ…Œ์ŠคํŠธํ•  ๊ฒฝ์šฐ, ์ด ๋ถ€๋ถ„์„ ๊ผญ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค!
# init a detector
model = init_detector(config_file, checkpoint_file, device=device)
# inference the demo image
inference_detector(model, 'demo/demo.jpg')

๊ฒ€์ฆ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„ , โ€œ๋ฐ˜๋“œ์‹œโ€ ์ฝ”๋“œ์— ์ œ์‹œ๋œ ๋งํฌ๋ฅผ ํ†ตํ•ด ํ•™์Šต๋œ ๋ชจ๋ธ์„ ๋‹ค์šด ๋ฐ›์•„ checkpoint/ ํด๋”์— ๋„ฃ์–ด์•ผ ํ•œ๋‹ค!!

๋˜ํ•œ, device ์„ค์ •์„ device='cpu'๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค!


์ด ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‹คํ–‰ํ•˜๋ฉด, ์•„๋ฌด๋Ÿฐ ์‹œ๊ฐ์  ๊ฒฐ๊ณผ๋ฅผ ์–ป์ง€ ๋ชป ํ•œ๋‹คโ€ฆ ๐Ÿ˜ฅ

๊ทธ๋ž˜์„œ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

from mmdet.apis import show_result_pyplot, init_detector, inference_detector

... # ์ค‘๋žต

# inference the demo image
result = inference_detector(model, 'demo/demo.jpg')

show_result_pyplot(model, 'demo/demo.jpg', result, 0.3)


์œ„์˜ ์ฝ”๋“œ๊ฐ€ ์ž˜ ์‹คํ–‰ ๋˜์—ˆ๋‹ค๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š”๋‹ค.


p.s. ๋ณธ์ธ์€ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, ์•„๋ž˜์™€ ๊ฐ™์€ Warning์„ ๋ฐ›์•˜๋‹ค.

mmdetection\mmdet\datasets\utils.py:60: UserWarning: "ImageToTensor" pipeline is replaced by "DefaultFormatBundle" for batch inference. It is recommended to manually replace it in the test data pipeline in your config file.
  'data pipeline in your config file.', UserWarning)

๊ทธ๋Ÿฐ๋ฐ, ์‹คํ–‰ ์ž์ฒด์—๋Š” ํฐ ๋ฌธ์ œ๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค!