https://learnopencv.com/fine-tuning-yolov12/

이 글은 최신 객체 탐지 모델인 YOLOv12, YOLOv11, 그리고 Darknet 기반 YOLOv7을 HRSC2016-MS 데이터셋에 맞게 미세 조정(fine-tuning)하는 과정을 다루고 있습니다. HRSC2016-MS는 다양한 크기, 밀도, 방향을 가진 선박의 항공 이미지를 포함하고 있어 모델 학습에 도전적인 데이터셋입니다.
5.결론 및 주요 시사점:
이 글은 HRSC2016-MS 데이터셋의 도전과제를 다루며, 데이터 준비와 구조화의 중요성을 강조하고 있습니다. 이를 통해 모델의 정확도를 향상시키는 방법을 제시합니다.
먼저 데이터셋에 대한 이해가 필요합니다
HRSC2016-MS 데이터셋은 해양 객체 탐지(Maritime Object Detection)를 위한 데이터셋으로, 항공 이미지에서 선박(Ship)을 탐지하는 작업에 사용됩니다.

HRSC2016-MS 데이터셋을 YOLO 기반 모델(YOLOv12, YOLOv11, YOLOv7)에서 최적화하기 위해 적절한 데이터 전처리와 구조화가 필요합니다. 이 과정에서 중요한 요소들을 단계별로 정리하면 다음과 같습니다.
HRSC2016-MS 데이터셋은 XML 형식(Pascal VOC 형식)을 사용합니다. 하지만 YOLO 모델은 텍스트 기반 어노테이션 형식을 요구합니다. 따라서 다음과 같은 변환 과정이 필요합니다.
class_id x_center y_center width height
모델의 일반화를 위해 데이터를 적절히 분할해야 합니다. 일반적으로 다음과 같은 비율을 사용합니다.
데이터 분할 시 고려할 점:
from sklearn.model_selection import train_test_split
import os
import shutil
# 데이터셋 경로
image_dir = "path/to/images"
label_dir = "path/to/labels"
# 파일 리스트 로드
image_files = [f for f in os.listdir(image_dir) if f.endswith('.jpg')]
train_files, val_test_files = train_test_split(image_files, test_size=0.2, random_state=42)
val_files, test_files = train_test_split(val_test_files, test_size=0.5, random_state=42)
# 데이터셋 이동 함수
def move_files(file_list, src_img_dir, src_lbl_dir, dest_img_dir, dest_lbl_dir):
os.makedirs(dest_img_dir, exist_ok=True)
os.makedirs(dest_lbl_dir, exist_ok=True)
for file in file_list:
shutil.move(os.path.join(src_img_dir, file), os.path.join(dest_img_dir, file))
shutil.move(os.path.join(src_lbl_dir, file.replace('.jpg', '.txt')), os.path.join(dest_lbl_dir, file.replace('.jpg', '.txt')))
# 데이터셋 분할 실행
move_files(train_files, image_dir, label_dir, "dataset/train/images", "dataset/train/labels")
move_files(val_files, image_dir, label_dir, "dataset/val/images", "dataset/val/labels")
move_files(test_files, image_dir, label_dir, "dataset/test/images", "dataset/test/labels")
작은 데이터셋에서는 데이터 증강(Data Augmentation)이 필수적입니다. 특히 HRSC2016-MS 데이터셋처럼 객체 크기와 방향이 다양한 경우, 여러 증강 기법을 적용할 수 있습니다.
✅ 적용 가능한 데이터 증강 기법
YOLOv12 및 YOLOv11에서는 기본적으로 Mosaic Augmentation과 MixUp을 활용하여 작은 객체 탐지 성능을 향상시킵니다.
from albumentations import (
HorizontalFlip, Rotate, RandomBrightnessContrast, RandomScale, GaussNoise, Compose
)
import cv2
import numpy as np
# 데이터 증강 함수
def augment_image(image):
transform = Compose([
HorizontalFlip(p=0.5),
Rotate(limit=20, p=0.5),
RandomScale(scale_limit=0.2, p=0.5),
RandomBrightnessContrast(p=0.3),
GaussNoise(var_limit=(10.0, 50.0), p=0.3)
])
augmented = transform(image=image)
return augmented['image']
# 이미지 로드 및 증강 적용 예시
image = cv2.imread("example.jpg")
augmented_image = augment_image(image)
cv2.imwrite("augmented_example.jpg", augmented_image)
YOLO 기반 모델을 훈련하려면, 데이터 디렉토리 구조를 적절히 구성해야 합니다.
디렉토리 구조 예시 (YOLO 형식)
dataset/
├── train/
│ ├── images/
│ │ ├── image1.jpg
│ │ ├── image2.jpg
│ ├── labels/
│ ├── image1.txt
│ ├── image2.txt
├── val/
│ ├── images/
│ ├── labels/
├── test/
│ ├── images/
│ ├── labels/
YOLO 모델을 훈련할 때 필요한 설정 파일을 생성합니다.
train.txt, val.txt, test.txt 생성
import os
def create_data_list(image_dir, output_file):
with open(output_file, 'w') as f:
for img_file in os.listdir(image_dir):
if img_file.endswith('.jpg'):
f.write(f"{image_dir}/{img_file}\n")
create_data_list("dataset/train/images", "train.txt")
create_data_list("dataset/val/images", "val.txt")
create_data_list("dataset/test/images", "test.txt")
YOLO 데이터 설정 파일 (data.yaml)
train: /path/to/dataset/train.txt
val: /path/to/dataset/val.txt
nc: 10 # 클래스 개수 (HRSC2016-MS 기준)
names: ['ship', 'boat', 'yacht', 'cargo', 'cruise', 'ferry', 'submarine', 'tanker', 'warship', 'other']
최근 YOLOv12 모델(Ultralytics 구현)을 사용한 실험 설정을 기반으로, 성능 최적화를 위해 적용된 구성 요소를 분석해 보겠습니다.
✅ YOLOv12 (Ultralytics Implementation) 사용
Ultralytics YOLOv12는 최신 YOLO 모델로, 기존 YOLO 시리즈보다 더 빠르고 정확한 객체 탐지 성능을 제공함.
어텐션(Aggregated Attention Mechanism) 기법이 추가되어 작은 객체 탐지 성능이 개선됨.
✅ 이미지 크기 설정: 640×640
✅ Batch Size: 8
✅ Epochs: 100


