Dynamic AIPP (Single Dynamic AIPP Input)

API Call Sequence

In the dynamic AIPP scenario, the model inference process is similar to the Model Management process, including initialization and deinitialization, runtime resource allocation and release, model building, model loading, model execution, and model unloading.

This section focuses on the differences between the two scenarios.

  • During model building: You need to set the parameters related to dynamic AIPP.

    When building a model, you need to call the API provided by pyacl to set the AIPP configuration required for model inference. The AIPP mode supported by the model has been configured during model building (using the insert_op_conf parameter of the ATC tool). For details about the ATC parameters, see Ascend Tensor Compiler (ATC).

    After model building, the AIPP inputs are added to the generated OM model. During model inference, the AIPP input values are provided.

    Assume that the AIPP configuration of input a is dynamic. In the generated .om model, input b is added to describe the AIPP configuration of input a. For details about how to prepare the data structure of input a for model execution, see Preparing Input/Output Data Structure for Model Execution. For details about how to prepare the data structure of input b and set the data of input b, see the following description.

  • Before model inference

Sample Code

Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to use.

 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
import acl
# ......

ACL_YUV420SP_U8 = 1

# 1. Load the model and set dynamic AIPP parameters.
# ......

# 2. Create data of type aclmdlDataset to describe the input data input_dataset and output data output_dataset of the model.
# ......

# 3. Customize a function for setting dynamic AIPP parameters.
def model_set_dynamic_aipp():
    # 3.1 Obtain the index of the dynamic AIPP input.
    index, ret = acl.mdl.get_input_index_by_name(model_desc, "ascend_dynamic_aipp_data")

    # 3.2 Set dynamic AIPP parameters.
    batch_number = 1
    aipp_dynamic_set = acl.mdl.create_aipp(batch_number)
    ret = acl.mdl.set_aipp_src_image_size(aipp_dynamic_set, 256, 224)
    ret = acl.mdl.set_aipp_input_format(aipp_dynamic_set, ACL_YUV420SP_U8)
    ret = acl.mdl.set_aipp_csc_params(aipp_dynamic_set, 1, 256, 443, 0, 256, -86, -178, 256, 0, 350, 0, 0, 0, 0, 128, 128)
    ret = acl.mdl.set_aipp_rbuv_swap_switch(aipp_dynamic_set, 0)
    ret = acl.mdl.set_aipp_dtc_pixel_mean(aipp_dynamic_set, 0, 0, 0, 0, 0)
    ret = acl.mdl.set_aipp_dtc_pixel_min(aipp_dynamic_set, 0, 0, 0, 0, 0)
    ret = acl.mdl.set_aipp_pixel_var_reci(aipp_dynamic_set, 1, 1, 1, 1, 0)
    ret = acl.mdl.set_aipp_crop_params(aipp_dynamic_set, 1, 0, 0, 224, 224, 0)
    ret = acl.mdl.set_input_aipp(model_id, input_dataset, index, aipp_dynamic_set)
    ret = acl.mdl.destroy_aipp(aipp_dynamic_set)	
    # ......

# 4. Customize a function and execute the model.
def model_execute(index):
    # 4.1 Call the custom function to set dynamic AIPP parameters.
    ret = model_set_dynamic_aipp()
    # 4.2 Execute the model. model_id indicates the ID of a successfully loaded model. input_ and output_ indicate the input and output of the model.
    ret = acl.mdl.execute(model_id, input_dataset, output_dataset)
    # ......

# 5. Process the model inference result.
# ......