์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

Ultralytics Docs: ์Šฌ๋ผ์ด์‹ฑ ์ถ”๋ก ์— SAHI์™€ ํ•จ๊ป˜ YOLOv8 ์‚ฌ์šฉ

SAHI (์Šฌ๋ผ์ด์‹ฑ ์ง€์› ํ•˜์ดํผ ์ถ”๋ก )์™€ YOLOv8 ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ Ultralytics ๋ฌธ์„œ์— ์˜ค์‹  ๊ฒƒ์„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ํฌ๊ด„์ ์ธ ๊ฐ€์ด๋“œ๋Š” YOLOv8 ์™€ ํ•จ๊ป˜ SAHI๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ํ•„์ˆ˜ ์ง€์‹์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•ฉ๋‹ˆ๋‹ค. SAHI๊ฐ€ ๋ฌด์—‡์ธ์ง€, ์Šฌ๋ผ์ด์Šค ์ถ”๋ก ์ด ๋Œ€๊ทœ๋ชจ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ค‘์š”ํ•œ ์ด์œ ์™€ ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ YOLOv8 ์™€ ํ†ตํ•ฉํ•˜์—ฌ ๊ฐœ์ฒด ๊ฐ์ง€ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค.

SAHI ์Šฌ๋ผ์ด์Šค ์ถ”๋ก  ๊ฐœ์š”

SAHI ์†Œ๊ฐœ

SAHI(์Šฌ๋ผ์ด์‹ฑ ์ง€์› ํ•˜์ดํผ ์ถ”๋ก )๋Š” ๋Œ€๊ทœ๋ชจ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€์˜ ๊ฐ์ฒด ๊ฐ์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ตœ์ ํ™”ํ•˜๋„๋ก ์„ค๊ณ„๋œ ํ˜์‹ ์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ํ•ต์‹ฌ ๊ธฐ๋Šฅ์€ ์ด๋ฏธ์ง€๋ฅผ ๊ด€๋ฆฌ ๊ฐ€๋Šฅํ•œ ์Šฌ๋ผ์ด์Šค๋กœ ๋ถ„ํ• ํ•˜๊ณ  ๊ฐ ์Šฌ๋ผ์ด์Šค์—์„œ ๊ฐ์ฒด ๊ฐ์ง€๋ฅผ ์‹คํ–‰ํ•œ ๋‹ค์Œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. SAHI๋Š” YOLO ์‹œ๋ฆฌ์ฆˆ๋ฅผ ๋น„๋กฏํ•œ ๋‹ค์–‘ํ•œ ๋ฌผ์ฒด ๊ฐ์ง€ ๋ชจ๋ธ๊ณผ ํ˜ธํ™˜๋˜๋ฏ€๋กœ ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค๋ฅผ ์ตœ์ ํ™”ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

SAHI์˜ ์ฃผ์š” ๊ธฐ๋Šฅ

  • ์›ํ™œํ•œ ํ†ตํ•ฉ: SAHI๋Š” YOLO ๋ชจ๋ธ๊ณผ ์†์‰ฝ๊ฒŒ ํ†ตํ•ฉ๋˜๋ฏ€๋กœ ๋งŽ์€ ์ฝ”๋“œ ์ˆ˜์ • ์—†์ด๋„ ์Šฌ๋ผ์ด์‹ฑ ๋ฐ ํƒ์ง€๋ฅผ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฆฌ์†Œ์Šค ํšจ์œจ์„ฑ: SAHI๋Š” ํฐ ์ด๋ฏธ์ง€๋ฅผ ์ž‘์€ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ตœ์ ํ™”ํ•˜์—ฌ ๋ฆฌ์†Œ์Šค๊ฐ€ ์ œํ•œ๋œ ํ•˜๋“œ์›จ์–ด์—์„œ ๊ณ ํ’ˆ์งˆ ํƒ์ง€๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
  • ๋†’์€ ์ •ํ™•๋„: SAHI๋Š” ์Šค๋งˆํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์Šคํ‹ฐ์นญ ํ”„๋กœ์„ธ์Šค ์ค‘์— ๊ฒน์น˜๋Š” ๊ฐ์ง€ ์ƒ์ž๋ฅผ ๋ณ‘ํ•ฉํ•จ์œผ๋กœ์จ ๊ฐ์ง€ ์ •ํ™•๋„๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.

์Šฌ๋ผ์ด์Šค ์ถ”๋ก ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?

