YOLOv5 λΆμ(2) - Train Custom Data
μΆμ²: https://docs.ultralytics.com/tutorials/train-custom-datasets/
Train Custom Data π - YOLOv5 Documentation
Train Custom Data π π This guide explains how to train your own custom dataset with YOLOv5 π. Before You Start Clone this repo, download tutorial dataset, and install requirements.txt dependencies, including Python>=3.8 and PyTorch>=1.7. $ git clo
docs.ultralytics.com
μμ μ¬μ΄νΈλ₯Ό μ°Έκ³ νμ¬ YOLOv5λ₯Ό 컀μ€ν λ°μ΄ν°λ‘ νμ΅νλ λ°©λ²μ κ°λ΅νκ² μμλ³΄κ² μ΅λλ€.
Train Custom Data
1. Create dataset.yaml
COCO128μ COCO train2017μ μΌλΆλΆμΌλ‘ Training, Validation λ°μ΄ν°λ‘ λλμ΄μ Έ μμΌλ©°, νμ΅ κ³Όμ μ΄ μ λμνλμ§ ν μ€νΈνκΈ°μ μ ν©ν©λλ€(νμ΅ λͺ©μ μ΄ μλλλ€)
data/coco128.yaml νμΌ
# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ../coco128/images/train2017/
val: ../coco128/images/train2017/
# number of classes
nc: 80
# class names
names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush']
2. Create Labels
λΌλ²¨μ YOLO formatμΌλ‘ μ€λΉ ν©λλ€. λ§μ½ μ΄λ―Έμ§μ 물체(Object)κ° μ‘΄μ¬νμ§ μλλ€λ©΄, *.txt νμΌμ μμ±λμ§ μμ΅λλ€.
νΉμ§
- νμ€μ νλμ μ΄λ―Έμ§λ₯Ό ν¬ν¨ν©λλ€.
- κ°κ°μ νμ (class, x_center, y_center, width, height) νμμ λλ€.
- μ’ν κ°μ 0 ~ 1 μ¬μ΄ κ°μΌλ‘ μ κ·ν λμ΄ μμ΅λλ€.
3. Organize Directories
μλμ κ°μ ν΄λ κ΅¬μ‘°λ‘ μ΄λ―Έμ§μ λΌλ²¨μ λ°°μΉν©λλ€. YOLOv5λ μ΄λ―Έμ§μ λμνλ λΌλ²¨μ μλμ μΌλ‘ μ°Ύμ΅λλ€(μλ§λ νμ₯μκ° λ€λ₯Έ λμΌ μ΄λ¦μΌλ‘ μ°Ύλ κ² κ°μ΅λλ€. e.g. 'im1.jpg <-> im1.txt' μ λ§€μΉλ¨)
|- yolov5
|- {λ°μ΄ν°μ μ΄λ¦}
|- images/im0.jpg, ... imN.jpg
|- labels/im0.txt, ...imN.txt
4. Select a Model
- μ¬μ΄νΈμμ λͺ©μ μ λ§λ λͺ¨λΈμ μ νν©λλ€. μ¦, μ μ΄κΈ°κ° μμ©κ°λ₯ν λ©λͺ¨λ¦¬ μ¬μ΄μ¦λ₯Ό κ³ λ €ν΄μ λͺ¨λΈμ μ νν©λλ€.
5. Train
- 미리 νμ΅λ κ°μ€μΉλ₯Ό λ€μ΄νκ³ , λ°°μΉμ¬μ΄μ¦ λ±μ μ€μ νμ¬ νμ΅ν©λλ€.
- Pretrained κ°μ€μΉ μ¬μ©νμ§ μκ³ , λλ€μΌλ‘ μ΄κΈ°νλ κ°μ€μΉ + yolov5X.yaml μ μ¬μ©νλ κ²μ μΆμ²νμ§ μμ΅λλ€.
(e.g. --weights '' --cfg yolov5s.yaml)
# Train YOLOv5s on COCO128 for 5 epochs
$ python train.py --img 640 --batch 16 --epochs 5 --data coco128.yaml --weights yolov5s.pt
- runs/train/expX ν΄λκ° μμ±λλ©΄μ νμ΅ κ³Όμ μ΄ μ μ₯λ©λλ€.
6. Visualize
Weights & Biases λ‘κΉ λꡬλ₯Ό μ 곡νμ¬ ν΄λΌμ°λ μμμ μ€μκ°μΌλ‘ νμ΅ νν©μ μ΄ν΄ λ³Ό μ μμ΅λλ€.
pip install wandb # μ€μΉ
λ¬Όλ‘ runs/train/expX ν΄λμ λ‘컬λ‘λ μ μ₯λμ΄ νμΈμ΄ κ°λ₯ν©λλ€. ν μ보λ νμ (+ results.txt νμΌ)λ‘ μ μ₯λμ΄ ν μ보λλ‘ μ΄μ΄ λ³Ό μ μμ΅λλ€.
from utils.plots import plot_results
plot_results(save_dir='runs/train/exp') # plot results.txt as results.png
AWS, GCP, Docker Image λ‘ νμ΅ νκ²½μ μ 곡ν©λλ€.