chaiNNer
chaiNNer copied to clipboard
Onnx model not quantizing after converted using chainner
I have converted Real-ESRGAN model using Chainner from pth to onnx format. But when I am quantizing this model using onnxruntime in int8 then I am getting following error. What could be the solution to resolve this error ?
Code:
import onnx
from onnxruntime.quantization import quantize_dynamic, QuantType
import os
def quantize_onnx_model(onnx_model_path, quantized_model_path):
onnx_opt_model = onnx.load(onnx_model_path)
quantize_dynamic(onnx_model_path,quantized_model_path,weight_type=QuantType.QUInt8)
logger.info(f"quantized model saved to:{quantized_model_path}")
quantize_onnx_model("weights/real_esrgan_chainner_fp16.onnx", "weights/real_esrgan_chainner_QUINT8.onnx")
print('ONNX full precision model size (MB):', os.path.getsize("weights/real_esrgan_chainner_fp16.onnx")/(1024*1024))
print('ONNX quantized model size (MB):', os.path.getsize("weights/real_esrgan_chainner_QUINT8.onnx")/(1024*1024))
Error:
ValueError Traceback (most recent call last)
Cell In[5], line 11
7 quantize_dynamic(onnx_model_path,quantized_model_path,weight_type=QuantType.QUInt8)
9 logger.info(f"quantized model saved to:{quantized_model_path}")
---> 11 quantize_onnx_model("weights/real_esrgan_chainner_fp16.onnx", "weights/real_esrgan_chainner_QUINT8.onnx")
13 print('ONNX full precision model size (MB):', os.path.getsize("weights/real_esrgan_chainner_fp16.onnx")/(1024*1024))
14 print('ONNX quantized model size (MB):', os.path.getsize("weights/real_esrgan_chainner_QUINT8.onnx")/(1024*1024))
Cell In[5], line 7, in quantize_onnx_model(onnx_model_path, quantized_model_path)
4 def quantize_onnx_model(onnx_model_path, quantized_model_path):
6 onnx_opt_model = onnx.load(onnx_model_path)
----> 7 quantize_dynamic(onnx_model_path,quantized_model_path,weight_type=QuantType.QUInt8)
9 logger.info(f"quantized model saved to:{quantized_model_path}")
File ~\anaconda3\Lib\site-packages\onnxruntime\quantization\quantize.py:642, in quantize_dynamic(model_input, model_output, op_types_to_quantize, per_channel, reduce_range, weight_type, nodes_to_quantize, nodes_to_exclude, use_external_data_format, extra_options)
625 extra_options["MatMulConstBOnly"] = True
627 quantizer = ONNXQuantizer(
628 model,
629 per_channel,
(...)
639 extra_options,
640 )
--> 642 quantizer.quantize_model()
643 quantizer.model.save_model_to_file(model_output, use_external_data_format)
File ~\anaconda3\Lib\site-packages\onnxruntime\quantization\onnx_quantizer.py:403, in ONNXQuantizer.quantize_model(self)
401 number_of_existing_new_nodes = len(self.new_nodes)
402 op_quantizer = CreateOpQuantizer(self, node)
--> 403 op_quantizer.quantize()
404 for i in range(number_of_existing_new_nodes, len(self.new_nodes)):
405 for output_name in self.new_nodes[i].output:
File ~\anaconda3\Lib\site-packages\onnxruntime\quantization\operators\conv.py:132, in ConvInteger.quantize(self)
123 nodes.append(
124 get_mul_node(
125 [cast_op_output, scales_mul_op_output],
(...)
128 )
129 )
131 if has_bias:
--> 132 self.add_bias(nodes, scaled_output_name)
134 self.quantizer.new_nodes += nodes
File ~\anaconda3\Lib\site-packages\onnxruntime\quantization\operators\conv.py:36, in ConvInteger.add_bias(self, nodes, scaled_output)
34 weight = find_by_name(node.input[1], model.initializer())
35 if weight is None:
---> 36 raise ValueError(f"Expected {node.input[1]} to be an initializer")
38 # Add reshape for correct broadcase
39 output = node.output[0]
ValueError: Expected /Constant_output_0 to be an initializer