λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ€–λ¨Έμ‹ λŸ¬λ‹/YOLO

YOLO - κ°„λ‹¨ν•œ 사물 인식 예제(YOLOv5, Colab)

by Janger 2023. 3. 12.
728x90
λ°˜μ‘ν˜•

YOLO(You Only Look Once)λŠ” λ”₯λŸ¬λ‹μ„ μ΄μš©ν•œ 사물 인식 ν”„λ ˆμž„μ›Œν¬λ‹€. λ§Žμ€ 인기 탓에 λ‹€μ–‘ν•œ 버전듀(v3, v4, v5...)이 μƒκ²¨λ‚˜κ³  μžˆλ‹€. λ‚΄κ°€ μ‚¬μš©ν•  예제의 버전은 YOLOv5이닀. 

 

κΉƒν—ˆλΈŒ

https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data

 

Train Custom Data

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite. Contribute to ultralytics/yolov5 development by creating an account on GitHub.

github.com

μœ„ κΉƒν—ˆλΈŒ νŽ˜μ΄μ§€λ₯Ό λ°”νƒ•μœΌλ‘œ 따라 ν•˜μ˜€λ‹€. 

 

Roboflow - μ»€μŠ€ν…€ 데이터셋 λ§Œλ“€κΈ°

https://app.roboflow.com/

 

Sign in to Roboflow

Even if you're not a machine learning expert, you can use Roboflow train a custom, state-of-the-art computer vision model on your own data.

app.roboflow.com

Roboflow에선 μ—¬λŸ¬ 이미지λ₯Ό 직접 마우슀둜 라벨링을 ν•˜μ—¬ λ‚˜λ§Œμ˜ 데이터셋을 λ§Œλ“€ μˆ˜κ°€ μžˆλ‹€. 

λ¬Όλ‘  λ‹€λ₯Έ μ‚¬λžŒμ΄ 미리 λ§Œλ“  데이터셋을 λ‹€μš΄λ‘œλ“œν•  μˆ˜λ„ 있음

 

생성을 ν•œ 데이터셋은 Export λ²„νŠΌμ„ 눌러 포맷을 YOLO v5 PyTorch둜 지정해야 ν•œλ‹€. 

 

 

 

Colab 예제 λ…ΈνŠΈ(YOLOv5)

https://colab.research.google.com/github/roboflow-ai/yolov5-custom-training-tutorial/blob/main/yolov5-custom-training.ipynb

 

YOLOv5-Custom-Training.ipynb

Run, share, and edit Python notebooks

colab.research.google.com

 

 

* λ…ΈνŠΈ 쀑간에 경둜 λ¬Έμ œκ°€ μžˆμ–΄μ„œ μ•½κ°„μ˜ μˆ˜μ •μ΄ ν•„μš”ν•˜μ˜€λ‹€. 

 

νŠΈλ ˆμ΄λ‹(train.py)
!python train.py --img 416 --batch 16 --epochs 150 --data data.yaml --weights yolov5s.pt --cache

YOLOv5 λͺ¨λΈμ„ ν•™μŠ΅μ‹œν‚€κΈ° μœ„ν•œ λͺ…령어이닀. 

 

--img 416 : μž…λ ₯ μ΄λ―Έμ§€μ˜ ν¬κΈ°λ₯Ό 416x416 ν”½μ…€λ‘œ μ„€μ •ν•©λ‹ˆλ‹€.

--batch 16 : ν•™μŠ΅ν•  λ•Œ μ‚¬μš©ν•˜λŠ” λ°°μΉ˜ ν¬κΈ°λ₯Ό 16으둜 μ„€μ •ν•©λ‹ˆλ‹€.

--epochs 150 : μ „체 λ°μ΄ν„°μ…‹μ„ ν•™μŠ΅ν•˜λŠ” νšŸμˆ˜λ₯Ό 150으둜 μ„€μ •ν•©λ‹ˆλ‹€.

--data data.yaml : ν•™μŠ΅μ— μ‚¬μš©ν•  λ°μ΄ν„°μ…‹μ„ μ§€μ •ν•©λ‹ˆλ‹€.

--weights yolov5s.pt : λ―Έλ¦¬ ν•™μŠ΅λœ YOLOv5 λͺ¨λΈ κ°€μ€‘μΉ˜ νŒŒμΌμ„ μ§€μ •ν•©λ‹ˆλ‹€. μ΄μ „에 ν•™μŠ΅ν•œ λͺ¨λΈμ„ λ‘œλ“œν•˜κ±°λ‚˜, YOLOv5 λͺ¨λΈμ˜ ν¬κΈ°λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€(yolov5s.pt, yolov5m.pt, yolov5l.pt, yolov5x.pt).

