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

์ฝœ๋ฐฑ

์ฝœ๋ฐฑ

Ultralytics ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ํŠธ๋ ˆ์ธ, ๋ฐธ, ๋‚ด๋ณด๋‚ด๊ธฐ, ์˜ˆ์ธก ๋ชจ๋“œ์˜ ์ „๋žต์  ๋‹จ๊ณ„์—์„œ ์ฝœ๋ฐฑ์„ ์ง„์ž…์ ์œผ๋กœ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์ฝœ๋ฐฑ์€ Trainer, Validator๋˜๋Š” Predictor ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฐ์ฒด์˜ ๋ชจ๋“  ์†์„ฑ์€ ๋ฌธ์„œ์˜ ์ฐธ์กฐ ์„น์…˜์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.



Watch: ๋งˆ์Šคํ„ฐ๋ง Ultralytics YOLOv8 : ์ฝœ๋ฐฑ

์˜ˆ์ œ

์˜ˆ์ธก์œผ๋กœ ์ถ”๊ฐ€ ์ •๋ณด ๋ฐ˜ํ™˜

์ด ์˜ˆ์ œ์—์„œ๋Š” ๊ฐ ๊ฒฐ๊ณผ ๊ฐ์ฒด์™€ ํ•จ๊ป˜ ์›๋ณธ ํ”„๋ ˆ์ž„์„ ๋ฐ˜ํ™˜ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

from ultralytics import YOLO


def on_predict_batch_end(predictor):
    """Handle prediction batch end by combining results with corresponding frames; modifies predictor results."""
    _, image, _, _ = predictor.batch

    # Ensure that image is a list
    image = image if isinstance(image, list) else [image]

    # Combine the prediction results with the corresponding frames
    predictor.results = zip(predictor.results, image)


# Create a YOLO model instance
model = YOLO(f"yolov8n.pt")

# Add the custom callback to the model
model.add_callback("on_predict_batch_end", on_predict_batch_end)

# Iterate through the results and frames
for result, frame in model.predict():  # or model.track()
    pass

๋ชจ๋“  ์ฝœ๋ฐฑ

์ง€์›๋˜๋Š” ๋ชจ๋“  ์ฝœ๋ฐฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ฝœ๋ฐฑ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

ํŠธ๋ ˆ์ด๋„ˆ ์ฝœ๋ฐฑ

์ฝœ๋ฐฑ ์„ค๋ช…
on_pretrain_routine_start ์‚ฌ์ „ ํ›ˆ๋ จ ๋ฃจํ‹ด์ด ์‹œ์ž‘๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_pretrain_routine_end ์‚ฌ์ „ ํ›ˆ๋ จ ๋ฃจํ‹ด์ด ๋๋‚˜๋ฉด ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_train_start ํŠธ๋ ˆ์ด๋‹ ์‹œ์ž‘ ์‹œ ํŠธ๋ฆฌ๊ฑฐ๋จ
on_train_epoch_start ๊ฐ ํ›ˆ๋ จ ์—ํฌํฌ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_train_batch_start ๊ฐ ๊ต์œก ๋ฐฐ์น˜๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
optimizer_step ์˜ตํ‹ฐ๋งˆ์ด์ € ๋‹จ๊ณ„์—์„œ ํŠธ๋ฆฌ๊ฑฐ๋จ
on_before_zero_grad ๊ทธ๋ผ๋ฐ์ด์…˜์ด 0์ด ๋˜๊ธฐ ์ „์— ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_train_batch_end ๊ฐ ๊ต์œก ๋ฐฐ์น˜๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_train_epoch_end ๊ฐ ํ›ˆ๋ จ ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_fit_epoch_end ๊ฐ ํ• ์—ํฌํฌ๊ฐ€ ๋๋‚  ๋•Œ๋งˆ๋‹ค ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_model_save ๋ชจ๋ธ์ด ์ €์žฅ๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_train_end ๊ต์œก ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_params_update ๋ชจ๋ธ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—…๋ฐ์ดํŠธ๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
teardown ๊ต์œก ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ •๋ฆฌ ์ค‘์ผ ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.

์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ธฐ ์ฝœ๋ฐฑ

์ฝœ๋ฐฑ ์„ค๋ช…
on_val_start ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_val_batch_start ๊ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋ฐฐ์น˜๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_val_batch_end ๊ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋ฐฐ์น˜๊ฐ€ ๋๋‚  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_val_end ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์ธก์ž ์ฝœ๋ฐฑ

์ฝœ๋ฐฑ ์„ค๋ช…
on_predict_start ์˜ˆ์ธก ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_predict_batch_start ๊ฐ ์˜ˆ์ธก ๋ฐฐ์น˜๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_predict_postprocess_end ์˜ˆ์ธก ํ›„์ฒ˜๋ฆฌ ์ข…๋ฃŒ ์‹œ ํŠธ๋ฆฌ๊ฑฐ๋จ
on_predict_batch_end ๊ฐ ์˜ˆ์ธก ๋ฐฐ์น˜๊ฐ€ ๋๋‚  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_predict_end ์˜ˆ์ธก ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.

๋‚ด๋ณด๋‚ด๊ธฐ ์ฝœ๋ฐฑ

์ฝœ๋ฐฑ ์„ค๋ช…
on_export_start ๋‚ด๋ณด๋‚ด๊ธฐ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹œ์ž‘๋  ๋•Œ ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.
on_export_end ๋‚ด๋ณด๋‚ด๊ธฐ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ํŠธ๋ฆฌ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.


Created 2023-11-12, Updated 2024-06-10
Authors: glenn-jocher (7), RizwanMunawar (1), Laughing-q (1)

๋Œ“๊ธ€