Onnx精度转换 FP32->FP16
1、依赖包
- onnx
- onnxmltools
2、转换
from onnxmltools.utils.float16_converter import convert_float_to_float16
from onnxmltools.utils import load_model,save_model
onnx_model = load_model("model.onnx")
fp16_model = convert_float_to_float16(onnx_model, keep_io_types=False)
save_model(fp16_model, "model-float16.onnx")
3、验证
import onnx
print(onnx.__version__)
onnx_model = onnx.load("model-float16.onnx")
try:
onnx.checker.check_model(onnx_model)
except Exception:
print("Model incorrect")
else:
print("Model correct")