From a39b9f160a8f006ae92ee0aa1fe7a26b4e99f2c2 Mon Sep 17 00:00:00 2001 From: tripleMu <865626@163.com> Date: Sat, 14 May 2022 23:55:29 +0800 Subject: [PATCH 1/2] Add fp16 onnx export for onnxruntime gpu infer --- yolort/runtime/ort_helper.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/yolort/runtime/ort_helper.py b/yolort/runtime/ort_helper.py index 5ba21cc0..ecca3122 100644 --- a/yolort/runtime/ort_helper.py +++ b/yolort/runtime/ort_helper.py @@ -20,6 +20,7 @@ def export_onnx( skip_preprocess: bool = False, opset_version: int = 11, batch_size: int = 1, + half:bool = False ) -> None: """ Export to ONNX models that can be used for ONNX Runtime inferencing. @@ -55,6 +56,7 @@ def export_onnx( skip_preprocess=skip_preprocess, opset_version=opset_version, batch_size=batch_size, + half=half, ) onnx_builder.to_onnx(onnx_path) @@ -94,6 +96,7 @@ def __init__( skip_preprocess: bool = False, opset_version: int = 11, batch_size: int = 1, + half:bool = False ) -> None: super().__init__() @@ -111,6 +114,7 @@ def __init__( if model is None: model = self._build_model() self.model = model + self.half = half # For exporting ONNX model self._opset_version = opset_version @@ -215,3 +219,15 @@ def to_onnx(self, onnx_path: str, **kwargs): dynamic_axes=self.dynamic_axes, **kwargs, ) + if self.half: + try: + import onnxmltools + from onnxmltools.utils.float16_converter import convert_float_to_float16 + except Exception as e: + print(f"onnxmltools dependency should be installed,do not build fp16 onnx,error message is {e}") + else: + onnxFP32 = onnxmltools.utils.load_model(onnx_path) + onnxFP16 = convert_float_to_float16(onnxFP32) + onnxmltools.utils.save_model(onnxFP16, onnx_path) + + From 3efe5f9f1b1ea7ce228308922e5057c074224530 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 May 2022 16:00:58 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- yolort/runtime/ort_helper.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/yolort/runtime/ort_helper.py b/yolort/runtime/ort_helper.py index ecca3122..042f5a2b 100644 --- a/yolort/runtime/ort_helper.py +++ b/yolort/runtime/ort_helper.py @@ -20,7 +20,7 @@ def export_onnx( skip_preprocess: bool = False, opset_version: int = 11, batch_size: int = 1, - half:bool = False + half: bool = False, ) -> None: """ Export to ONNX models that can be used for ONNX Runtime inferencing. @@ -96,7 +96,7 @@ def __init__( skip_preprocess: bool = False, opset_version: int = 11, batch_size: int = 1, - half:bool = False + half: bool = False, ) -> None: super().__init__() @@ -224,10 +224,10 @@ def to_onnx(self, onnx_path: str, **kwargs): import onnxmltools from onnxmltools.utils.float16_converter import convert_float_to_float16 except Exception as e: - print(f"onnxmltools dependency should be installed,do not build fp16 onnx,error message is {e}") + print( + f"onnxmltools dependency should be installed,do not build fp16 onnx,error message is {e}" + ) else: onnxFP32 = onnxmltools.utils.load_model(onnx_path) onnxFP16 = convert_float_to_float16(onnxFP32) onnxmltools.utils.save_model(onnxFP16, onnx_path) - -