์Šฌ๋ผ์ด์Šค ์ถ”๋ก ์€ ํฐ ์ด๋ฏธ์ง€๋‚˜ ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋ฅผ ์ž‘์€ ์„ธ๊ทธ๋จผํŠธ(์Šฌ๋ผ์ด์Šค)๋กœ ์„ธ๋ถ„ํ™”ํ•˜๊ณ , ์ด๋Ÿฌํ•œ ์Šฌ๋ผ์ด์Šค์—์„œ ๊ฐ์ฒด ๊ฐ์ง€๋ฅผ ์ˆ˜ํ–‰ํ•œ ๋‹ค์Œ, ์Šฌ๋ผ์ด์Šค๋ฅผ ๋‹ค์‹œ ์ปดํŒŒ์ผํ•˜์—ฌ ์›๋ณธ ์ด๋ฏธ์ง€์—์„œ ๊ฐ์ฒด ์œ„์น˜๋ฅผ ์žฌ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ์ˆ ์€ ์ปดํ“จํŒ… ๋ฆฌ์†Œ์Šค๊ฐ€ ์ œํ•œ๋˜์–ด ์žˆ๊ฑฐ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ดˆ๊ณ ํ•ด์ƒ๋„ ์ด๋ฏธ์ง€๋กœ ์ž‘์—…ํ•  ๋•Œ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

์Šฌ๋ผ์ด์Šค ์ถ”๋ก ์˜ ์ด์ 

  • ์ปดํ“จํŒ… ๋ถ€๋‹ด ๊ฐ์†Œ: ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด์Šค๊ฐ€ ์ž‘์„์ˆ˜๋ก ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ๋นจ๋ผ์ง€๊ณ  ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ ์–ด์ ธ ์ €์‚ฌ์–‘ ํ•˜๋“œ์›จ์–ด์—์„œ ๋” ์›ํ™œํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ณด์กด๋œ ๊ฐ์ง€ ํ’ˆ์งˆ: ๊ฐ ์Šฌ๋ผ์ด์Šค๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋ฏ€๋กœ, ์Šฌ๋ผ์ด์Šค๊ฐ€ ๊ด€์‹ฌ ์žˆ๋Š” ๋ฌผ์ฒด๋ฅผ ์บก์ฒ˜ํ•  ์ˆ˜ ์žˆ์„ ๋งŒํผ ์ถฉ๋ถ„ํžˆ ํฌ๋‹ค๋ฉด ๋ฌผ์ฒด ๊ฐ์ง€ ํ’ˆ์งˆ์ด ์ €ํ•˜๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • ํ–ฅ์ƒ๋œ ํ™•์žฅ์„ฑ: ์ด ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ํ•ด์ƒ๋„์— ๋”ฐ๋ผ ๋ฌผ์ฒด ๊ฐ์ง€ ๊ธฐ๋Šฅ์„ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์œ„์„ฑ ์ด๋ฏธ์ง€์—์„œ ์˜๋ฃŒ ์ง„๋‹จ์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ ๋‹ค์–‘ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ด์ƒ์ ์ž…๋‹ˆ๋‹ค.

YOLOv8 SAHI ์—†์Œ YOLOv8 SAHI์™€ ํ•จ๊ป˜
YOLOv8 SAHI ์—†์Œ YOLOv8 SAHI์™€ ํ•จ๊ป˜

์„ค์น˜ ๋ฐ ์ค€๋น„

์„ค์น˜

์‹œ์ž‘ํ•˜๋ ค๋ฉด ์ตœ์‹  ๋ฒ„์ „์˜ SAHI ๋ฐ Ultralytics ์„ ์„ค์น˜ํ•˜์„ธ์š”:

pip install -U ultralytics sahi

๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฐ ๋ฆฌ์†Œ์Šค ๋‹ค์šด๋กœ๋“œ

ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ค๊ณ  YOLOv8 ๋ชจ๋ธ๊ณผ ๋ช‡ ๊ฐ€์ง€ ํ…Œ์ŠคํŠธ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

from pathlib import Path

from IPython.display import Image
from sahi import AutoDetectionModel
from sahi.predict import get_prediction, get_sliced_prediction, predict
from sahi.utils.cv import read_image
from sahi.utils.file import download_from_url
from sahi.utils.yolov8 import download_yolov8s_model

# Download YOLOv8 model
yolov8_model_path = "models/yolov8s.pt"
download_yolov8s_model(yolov8_model_path)

# Download test images
download_from_url(
    "https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg",
    "demo_data/small-vehicles1.jpeg",
)
download_from_url(
    "https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png",
    "demo_data/terrain2.png",
)

ํ‘œ์ค€ ์ถ”๋ก ์„ ์‚ฌ์šฉํ•œ YOLOv8

๋ชจ๋ธ ์ธ์Šคํ„ด์Šคํ™”

๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ์ฒด ๊ฐ์ง€๋ฅผ ์œ„ํ•ด YOLOv8 ๋ชจ๋ธ์„ ์ธ์Šคํ„ด์Šคํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

