export.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. """
  3. Export a YOLOv5 PyTorch model to other formats. TensorFlow exports authored by https://github.com/zldrobit
  4. Format | `export.py --include` | Model
  5. --- | --- | ---
  6. PyTorch | - | yolov5s.pt
  7. TorchScript | `torchscript` | yolov5s.torchscript
  8. ONNX | `onnx` | yolov5s.onnx
  9. OpenVINO | `openvino` | yolov5s_openvino_model/
  10. TensorRT | `engine` | yolov5s.engine
  11. CoreML | `coreml` | yolov5s.mlmodel
  12. TensorFlow SavedModel | `saved_model` | yolov5s_saved_model/
  13. TensorFlow GraphDef | `pb` | yolov5s.pb
  14. TensorFlow Lite | `tflite` | yolov5s.tflite
  15. TensorFlow Edge TPU | `edgetpu` | yolov5s_edgetpu.tflite
  16. TensorFlow.js | `tfjs` | yolov5s_web_model/
  17. Requirements:
  18. $ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime openvino-dev tensorflow-cpu # CPU
  19. $ pip install -r requirements.txt coremltools onnx onnx-simplifier onnxruntime-gpu openvino-dev tensorflow # GPU
  20. Usage:
  21. $ python path/to/export.py --weights yolov5s.pt --include torchscript onnx openvino engine coreml tflite ...
  22. Inference:
  23. $ python path/to/detect.py --weights yolov5s.pt # PyTorch
  24. yolov5s.torchscript # TorchScript
  25. yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
  26. yolov5s.xml # OpenVINO
  27. yolov5s.engine # TensorRT
  28. yolov5s.mlmodel # CoreML (macOS-only)
  29. yolov5s_saved_model # TensorFlow SavedModel
  30. yolov5s.pb # TensorFlow GraphDef
  31. yolov5s.tflite # TensorFlow Lite
  32. yolov5s_edgetpu.tflite # TensorFlow Edge TPU
  33. TensorFlow.js:
  34. $ cd .. && git clone https://github.com/zldrobit/tfjs-yolov5-example.git && cd tfjs-yolov5-example
  35. $ npm install
  36. $ ln -s ../../yolov5/yolov5s_web_model public/yolov5s_web_model
  37. $ npm start
  38. """
  39. import argparse
  40. import json
  41. import os
  42. import platform
  43. import subprocess
  44. import sys
  45. import time
  46. import warnings
  47. from pathlib import Path
  48. import pandas as pd
  49. import torch
  50. from torch.utils.mobile_optimizer import optimize_for_mobile
  51. FILE = Path(__file__).resolve()
  52. ROOT = FILE.parents[0] # YOLOv5 root directory
  53. if str(ROOT) not in sys.path:
  54. sys.path.append(str(ROOT)) # add ROOT to PATH
  55. if platform.system() != 'Windows':
  56. ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
  57. from models.experimental import attempt_load
  58. from models.yolo import Detect
  59. from utils.datasets import LoadImages
  60. from utils.general import (LOGGER, check_dataset, check_img_size, check_requirements, check_version, colorstr,
  61. file_size, print_args, url2file)
  62. from utils.torch_utils import select_device
  63. def export_formats():
  64. # YOLOv5 export formats
  65. x = [
  66. ['PyTorch', '-', '.pt', True],
  67. ['TorchScript', 'torchscript', '.torchscript', True],
  68. ['ONNX', 'onnx', '.onnx', True],
  69. ['OpenVINO', 'openvino', '_openvino_model', False],
  70. ['TensorRT', 'engine', '.engine', True],
  71. ['CoreML', 'coreml', '.mlmodel', False],
  72. ['TensorFlow SavedModel', 'saved_model', '_saved_model', True],
  73. ['TensorFlow GraphDef', 'pb', '.pb', True],
  74. ['TensorFlow Lite', 'tflite', '.tflite', False],
  75. ['TensorFlow Edge TPU', 'edgetpu', '_edgetpu.tflite', False],
  76. ['TensorFlow.js', 'tfjs', '_web_model', False],]
  77. return pd.DataFrame(x, columns=['Format', 'Argument', 'Suffix', 'GPU'])
  78. def export_torchscript(model, im, file, optimize, prefix=colorstr('TorchScript:')):
  79. # YOLOv5 TorchScript model export
  80. try:
  81. LOGGER.info(f'\n{prefix} starting export with torch {torch.__version__}...')
  82. f = file.with_suffix('.torchscript')
  83. ts = torch.jit.trace(model, im, strict=False)
  84. d = {"shape": im.shape, "stride": int(max(model.stride)), "names": model.names}
  85. extra_files = {'config.txt': json.dumps(d)} # torch._C.ExtraFilesMap()
  86. if optimize: # https://pytorch.org/tutorials/recipes/mobile_interpreter.html
  87. optimize_for_mobile(ts)._save_for_lite_interpreter(str(f), _extra_files=extra_files)
  88. else:
  89. ts.save(str(f), _extra_files=extra_files)
  90. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  91. return f
  92. except Exception as e:
  93. LOGGER.info(f'{prefix} export failure: {e}')
  94. def export_onnx(model, im, file, opset, train, dynamic, simplify, prefix=colorstr('ONNX:')):
  95. # YOLOv5 ONNX export
  96. try:
  97. check_requirements(('onnx',))
  98. import onnx
  99. LOGGER.info(f'\n{prefix} starting export with onnx {onnx.__version__}...')
  100. f = file.with_suffix('.onnx')
  101. torch.onnx.export(
  102. model,
  103. im,
  104. f,
  105. verbose=False,
  106. opset_version=opset,
  107. training=torch.onnx.TrainingMode.TRAINING if train else torch.onnx.TrainingMode.EVAL,
  108. do_constant_folding=not train,
  109. input_names=['images'],
  110. output_names=['output'],
  111. dynamic_axes={
  112. 'images': {
  113. 0: 'batch',
  114. 2: 'height',
  115. 3: 'width'}, # shape(1,3,640,640)
  116. 'output': {
  117. 0: 'batch',
  118. 1: 'anchors'} # shape(1,25200,85)
  119. } if dynamic else None)
  120. # Checks
  121. model_onnx = onnx.load(f) # load onnx model
  122. onnx.checker.check_model(model_onnx) # check onnx model
  123. # Metadata
  124. d = {'stride': int(max(model.stride)), 'names': model.names}
  125. for k, v in d.items():
  126. meta = model_onnx.metadata_props.add()
  127. meta.key, meta.value = k, str(v)
  128. onnx.save(model_onnx, f)
  129. # Simplify
  130. if simplify:
  131. try:
  132. check_requirements(('onnx-simplifier',))
  133. import onnxsim
  134. LOGGER.info(f'{prefix} simplifying with onnx-simplifier {onnxsim.__version__}...')
  135. model_onnx, check = onnxsim.simplify(model_onnx,
  136. dynamic_input_shape=dynamic,
  137. input_shapes={'images': list(im.shape)} if dynamic else None)
  138. assert check, 'assert check failed'
  139. onnx.save(model_onnx, f)
  140. except Exception as e:
  141. LOGGER.info(f'{prefix} simplifier failure: {e}')
  142. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  143. return f
  144. except Exception as e:
  145. LOGGER.info(f'{prefix} export failure: {e}')
  146. def export_openvino(model, im, file, prefix=colorstr('OpenVINO:')):
  147. # YOLOv5 OpenVINO export
  148. try:
  149. check_requirements(('openvino-dev',)) # requires openvino-dev: https://pypi.org/project/openvino-dev/
  150. import openvino.inference_engine as ie
  151. LOGGER.info(f'\n{prefix} starting export with openvino {ie.__version__}...')
  152. f = str(file).replace('.pt', '_openvino_model' + os.sep)
  153. cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f}"
  154. subprocess.check_output(cmd, shell=True)
  155. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  156. return f
  157. except Exception as e:
  158. LOGGER.info(f'\n{prefix} export failure: {e}')
  159. def export_coreml(model, im, file, int8, half, prefix=colorstr('CoreML:')):
  160. # YOLOv5 CoreML export
  161. try:
  162. check_requirements(('coremltools',))
  163. import coremltools as ct
  164. LOGGER.info(f'\n{prefix} starting export with coremltools {ct.__version__}...')
  165. f = file.with_suffix('.mlmodel')
  166. ts = torch.jit.trace(model, im, strict=False) # TorchScript model
  167. ct_model = ct.convert(ts, inputs=[ct.ImageType('image', shape=im.shape, scale=1 / 255, bias=[0, 0, 0])])
  168. bits, mode = (8, 'kmeans_lut') if int8 else (16, 'linear') if half else (32, None)
  169. if bits < 32:
  170. if platform.system() == 'Darwin': # quantization only supported on macOS
  171. with warnings.catch_warnings():
  172. warnings.filterwarnings("ignore", category=DeprecationWarning) # suppress numpy==1.20 float warning
  173. ct_model = ct.models.neural_network.quantization_utils.quantize_weights(ct_model, bits, mode)
  174. else:
  175. print(f'{prefix} quantization only supported on macOS, skipping...')
  176. ct_model.save(f)
  177. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  178. return ct_model, f
  179. except Exception as e:
  180. LOGGER.info(f'\n{prefix} export failure: {e}')
  181. return None, None
  182. def export_engine(model, im, file, train, half, simplify, workspace=4, verbose=False, prefix=colorstr('TensorRT:')):
  183. # YOLOv5 TensorRT export https://developer.nvidia.com/tensorrt
  184. try:
  185. assert im.device.type != 'cpu', 'export running on CPU but must be on GPU, i.e. `python export.py --device 0`'
  186. check_requirements(('nvidia-tensorrt',), cmds=('-U --index-url https://pypi.ngc.nvidia.com',))
  187. import tensorrt as trt
  188. if trt.__version__[0] == '7': # TensorRT 7 handling https://github.com/ultralytics/yolov5/issues/6012
  189. grid = model.model[-1].anchor_grid
  190. model.model[-1].anchor_grid = [a[..., :1, :1, :] for a in grid]
  191. export_onnx(model, im, file, 12, train, False, simplify) # opset 12
  192. model.model[-1].anchor_grid = grid
  193. else: # TensorRT >= 8
  194. check_version(trt.__version__, '8.0.0', hard=True) # require tensorrt>=8.0.0
  195. export_onnx(model, im, file, 13, train, False, simplify) # opset 13
  196. onnx = file.with_suffix('.onnx')
  197. LOGGER.info(f'\n{prefix} starting export with TensorRT {trt.__version__}...')
  198. assert onnx.exists(), f'failed to export ONNX file: {onnx}'
  199. f = file.with_suffix('.engine') # TensorRT engine file
  200. logger = trt.Logger(trt.Logger.INFO)
  201. if verbose:
  202. logger.min_severity = trt.Logger.Severity.VERBOSE
  203. builder = trt.Builder(logger)
  204. config = builder.create_builder_config()
  205. config.max_workspace_size = workspace * 1 << 30
  206. # config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, workspace << 30) # fix TRT 8.4 deprecation notice
  207. flag = (1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH))
  208. network = builder.create_network(flag)
  209. parser = trt.OnnxParser(network, logger)
  210. if not parser.parse_from_file(str(onnx)):
  211. raise RuntimeError(f'failed to load ONNX file: {onnx}')
  212. inputs = [network.get_input(i) for i in range(network.num_inputs)]
  213. outputs = [network.get_output(i) for i in range(network.num_outputs)]
  214. LOGGER.info(f'{prefix} Network Description:')
  215. for inp in inputs:
  216. LOGGER.info(f'{prefix}\tinput "{inp.name}" with shape {inp.shape} and dtype {inp.dtype}')
  217. for out in outputs:
  218. LOGGER.info(f'{prefix}\toutput "{out.name}" with shape {out.shape} and dtype {out.dtype}')
  219. LOGGER.info(f'{prefix} building FP{16 if builder.platform_has_fast_fp16 else 32} engine in {f}')
  220. if builder.platform_has_fast_fp16:
  221. config.set_flag(trt.BuilderFlag.FP16)
  222. with builder.build_engine(network, config) as engine, open(f, 'wb') as t:
  223. t.write(engine.serialize())
  224. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  225. return f
  226. except Exception as e:
  227. LOGGER.info(f'\n{prefix} export failure: {e}')
  228. def export_saved_model(model,
  229. im,
  230. file,
  231. dynamic,
  232. tf_nms=False,
  233. agnostic_nms=False,
  234. topk_per_class=100,
  235. topk_all=100,
  236. iou_thres=0.45,
  237. conf_thres=0.25,
  238. keras=False,
  239. prefix=colorstr('TensorFlow SavedModel:')):
  240. # YOLOv5 TensorFlow SavedModel export
  241. try:
  242. import tensorflow as tf
  243. from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
  244. from models.tf import TFDetect, TFModel
  245. LOGGER.info(f'\n{prefix} starting export with tensorflow {tf.__version__}...')
  246. f = str(file).replace('.pt', '_saved_model')
  247. batch_size, ch, *imgsz = list(im.shape) # BCHW
  248. tf_model = TFModel(cfg=model.yaml, model=model, nc=model.nc, imgsz=imgsz)
  249. im = tf.zeros((batch_size, *imgsz, ch)) # BHWC order for TensorFlow
  250. _ = tf_model.predict(im, tf_nms, agnostic_nms, topk_per_class, topk_all, iou_thres, conf_thres)
  251. inputs = tf.keras.Input(shape=(*imgsz, ch), batch_size=None if dynamic else batch_size)
  252. outputs = tf_model.predict(inputs, tf_nms, agnostic_nms, topk_per_class, topk_all, iou_thres, conf_thres)
  253. keras_model = tf.keras.Model(inputs=inputs, outputs=outputs)
  254. keras_model.trainable = False
  255. keras_model.summary()
  256. if keras:
  257. keras_model.save(f, save_format='tf')
  258. else:
  259. spec = tf.TensorSpec(keras_model.inputs[0].shape, keras_model.inputs[0].dtype)
  260. m = tf.function(lambda x: keras_model(x)) # full model
  261. m = m.get_concrete_function(spec)
  262. frozen_func = convert_variables_to_constants_v2(m)
  263. tfm = tf.Module()
  264. tfm.__call__ = tf.function(lambda x: frozen_func(x)[:4] if tf_nms else frozen_func(x)[0], [spec])
  265. tfm.__call__(im)
  266. tf.saved_model.save(tfm,
  267. f,
  268. options=tf.saved_model.SaveOptions(experimental_custom_gradients=False)
  269. if check_version(tf.__version__, '2.6') else tf.saved_model.SaveOptions())
  270. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  271. return keras_model, f
  272. except Exception as e:
  273. LOGGER.info(f'\n{prefix} export failure: {e}')
  274. return None, None
  275. def export_pb(keras_model, im, file, prefix=colorstr('TensorFlow GraphDef:')):
  276. # YOLOv5 TensorFlow GraphDef *.pb export https://github.com/leimao/Frozen_Graph_TensorFlow
  277. try:
  278. import tensorflow as tf
  279. from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
  280. LOGGER.info(f'\n{prefix} starting export with tensorflow {tf.__version__}...')
  281. f = file.with_suffix('.pb')
  282. m = tf.function(lambda x: keras_model(x)) # full model
  283. m = m.get_concrete_function(tf.TensorSpec(keras_model.inputs[0].shape, keras_model.inputs[0].dtype))
  284. frozen_func = convert_variables_to_constants_v2(m)
  285. frozen_func.graph.as_graph_def()
  286. tf.io.write_graph(graph_or_graph_def=frozen_func.graph, logdir=str(f.parent), name=f.name, as_text=False)
  287. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  288. return f
  289. except Exception as e:
  290. LOGGER.info(f'\n{prefix} export failure: {e}')
  291. def export_tflite(keras_model, im, file, int8, data, nms, agnostic_nms, prefix=colorstr('TensorFlow Lite:')):
  292. # YOLOv5 TensorFlow Lite export
  293. try:
  294. import tensorflow as tf
  295. LOGGER.info(f'\n{prefix} starting export with tensorflow {tf.__version__}...')
  296. batch_size, ch, *imgsz = list(im.shape) # BCHW
  297. f = str(file).replace('.pt', '-fp16.tflite')
  298. converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
  299. converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
  300. converter.target_spec.supported_types = [tf.float16]
  301. converter.optimizations = [tf.lite.Optimize.DEFAULT]
  302. if int8:
  303. from models.tf import representative_dataset_gen
  304. dataset = LoadImages(check_dataset(data)['train'], img_size=imgsz, auto=False) # representative data
  305. converter.representative_dataset = lambda: representative_dataset_gen(dataset, ncalib=100)
  306. converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
  307. converter.target_spec.supported_types = []
  308. converter.inference_input_type = tf.uint8 # or tf.int8
  309. converter.inference_output_type = tf.uint8 # or tf.int8
  310. converter.experimental_new_quantizer = True
  311. f = str(file).replace('.pt', '-int8.tflite')
  312. if nms or agnostic_nms:
  313. converter.target_spec.supported_ops.append(tf.lite.OpsSet.SELECT_TF_OPS)
  314. tflite_model = converter.convert()
  315. open(f, "wb").write(tflite_model)
  316. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  317. return f
  318. except Exception as e:
  319. LOGGER.info(f'\n{prefix} export failure: {e}')
  320. def export_edgetpu(keras_model, im, file, prefix=colorstr('Edge TPU:')):
  321. # YOLOv5 Edge TPU export https://coral.ai/docs/edgetpu/models-intro/
  322. try:
  323. cmd = 'edgetpu_compiler --version'
  324. help_url = 'https://coral.ai/docs/edgetpu/compiler/'
  325. assert platform.system() == 'Linux', f'export only supported on Linux. See {help_url}'
  326. if subprocess.run(cmd + ' >/dev/null', shell=True).returncode != 0:
  327. LOGGER.info(f'\n{prefix} export requires Edge TPU compiler. Attempting install from {help_url}')
  328. sudo = subprocess.run('sudo --version >/dev/null', shell=True).returncode == 0 # sudo installed on system
  329. for c in (
  330. 'curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -',
  331. 'echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list',
  332. 'sudo apt-get update', 'sudo apt-get install edgetpu-compiler'):
  333. subprocess.run(c if sudo else c.replace('sudo ', ''), shell=True, check=True)
  334. ver = subprocess.run(cmd, shell=True, capture_output=True, check=True).stdout.decode().split()[-1]
  335. LOGGER.info(f'\n{prefix} starting export with Edge TPU compiler {ver}...')
  336. f = str(file).replace('.pt', '-int8_edgetpu.tflite') # Edge TPU model
  337. f_tfl = str(file).replace('.pt', '-int8.tflite') # TFLite model
  338. cmd = f"edgetpu_compiler -s -o {file.parent} {f_tfl}"
  339. subprocess.run(cmd, shell=True, check=True)
  340. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  341. return f
  342. except Exception as e:
  343. LOGGER.info(f'\n{prefix} export failure: {e}')
  344. def export_tfjs(keras_model, im, file, prefix=colorstr('TensorFlow.js:')):
  345. # YOLOv5 TensorFlow.js export
  346. try:
  347. check_requirements(('tensorflowjs',))
  348. import re
  349. import tensorflowjs as tfjs
  350. LOGGER.info(f'\n{prefix} starting export with tensorflowjs {tfjs.__version__}...')
  351. f = str(file).replace('.pt', '_web_model') # js dir
  352. f_pb = file.with_suffix('.pb') # *.pb path
  353. f_json = f + '/model.json' # *.json path
  354. cmd = f'tensorflowjs_converter --input_format=tf_frozen_model ' \
  355. f'--output_node_names="Identity,Identity_1,Identity_2,Identity_3" {f_pb} {f}'
  356. subprocess.run(cmd, shell=True)
  357. with open(f_json) as j:
  358. json = j.read()
  359. with open(f_json, 'w') as j: # sort JSON Identity_* in ascending order
  360. subst = re.sub(
  361. r'{"outputs": {"Identity.?.?": {"name": "Identity.?.?"}, '
  362. r'"Identity.?.?": {"name": "Identity.?.?"}, '
  363. r'"Identity.?.?": {"name": "Identity.?.?"}, '
  364. r'"Identity.?.?": {"name": "Identity.?.?"}}}', r'{"outputs": {"Identity": {"name": "Identity"}, '
  365. r'"Identity_1": {"name": "Identity_1"}, '
  366. r'"Identity_2": {"name": "Identity_2"}, '
  367. r'"Identity_3": {"name": "Identity_3"}}}', json)
  368. j.write(subst)
  369. LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
  370. return f
  371. except Exception as e:
  372. LOGGER.info(f'\n{prefix} export failure: {e}')
  373. @torch.no_grad()
  374. def run(
  375. data=ROOT / 'data/coco128.yaml', # 'dataset.yaml path'
  376. weights=ROOT / 'yolov5s.pt', # weights path
  377. imgsz=(640, 640), # image (height, width)
  378. batch_size=1, # batch size
  379. device='cpu', # cuda device, i.e. 0 or 0,1,2,3 or cpu
  380. include=('torchscript', 'onnx'), # include formats
  381. half=False, # FP16 half-precision export
  382. inplace=False, # set YOLOv5 Detect() inplace=True
  383. train=False, # model.train() mode
  384. optimize=False, # TorchScript: optimize for mobile
  385. int8=False, # CoreML/TF INT8 quantization
  386. dynamic=False, # ONNX/TF: dynamic axes
  387. simplify=False, # ONNX: simplify model
  388. opset=12, # ONNX: opset version
  389. verbose=False, # TensorRT: verbose log
  390. workspace=4, # TensorRT: workspace size (GB)
  391. nms=False, # TF: add NMS to model
  392. agnostic_nms=False, # TF: add agnostic NMS to model
  393. topk_per_class=100, # TF.js NMS: topk per class to keep
  394. topk_all=100, # TF.js NMS: topk for all classes to keep
  395. iou_thres=0.45, # TF.js NMS: IoU threshold
  396. conf_thres=0.25, # TF.js NMS: confidence threshold
  397. ):
  398. t = time.time()
  399. include = [x.lower() for x in include] # to lowercase
  400. formats = tuple(export_formats()['Argument'][1:]) # --include arguments
  401. flags = [x in include for x in formats]
  402. assert sum(flags) == len(include), f'ERROR: Invalid --include {include}, valid --include arguments are {formats}'
  403. jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs = flags # export booleans
  404. file = Path(url2file(weights) if str(weights).startswith(('http:/', 'https:/')) else weights) # PyTorch weights
  405. # Load PyTorch model
  406. device = select_device(device)
  407. if half:
  408. assert device.type != 'cpu' or coreml, '--half only compatible with GPU export, i.e. use --device 0'
  409. model = attempt_load(weights, map_location=device, inplace=True, fuse=True) # load FP32 model
  410. nc, names = model.nc, model.names # number of classes, class names
  411. # Checks
  412. imgsz *= 2 if len(imgsz) == 1 else 1 # expand
  413. assert nc == len(names), f'Model class count {nc} != len(names) {len(names)}'
  414. # Input
  415. gs = int(max(model.stride)) # grid size (max stride)
  416. imgsz = [check_img_size(x, gs) for x in imgsz] # verify img_size are gs-multiples
  417. im = torch.zeros(batch_size, 3, *imgsz).to(device) # image size(1,3,320,192) BCHW iDetection
  418. # Update model
  419. if half and not coreml:
  420. im, model = im.half(), model.half() # to FP16
  421. model.train() if train else model.eval() # training mode = no Detect() layer grid construction
  422. for k, m in model.named_modules():
  423. if isinstance(m, Detect):
  424. m.inplace = inplace
  425. m.onnx_dynamic = dynamic
  426. m.export = True
  427. for _ in range(2):
  428. y = model(im) # dry runs
  429. shape = tuple(y[0].shape) # model output shape
  430. LOGGER.info(f"\n{colorstr('PyTorch:')} starting from {file} with output shape {shape} ({file_size(file):.1f} MB)")
  431. # Exports
  432. f = [''] * 10 # exported filenames
  433. warnings.filterwarnings(action='ignore', category=torch.jit.TracerWarning) # suppress TracerWarning
  434. if jit:
  435. f[0] = export_torchscript(model, im, file, optimize)
  436. if engine: # TensorRT required before ONNX
  437. f[1] = export_engine(model, im, file, train, half, simplify, workspace, verbose)
  438. if onnx or xml: # OpenVINO requires ONNX
  439. f[2] = export_onnx(model, im, file, opset, train, dynamic, simplify)
  440. if xml: # OpenVINO
  441. f[3] = export_openvino(model, im, file)
  442. if coreml:
  443. _, f[4] = export_coreml(model, im, file, int8, half)
  444. # TensorFlow Exports
  445. if any((saved_model, pb, tflite, edgetpu, tfjs)):
  446. if int8 or edgetpu: # TFLite --int8 bug https://github.com/ultralytics/yolov5/issues/5707
  447. check_requirements(('flatbuffers==1.12',)) # required before `import tensorflow`
  448. assert not (tflite and tfjs), 'TFLite and TF.js models must be exported separately, please pass only one type.'
  449. model, f[5] = export_saved_model(model.cpu(),
  450. im,
  451. file,
  452. dynamic,
  453. tf_nms=nms or agnostic_nms or tfjs,
  454. agnostic_nms=agnostic_nms or tfjs,
  455. topk_per_class=topk_per_class,
  456. topk_all=topk_all,
  457. conf_thres=conf_thres,
  458. iou_thres=iou_thres) # keras model
  459. if pb or tfjs: # pb prerequisite to tfjs
  460. f[6] = export_pb(model, im, file)
  461. if tflite or edgetpu:
  462. f[7] = export_tflite(model, im, file, int8=int8 or edgetpu, data=data, nms=nms, agnostic_nms=agnostic_nms)
  463. if edgetpu:
  464. f[8] = export_edgetpu(model, im, file)
  465. if tfjs:
  466. f[9] = export_tfjs(model, im, file)
  467. # Finish
  468. f = [str(x) for x in f if x] # filter out '' and None
  469. if any(f):
  470. LOGGER.info(f'\nExport complete ({time.time() - t:.2f}s)'
  471. f"\nResults saved to {colorstr('bold', file.parent.resolve())}"
  472. f"\nDetect: python detect.py --weights {f[-1]}"
  473. f"\nPyTorch Hub: model = torch.hub.load('ultralytics/yolov5', 'custom', '{f[-1]}')"
  474. f"\nValidate: python val.py --weights {f[-1]}"
  475. f"\nVisualize: https://netron.app")
  476. return f # return list of exported files/dirs
  477. def parse_opt():
  478. parser = argparse.ArgumentParser()
  479. parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path')
  480. parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model.pt path(s)')
  481. parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640, 640], help='image (h, w)')
  482. parser.add_argument('--batch-size', type=int, default=1, help='batch size')
  483. parser.add_argument('--device', default='cpu', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
  484. parser.add_argument('--half', action='store_true', help='FP16 half-precision export')
  485. parser.add_argument('--inplace', action='store_true', help='set YOLOv5 Detect() inplace=True')
  486. parser.add_argument('--train', action='store_true', help='model.train() mode')
  487. parser.add_argument('--optimize', action='store_true', help='TorchScript: optimize for mobile')
  488. parser.add_argument('--int8', action='store_true', help='CoreML/TF INT8 quantization')
  489. parser.add_argument('--dynamic', action='store_true', help='ONNX/TF: dynamic axes')
  490. parser.add_argument('--simplify', action='store_true', help='ONNX: simplify model')
  491. parser.add_argument('--opset', type=int, default=12, help='ONNX: opset version')
  492. parser.add_argument('--verbose', action='store_true', help='TensorRT: verbose log')
  493. parser.add_argument('--workspace', type=int, default=4, help='TensorRT: workspace size (GB)')
  494. parser.add_argument('--nms', action='store_true', help='TF: add NMS to model')
  495. parser.add_argument('--agnostic-nms', action='store_true', help='TF: add agnostic NMS to model')
  496. parser.add_argument('--topk-per-class', type=int, default=100, help='TF.js NMS: topk per class to keep')
  497. parser.add_argument('--topk-all', type=int, default=100, help='TF.js NMS: topk for all classes to keep')
  498. parser.add_argument('--iou-thres', type=float, default=0.45, help='TF.js NMS: IoU threshold')
  499. parser.add_argument('--conf-thres', type=float, default=0.25, help='TF.js NMS: confidence threshold')
  500. parser.add_argument('--include',
  501. nargs='+',
  502. default=['torchscript', 'onnx'],
  503. help='torchscript, onnx, openvino, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs')
  504. opt = parser.parse_args()
  505. print_args(vars(opt))
  506. return opt
  507. def main(opt):
  508. for opt.weights in (opt.weights if isinstance(opt.weights, list) else [opt.weights]):
  509. run(**vars(opt))
  510. if __name__ == "__main__":
  511. opt = parse_opt()
  512. main(opt)