Dynamic Image Size (Setting Image Size Profiles)

Principles

If the dynamic image size feature is involved during model inference, call the AscendCL API to set the image size. The image size supported by the model has been configured during model building (by using the dynamic_image_size parameter of ATC).

For details about the ATC parameter, see ATC Instructions.

  • For the same model, only one of the aclmdlSetDynamicBatchSize, aclmdlSetDynamicHWSize, and aclmdlSetInputDynamicDims APIs can be called.
  • For output buffer allocation for model inference, you can allocate buffer based on the runtime dimensions. To be accurate, you can call aclmdlGetOutputSizeByIndex to obtain the required buffer size before allocating the buffer.
  • If dynamic AIPP and dynamic image size are both enabled:
    • If the image cropping, resizing, or padding function is enabled in setting the dynamic AIPP attributes, dynamic image size becomes unavailable.
    • If the image cropping, resizing, or padding function is not enabled in setting the dynamic AIPP attributes, dynamic AIPP and dynamic image size are both available. Ensure sure that the allowed maximum image size profile is passed to the aclmdlSetAIPPSrcImageSize call and aclmdlSetDynamicHWSize call.
    • For data nodes that require dynamic AIPP, the input buffer must be allocated based on the maximum allowed image resolution.

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 be built or run.

//1. Load the model and then set the runtime image size.
// ......

//2. Prepare the model description modelDesc_, and the model inputs input_ and model outputs output_.
// ......

//3. Customize a function to set the runtime image size.
int  ModelSetDynamicInfo()
{
		size_t index;
                //3.1 Obtain the indexes of the inputs with dynamic image size. The input name is fixed to ACL_DYNAMIC_TENSOR_NAME.
		aclError ret = aclmdlGetInputIndexByName(modelDesc_, ACL_DYNAMIC_TENSOR_NAME, &index);
                //3.2 Set the image size. modelId_ indicates the ID of a successfully loaded model, input_ indicates data of type aclmdlDataset, and index indicates the index of the input with dynamic image size.
                uint64_t height = 224;
		uint64_t width = 224;
		ret = aclmdlSetDynamicHWSize(modelId_, input_, index, height, width);
                // ......
}

//4. Customize a function to execute the model.
int ModelExecute(int index)
{
        aclError ret;
        //4.1 Call the user-defined function to set the runtime image size.
	ret = ModelSetDynamicInfo();
        //4.2 Execute the model. modelId_ indicates the ID of a successfully loaded model, input_ indicates the model inputs, and output_ indicates the model outputs.
        ret = aclmdlExecute(modelId_, input_, output_);
        // ......
}

//5. Process the model inference result.
// TODO