|
|
|
|
|
|
|
|
LOGGER.info(f'{prefix} export failure: {e}') |
|
|
LOGGER.info(f'{prefix} export failure: {e}') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def export_openvino(model, im, file, half, prefix=colorstr('OpenVINO:')): |
|
|
|
|
|
|
|
|
def export_openvino(file, half, prefix=colorstr('OpenVINO:')): |
|
|
# YOLOv5 OpenVINO export |
|
|
# YOLOv5 OpenVINO export |
|
|
try: |
|
|
try: |
|
|
check_requirements(('openvino-dev',)) # requires openvino-dev: https://pypi.org/project/openvino-dev/ |
|
|
check_requirements(('openvino-dev',)) # requires openvino-dev: https://pypi.org/project/openvino-dev/ |
|
|
|
|
|
|
|
|
f = str(file).replace('.pt', f'_openvino_model{os.sep}') |
|
|
f = str(file).replace('.pt', f'_openvino_model{os.sep}') |
|
|
|
|
|
|
|
|
cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f} --data_type {'FP16' if half else 'FP32'}" |
|
|
cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f} --data_type {'FP16' if half else 'FP32'}" |
|
|
subprocess.check_output(cmd, shell=True) |
|
|
|
|
|
|
|
|
subprocess.check_output(cmd.split()) |
|
|
|
|
|
|
|
|
LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)') |
|
|
LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)') |
|
|
return f |
|
|
return f |
|
|
|
|
|
|
|
|
return None, None |
|
|
return None, None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def export_pb(keras_model, im, file, prefix=colorstr('TensorFlow GraphDef:')): |
|
|
|
|
|
|
|
|
def export_pb(keras_model, file, prefix=colorstr('TensorFlow GraphDef:')): |
|
|
# YOLOv5 TensorFlow GraphDef *.pb export https://github.com/leimao/Frozen_Graph_TensorFlow |
|
|
# YOLOv5 TensorFlow GraphDef *.pb export https://github.com/leimao/Frozen_Graph_TensorFlow |
|
|
try: |
|
|
try: |
|
|
import tensorflow as tf |
|
|
import tensorflow as tf |
|
|
|
|
|
|
|
|
LOGGER.info(f'\n{prefix} export failure: {e}') |
|
|
LOGGER.info(f'\n{prefix} export failure: {e}') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def export_edgetpu(keras_model, im, file, prefix=colorstr('Edge TPU:')): |
|
|
|
|
|
|
|
|
def export_edgetpu(file, prefix=colorstr('Edge TPU:')): |
|
|
# YOLOv5 Edge TPU export https://coral.ai/docs/edgetpu/models-intro/ |
|
|
# YOLOv5 Edge TPU export https://coral.ai/docs/edgetpu/models-intro/ |
|
|
try: |
|
|
try: |
|
|
cmd = 'edgetpu_compiler --version' |
|
|
cmd = 'edgetpu_compiler --version' |
|
|
|
|
|
|
|
|
f_tfl = str(file).replace('.pt', '-int8.tflite') # TFLite model |
|
|
f_tfl = str(file).replace('.pt', '-int8.tflite') # TFLite model |
|
|
|
|
|
|
|
|
cmd = f"edgetpu_compiler -s -o {file.parent} {f_tfl}" |
|
|
cmd = f"edgetpu_compiler -s -o {file.parent} {f_tfl}" |
|
|
subprocess.run(cmd, shell=True, check=True) |
|
|
|
|
|
|
|
|
subprocess.run(cmd.split(), check=True) |
|
|
|
|
|
|
|
|
LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)') |
|
|
LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)') |
|
|
return f |
|
|
return f |
|
|
|
|
|
|
|
|
LOGGER.info(f'\n{prefix} export failure: {e}') |
|
|
LOGGER.info(f'\n{prefix} export failure: {e}') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def export_tfjs(keras_model, im, file, prefix=colorstr('TensorFlow.js:')): |
|
|
|
|
|
|
|
|
def export_tfjs(file, prefix=colorstr('TensorFlow.js:')): |
|
|
# YOLOv5 TensorFlow.js export |
|
|
# YOLOv5 TensorFlow.js export |
|
|
try: |
|
|
try: |
|
|
check_requirements(('tensorflowjs',)) |
|
|
check_requirements(('tensorflowjs',)) |
|
|
|
|
|
|
|
|
f_json = f'{f}/model.json' # *.json path |
|
|
f_json = f'{f}/model.json' # *.json path |
|
|
|
|
|
|
|
|
cmd = f'tensorflowjs_converter --input_format=tf_frozen_model ' \ |
|
|
cmd = f'tensorflowjs_converter --input_format=tf_frozen_model ' \ |
|
|
f'--output_node_names="Identity,Identity_1,Identity_2,Identity_3" {f_pb} {f}' |
|
|
|
|
|
subprocess.run(cmd, shell=True) |
|
|
|
|
|
|
|
|
f'--output_node_names=Identity,Identity_1,Identity_2,Identity_3 {f_pb} {f}' |
|
|
|
|
|
subprocess.run(cmd.split()) |
|
|
|
|
|
|
|
|
with open(f_json) as j: |
|
|
with open(f_json) as j: |
|
|
json = j.read() |
|
|
json = j.read() |
|
|
|
|
|
|
|
|
if onnx or xml: # OpenVINO requires ONNX |
|
|
if onnx or xml: # OpenVINO requires ONNX |
|
|
f[2] = export_onnx(model, im, file, opset, train, dynamic, simplify) |
|
|
f[2] = export_onnx(model, im, file, opset, train, dynamic, simplify) |
|
|
if xml: # OpenVINO |
|
|
if xml: # OpenVINO |
|
|
f[3] = export_openvino(model, im, file, half) |
|
|
|
|
|
|
|
|
f[3] = export_openvino(file, half) |
|
|
if coreml: |
|
|
if coreml: |
|
|
_, f[4] = export_coreml(model, im, file, int8, half) |
|
|
_, f[4] = export_coreml(model, im, file, int8, half) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conf_thres=conf_thres, |
|
|
conf_thres=conf_thres, |
|
|
iou_thres=iou_thres) # keras model |
|
|
iou_thres=iou_thres) # keras model |
|
|
if pb or tfjs: # pb prerequisite to tfjs |
|
|
if pb or tfjs: # pb prerequisite to tfjs |
|
|
f[6] = export_pb(model, im, file) |
|
|
|
|
|
|
|
|
f[6] = export_pb(model, file) |
|
|
if tflite or edgetpu: |
|
|
if tflite or edgetpu: |
|
|
f[7] = export_tflite(model, im, file, int8=int8 or edgetpu, data=data, nms=nms, agnostic_nms=agnostic_nms) |
|
|
f[7] = export_tflite(model, im, file, int8=int8 or edgetpu, data=data, nms=nms, agnostic_nms=agnostic_nms) |
|
|
if edgetpu: |
|
|
if edgetpu: |
|
|
f[8] = export_edgetpu(model, im, file) |
|
|
|
|
|
|
|
|
f[8] = export_edgetpu(file) |
|
|
if tfjs: |
|
|
if tfjs: |
|
|
f[9] = export_tfjs(model, im, file) |
|
|
|
|
|
|
|
|
f[9] = export_tfjs(file) |
|
|
|
|
|
|
|
|
# Finish |
|
|
# Finish |
|
|
f = [str(x) for x in f if x] # filter out '' and None |
|
|
f = [str(x) for x in f if x] # filter out '' and None |