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 ํํ ๋ฆฌ์ผ ๋ ธํธ๋ถ์์ ์คํ๋ฉ๋๋ค. . ์ฌ์์ฐํ๋ ค๋ฉด:
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๋ฅผ ์ฐธ์กฐํ์ธ์. ํ
์ด๋ธ.
๐ก ํ๋ก ํ: ์ถ๊ฐ --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 )๊ณผ ํจ๊ป ์ ์ฅ๋ฉ๋๋ค:
๋ด๋ณด๋ธ ๋ชจ๋ธ์ ์๊ฐํํ ๋๋ ๋คํธ๋ก ๋ทฐ์ด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค:
์ต์คํฌํธ๋ ๋ชจ๋ธ ์ฌ์ฉ ์์
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++ ์ถ๋ก :
- https://github.com/Hexmagic/ONNX-yolov5/blob/master/src/test.cpp
- https://github.com/doleron/yolov5-opencv-cpp-python
YOLOv5 OpenVINO C++ ์ถ๋ก ์์ :
- https://github.com/dacquaviva/yolov5-openvino-cpp-python
- https://github.com/UNeedCryDear/yolov5-seg-opencv-dnn-cpp
TensorFlow.js ์น ๋ธ๋ผ์ฐ์ ์ถ๋ก
์ง์ ํ๊ฒฝ
Ultralytics ๋ ๋ฐ๋ก ์ฌ์ฉํ ์ ์๋ ๋ค์ํ ํ๊ฒฝ์ ์ ๊ณตํ๋ฉฐ, ๊ฐ ํ๊ฒฝ์๋ ๋ค์๊ณผ ๊ฐ์ ํ์ ์ข ์์ฑ์ด ์ฌ์ ์ค์น๋์ด ์์ต๋๋ค. CUDA, CUDNN, Python, ๋ฐ PyTorch์ ๊ฐ์ ํ์ ์ข ์ ์์๋ฅผ ์ค์นํ์ฌ ํ๋ก์ ํธ๋ฅผ ์์ํ ์ ์์ต๋๋ค.
- ๋ฌด๋ฃ GPU ๋ ธํธ๋ถ:
- Google Cloud: GCP ๋น ๋ฅธ ์์ ๊ฐ์ด๋
- Amazon: AWS ๋น ๋ฅธ ์์ ๊ฐ์ด๋
- Azure: AzureML ๋น ๋ฅธ ์์ ๊ฐ์ด๋
- Docker: Docker ๋น ๋ฅธ ์์ ๊ฐ์ด๋
ํ๋ก์ ํธ ์ํ
์ด ๋ฐฐ์ง๋ ๋ชจ๋ YOLOv5 GitHub Actions ์ง์์ ํตํฉ(CI) ํ ์คํธ๊ฐ ์ฑ๊ณต์ ์ผ๋ก ํต๊ณผ๋์์์ ๋ํ๋ ๋๋ค. ์ด๋ฌํ CI ํ ์คํธ๋ ๊ต์ก, ๊ฒ์ฆ, ์ถ๋ก , ๋ด๋ณด๋ด๊ธฐ ๋ฐ ๋ฒค์น๋งํฌ ๋ฑ ๋ค์ํ ์ฃผ์ ์ธก๋ฉด์์ YOLOv5 ์ ๊ธฐ๋ฅ๊ณผ ์ฑ๋ฅ์ ์๊ฒฉํ๊ฒ ํ์ธํฉ๋๋ค. 24์๊ฐ๋ง๋ค ๊ทธ๋ฆฌ๊ณ ์๋ก์ด ์ปค๋ฐ์ด ์์ ๋๋ง๋ค ํ ์คํธ๋ฅผ ์ํํ์ฌ macOS, Windows ๋ฐ Ubuntu์์ ์ผ๊ด๋๊ณ ์์ ์ ์ธ ์๋์ ๋ณด์ฅํฉ๋๋ค.