이번에 Object Detection 경진대회를 나가게 되면서 YOLOv8을 사용해보게 되었다.

이번 글에서는 YOLO라는 모델에 대한 자세한 설명보다는, 

Custom Dataset을 어떻게 YOLOv8에 Training 시키는지에 대한 자세한 설명과 코드 위주로 정리해보고자 한다.


1. YOLOv8이란?

우선, 기본적으로 YOLO(You Only Look Once)라는 모델은 객체 탐지(Object Detection)를 위한 딥러닝 모델 중 하나로서, 실시간 객체 탐지 및 분류 태스크에 널리 사용된다.

공식 깃허브 : 

 

GitHub - ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite

NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite - GitHub - ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite

github.com

2. YOLOv8 설치

아래 코드를 실행해서 yolov8를 설치해주자.

!pip install ultralytics

3. Custom Dataset 구조

가장 애를 먹었던 부분인데, 바로 데이터셋 구조 설정이다. 다음 그림과 같은 파일 구조를 띄도록 해준다. 

yolov8 파일 구조

경로 설정에 계속 문제가 생기면 상대경로 말고 절대경로로 지정하는 것을 추천한다.

4. coco8.yaml 파일 설정 

  • path : dataset root 경로
  • train : training set 경로
  • val : validation set 경로
  • test : test set 경로
  • classes : 클래스 정보
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/coco8  # dataset root dir
train: images/train  # train images (relative to 'path') 4 images
val: images/val  # val images (relative to 'path') 4 images
test:  # test images (optional)

# Classes (80 COCO classes)
names:
  0: person
  1: bicycle
  2: car
  ...
  77: teddy bear
  78: hair drier
  79: toothbrush

5. Training

다음 코드를 실행하여 Training 해주자.

from ultralytics import YOLO

# Load a model
model = YOLO('yolov8n.pt')  # load a pretrained model (recommended for training)

# Train the model
results = model.train(data='coco8.yaml', epochs=100, imgsz=640)

6. Inference

from ultralytics import YOLO

# 위에서 Trained된 모델 불러오기
model = YOLO('yolov8n.pt')

# inference 하고 싶은 이미지 불러오기
source = 'path/to/image.jpg'

# Run inference on the source
results = model(source)  # list of Results objects
반응형
SMALL

+ Recent posts