How Do I Enable AIPP?

Enabling AIPP during model conversion can help you complete data preprocessing for inference and achieve flexible image processing with the aid of a dedicated accelerator module. The following introduces how to enable AIPP during model conversion.

This section uses the TensorFlow ResNet-50 network model as an example to describe how to enable the static AIPP function during model conversion. After the AIPP function is enabled, if the test image provided for model inference does not meet requirements (including the image format and size), this image will be turned into a qualified image through model conversion, and the image information will be solidified into the generated offline model. After model conversion, the AIPP function is inserted into the offline model as an AIPP operator.

The ResNet-50 network model requires an RGB input image with the size of 224 x 224. Here, we assume that the test image provided for model inference is a YUV420SP image with the size of 250 x 250, and the valid area starts from the pixel coordinates (0, 0) in the upper-left corner. Table 1 describes the operations required during AIPP enabling.

Table 1 Scenario analysis

Category

ResNet-50 Requirement

Test Image

Required Operation

Image format

RGB

YUV420SP

In this scenario, AIPP CSC needs to be enabled to convert YUV420SP format into RGB format required by the model. For details about CSC, see CSC Configuration.

Image size

224 x 224

250 x 250

In this scenario, the size of the provided test image is 250 x 250, which is greater than the required size 224 x 224. Therefore, AIPP cropping needs to be enabled, and the cropping starts from (0, 0), that is, horizontal and vertical coordinates (load_start_pos_h and load_start_pos_w) are 0. During inference, the 224 x 224 valid area is selected based on the start coordinates (0, 0).

