Start export implementation (#110)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Glenn Jocher
2022-12-29 14:17:14 +01:00
committed by GitHub
parent c1b38428bc
commit 92dad1c1b5
32 changed files with 827 additions and 222 deletions

View File

@ -1,49 +0,0 @@
# Ultralytics, GPL-3.0 license
# Parameters
nc: 80 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32
# YOLOv5 v6.0 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 3, C3, [1024]],
[-1, 1, SPPF, [1024, 5]], # 9
]
# YOLOv5 v6.0 head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]

View File

@ -1,64 +1,16 @@
import torch
from ultralytics import YOLO
from ultralytics.nn.modules import Detect, Segment
def export_onnx(model, file):
# YOLOv5 ONNX export
import onnx
im = torch.zeros(1, 3, 640, 640)
model.eval()
model(im, profile=True)
for k, m in model.named_modules():
if isinstance(m, (Detect, Segment)):
m.export = True
torch.onnx.export(
model,
im,
file,
verbose=False,
opset_version=12,
do_constant_folding=True, # WARNING: DNN inference with torch>=1.12 may require do_constant_folding=False
input_names=['images'])
# Checks
model_onnx = onnx.load(file) # load onnx model
onnx.checker.check_model(model_onnx) # check onnx model
# Metadata
d = {'stride': int(max(model.stride)), 'names': model.names}
for k, v in d.items():
meta = model_onnx.metadata_props.add()
meta.key, meta.value = k, str(v)
onnx.save(model_onnx, file)
if __name__ == "__main__":
model = YOLO()
print("yolov8n")
model.new("yolov8n.yaml")
print("yolov8n-seg")
model.new("yolov8n-seg.yaml")
print("yolov8s")
model.new("yolov8s.yaml")
# export_onnx(model.model, "yolov8s.onnx")
print("yolov8s-seg")
model.new("yolov8s-seg.yaml")
# export_onnx(model.model, "yolov8s-seg.onnx")
print("yolov8m")
model.new("yolov8m.yaml")
print("yolov8m-seg")
model.new("yolov8m-seg.yaml")
print("yolov8l")
model.new("yolov8l.yaml")
print("yolov8l-seg")
model.new("yolov8l-seg.yaml")
print("yolov8x")
model.new("yolov8x.yaml")
print("yolov8x-seg")
model.new("yolov8x-seg.yaml")
YOLO.new("yolov8n.yaml")
YOLO.new("yolov8n-seg.yaml")
YOLO.new("yolov8s.yaml")
YOLO.new("yolov8s-seg.yaml")
YOLO.new("yolov8m.yaml")
YOLO.new("yolov8m-seg.yaml")
YOLO.new("yolov8l.yaml")
YOLO.new("yolov8l-seg.yaml")
YOLO.new("yolov8x.yaml")
YOLO.new("yolov8x-seg.yaml")
# n vs n-seg: 8.9GFLOPs vs 12.8GFLOPs, 3.16M vs 3.6M. ch[0] // 4 (11.9GFLOPs, 3.39M)
# s vs s-seg: 28.8GFLOPs vs 44.4GFLOPs, 11.1M vs 12.9M. ch[0] // 4 (39.5GFLOPs, 11.7M)

View File

@ -2,11 +2,9 @@ import cv2
import hydra
from ultralytics.yolo.data import build_dataloader
from ultralytics.yolo.utils import ROOT
from ultralytics.yolo.utils import DEFAULT_CONFIG
from ultralytics.yolo.utils.plotting import plot_images
DEFAULT_CONFIG = ROOT / "yolo/utils/configs/default.yaml"
class Colors:
# Ultralytics color palette https://ultralytics.com/

View File

@ -2,11 +2,9 @@ import cv2
import hydra
from ultralytics.yolo.data import build_dataloader
from ultralytics.yolo.utils import ROOT
from ultralytics.yolo.utils import DEFAULT_CONFIG
from ultralytics.yolo.utils.plotting import plot_images
DEFAULT_CONFIG = ROOT / "yolo/utils/configs/default.yaml"
class Colors:
# Ultralytics color palette https://ultralytics.com/

View File

@ -3,11 +3,11 @@ from ultralytics.yolo.utils.checks import check_yaml
def test_model_parser():
cfg = check_yaml("../assets/dummy_model.yaml") # check YAML
cfg = check_yaml("yolov8n.yaml") # check YAML
# Create model
model = DetectionModel(cfg)
print(model)
model.info()
'''
# Options
if opt.line_profile: # profile layer by layer

View File

@ -62,6 +62,35 @@ def test_model_train_pretrained():
model(img)
def test_exports():
"""
Format Argument Suffix CPU GPU
0 PyTorch - .pt True True
1 TorchScript torchscript .torchscript True True
2 ONNX onnx .onnx True True
3 OpenVINO openvino _openvino_model True False
4 TensorRT engine .engine False True
5 CoreML coreml .mlmodel True False
6 TensorFlow SavedModel saved_model _saved_model True True
7 TensorFlow GraphDef pb .pb True True
8 TensorFlow Lite tflite .tflite True False
9 TensorFlow Edge TPU edgetpu _edgetpu.tflite False False
10 TensorFlow.js tfjs _web_model False False
11 PaddlePaddle paddle _paddle_model True True
"""
from ultralytics import YOLO
from ultralytics.yolo.engine.exporter import export_formats
print(export_formats())
model = YOLO.new("yolov8n.yaml")
model.export(format='torchscript')
model.export(format='onnx')
model.export(format='openvino')
model.export(format='coreml')
model.export(format='paddle')
def test():
test_model_forward()
test_model_info()