πŸ€– AI/ML

YOLO

You Only Look Once

A real-time object detection algorithm. Detects objects in a single forward pass.

πŸ“– Detailed Explanation

YOLO (You Only Look Once) is a real-time object detection algorithm proposed by Joseph Redmon in 2015. Unlike the earlier R-CNN family, which performed region proposal and classification in two stages, YOLO processes the entire image in a single forward pass of a neural network and predicts bounding boxes and class probabilities simultaneously. This single-stage approach made real-time processing possible.

The core idea of YOLO is to divide an image into an S x S grid, where each grid cell predicts B bounding boxes and C class probabilities. Each bounding box has five values (x, y, w, h, confidence), and confidence is the product of the probability that an object exists in the box and the IoU (Intersection over Union). Detection, converted into this regression problem, is processed very quickly on a GPU.

YOLO has evolved through various versions. From YOLOv1 (2015) to YOLOv3 (2018, Darknet-53 backbone), it was developed by the original author, and afterwards YOLOv4 and YOLOv5 emerged under community leadership. YOLOv8 (2023), developed by Ultralytics, improved accuracy and speed with an anchor-free approach and a decoupled head structure. YOLOv9 (2024) resolved the information loss problem with PGI (Programmable Gradient Information) and the GELAN architecture, and YOLO11 (2024) achieved higher accuracy with 22% fewer parameters.

YOLO is widely used in fields that require real-time object detection, such as autonomous vehicles, CCTV analysis, drone video processing, sports analytics, and manufacturing quality inspection. In addition to detection, it supports various computer vision tasks such as segmentation, pose estimation, and tracking. It can be exported to various formats such as TensorRT, ONNX, and CoreML for edge device deployment.

πŸ’» Code Example

# YOLO11 객체 탐지 예제 (Ultralytics)
from ultralytics import YOLO
import cv2

# ============================================
# 1. λͺ¨λΈ λ‘œλ“œ 및 μΆ”λ‘ 
# ============================================
# 사전 ν•™μŠ΅λœ YOLO11 λͺ¨λΈ λ‘œλ“œ (n, s, m, l, x 크기)
model = YOLO("yolo11n.pt")  # nano 버전 (κ°€μž₯ 빠름)
# model = YOLO("yolo11s.pt")  # small
# model = YOLO("yolo11m.pt")  # medium
# model = YOLO("yolo11l.pt")  # large (정확도 μ΅œλŒ€)

# 이미지 μΆ”λ‘ 
results = model("image.jpg")

# κ²°κ³Ό 확인
for result in results:
    boxes = result.boxes  # λ°”μš΄λ”© λ°•μŠ€
    print(f"νƒμ§€λœ 객체 수: {len(boxes)}")

    for box in boxes:
        # μ’Œν‘œ, 클래슀, 신뒰도
        x1, y1, x2, y2 = box.xyxy[0].tolist()
        conf = box.conf[0].item()
        cls_id = int(box.cls[0].item())
        cls_name = model.names[cls_id]
        print(f"  - {cls_name}: {conf:.2f} at ({x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f})")

# κ²°κ³Ό μ‹œκ°ν™” 및 μ €μž₯
result.save("result.jpg")

# ============================================
# 2. λΉ„λ””μ˜€/μ›ΉμΊ  μ‹€μ‹œκ°„ μΆ”λ‘ 
# ============================================
# μ›ΉμΊ  μ‹€μ‹œκ°„ 탐지
results = model.predict(source=0, show=True, stream=True)
for r in results:
    pass  # μ‹€μ‹œκ°„ ν™”λ©΄ ν‘œμ‹œ

# λΉ„λ””μ˜€ 파일 처리
results = model.predict(
    source="video.mp4",
    save=True,            # κ²°κ³Ό μ €μž₯
    conf=0.5,             # 신뒰도 μž„κ³„κ°’
    iou=0.45,             # NMS IoU μž„κ³„κ°’
    device="cuda:0"       # GPU μ‚¬μš©
)

# ============================================
# 3. μ»€μŠ€ν…€ 데이터셋 ν•™μŠ΅
# ============================================
# YAML ν˜•μ‹μ˜ 데이터셋 μ„€μ • ν•„μš”
# data.yaml μ˜ˆμ‹œ:
# train: ./train/images
# val: ./val/images
# nc: 3  # 클래슀 수
# names: ['cat', 'dog', 'bird']