The detailed implementation procedure is as follows:

  1. Obtain a TensorFlow model.

    Click here to download the *.pb model file of the ResNet-50 network and upload the file to any directory in the development environment as the CANN running user, for example, $HOME/module/.

  2. Construct an AIPP configuration file, for example, insert_op.cfg.

    A static AIPP configuration template consists of: AIPP mode (static AIPP or dynamic AIPP), source image information (image format and size), image size change (cropping and padding), and CSC. The following describes how to configure the information:

    1. Set the AIPP mode by the aipp_mode parameter, for example:
             aipp_mode : static           # Static AIPP is configured.
    2. Configure the source image information.
             input_format : YUV420SP_U8     # Input image format for AIPP
             src_image_size_w : 250         # Width and height of the source image for AIPP
             src_image_size_h : 250
    3. Resize the image.

      You can change the image size by image cropping and padding. In this example, you need to configure information such as the start position of image cropping and the size of the cropped image. Padding is required if the image size after cropping does not meet the model requirements.

      AIPP provides a more convenient configuration mode. If cropping is enabled without padding, the cropping size can be set to 0 or not configured. In this case, the width and height of the cropped image are obtained from those of the model parameter --input_shape. In this example, the size of the cropped image is not configured:
             crop: true                     # Image cropping switch
             load_start_pos_h: 0            # Horizontal and vertical coordinates of the cropping start position
             load_start_pos_w: 0
    4. Perform CSC.
      Set CSC by the csc_switch parameter, and use it together with the matrix_r*c* and rbuv_swap_switch parameters. AIPP provides a quite convenient function. Once you confirm the image formats before and after AIPP processing, you can directly use the CSC parameter values without making any modifications. That is, you can directly copy the preceding parameters from the template. For details about template examples and more configuration templates, see CSC Configuration. The following is a configuration example for this scenario:
             csc_switch : true              # CSC switch. true indicates that CSC is enabled.
             rbuv_swap_switch : false       # R/B or U/V channel swap switch. In this example, channel swap is disabled.
             matrix_r0c0 : 256              # CSC coefficient
             matrix_r0c1 : 0
             matrix_r0c2 : 359
             matrix_r1c0 : 256
             matrix_r1c1 : -88
             matrix_r1c2 : -183
             matrix_r2c0 : 256
             matrix_r2c1 : 454
             matrix_r2c2 : 0
             input_bias_0 : 0
             input_bias_1 : 128
             input_bias_2 : 128

    Add all the preceding parameters to the insert_op.cfg file, which is the AIPP configuration file to be constructed. The following is an example:

    aipp_op {
           aipp_mode : static             # AIPP mode
           input_format : YUV420SP_U8     # Input image format for AIPP
           src_image_size_w : 250         # Width and height of the source image for AIPP
           src_image_size_h : 250
           crop: true                     # Image cropping switch, which is used to change the image size.
           load_start_pos_h: 0            # Horizontal and vertical coordinates of the cropping start position
           load_start_pos_w: 0
           csc_switch : true              # CSC switch
           matrix_r0c0 : 256              # CSC coefficient, which does not need to be modified by users
           matrix_r0c1 : 0
           matrix_r0c2 : 359
           matrix_r1c0 : 256
           matrix_r1c1 : -88
           matrix_r1c2 : -183
           matrix_r2c0 : 256
           matrix_r2c1 : 454
           matrix_r2c2 : 0
           input_bias_0 : 0
           input_bias_1 : 128
           input_bias_2 : 128
    }

    You can view more AIPP configuration examples by referring to AIPP Configuration Sample or Sample Reference, or construct your own configuration file by referring to Configuration File Template. Upload the insert_op.cfg file to the Linux server where the ATC tool is installed.

  3. Include --insert_op_conf in your atc command to insert the AIPP operator. Then run this command to generate an offline model (the path and file arguments in the command are for reference only).
    atc --model=$HOME/module/resnet50_tensorflow*.pb --framework=3 --output=$HOME/module/out/tf_resnet50 --soc_version=<soc_version> --insert_op_conf=$HOME/module/insert_op.cfg  
    For details about the command-line options, see Command Line Options. Check that your model is converted successfully.
    1
    ATC run success
    

    Find the generated offline model (for example, tf_resnet50.om) in the directory specified by the --output argument.

  4. (Optional) If you want to view the AIPP operator details in the offline model, convert the offline model into a .json file.
    atc --mode=1 --om=$HOME/module/out/tf_resnet50.om  --json=$HOME/module/out/tf_resnet50.json

    The following JSON file sample contains the AIPP operator information (all attribute values are for reference only).

    {
                  "key": "aipp",
                  "value": {
                    "func": {
                      "attr": [
                        {
                          "key": "mean_chn_0",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "mean_chn_1",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "mean_chn_2",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "mean_chn_3",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "csc_switch",
                          "value": {
                            "b": true
                          }
                        },
                        {
                          "key": "input_format",
                          "value": {
                            "i": 1
                          }
                        },
                        {
                          "key": "input_bias_0",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "input_bias_1",
                          "value": {
                            "i": 128
                          }
                        },
                        {
                          "key": "input_bias_2",
                          "value": {
                            "i": 128
                          }
                        },
                        {
                          "key": "aipp_mode",
                          "value": {
                            "i": 1
                          }
                        },
                        {
                          "key": "src_image_size_h",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "crop_size_h",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "matrix_r0c0",
                          "value": {
                            "i": 256
                          }
                        },
                        {
                          "key": "matrix_r0c1",
                          "value": {
                            "i": 443
                          }
                        },
                        {
                          "key": "matrix_r0c2",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "src_image_size_w",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "crop_size_w",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "rbuv_swap_switch",
                          "value": {
                            "b": false
                          }
                        },
                        {
                          "key": "padding",
                          "value": {
                            "b": false
                          }
                        },
                        {
                          "key": "ax_swap_switch",
                          "value": {
                            "b": false
                          }
                        },
                        {
                          "key": "top_padding_size",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "matrix_r1c0",
                          "value": {
                            "i": 256
                          }
                        },
                        {
                          "key": "matrix_r1c1",
                          "value": {
                            "i": -86
                          }
                        },
                        {
                          "key": "matrix_r1c2",
                          "value": {
                            "i": -178
                          }
                        },
                        {
                          "key": "resize",
                          "value": {
                            "b": false
                          }
                        },
                        {
                          "key": "resize_output_h",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "related_input_rank",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "load_start_pos_h",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "matrix_r2c0",
                          "value": {
                            "i": 256
                          }
                        },
                        {
                          "key": "matrix_r2c1",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "matrix_r2c2",
                          "value": {
                            "i": 350
                          }
                        },
                        {
                          "key": "resize_output_w",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "var_reci_chn_0",
                          "value": {
                            "f": "1"
                          }
                        },
                        {
                          "key": "var_reci_chn_1",
                          "value": {
                            "f": "1"
                          }
                        },
                        {
                          "key": "var_reci_chn_2",
                          "value": {
                            "f": "1"
                          }
                        },
                        {
                          "key": "load_start_pos_w",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "var_reci_chn_3",
                          "value": {
                            "f": "1"
                          }
                        },
                        {
                          "key": "single_line_mode",
                          "value": {
                            "b": false
                          }
                        },
                        {
                          "key": "output_bias_0",
                          "value": {
                            "i": 16
                          }
                        },
                        {
                          "key": "output_bias_1",
                          "value": {
                            "i": 128
                          }
                        },
                        {
                          "key": "output_bias_2",
                          "value": {
                            "i": 128
                          }
                        },
                        {
                          "key": "right_padding_size",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "bottom_padding_size",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "min_chn_0",
                          "value": {
                            "f": "0"
                          }
                        },
                        {
                          "key": "min_chn_1",
                          "value": {
                            "f": "0"
                          }
                        },
                        {
                          "key": "min_chn_2",
                          "value": {
                            "f": "0"
                          }
                        },
                        {
                          "key": "min_chn_3",
                          "value": {
                            "f": "0"
                          }
                        },
                        {
                          "key": "crop",
                          "value": {
                            "b": false
                          }
                        },
                        {
                          "key": "cpadding_value",
                          "value": {
                            "f": "0"
                          }
                        },
                        {
                          "key": "left_padding_size",
                          "value": {
                            "i": 0
                          }
                        }
                      ]
                    }
                  }
                }