YOLOv8, YOLOv10, YOLO11 환경 구성 및 데이터셋 구축 가이드

환경 구성

Anaconda 설치 및 가상환경 설정

Anaconda는 데이터 과학 워크플로우를 단순화하는 통합 플랫폼입니다. 설치 후 다음 명령으로 확인:

conda --version
conda info

프로젝트별 독립 환경 생성:

conda create --name yolov8_env python=3.8.10
conda activate yolov8_env

CUDA 및 cuDNN 설치

NVIDIA GPU가 필요하며 다음 명령으로 지원 버전 확인:

nvidia-smi

CUDA 11.x와 호환되는 cuDNN 설치 후 CUDA 디렉토리에 복사합니다.

PyTorch 및 YOLO 종속성 설치

YOLOv8 설치:

pip install ultralytics

YOLOv10 설치:

pip install -r requirements.txt
pip install -e .

데이터 라벨링 도구

LabelImg 설치 및 실행:

conda create --name labelImg_env python=3.8.10
pip install labelImg
labelImg

데이터셋 구축

디렉토리 구조

프로젝트 루트에 data 폴더 생성 후 하위 디렉토리 구성:

  • Annotations: XML 라벨 파일
  • images: 원본 이미지
  • ImageSets: 데이터 분할 정보
  • labels: YOLO 형식 TXT 파일

데이터 분할

데이터셋을 학습/검증/테스트 세트로 분할:

import os
import random

dataset_ratio = 0.85
validation_ratio = 0.15
label_files = os.listdir('data/Annotations')

indices = list(range(len(label_files)))
split_point = int(len(label_files) * dataset_ratio)

train_indices = random.sample(indices, split_point)
validation_indices = list(set(indices) - set(train_indices))

with open('data/ImageSets/train.txt', 'w') as f_train:
    for idx in train_indices:
        f_train.write(f"{label_files[idx][:-4]}\n")
        
with open('data/ImageSets/val.txt', 'w') as f_val:
    for idx in validation_indices:
        f_val.write(f"{label_files[idx][:-4]}\n")

데이터 형식 변환

XML을 YOLO TXT 형식으로 변환:

import xml.etree.ElementTree as ET

def convert_to_yolo(size, box):
    dw, dh = 1./size[0], 1./size[1]
    x_center = (box[0] + box[2])/2.0
    y_center = (box[1] + box[3])/2.0
    width = box[2] - box[0]
    height = box[3] - box[1]
    return [x_center*dw, y_center*dh, width*dw, height*dh]

def parse_xml(xml_path):
    tree = ET.parse(xml_path)
    root = tree.getroot()
    
    size = root.find('size')
    img_w = int(size.find('width').text)
    img_h = int(size.find('height').text)
    
    objects = []
    for obj in root.iter('object'):
        cls_name = obj.find('name').text
        bbox = obj.find('bndbox')
        coords = [float(bbox.find('xmin').text), float(bbox.find('ymin').text),
                 float(bbox.find('xmax').text), float(bbox.find('ymax').text)]
        objects.append((cls_name, coords))
    
    return img_w, img_h, objects

YAML 설정 파일

train: /project/data/train.txt
val: /project/data/val.txt

nc: 3
names: ["cat", "dog", "bird"]

모델 학습 및 추론

학습 실행

yolo train data=config.yaml model=yolov8s.pt epochs=150 imgsz=640

Python 학습 스크립트

from ultralytics import YOLO

detection_model = YOLO("yolov8n.yaml").load("yolov8n.pt")
training_results = detection_model.train(
    data="dataset_config.yaml",
    epochs=100,
    imgsz=640,
    device="cuda"
)

추론 실행

from ultralytics import YOLO

trained_model = YOLO('best.pt')
prediction_results = trained_model.predict('input_image.jpg', save=True)

Qt 기반 추론 인터페이스

from PyQt6.QtWidgets import QApplication, QMainWindow
import cv2
from ultralytics import YOLO

class InferenceApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.detection_model = YOLO('best.pt')
        # UI 구성 코드 생략
        
    def process_frame(self, frame):
        results = self.detection_model(frame)[0]
        return results.plot()

app = QApplication([])
window = InferenceApp()
window.show()
app.exec()

태그: YOLOv8 YOLOv10 YOLO11 PyTorch CUDA

5월 23일 05:40에 게시됨