model = YOLO("yolo11n.pt")  # 사전 ν•™μŠ΅ κ°€μ€‘μΉ˜λ‘œ μ‹œμž‘

# ν•™μŠ΅ μ‹œμž‘
results = model.train(
    data="data.yaml",
    epochs=100,
    imgsz=640,
    batch=16,
    device="cuda:0",
    patience=20,          # Early stopping
    save=True,
    project="runs/detect",
    name="my_model"
)

# ============================================
# 4. λͺ¨λΈ 내보내기 (배포용)
# ============================================
# ONNX 포맷 (λ²”μš©)
model.export(format="onnx", simplify=True)

# TensorRT (NVIDIA GPU μ΅œμ ν™”)
model.export(format="engine", device=0)

# CoreML (Apple λ””λ°”μ΄μŠ€)
model.export(format="coreml")

# TFLite (λͺ¨λ°”일/μ—£μ§€)
model.export(format="tflite")

# ============================================
# 5. λ‹€μ–‘ν•œ νƒœμŠ€ν¬
# ============================================
# μΈμŠ€ν„΄μŠ€ μ„Έκ·Έλ©˜ν…Œμ΄μ…˜
seg_model = YOLO("yolo11n-seg.pt")
seg_results = seg_model("image.jpg")

# 포즈 μΆ”μ •
pose_model = YOLO("yolo11n-pose.pt")
pose_results = pose_model("image.jpg")

# 이미지 λΆ„λ₯˜
cls_model = YOLO("yolo11n-cls.pt")
cls_results = cls_model("image.jpg")

# 객체 좔적 (BoT-SORT, ByteTrack)
track_results = model.track(
    source="video.mp4",
    tracker="botsort.yaml",  # λ˜λŠ” "bytetrack.yaml"
    persist=True
)

πŸ“Š Performance & Cost: Comparison by Version (YOLOv8, YOLOv9, YOLO11)

COCO val2017 benchmark (640x640 resolution, based on NVIDIA T4 GPU)

Model mAP@50-95 Parameter FLOPs Inference speed
YOLOv8n 37.3% 3.2M 8.7G ~16ms (~61 FPS)
YOLOv8s 44.9% 11.2M 28.6G ~21ms (~48 FPS)
YOLOv8m 50.2% 25.9M 78.9G ~33ms (~30 FPS)
YOLOv9t 38.3% 2.0M 7.7G ~15ms (~67 FPS)
YOLOv9s 46.8% 7.2M 26.7G ~18ms (~56 FPS)
YOLOv9c 53.0% 25.5M 102.8G ~28ms (~36 FPS)
YOLO11n 39.5% 2.6M 6.5G ~17ms (~57 FPS)
YOLO11s 47.0% 9.4M 21.5G ~24ms (~42 FPS)
YOLO11m 51.5% 20.1M 68.0G ~27ms (~37 FPS)
YOLO11l 53.4% 25.3M 86.9G ~33ms (~30 FPS)

Key features by version:

Recommended choices:

* Cost: Ultralytics YOLO is AGPL-3.0 open source (free). A commercial license requires contacting Ultralytics Enterprise.

πŸ—£ How It's Said in Practice

"If you need real-time detection, try YOLO11n first, and if the accuracy is insufficient, move up to s or m."

-> When discussing model size selection

"If you convert to TensorRT and deploy, inference speed gets 2-3 times faster."

-> When optimizing production deployment

"When fine-tuning on custom data, start from the pretrained weights."

-> When guiding custom training

"YOLO11 has the best performance relative to its parameters, so I start new projects with it."

-> When advising on version selection

⚠ Common Mistakes & Cautions

Input image size mismatch

If you train at 640x640 but use a different size at inference, performance drops. Match the imgsz parameter.

Setting the conf threshold too low

With a low value such as conf=0.1, false positives (False Positive) surge. Usually start between 0.25 and 0.5.

Excessive data augmentation

Excessive augmentation such as mosaic and mixup is actually harmful for detecting small objects. Turn off augmentation for the last 10 epochs (close_mosaic option).

Correct approach: incremental optimization

1) Establish a baseline with default settings 2) Adjust the conf/iou thresholds 3) Change the model size 4) Fine-tune hyperparameters 5) Optimize the deployment format (ONNX/TensorRT)

πŸ”— Related Terms

πŸ“š Learn More