MLDL_์ •๋ฆฌ/Sample

[DL] - Object Detection / MMDetection Package

KimTory 2022. 3. 7. 23:40

๐Ÿ‘‰ MMDeteection ๊ฐœ์š”

  • ์นญํ™” ๋Œ€ํ•™์˜ ์ฃผ๋„๋กœ ๋งŒ๋“ค์–ด์ง„ C.V Open Source Project์ธ OpenMMLab์—์„œ ์‹œ์ž‘
  • ๋‹ค์–‘ํ•œ Object Detection, Segmentation ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ Package๋กœ ๊ตฌํ˜„ ์ œ๊ณต
  • ๊ตฌํ˜„ ์„ฑ๋Šฅ, ํšจ์œจ์ ์ธ ๋ชจ๋“ˆ ์„ค๊ณ„, Config ๊ธฐ๋ฐ˜์œผ๋กœ ํ•™์Šต / ํ‰๊ฐ€ ๊นŒ์ง€ ์‹คํ–‰๋˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ ์ ์šฉ
  • Pytorch ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„

๐Ÿ‘‰ MMDeteection ๊ตฌํ˜„

# config ํŒŒ์ผ์„ ์„ค์ •ํ•˜๊ณ , ๋‹ค์šด๋กœ๋“œ ๋ฐ›์€ pretrained ๋ชจ๋ธ์„ checkpoint๋กœ ์„ค์ •. 
config_file = '/content/mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = '/content/mmdetection/checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'

# config ํŒŒ์ผ๊ณผ pretrained ๋ชจ๋ธ์„ ๊ธฐ๋ฐ˜์œผ๋กœ Detector ๋ชจ๋ธ์„ ์ƒ์„ฑ. 
from mmdet.apis import init_detector, inference_detector

model = init_detector(config_file, checkpoint_file, device='cuda:0')

# mmdetection์€ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์ธ์ž๋กœ ์ฃผ๋ฉด ๋ฌด์กฐ๊ฑด mmdetection ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•จ. 
%cd mmdetection

from mmdet.apis import init_detector, inference_detector

# init_detector() ์ธ์ž๋กœ config์™€ checkpoint๋ฅผ ์ž…๋ ฅํ•จ. 
model = init_detector(config='configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py', checkpoint='checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth')

import cv2
import matplotlib.pyplot as plt
img = '/content/mmdetection/demo/demo.jpg'

img_arr  = cv2.cvtColor(cv2.imread(img), cv2.COLOR_BGR2RGB)
plt.figure(figsize=(12, 12))
plt.imshow(img_arr)

 

img = '/content/mmdetection/demo/demo.jpg'
# inference_detector์˜ ์ธ์ž๋กœ string(file๊ฒฝ๋กœ), ndarray๊ฐ€ ๋‹จ์ผ ๋˜๋Š” listํ˜•ํƒœ๋กœ ์ž…๋ ฅ ๋  ์ˆ˜ ์žˆ์Œ. 
results = inference_detector(model, img)

from mmdet.apis import show_result_pyplot
# inference ๋œ ๊ฒฐ๊ณผ๋ฅผ ์›๋ณธ ์ด๋ฏธ์ง€์— ์ ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด image๋กœ ์ƒ์„ฑ(bbox ์ฒ˜๋ฆฌ๋œ image)
# Default๋กœ score threshold๊ฐ€ 0.3 ์ด์ƒ์ธ Object๋“ค๋งŒ ์‹œ๊ฐํ™” ์ ์šฉ. show_result_pyplot์€ model.show_result()๋ฅผ ํ˜ธ์ถœ. 
show_result_pyplot(model, img, results)

 

๐Ÿ‘‰ DataSet / COCO Data Set / 80๊ฐœ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„

๐Ÿ‘‰ Opencv ๊ธฐ๋ฐ˜ ๊ตฌํ˜„

import cv2

# RGB๊ฐ€ ์•„๋‹Œ BGR๋กœ ์ž…๋ ฅ
img_arr = cv2.imread('/content/mmdetection/demo/demo.jpg')
results = inference_detector(model, img_arr)

show_result_pyplot(model, img_arr, results)

→ array๋ฅผ inference_detector()์— ์ž…๋ ฅํ•  ๊ฒฝ์šฐ, ์›๋ณธ array๋ฅผ BGR ํ˜•ํƒœ๋กœ ์ž…๋ ฅ ํ•„์š”

   (RGB ๋ณ€ํ™˜์€ ๋‚ด๋ถ€์—์„œ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ, BGR๋กœ ์ž…๋ ฅ ํ•„์š”, RGB ๋ณ€ํ™˜ ์‹œ, ๋ฐ˜๋Œ€๋กœ ์ถœ๋ ฅ๋จ)

'MLDL_์ •๋ฆฌ > Sample' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[DL] OpenAOI Git  (0) 2022.05.09
[DL] CIFAR-10  (0) 2022.03.22
[DL] - MediaPipe / Video pose Detection  (0) 2022.03.06
[DL] - MediaPipe / Video Object Detection  (0) 2022.03.06
[DL] - ์ •ํ™•๋„ ์ธก์ •  (0) 2022.03.01