vai_q_tensorflow2 Usage - 1.3 English

Vitis AI User Guide (UG1414)

Document ID
UG1414
Release Date
2021-02-03
Version
1.3 English
vitis_quantize.VitisQuantizer(model, custom_quantize_strategy=None)

The construction function of class VitisQuantizer.

Arguments:

model
A tf.keras.Model object, containing the configurations for quantization.
custom_quantize_strategy
A custom quantize strategy json file.
vitis_quantize.VitisQuantizer.quantize_model(
calib_dataset=None,
fold_conv_bn=True,
fold_bn=True,
replace_relu6=True,
include_cle=True,
cle_steps=10)

This function quantizes the float model, including model optimization, weights quantization and activation quantize calibration.

Arguments:

calib_dataset
A tf.data.Dataset or np.numpy object, the calibration dataset.
fold_conv_bn
A bool object, whether to fold the batchnorm layers into previous Conv2D/DepthwiseConv2D/TransposeConv2D/Dense layers
fold_bn
A bool object, whether to fold the batchnorm layer’s moving_mean and moving_variance values into the gamma and beta values.
replace_relu6
A bool object, whether to replace the Relu6 layers with Relu layers.
include_cle
A bool object, whether to do Cross Layer Equalization before quantization.
cle_steps
A int object, the iteration steps to do Cross Layer Equalization.
vitis_quantize.VitisQuantizer.dump_model(
model,
dataset=None,
output_dir=’./dump_results’,
weights_only=False)

This function dumps the simulation results of the quantized model, including model optimization, weights quantizing and activation quantize calibration.

Arguments:

model
A tf.keras.Model object, containing the configurations for quantization.
dataset
A tf.data.Dataset or np.numpy object, the dataset used to dump, not needed if weights_only is set to True.
output_dir
A string object, the directory to save the dump results.
weights_only
A bool object, set to True to only dump the weights, set to False will also dump the activation results.

Examples

Quantize
from tensorflow_model_optimization.quantization.keras import vitis_quantize
quantizer = vitis_quantize.VitisQuantizer(model)
quantized_model = quantizer.quantize_model(calib_dataset=calib_dataset)
Evaluate quantized model
quantized_model.compile(loss=your_loss, metrics=your_metrics)
quantized_model.evaluate(eval_dataset)
Load quantized model
from tensorflow_model_optimization.quantization.keras import vitis_quantize
with vitis_quantize.quantize_scope():
    model = keras.models.load_model('./quantized_model.h5')
Dump quantized model
from tensorflow_model_optimization.quantization.keras import vitis_quantize
with vitis_quantize.quantize_scope():
    quantized_model = keras.models.load_model('./quantized_model.h5')
    vitis_quantize.VitisQuantizer.dump_model(quantized_model, dump_dataset)