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

TFLite, ONNX, CoreML, TensorRT ๋‚ด๋ณด๋‚ด๊ธฐ

์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ํ•™์Šต๋œ YOLOv5 ๐Ÿš€ ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. PyTorch ์—์„œ ONNX ๋ฐ TorchScript ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

์‹œ์ž‘ํ•˜๊ธฐ ์ „์—

๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋ณต์ œํ•˜๊ณ  ์š”๊ตฌ์‚ฌํ•ญ.txt๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. Python>=3.8.0 ํ™˜๊ฒฝ์„ ํฌํ•จํ•˜์—ฌ PyTorch>=1.8. ๋ชจ๋ธ ๋ฐ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋Š” ์ตœ์‹  YOLOv5 ๋ฆด๋ฆฌ์Šค์—์„œ ์ž๋™์œผ๋กœ ๋‹ค์šด๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค.

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

For TensorRT ๋‚ด๋ณด๋‚ด๊ธฐ ์˜ˆ์ œ ( GPU ํ•„์š” )๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ๋…ธํŠธ๋ถ ๋ถ€๋ก ์„น์…˜์— ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝœ๋žฉ์—์„œ ์—ด๊ธฐ

ํ˜•์‹

YOLOv5 ์ถ”๋ก ์€ ๊ณต์‹์ ์œผ๋กœ 11๊ฐœ ํ˜•์‹์ด ์ง€์›๋ฉ๋‹ˆ๋‹ค:

ํ”„๋กœ ํŒ: ONNX ๋˜๋Š” OpenVINO ์œผ๋กœ ๋‚ด๋ณด๋‚ด๋ฉด CPU ์†๋„๊ฐ€ ์ตœ๋Œ€ 3๋ฐฐ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. CPU ๋ฒค์น˜๋งˆํฌ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ๐Ÿ’ก ํ”„๋กœํŒ: TensorRT ์œผ๋กœ ๋‚ด๋ณด๋‚ด๋ฉด GPU ์†๋„๊ฐ€ ์ตœ๋Œ€ 5๋ฐฐ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค. GPU ๋ฒค์น˜๋งˆํฌ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

ํ˜•์‹ export.py --include ๋ชจ๋ธ
PyTorch - yolov5s.pt
TorchScript torchscript yolov5s.torchscript
ONNX onnx yolov5s.onnx
OpenVINO openvino yolov5s_openvino_model/
TensorRT engine yolov5s.engine
CoreML coreml yolov5s.mlmodel
TensorFlow SavedModel saved_model yolov5s_saved_model/
TensorFlow GraphDef pb yolov5s.pb
TensorFlow Lite tflite yolov5s.tflite
TensorFlow Edge TPU edgetpu yolov5s_edgetpu.tflite
TensorFlow.js tfjs yolov5s_web_model/
PaddlePaddle paddle yolov5s_paddle_model/

๋ฒค์น˜๋งˆํฌ

์•„๋ž˜ ๋ฒค์น˜๋งˆํฌ๋Š” Colab Pro์™€ YOLOv5 ํŠœํ† ๋ฆฌ์–ผ ๋…ธํŠธ๋ถ์—์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ฝœ๋žฉ์—์„œ ์—ด๊ธฐ. ์žฌ์ƒ์‚ฐํ•˜๋ ค๋ฉด:

python benchmarks.py --weights yolov5s.pt --imgsz 640 --device 0

Colab Pro V100 GPU

benchmarks: weights=/content/yolov5/yolov5s.pt, imgsz=640, batch_size=1, data=/content/yolov5/data/coco128.yaml, device=0, half=False, test=False
Checking setup...
YOLOv5 ๐Ÿš€ v6.1-135-g7926afc torch 1.10.0+cu111 CUDA:0 (Tesla V100-SXM2-16GB, 16160MiB)
Setup complete โœ… (8 CPUs, 51.0 GB RAM, 46.7/166.8 GB disk)

Benchmarks complete (458.07s)
                   Format  mAP@0.5:0.95  Inference time (ms)
0                 PyTorch        0.4623                10.19
1             TorchScript        0.4623                 6.85
2                    ONNX        0.4623                14.63
3                OpenVINO           NaN                  NaN
4                TensorRT        0.4617                 1.89
5                  CoreML           NaN                  NaN
6   TensorFlow SavedModel        0.4623                21.28
7     TensorFlow GraphDef        0.4623                21.22
8         TensorFlow Lite           NaN                  NaN
9     TensorFlow Edge TPU           NaN                  NaN
10          TensorFlow.js           NaN                  NaN

