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 ResNet50 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, 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
    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. true indicates that CSC is enabled.
           rbuv_swap_switch : false       # Channel swapping 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, welcome to the next use.
    

    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. The command is as follows:
    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 aipp attribute values are for reference only.)

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    {
                  "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": 250
                          }
                        },
                        {
                          "key": "crop_size_h",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "matrix_r0c0",
                          "value": {
                            "i": 256
                          }
                        },
                        {
                          "key": "matrix_r0c1",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "key": "matrix_r0c2",
                          "value": {
                            "i": 359
                          }
                        },
                        {
                          "key": "src_image_size_w",
                          "value": {
                            "i": 250
                          }
                        },
                        {
                          "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": -88
                          }
                        },
                        {
                          "key": "matrix_r1c2",
                          "value": {
                            "i": -183
                          }
                        },
                        {
                          "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": 454
                          }
                        },
                        {
                          "key": "matrix_r2c2",
                          "value": {
                            "i": 0
                          }
                        },
                        {
                          "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
                          }
                        }
                      ]
                    }
                  }
                }