Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fp16 onnx export for onnxruntime gpu infer #404

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions yolort/runtime/ort_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -94,6 +96,7 @@ def __init__(
skip_preprocess: bool = False,
opset_version: int = 11,
batch_size: int = 1,
half: bool = False,
) -> None:

super().__init__()
Expand All @@ -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
Expand Down Expand Up @@ -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)