세 개의 모델 모두 주어진 실제(Ground Truth) 이미지에서 유사한 성능을 보였다. Darknet은 다른 모델들이 놓치거나 덜 정확하게 탐지한 작은 객체들을 더 정확하게 식별하는 성능을 보였다.

평가된 모든 모델은 밀집된 작은 객체를 탐지하는 데 강력한 성능을 보였다. 그러나 YOLOv11은 중복 탐지(overlapping detections)를 나타냈으며, 이는 불필요하거나 덜 정확한 예측을 의미할 수 있다. 전반적으로, 모델들은 다양한 수준의 중복과 정확도를 보이며 탐지 작업을 효과적으로 수행했다.

YOLOv12는 Ground Truth 이미지에서 표시된 객체를 탐지할 수 있었던 유일한 모델이었다. 전반적으로 모든 모델이 객체 탐지에서 유사한 성능을 보였지만, YOLOv11은 여전히 중복 탐지(overlapping detections)를 발생시켜 불필요한 예측이나 정확도 감소 가능성을 나타냈다.

주어진 이미지는 특히 밀집된 작은 객체 탐지 상황에서 객체 탐지 모델을 평가하는 데 이상적인 테스트 사례이다. 단순한 비교에서는 YOLOv12가 더 많은 객체를 탐지했기 때문에 다른 모델보다 우수해 보일 수 있다. 그러나 자세한 분석 결과, YOLOv12의 많은 탐지가 중복되었거나 불필요한 예측(오검출)임이 밝혀졌다. 반면, Darknet 기반 YOLOv7은 탐지한 객체 수는 적었지만, 오검출과 중복이 최소화된 높은 정확도를 보였다. 객체 탐지 모델의 성능을 평가할 때는 단순한 탐지 개수가 아니라, 정확도(Precision), 재현율(Recall), 오검출율(False Detection Rate) 등을 종합적으로 고려해야 한다.