quantize_model
Function Usage
Quantizes the input graph structure to be quantized based on the given quantization configuration file, inserts quantization operators into the input graph structure, generates the quantization factor record file record_file, and returns the list of new quantization operators.
Prototype
quant_add_ops = quantize_model(graph, config_file, record_file, calib_outputs=None)
Command-Line Options
Option |
Input/Return |
Description |
Restriction |
|---|---|---|---|
graph |
Input |
A tf.Graph of the model to be quantized. |
A tf.Graph. |
config_file |
Input |
User-defined quantization configuration file, which specifies the configuration of each layer to be quantized in the tf.Graph. |
A string |
record_file |
Input |
Directory of the quantization factor record file, including the file name. |
A string |
calib_outputs |
Input |
List of output nodes of the graph. When the output nodes change due to graph modification, this list is updated accordingly. |
A list. Default: None |
quant_add_ops |
Return |
List of operator variables inserted for quantization. NOTE:
The variable values in the list cannot be found in the model training parameter file. Therefore, if the model training parameters are directly restored, an error indicating that the variables cannot be found occurs. Therefore, before restoring the model training parameters, you need to Remove the variable values in the quant_add_ops list from the recovery list. For details about how to remove the variable values, see How Do I Restore the Model Training Parameters After Quantization Operators Are Inserted? |
A list of strings. |
Returns
List of quantized layers on the network.
The quantize_model call performs fusion on the graph, which might alter the output nodes. For example, Conv+BN (or Conv+BiasAdd+BN) is fused into Conv+BiasAdd, and the output node equivalent to BN is BiasAdd.
Examples
1 2 3 4 5 6 7 8 9 | import amct_tensorflow as amct # Build a network to be quantized. network = build_network() # Insert the quantization API. amct.quantize_model( graph=tf.get_default_graph(), config_file="./configs/config.json", record_file="./record_scale_offset.txt") |