【发布时间】:2021-11-09 13:31:13
【问题描述】:
我正在使用我需要量化以减小其大小的 ONNX 模型,为此我遵循官方 documentation 上的说明:
import onnx
from onnxruntime.quantization import quantize_dynamic, QuantType
model_fp32 = 'path/to/the/model.onnx'
model_quant = 'path/to/the/model.quant.onnx'
quantized_model = quantize_dynamic(model_fp32, model_quant, weight_type=QuantType.QUInt8)
但是当我运行它时,我收到以下警告:
WARNING:root:The original model opset version is 9, which does not support quantization. Please update the model to opset >= 11. Updating the model automatically to opset 11. Please verify the quantized model.
我测试了量化模型并没有工作,它会产生这个错误:
INVALID_GRAPH : Load model from model_a2_quant.onnx failed:This is an invalid model. Error in Node:Upsample__477 : Op registered for Upsample is deprecated in domain_version of 11
此时我有什么替代方法来量化模型?
我从这个 repo 获得了张量流中的原始模型:https://github.com/ciber-lab/pictor-ppe
并使用以下代码将其转换为 ONNX:
# input and output
input_tensor = Input( shape=(input_shape[0], input_shape[1], 3) ) # input
num_out_filters = ( num_anchors//3 ) * ( 5 + num_classes ) # output
## Build and load the model
model = yolo_body(input_tensor, num_out_filters)
weight_path = 'ONNX_demo/models/pictor-ppe-v302-a1-yolo-v3-weights.h5'
model.load_weights( weight_path )
tf.saved_model.save(model, "ONNX_demo/models/save_model")
# convert it to ONNX format:
python3 -m tf2onnx.convert --saved-model "ONNX_demo/models/save_model" --output "ONNX_demo/models/model.onnx"
【问题讨论】:
标签: python tensorflow machine-learning onnx onnxruntime