Colab Pro CPU

benchmarks: weights=/content/yolov5/yolov5s.pt, imgsz=640, batch_size=1, data=/content/yolov5/data/coco128.yaml, device=cpu, half=False, test=False
Checking setup...
YOLOv5 ๐Ÿš€ v6.1-135-g7926afc torch 1.10.0+cu111 CPU
Setup complete โœ… (8 CPUs, 51.0 GB RAM, 41.5/166.8 GB disk)

Benchmarks complete (241.20s)
                   Format  mAP@0.5:0.95  Inference time (ms)
0                 PyTorch        0.4623               127.61
1             TorchScript        0.4623               131.23
2                    ONNX        0.4623                69.34
3                OpenVINO        0.4623                66.52
4                TensorRT           NaN                  NaN
5                  CoreML           NaN                  NaN
6   TensorFlow SavedModel        0.4623               123.79
7     TensorFlow GraphDef        0.4623               121.57
8         TensorFlow Lite        0.4623               316.61
9     TensorFlow Edge TPU           NaN                  NaN
10          TensorFlow.js           NaN                  NaN

ํ•™์Šต๋œ YOLOv5 ๋ชจ๋ธ ๋‚ด๋ณด๋‚ด๊ธฐ

์ด ๋ช…๋ น์€ ์‚ฌ์ „ ํ•™์Šต๋œ YOLOv5s ๋ชจ๋ธ์„ TorchScript ๋ฐ ONNX ํ˜•์‹์œผ๋กœ ๋‚ด๋ณด๋ƒ…๋‹ˆ๋‹ค. yolov5s.pt ์€ ๋‘ ๋ฒˆ์งธ๋กœ ์ž‘์€ ๋ชจ๋ธ์ธ '์Šค๋ชฐ' ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์˜ต์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. yolov5n.pt, yolov5m.pt, yolov5l.pt ๊ทธ๋ฆฌ๊ณ  yolov5x.pt์™€ ํ•จ๊ป˜ P6์— ๋Œ€์‘ํ•˜๋Š” ์ œํ’ˆ, ์ฆ‰ yolov5s6.pt ๋˜๋Š” ์‚ฌ์šฉ์ž ์ง€์ • ๊ต์œก ์ฒดํฌํฌ์ธํŠธ(์˜ˆ runs/exp/weights/best.pt. ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ README๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ํ…Œ์ด๋ธ”.

python export.py --weights yolov5s.pt --include torchscript onnx

๐Ÿ’ก ํ”„๋กœ ํŒ: ์ถ”๊ฐ€ --half FP16 ์ ˆ๋ฐ˜์œผ๋กœ ๋ชจ๋ธ ๋‚ด๋ณด๋‚ด๊ธฐ ์ •๋ฐ€๋„ ์ž‘์€ ํŒŒ์ผ ํฌ๊ธฐ์šฉ

์ถœ๋ ฅ:

export: data=data/coco128.yaml, weights=['yolov5s.pt'], imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, train=False, keras=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=12, verbose=False, workspace=4, nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['torchscript', 'onnx']
YOLOv5 ๐Ÿš€ v6.2-104-ge3e5122 Python-3.8.0 torch-1.12.1+cu113 CPU

Downloading https://github.com/ultralytics/yolov5/releases/download/v6.2/yolov5s.pt to yolov5s.pt...
100% 14.1M/14.1M [00:00<00:00, 274MB/s]

Fusing layers...
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients

PyTorch: starting from yolov5s.pt with output shape (1, 25200, 85) (14.1 MB)

TorchScript: starting export with torch 1.12.1+cu113...
TorchScript: export success โœ… 1.7s, saved as yolov5s.torchscript (28.1 MB)

ONNX: starting export with onnx 1.12.0...
ONNX: export success โœ… 2.3s, saved as yolov5s.onnx (28.0 MB)

Export complete (5.5s)
Results saved to /content/yolov5
Detect:          python detect.py --weights yolov5s.onnx
Validate:        python val.py --weights yolov5s.onnx
PyTorch Hub:     model = torch.hub.load('ultralytics/yolov5', 'custom', 'yolov5s.onnx')
Visualize:       https://netron.app/

๋‚ด๋ณด๋‚ธ 3๊ฐœ์˜ ๋ชจ๋ธ์€ ์›๋ณธ ๋ชจ๋ธ( PyTorch )๊ณผ ํ•จ๊ป˜ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค:

YOLO ๋‚ด๋ณด๋‚ด๊ธฐ ์œ„์น˜

๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ์„ ์‹œ๊ฐํ™”ํ•  ๋•Œ๋Š” ๋„คํŠธ๋ก  ๋ทฐ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค:

YOLO ๋ชจ๋ธ ์‹œ๊ฐํ™”

์ต์ŠคํฌํŠธ๋œ ๋ชจ๋ธ ์‚ฌ์šฉ ์˜ˆ์‹œ

detect.py ๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ์—์„œ ์ถ”๋ก ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค:

python detect.py --weights yolov5s.pt                 # PyTorch
                           yolov5s.torchscript        # TorchScript
                           yolov5s.onnx               # ONNX Runtime or OpenCV DNN with dnn=True
                           yolov5s_openvino_model     # OpenVINO
                           yolov5s.engine             # TensorRT
                           yolov5s.mlmodel            # CoreML (macOS only)
                           yolov5s_saved_model        # TensorFlow SavedModel
                           yolov5s.pb                 # TensorFlow GraphDef
                           yolov5s.tflite             # TensorFlow Lite
                           yolov5s_edgetpu.tflite     # TensorFlow Edge TPU
                           yolov5s_paddle_model       # PaddlePaddle

val.py ๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค:

python val.py --weights yolov5s.pt                 # PyTorch
                        yolov5s.torchscript        # TorchScript
                        yolov5s.onnx               # ONNX Runtime or OpenCV DNN with dnn=True
                        yolov5s_openvino_model     # OpenVINO
                        yolov5s.engine             # TensorRT
                        yolov5s.mlmodel            # CoreML (macOS Only)
                        yolov5s_saved_model        # TensorFlow SavedModel
                        yolov5s.pb                 # TensorFlow GraphDef
                        yolov5s.tflite             # TensorFlow Lite
                        yolov5s_edgetpu.tflite     # TensorFlow Edge TPU
                        yolov5s_paddle_model       # PaddlePaddle

๋‚ด๋ณด๋‚ธ YOLOv5 ๋ชจ๋ธ๊ณผ ํ•จ๊ป˜ PyTorch Hub๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค:

import torch

# Model
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s.pt")
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s.torchscript ")  # TorchScript
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s.onnx")  # ONNX Runtime
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s_openvino_model")  # OpenVINO
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s.engine")  # TensorRT
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s.mlmodel")  # CoreML (macOS Only)
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s_saved_model")  # TensorFlow SavedModel
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s.pb")  # TensorFlow GraphDef
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s.tflite")  # TensorFlow Lite
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s_edgetpu.tflite")  # TensorFlow Edge TPU
model = torch.hub.load("ultralytics/yolov5", "custom", "yolov5s_paddle_model")  # PaddlePaddle

# Images
img = "https://ultralytics.com/images/zidane.jpg"  # or file, Path, PIL, OpenCV, numpy, list

# Inference
results = model(img)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.

OpenCV DNN ์ถ”๋ก 

ONNX ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ OpenCV ์ถ”๋ก :

python export.py --weights yolov5s.pt --include onnx

python detect.py --weights yolov5s.onnx --dnn  # detect
python val.py --weights yolov5s.onnx --dnn  # validate

C++ ์ถ”๋ก 

YOLOv5 ๋‚ด๋ณด๋‚ธ ONNX ๋ชจ๋ธ ์˜ˆ์ œ์— ๋Œ€ํ•œ OpenCV DNN C++ ์ถ”๋ก :

YOLOv5 OpenVINO C++ ์ถ”๋ก  ์˜ˆ์ œ:

TensorFlow.js ์›น ๋ธŒ๋ผ์šฐ์ € ์ถ”๋ก 

์ง€์› ํ™˜๊ฒฝ

Ultralytics ๋Š” ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•˜๋ฉฐ, ๊ฐ ํ™˜๊ฒฝ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•„์ˆ˜ ์ข…์†์„ฑ์ด ์‚ฌ์ „ ์„ค์น˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. CUDA, CUDNN, Python, ๋ฐ PyTorch์™€ ๊ฐ™์€ ํ•„์ˆ˜ ์ข…์† ์š”์†Œ๋ฅผ ์„ค์น˜ํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ์ƒํƒœ

YOLOv5 CI

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

๐Ÿ“…1 ๋…„ ์ „ ์ƒ์„ฑ๋จ โœ๏ธ 1๊ฐœ์›” ์ „ ์—…๋ฐ์ดํŠธ๋จ

๋Œ“๊ธ€