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 - 컀μ€ν λ°μ΄ν°μ λ§λ€κΈ°
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)
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