AIPP deployment refers to the configuration of dynamic AIPP parameters as required to enable AIPP during dynamic AIPP inference.
The following APIs are used to set AIPP parameters. For more setting and query APIs, see API Reference.
Table 1 AIPP setting APIs in CANN Kit for model inference
| API | Description |
|---|---|
| HiAI_AippParam* HMS_HiAIAippParam_Create(uint32_t batchNum); | Creates a dynamic AIPP configuration instance. |
| void HMS_HiAIAippParam_Destroy(HiAI_AippParam** aippParam); | Destroys a dynamic AIPP configuration instance. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetInputIndex(HiAI_AippParam* aippParam, uint32_t inputIndex); | Sets the index of the input on which the dynamic AIPP configuration takes effect. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetInputAippIndex(HiAI_AippParam* aippParam, uint32_t inputAippIndex); | Sets the index of the output, on whose input the dynamic AIPP configuration takes effect, where the input has more than one output. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetInputFormat(HiAI_AippParam* aippParam, HiAI_ImageFormat inputFormat); | Sets the input image format. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetInputShape(HiAI_AippParam* aippParam, uint32_t srcImageW, uint32_t srcImageH); | Sets the original width and height of the input image. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetCscConfig(HiAI_AippParam* aippParam, HiAI_ImageFormat inputFormat, HiAI_ImageFormat outputFormat, HiAI_ImageColorSpace space); | Sets CSC parameters. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetChannelSwapConfig(HiAI_AippParam* aippParam, bool rbuvSwapSwitch, bool axSwapSwitch); | Sets channel swapping parameters. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetCropConfig(HiAI_AippParam* aippParam, uint32_t batchIndex, uint32_t startPosW, uint32_t startPosH, uint32_t croppedW, uint32_t croppedH); | Sets image cropping parameters. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetResizeConfig(HiAI_AippParam* aippParam, uint32_t batchIndex, uint32_t resizedW, uint32_t resizedH); | Sets image resizing parameters. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetPadConfig(HiAI_AippParam* aippParam, uint32_t batchIndex, uint32_t leftPadSize, uint32_t rightPadSize, uint32_t topPadSize, uint32_t bottomPadSize); | Sets the number of pixels padded to the left, right, top, and bottom of an image. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetChannelPadding(HiAI_AippParam* aippParam, uint32_t batchIndex, uint32_t paddingValues[], uint32_t channelCount); | Sets the padding value for a channel. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetRotationAngle(HiAI_AippParam* aippParam, uint32_t batchIndex, float rotationAngle); | Sets image rotation parameters. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetDtcMeanPixel(HiAI_AippParam* aippParam, uint32_t batchIndex, uint32_t meanPixel[], uint32_t channelCount); | Sets the channel pixel mean value for data type conversion. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetDtcMinPixel(HiAI_AippParam* aippParam, uint32_t batchIndex, float minPixel[], uint32_t channelCount); | Sets the channel pixel minimum value for data type conversion. |
| OH_NN_ReturnCode HMS_HiAIAippParam_SetDtcVarReciPixel(HiAI_AippParam* aippParam, uint32_t batchIndex, float varReciPixel[], uint32_t channelCount); | Sets the channel pixel variance for data type conversion. |
| OH_NN_ReturnCode HMS_HiAITensor_SetAippParams(NN_Tensor* tensor, HiAI_AippParam* aippParams[], size_t aippNum); | Sets AIPP parameters for the input tensor. |
Call HMS_HiAIAippParam_Create to create a dynamic AIPP configuration instance.
Set configurations about image computation.
Set configurations about the input image for dynamic AIPP.
Set the following dynamic AIPP parameters as required.
Configure the AIPP in NN_Tensor.
Construct the input and output tensors, and call HMS_HiAITensor_SetAippParams to set AIPP parameters for the input tensor.
Perform model inference.
Call HMS_HiAIAippParam_Destroy to destroy the dynamic AIPP configuration instance.
Assume that the training set of a model consists of RGB888 images. With dynamic AIPP enabled, YUYV images can be used as the input for model inference. If the sizes of input images for model inference are different from those of images in the training set, you can use AIPP to crop, resize, or pad the images to change their sizes. In the following sample code, you will see how to implement AIPP cropping, resizing, and padding through NDK APIs, and pre-process a 480 x 480 YUYV image into a 224 x 224 input.
constexpr uint32_t BATCH_NUM = 1;
// Create a dynamic AIPP configuration instance with a batch size of 1.
aippPara_ = HMS_HiAIAippParam_Create(BATCH_NUM);
// Set the index of the input linked with the AippParam object, in the case of multiple inputs.
uint32_t inputIndex = 0;
OH_NN_ReturnCode ret = HMS_HiAIAippParam_SetInputIndex(aippPara_, inputIndex);
// Specify which output branch of the input the AippParam object applies to when data contains multiple output branches.
uint32_t validInputAippIndex = 0;
HMS_HiAIAippParam_SetInputAippIndex(aippPara_, validInputAippIndex);
// Set the input image format for the AippParam object.
HMS_HiAIAippParam_SetInputFormat(aippPara_, HIAI_RGB888_U8);
// Set the input image width and height for the AippParam object.
HMS_HiAIAippParam_SetInputShape(aippPara_, width, height);
uint32_t chnNum = 3;
uint32_t pixelMeanPara[3] = {0, 0, 0};
float minPixel[3] = {0.0, 0.0, 0.0};
float varReciPixel[3] = {1/255.0, 1/255.0, 1/255.0};
HMS_HiAIAippParam_SetDtcMinPixel(aippPara_, 0, minPixel, chnNum);
HMS_HiAIAippParam_SetDtcMeanPixel(aippPara_, 0, pixelMeanPara, chnNum);
HMS_HiAIAippParam_SetDtcVarReciPixel(aippPara_, 0, varReciPixel, chnNum);
// ...
// Obtain the number of input tensors.
size_t inputCount = 0;
OH_NN_ReturnCode ret = OH_NNExecutor_GetInputCount(executor_, &inputCount);
if (ret != OH_NN_SUCCESS || inputCount != inputData.size()) {
OH_LOG_ERROR(LOG_APP, "OH_NNExecutor_GetInputCount failed, size mismatch");
return OH_NN_FAILED;
}
for (size_t i = 0; i < inputCount; ++i) {
std::vector<int32_t> dims = {1, 3, static_cast<int32_t>(width), static_cast<int32_t>(height)};
// Create the description of an input tensor based on the specified index value.
NN_TensorDesc *tensorDesc = OH_NNExecutor_CreateInputTensorDesc(executor_, i);
// Set the data shape of an NN_TensorDesc instance.
OH_NNTensorDesc_SetShape(tensorDesc, dims.data(), dims.size());
// Calculate the required tensor size based on NN_TensorDesc and HiAI_ImageFormat.
size_t tensorSize = HMS_HiAITensor_GetSizeWithImageFormat(tensorDesc, HiAI_ImageFormat::HIAI_RGB888_U8);
if (tensorSize == 0 || tensorSize != inputData[0].second) {
// Release an NN_TensorDesc instance.
OH_NNTensorDesc_Destroy(&tensorDesc);
OH_LOG_ERROR(LOG_APP, "OH_NNExecutor_GetInputCount failed, size mismatch tensorSize %d"
"inputData[0].second %d", tensorSize, inputData[0].second);
return OH_NN_FAILED;
}
// Create an NN_Tensor instance based on the specified memory size and NN_TensorDesc instance.
NN_Tensor* tensor = OH_NNTensor_CreateWithSize(deviceID_, tensorDesc, tensorSize);
HiAI_AippParam* aippParas[1] = {aippPara_};
//Set AippParams for NN_Tensor.
ret = HMS_HiAITensor_SetAippParams(tensor, aippParas, 1);
if (ret != OH_NN_SUCCESS) {
OH_LOG_ERROR(LOG_APP, "SetAippParams failed");
return OH_NN_FAILED;
}
if (tensor != nullptr) {
inputTensors_.push_back(tensor);
}
// Release an NN_TensorDesc instance.
OH_NNTensorDesc_Destroy(&tensorDesc);
}