Post

[Deep Learning] YOLO V3를 사용해보자!

Object Detection 모델 중 성능이 준수한 YOLO V3를 사용해보자.

✅ YOLO V3 사용하기

Object Detection 모델 중 성능이 준수한 YOLO V3를 무작정 사용해보자! 아래 코드는 Colab을 기준으로 작성했다.

[ultralytics/yolov3 Github Page]

✔️ 환경 구성

해당 Github를 Clone 해오자.

1
!git clone https://github.com/ultralytics/yolov3.git

그리고 버전 업데이트에 따른 경고 문구가 뜬다고 해서(동작하는데는 이상이 없다고는 함) 아래와 같은 코드를 실행해 requirements.txt를 수정하면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
## yolov3 폴더 requirements.txt 수정 필요
## setuptools<=64.0.2

temp_str = 'setuptools<=64.0.2\n' 
f = open('/content/yolov3/requirements.txt', 'r') 
f_str = f.readlines() 
f.close() 

f2 = open('/content/yolov3/requirements.txt', 'w') 

for idx, val in enumerate(f_str) : 
    if 'setuptools' in val : 
        idx_v = idx 
        f_str.remove(val) 
        f_str.insert(idx_v, temp_str) 

for val in f_str : 
    f2.write(val) 
    
f2.close()

그리고 requirements.txt 를 실행해서 환경을 구성하자.

1
!cd yolov3; pip install -r requirements.txt

✔️ Pretrained Weight 다운로드

[Pretrained Weights Page] 해당 페이지로 가서 Pretrained Weight 파일을 다운로드 받는다. 해당 페이지에 들어가서 아래로 조금 내리면 Pretrained Weight 파일들이 있고, 원하는 파일을 다운 받으면 된다. 나는 기본인 yolov3.pt 파일을 받았다. 마찬가지로 wget으로 받아주면 된다.

image

1
2
!mkdir /content/yolov3/pretrained
!wget -O /content/yolov3/pretrained/yolov3.pt https://github.com/ultralytics/yolov3/releases/download/v9.6.0/yolov3.pt

✔️ 이미지, 동영상 준비

!wget -O [저장할 파일명] [파일 주소]로 데이터를 다운로드 받을 수 있다.

아니면 구글 드라이브를 마운트 하거나, 해당 세션에 드래그 앤 드랍으로 파일을 임시로 올릴 수 있다.

나는 저작권 무료사진 아무거나 가져와보았다. (동영상을 하려면 무조건 GPU로.. 아니면 너무 느려요)

1
!wget -O /content/images/test.jpg https://cdn.pixabay.com/photo/2020/09/22/22/32/people-5594462__340.jpg

✔️ detect.py 실행

명령어 도움말은 python detect.py -h로 확인할 수 있다.

confidence score가 설정한 conf-thres보다 높은 결과를 보여주며, NMS 적용 시 IoU 값이 iou-thres를 넘기는 경우 제거하게 된다.

1
2
3
4
5
6
7
8
9
!cd yolov3; python detect.py \
    --weights '/content/yolov3/pretrained/yolov3.pt' \
    --source '/content/images' \
    --project '/content/detected' \
    --name 'imgs' \
    --conf-thres 0.5 \
    --iou-thres 0.45 \
    --line-thickness 2 \
    --exist-ok

✔️ Detect Image 확인

1
2
3
4
5
6
from IPython.display import Image
import os

files = os.listdir('/content/detected/imgs')
for f in files:
    display(Image(os.path.join('/content/detected/imgs', f)))

다운로드

[YOLO 실습 코드]

This post is licensed under CC BY 4.0 by the author.

[Deep Learning] keras 콜백 함수 ReduceLROnPlateau

[Deep Learning] YOLO V5를 Transfer Learning 해보자!