--cache : ν•™μŠ΅ κ³Όμ •μ—μ„œ μ‚¬μš©λ˜λŠ” μΊμ‹œλ₯Ό μ €μž₯ν•˜κ³  μž¬μ‚¬μš©ν•©λ‹ˆλ‹€. μ΄λ₯Ό ν†΅ν•΄ ν•™μŠ΅ μ†λ„λ₯Ό λ†’일 μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

 

 

 

감지(detect.py)

 

 

!python detect.py --weights /content/yolov5/runs/train/exp/weights/best.pt --img 416 --conf 0.1 --source /content/yolov5/test/images

μœ„μ˜ νŠΈλ ˆλ‹ˆμž‰μ„ λ‹€ 마치게 되면 /content/yolov5/runs/train/exp/weights/best.ptλΌλŠ” μ΅œμ’… λͺ¨λΈμ΄ 생성이 λœλ‹€. (.ptλŠ” νŒŒμ΄ν† μΉ˜μ˜ λͺ¨λΈ ν™•μž₯자)

 

--source μΈμžλ‘œλŠ” ν…ŒμŠ€νŠΈλ₯Ό μ‹œμΌœμ€„ 원본 μ΄λ―Έμ§€λ“€μ˜ 폴더 경둜λ₯Ό μž…λ ₯ν•œλ‹€. 

 

 

감지 κ²°κ³Ό 확인
import glob
from IPython.display import Image, display

for imageName in glob.glob('/content/yolov5/runs/detect/exp/*.jpg'): #assuming JPG
    display(Image(filename=imageName))
    print("\n")

감지가 μ™„λ£Œ λ˜μ—ˆλ‹€λ©΄ /content/yolov5/runs/detect/exp/ 폴더(exp2, exp3둜 μ¦κ°€ν•˜κΈ°λ„ ν•œλ‹€. ) μ•ˆμ—λŠ” 좜λ ₯된 이미지가 μžˆμœΌλ‹ˆ 파이썬 슀크립트둜 좜λ ₯된 이미지듀을 ν™•μΈν•œλ‹€. 

 

 

 

 

pypi - yolov5 λͺ¨λ“ˆλ‘œ μ‹€ν–‰

 

!pip install yolov5

(yolov5 λͺ¨λ“ˆ μΆ”κ°€ μ„€μΉ˜)

 

https://pypi.org/project/yolov5/

 

yolov5

Packaged version of the Yolov5 object detector

pypi.org

 

또 κ°„λ‹¨νžˆ yolov5 λͺ¨λ“ˆμ„ μ„€μΉ˜ν•΄ .pt λͺ¨λΈ νŒŒμΌμ„ λΆˆλŸ¬μ™€ μ„€μ • 값을 λ‚΄ λ§˜λŒ€λ‘œ μ‘°μ ˆν•˜κ±°λ‚˜ 이미지 좜λ ₯을 cv2둜 νŽΈμ§‘ν•΄μ„œ λ‚˜μ˜€κ²Œλ” μˆ˜μ •ν•˜λŠ” λ“± λ‹€μ–‘ν•œ μ»€μŠ€ν„°λ§ˆμ΄μ§•μ΄ κ°€λŠ₯ν•˜λ‹€. 

 

import yolov5
import cv2
from IPython.display import display, Image


model = yolov5.load('/content/yolov5/runs/train/exp2/weights/best.pt')

# set model parameters
model.conf = 0.25  # NMS confidence threshold
model.iou = 0.45  # NMS IoU threshold
model.agnostic = False  # NMS class-agnostic
model.multi_label = False  # NMS multiple labels per box
model.max_det = 1000  # maximum number of detections per image

# set image
img = '/content/image.jpg'

# perform inference
results = model(img)

# inference with larger input size
results = model(img, size=1280)

# inference with test time augmentation
results = model(img, augment=True)

# parse results
predictions = results.pred[0]
boxes = predictions[:, :4] # x1, y1, x2, y2
scores = predictions[:, 4]
categories = predictions[:, 5]

# 이미지에 경계 μƒμž 그리기
img = cv2.imread(img)
for box, score, label in zip(boxes, scores, categories):
    x1, y1, x2, y2 = box.int().tolist()
    cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
    cv2.putText(img, str(round(score.item(), 2) * 100) + "%", (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

display(Image(data=cv2.imencode('.png', img)[1]))


# show detection bounding boxes on image
# results.show()

 

 

 

 

 

μ—°μŠ΅μš©μœΌλ‘œ λ§Œλ“  μ–Όκ΅΄ 인식 μ½”λž© λ…ΈνŠΈλΆ

https://colab.research.google.com/drive/1GdaLVQF9XJuGUDdo21uTxVvXkv6oIPwe

 

Google Colaboratory Notebook

Run, share, and edit Python notebooks

colab.research.google.com

 

728x90
λ°˜μ‘ν˜•