detection_model = AutoDetectionModel.from_pretrained(
    model_type="yolov8",
    model_path=yolov8_model_path,
    confidence_threshold=0.3,
    device="cpu",  # or 'cuda:0'
)

ํ‘œ์ค€ ์˜ˆ์ธก ์ˆ˜ํ–‰

์ด๋ฏธ์ง€ ๊ฒฝ๋กœ ๋˜๋Š” ๋ฉ์–ด๋ฆฌ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ์ค€ ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

# With an image path
result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)

# With a numpy image
result = get_prediction(read_image("demo_data/small-vehicles1.jpeg"), detection_model)

๊ฒฐ๊ณผ ์‹œ๊ฐํ™”

์˜ˆ์ธก๋œ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค์™€ ๋งˆ์Šคํฌ๋ฅผ ๋‚ด๋ณด๋‚ด๊ณ  ์‹œ๊ฐํ™”ํ•ฉ๋‹ˆ๋‹ค:

result.export_visuals(export_dir="demo_data/")
Image("demo_data/prediction_visual.png")

์Šฌ๋ผ์ด์Šค ์ถ”๋ก ์„ ์‚ฌ์šฉํ•œ YOLOv8

์Šฌ๋ผ์ด์Šค ํฌ๊ธฐ์™€ ๊ฒน์นจ ๋น„์œจ์„ ์ง€์ •ํ•˜์—ฌ ์Šฌ๋ผ์ด์Šค ์ถ”๋ก ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค:

result = get_sliced_prediction(
    "demo_data/small-vehicles1.jpeg",
    detection_model,
    slice_height=256,
    slice_width=256,
    overlap_height_ratio=0.2,
    overlap_width_ratio=0.2,
)

์˜ˆ์ธก ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ

SAHI๋Š” PredictionResult ๊ฐ์ฒด๋ฅผ ๋‹ค์–‘ํ•œ ์–ด๋…ธํ…Œ์ด์…˜ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

# Access the object prediction list
object_prediction_list = result.object_prediction_list

# Convert to COCO annotation, COCO prediction, imantics, and fiftyone formats
result.to_coco_annotations()[:3]
result.to_coco_predictions(image_id=1)[:3]
result.to_imantics_annotations()[:3]
result.to_fiftyone_detections()[:3]

๋ฐฐ์น˜ ์˜ˆ์ธก

์ด๋ฏธ์ง€ ๋””๋ ‰ํ† ๋ฆฌ์— ๋Œ€ํ•œ ์ผ๊ด„ ์˜ˆ์ธก์˜ ๊ฒฝ์šฐ:

predict(
    model_type="yolov8",
    model_path="path/to/yolov8n.pt",
    model_device="cpu",  # or 'cuda:0'
    model_confidence_threshold=0.4,
    source="path/to/dir",
    slice_height=256,
    slice_width=256,
    overlap_height_ratio=0.2,
    overlap_width_ratio=0.2,
)

์ด์ œ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค! ์ด์ œ ํ‘œ์ค€ ์ถ”๋ก ๊ณผ ์Šฌ๋ผ์ด์Šค ์ถ”๋ก  ๋ชจ๋‘์— YOLOv8 ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ธ์šฉ ๋ฐ ๊ฐ์‚ฌ

์—ฐ๊ตฌ ๋˜๋Š” ๊ฐœ๋ฐœ ์ž‘์—…์— SAHI๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ์›๋ณธ SAHI ๋…ผ๋ฌธ์„ ์ธ์šฉํ•˜๊ณ  ์ €์ž๋ฅผ ์ธ์ •ํ•ด ์ฃผ์„ธ์š”:

@article{akyon2022sahi,
  title={Slicing Aided Hyper Inference and Fine-tuning for Small Object Detection},
  author={Akyon, Fatih Cagatay and Altinuc, Sinan Onur and Temizel, Alptekin},
  journal={2022 IEEE International Conference on Image Processing (ICIP)},
  doi={10.1109/ICIP46576.2022.9897990},
  pages={966-970},
  year={2022}
}

์ปดํ“จํ„ฐ ๋น„์ „ ์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ์œ„ํ•ด ์ด ๊ท€์ค‘ํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์œ ์ง€ ๊ด€๋ฆฌํ•ด ์ฃผ์‹  SAHI ์—ฐ๊ตฌ ๊ทธ๋ฃน์— ๊ฐ์‚ฌ์˜ ๋ง์”€์„ ์ „ํ•ฉ๋‹ˆ๋‹ค. SAHI์™€ ์ œ์ž‘์ž์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ SAHI GitHub ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.



Created 2023-11-12, Updated 2024-06-02
Authors: glenn-jocher (5)

๋Œ“๊ธ€