forked from ManuelZ/tv_automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolov8_train.py
50 lines (35 loc) · 1.33 KB
/
yolov8_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Standard Library imports
import os
from enum import Enum
from pathlib import Path
# External imports
from ultralytics import YOLO
class Mode(Enum):
TRAIN_DETECTION = 1
TRAIN_CLASSIFICATION = 2
def load_model(mode):
""" """
# COCO-pretrained
if mode == Mode.TRAIN_DETECTION:
model_path = os.path.join(".", "models", "yolov8n.pt")
# Pretrained
elif mode == Mode.TRAIN_CLASSIFICATION:
model_path = os.path.join(".", "models", "yolov8n-cls.pt")
return model_path
if __name__ == "__main__":
mode = Mode.TRAIN_DETECTION
model_path = load_model(mode)
model = YOLO(model_path)
if mode == Mode.TRAIN_DETECTION:
# imgsz must be a multiple of 32
results = model.train(data="config.yml", epochs=100, imgsz=256, workers=0)
# https://github.com/ultralytics/ultralytics/tree/main/examples/YOLOv8-OpenCV-ONNX-Python
model.export(format="onnx", half=True)
if mode == Mode.TRAIN_CLASSIFICATION:
data_dir = Path("data", "classification_yolov8", "images")
# imgsz must be a multiple of 32
results = model.train(
data=data_dir, epochs=100, imgsz=256, workers=0, augment=False
)
# https://github.com/ultralytics/ultralytics/tree/main/examples/YOLOv8-OpenCV-ONNX-Python
model.export(format="onnx", half=True)