convert_model
Description
Based on the computed quantization factors, converts a TensorFlow model to a model for both accuracy simulation in the TensorFlow environment and for inference on the Ascend AI Processor.
Restrictions
- The user model must match the quantization factor record file. For example, if the "Conv+BN" structure is fused before computing the quantization factors of Conv, the "Conv+BN" structure in the TensorFlow model to be converted also needs to be fused in advance.
- The format and content of the quantization factor record file must comply with the AMCT requirements defined in Quantization Factor Record File.
- Only the following layers support quantization: Conv2D, MatMul, DepthwiseConv2dNative, and Conv2DBackpropInput, and AVE Pooling.
- This API supports the fusion of the "Conv+BN", "Depthwise_Conv+BN", and "Group_conv+BN" structures in the user model. Layer-level fusion switch is supported.
- Quantization of only an original floating-point model is supported. The model cannot be quantized if the input model contains any of the following custom quantization layers: QuantIfmr, QuantArq, SearchN, AscendQuant, AscendDequant, AscendAntiQuant, and AscendWeightQuant.
Prototype
convert_model(pb_model, outputs, record_file, save_path)
Parameters
Parameter |
Input/Return |
Description |
Restrictions |
---|---|---|---|
pb_model |
Input |
Original .pb model file to be quantized. |
A string. Must be an inference graph containing no training-mode operators. For example, is_training of the FusedBatchNormV3 operator must be False. |
outputs |
Input |
List of output operators in the graph. |
A list. |
record_file |
Input |
Computed quantization factor record file (.txt) |
A string. |
save_path |
Input |
Model save path. Must be prefixed with the model name, for example, ./quantized_model/*model*. |
A string. |
Returns
None
Outputs
Outputs a .pb model file that can be used for both accuracy simulation in the TensorFlow environment and for inference on the Ascend AI Processor.
When quantization is performed again, the existing files in the output directory will be overwritten upon this API call.
Example
import amct_tensorflow as amct convert_model(pb_model='./user_model.pb', outputs=["model/outputs"], record_file='./record_quantized.txt', save_path='./quantized_model/model')