TensorFlow Developers
Provide the PB model to be quantized.
PyTorch Developers
Provide the .py model definition file and the .pth model parameter file for the model to be quantized.
ONNX Developers
Provide the ONNX model to be quantized.
You need to provide the calibration set in the .bin or image format. Input data in .bin format must be stored using the methods described in Table 1. The image data is stored in the folder for storing test images. By default, the input data in image format is read from the BGR channels. The read data format is NCHW, where N indicates the number of provided images.
The size reduction tool supports the following image formats: ".bmp", ".dib", ".jpeg", ".jpg", ".jpe", ".png", ".webp", ".pbm", ".pgm", ".ppm", ".tiff", ".tif", ".BMP", ".DIB", ".JPEG", ".JPG", ".JPE", ".PNG", ".WEBP", ".PBM", ".PGM", ".PPM", ".TIFF", and ".TIF".
The input data in .bin format supports two definition modes. One applies to any dimension except 4-dimensional, while the other is tailored for 4-dimensional data. We recommend that you use the arbitrary-dimension definition whenever possible.
For input data of any dimension (excluding 4-dimensional), the .bin file needs to be defined as follows. For example, for 3-dimensional data (50, 100, 300), the resulting data shape after reading is (50, 100, 300).
Table 1 Binary calibration set for any dimension (excluding 4-dimensional)
| File Header/Data | Address Offset | Type | Value | Description |
|---|---|---|---|---|
| File header (20 bytes in total) | 0000 | 32bit int | 610 | Magic number Verifies the validity of the file when magic number is 610. |
| File header (20 bytes in total) | 0004 | 32bit int | 3 | Input data rank |
| File header (20 bytes in total) | 0008 | 32bit int | 50 | Input dimension 1 |
| File header (20 bytes in total) | 0012 | 32bit int | 100 | Input dimension 2 |
| File header (20 bytes in total) | 0016 | 32bit int | 300 | Input dimension 3 |
| Data | N/A | Float32 | N/A | Data volume = 50 x 100 x 300 |
The .bin file for 4-dimensional input data can be defined according to Table 1 or Table 2. For example, for 4-dimensional data (50, 3, 28, 28), the resulting data shape after reading is (50, 3, 28, 28).
Table 2 Four-dimensional binary calibration set
| File Header/Data | Address Offset | Type | Value | Description |
|---|---|---|---|---|
| File header (20 bytes in total) | 0000 | 32bit int | 510 | Magic number Verifies the validity of the file when magic number is 510. |
| File header (20 bytes in total) | 0004 | 32bit int | 50 | Input num |
| File header (20 bytes in total) | 0008 | 32bit int | 3 | Input channels |
| File header (20 bytes in total) | 0012 | 32bit int | 28 | Input height |
| File header (20 bytes in total) | 0016 | 32bit int | 28 | Input width |
| Data | N/A | Float32 | N/A | Data volume = 50 x 3 x 28 x 28 |
The size reduction tool provides a script to support the conversion of .bin files. For details, see tools_dopt/dopt_tf_py3/demo/quant8-8/notrain/bin_data_preprocessing.py in Tools Download.
The following table describes the parameters in config.prototxt. In BINARY mode, no pre-processing is performed. In IMAGE mode, pre-processing is performed based on the mean value and variance you have provided.
| Parameter | Description | Mandatory |
|---|---|---|
| strategy | Optimization strategy. Currently, only Quant_INT8-8 is supported for all frameworks. | No (default to Quant_INT8-8) |
| device | Whether to use the GPU or CPU for quantization. - USE_GPU: GPU mode - USE_CPU: CPU mode | No (default to CPU mode) |
| exclude_op | Quantization operator blocklist. Operators in the blocklist will not be quantized. You can configure the blocklist using either of the following methods or a combination of the two methods: - Uses one exclude_op, which contains multiple op_name, separated by semicolons (;). - Uses multiple exclude_op, each of which contains an op_name. If an op_name does not exist in the model, an error is reported. Note: The value of exclude_op for an ONNX file should be the weight name. | No. The default value is None. |
| preprocess_parameter | Path of the calibration data configuration file. | Yes |
The following table describes the sub-parameters of preprocess_parameter. If a model has multiple inputs, preprocess_parameter must be configured for each input.
| Parameter | Description | Mandatory |
|---|---|---|
| input_type | Whether to use the binary file input or image format input. - BINARY: binary input - IMAGE: image input | No (default to IMAGE) |
| image_format | Image format. This parameter is valid only in IMAGE mode. - BGR: BGR image input - RGB: RGB image input | No (default to BGR) |
| mean_value | Average value parameter for image pre-processing. This parameter takes effect only in IMAGE mode. The value is of the float type and ranges from 0.0 to 255.0. The number of mean_value must be the same as dimension C of the input. | No (dimension C of the input is 0.0 by default) |
| standard_deviation | Standard deviation parameter for image pre-processing. This parameter takes effect only in IMAGE mode. The value is of the float type and must be greater than or equal to 0.0. | No (default to 0.0) |
| input_file_path | Absolute path of the input calibration set, that is, the path of the .bin file, or the folder that stores images. Example: /path/to/user/data. | Yes |
In IMAGE input mode, the toolchain processes images as follows: image = (image - mean_value) / standard_deviation
In BINARY mode, the toolchain does not process the input data. That is, the image_format, mean_value, and standard_deviation parameters are invalid.
The following is a configuration example:
BINARY input mode
strategy: "Quant_INT8-8"
device: USE_GPU
// Provide different .bin files for the multi-input scenario.
preprocess_parameter:
{
input_type: BINARY
input_file_path: "path/to/user/bin/caffe_inception_calibrationset1.bin"
}
preprocess_parameter:
{
input_type: BINARY
input_file_path: "path/to/user/bin/caffe_inception_calibrationset2.bin"
}
// ...
exclude_op: "conv1"
exclude_op: "conv2;conv3" IMAGE input mode
strategy: "Quant_INT8-8"
device: USE_GPU
// Provide different image paths for the multi-input scenario.
preprocess_parameter:
{
input_type: IMAGE
image_format: BGR
mean_value: 104.0
mean_value: 113.0
mean_value: 123.0
standard_deviation: 0.5
input_file_path: "path/to/user/images1/"
}
preprocess_parameter:
{
input_type: IMAGE
image_format: BGR
mean_value: 104.0
mean_value: 113.0
mean_value: 123.0
standard_deviation: 0.5
input_file_path: "path/to/user/images2/"
}
// ...
exclude_op: "conv1"
exclude_op: "conv2;conv3" This function is supported in TensorFlow 2.8 CPU or GPU version. Install the environment as required. Skip this step if the environment is ready.
Install TensorFlow 2.8 as follows:
pip3 install tensorflow-gpu==2.8
Run python3 tools_dopt/dopt_tf_py3/dopt_so.py. The parameters for running the script in Python3 are as follows:
The parameters for running the script to quantize the TensorFlow model in non-training mode are as follows:
The path can contain uppercase letters, lowercase letters, digits, and underscores (_).
The file name can contain uppercase letters, lowercase letters, digits, underscores (_), and periods (.).
| Parameter | Mandatory | Description |
|---|---|---|
| -m, --mode | Yes | Running mode. 0: non-training mode (currently, the value can only be 0) |
| --framework | Yes | Deep learning framework. 3: TensorFlow 5: PyTorch or ONNX |
| --model | Yes | Path of the original model file. PB models are supported. |
| --cal_conf | Yes | Path of the quantization configuration file for the calibration mode. For details about the quantization configuration file, see Filling in the config.prototxt File. |
| --output | Yes | Absolute path of the quantized model file, for example, /path/to/out/resnet18.pb. |
| --input_format | Yes | Input data format, which can be NHWC or NCHW. When you select the IMAGE format or the .bin file whose file header is 510 as the input data, and select NHWC as the input data format, the tool automatically adjusts the channel sequence. When you select the .bin file whose file header is 610 as the input data, the tool will not adjust the channel sequence. |
| --input_shape | Yes | Shape of the input data. Example: input_name1:n1, c1, h1, w1; input_name2: n2, c2, h2, w2 input_name must be the node name in the network model before conversion. If there are multiple input shapes, separate them by using semicolons (;). The input dimensions of input_shape must be consistent with input node specified in the network model. See the following two examples: If the input node is input_shape_network: none, 224, 224, 3; with the last three dimensions specified before conversion, the second, third, and fourth dimensions of input_shape must be 224, 224, and 3. If the input node is input_shape_network: 1, 224, 224, 3 before conversion, with all the four dimensions specified, input_shape can only be 1, 224, 224, 3. |
| --out_nodes | Yes | Output node. For example, "node_name1; node_name2; node_name3". node_name must be the node name in the network model before model conversion. |
| --compress_conf | Yes | Path of the binary file converted from the model file, For example, param_file is a lightweight configuration file. The file converted using the OMG will be used as the input of the compress_conf parameter. |
| --device_idx | No | Device ID of the GPU or CPU. The default value is 0. |
After the quantization script is executed, the .pb file and quantization configuration file are output, with the names you have input respectively for --output and --compress_conf. For example, if you input quantmodel.pb for --output and param for --compress_conf, quantmodel.pb and param will be output.
This function is only available in PyTorch 1.11. Run the following command to install the dependency:
pip3 install torch==1.11
Run python3 tools_dopt/dopt_pytorch_py3/dopt_so.py. The parameters for running the script to quantize the PyTorch model in non-training mode are as follows:
The path can contain uppercase letters, lowercase letters, digits, and underscores (_).
The file name can contain uppercase letters, lowercase letters, digits, underscores (_), and periods (.).
| Parameter | Mandatory | Description |
|---|---|---|
| -m, --mode | Yes | Running mode. 0: non-training mode (currently, the value can only be 0) |
| --framework | Yes | Deep learning framework. 3: TensorFlow 5: PyTorch or ONNX |
| --model | Yes | Path of the PyTorch model definition file. |
| --weight | Yes | Path of the .pth PyTorch model parameter file. |
| --cal_conf | Yes | Path of the quantization configuration file for the calibration mode. For details about the quantization configuration file, see Filling in the config.prototxt File. |
| --output | Yes | Absolute path of the quantized model file, for example, /path_to_out/resnet.pt. |
| --input_shape | Yes | Shape of the input data. Example: input_name1:n1, c1, h1, w1; input_name2: n2, c2, h2, w2 input_name is the input parameter of the forward() function in the model file. input_shape specifies dimension information of the input data, which is used to convert the PyTorch model. The shape must be the same as that of the input node of the actual model. |
| --compress_conf | Yes | Path of the binary file converted from the model file, For example, param_file is a lightweight configuration file. The file converted using the OMG will be used as the input of the compress_conf parameter. |
| --device_idx | No | Device ID of the GPU or CPU. The default value is 0. |
| --input_format | No (for PyTorch) | Input data format, which can be NHWC or NCHW. When you select the IMAGE format or the .bin file whose file header is 510 as the input data, and select NHWC as the input data format, the tool automatically adjusts the channel sequence. When you select the .bin file whose file header is 610 as the input data, the tool will not adjust the channel sequence. |
| --out_nodes | No (for PyTorch) | Output node. For example, "node_name1; node_name2; node_name3". node_name must be the node name in the network model before model conversion. |
After the quantization script is executed, the .pt file and quantization configuration file are output, with the names you have input respectively for --output and --compress_conf. For example, if you input quantmodel.pt for --output and param for --compress_conf, quantmodel.pt and param will be output.
This function is supported in the ONNX Runtime 1.15 CPU version and the ONNX environment. Install the environment as required. Skip this step if the environment is ready.
Install ONNX as follows:
pip3 install onnx==1.14
Install other dependencies:
pip3 install protobuf==3.20.0
Install the ONNX Runtime CPU version as follows:
pip3 install onnxruntime==1.15
Run python3 tools_dopt/dopt_onnx_py3/dopt_so.py. The parameters for running the script in Python3 are as follows:
The parameters for running the script to quantize the ONNX model in non-training mode are as follows:
The path can contain uppercase letters, lowercase letters, digits, and underscores (_).
The file name can contain uppercase letters, lowercase letters, digits, underscores (_), and periods (.).
| Parameter | Mandatory | Description |
|---|---|---|
| -m, --mode | Yes | Running mode. 0: non-training mode (currently, the value can only be 0) |
| --framework | Yes | Deep learning framework. 3: TensorFlow 5: PyTorch or ONNX |
| --weight | No (for ONNX) | Path of the weight file. |
| --model | Yes | Path of the original model file. ONNX models are supported. |
| --cal_conf | Yes | Path of the quantization configuration file for the calibration mode. For details about the quantization configuration file, see Filling in the config.prototxt File. |
| --output | Yes | Absolute path of the model file after quantization, for example, /path_to_out/resnet.onnx. |
| --input_format | Yes | Input data format, which can be NHWC or NCHW. When you select the IMAGE format or the .bin file whose file header is 510 as the input data, and select NHWC as the input data format, the tool automatically adjusts the channel sequence. When you select the .bin file whose file header is 610 as the input data, the tool will not adjust the channel sequence. |
| --input_shape | Yes | Shape of the input data. Example: input_name1:n1, c1, h1, w1; input_name2: n2, c2, h2, w2 input_name must be the node name in the network model before conversion. If there are multiple input shapes, separate them by using semicolons (;). The input dimensions of input_shape must be consistent with input node specified in the network model. See the following two examples: If the input node is input_shape_network: none, 224, 224, 3; with the last three dimensions specified before conversion, the second, third, and fourth dimensions of input_shape must be 224, 224, and 3. If the input node is input_shape_network: 1, 224, 224, 3 before conversion, with all the four dimensions specified, input_shape can only be 1, 224, 224, 3. |
| --out_nodes | Yes | Output node. For example, "node_name1; node_name2; node_name3". node_name must be the node name in the network model before model conversion. |
| --compress_conf | Yes | Path of the binary file converted from the model file, For example, param_file is a lightweight configuration file. The file converted using the OMG will be used as the input of the compress_conf parameter. |
| --device_idx | No (for ONNX) | Device ID of the GPU or CPU. The default value is 0. |
After the quantization script is executed, the .onnx file and quantization configuration file are output, with the names you have input respectively for --output and --compress_conf. For example, if you input quantmodel.onnx for --output and param for --compress_conf, quantmodel.onnx and param will be output.