For the Ascend 310 AI Processor, the high-order filtering algorithm developed by Huawei is used by default.
For the Ascend 910 AI Processor, the high-order filtering algorithm developed by Huawei is used by default.
For the Ascend 310P AI Processor, the conventional bilinear algorithm is used by default.
You can also specify a desired algorithm by using the acldvppSetResizeConfigInterpolation call.
You can view the complete code in Sample Overview.
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code sample of key steps for your reference only.
//1. Initialize AscendCL.
aclRet = aclInit(nullptr);
//2. Allocate runtime resources including the device, context, and stream in sequence.
aclrtContext context_;
aclrtStream stream_;
aclrtSetDevice(0);
aclrtCreateContext(&context_, 0);
aclrtCreateStream(&stream_);
//3. Create image resizing configuration data and specify a resizing algorithm.
//resizeConfig_ is of type acldvppResizeConfig.
acldvppResizeConfig *resizeConfig_ = acldvppCreateResizeConfig();
aclError ret = acldvppSetResizeConfigInterpolation(resizeConfig, 0);
//4. Create description of the data processing channel. dvppChannelDesc_ is of type acldvppChannelDesc.
dvppChannelDesc_ = acldvppCreateChannelDesc();
//5. Create a data processing channel.
ret = acldvppCreateChannel(dvppChannelDesc_);
//6. Allocate input buffer (run mode specific).
//Call aclrtGetRunMode to obtain the run mode of the software stack. If ACL_DEVICE is returned, allocate and use the device buffer. If ACL_HOST is returned, transfer the input image data to the device by using the aclrtMemcpy call. After the data transfer is complete, the buffer needs to be freed in a timely manner.
aclrtRunMode runMode;
ret = aclrtGetRunMode(&runMode);
//inputPicWidth and inputPicHeight respectively indicate the width and height of an image after alignment. The following uses a YUV420SP image as an example.
uint32_t resizeInBufferSize = inputPicWidth * inputPicHeight * 3 / 2;
if (runMode == ACL_HOST) {
//Allocate host buffer vpcInHostBuffer.
void* vpcInHostBuffer = nullptr;
vpcInHostBuffer = malloc(resizeInBufferSize);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, vpcInHostBuffer, resizeInBufferSize);
//Allocate the device buffer resizeInDevBuffer_.
aclRet = acldvppMalloc(&resizeInDevBuffer_, resizeInBufferSize);
//Transfer the input image data to the device by using the aclrtMemcpy call.
aclRet = aclrtMemcpy(resizeInDevBuffer_, resizeInBufferSize, vpcInHostBuffer, resizeInBufferSize, ACL_MEMCPY_HOST_TO_DEVICE);
//Free the buffer in a timely manner after data transfer is complete.
free(vpcInHostBuffer);
} else {
//Allocate input buffer resizeInDevBuffer_ on the device.
ret = acldvppMalloc(&resizeInDevBuffer_, resizeInBufferSize);
//Load the input image into the buffer. The ReadPicFile function is defined by the user.
ReadPicFile(picName, resizeInDevBuffer_, resizeInBufferSize);
}
//7. Allocate output buffer resizeOutBufferDev_. The buffer size resizeOutBufferSize_ is calculated based on the formula.
//outputPicWidth and outputPicHeight respectively indicate the width and height after image alignment. The following uses a YUV420SP image as an example.
uint32_t resizeOutBufferSize_ = outputPicWidth * outputPicHeight * 3 / 2;
ret = acldvppMalloc(&resizeOutBufferDev_, resizeOutBufferSize_)
//8. Create the description of the input image to be resized and set the attribute values.
//resizeInputDesc_ is of type acldvppPicDesc.
resizeInputDesc_ = acldvppCreatePicDesc();
acldvppSetPicDescData(resizeInputDesc_, resizeInDevBuffer_);
acldvppSetPicDescFormat(resizeInputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
acldvppSetPicDescWidth(resizeInputDesc_, inputWidth_);
acldvppSetPicDescHeight(resizeInputDesc_, inputHeight_);
acldvppSetPicDescWidthStride(resizeInputDesc_, inputWidthStride);
acldvppSetPicDescHeightStride(resizeInputDesc_, inputHeightStride);
acldvppSetPicDescSize(resizeInputDesc_, resizeInBufferSize);
//9. Create the description of the resized image and set the attribute values.
//If the resized output image is used as the input for model inference, the output width and height must meet the requirements of the model.
//resizeOutputDesc_ is of type acldvppPicDesc.
resizeOutputDesc_ = acldvppCreatePicDesc();
acldvppSetPicDescData(resizeOutputDesc_, resizeOutBufferDev_);
acldvppSetPicDescFormat(resizeOutputDesc_, PIXEL_FORMAT_YUV_SEMIPLANAR_420);
acldvppSetPicDescWidth(resizeOutputDesc_, resizeOutputWidth_);
acldvppSetPicDescHeight(resizeOutputDesc_, resizeOutputHeight_);
acldvppSetPicDescWidthStride(resizeOutputDesc_, resizeOutputWidthStride);
acldvppSetPicDescHeightStride(resizeOutputDesc_, resizeOutputHeightStride);
acldvppSetPicDescSize(resizeOutputDesc_, resizeOutBufferSize_);
//10. Perform asynchronous resizing and call aclrtSynchronizeStream to wait for the stream tasks to complete.
ret = acldvppVpcResizeAsync(dvppChannelDesc_, resizeInputDesc_,
resizeOutputDesc_, resizeConfig_, stream_);
ret = aclrtSynchronizeStream(stream_);
//11. After resizing is complete, destroy the allocations, including the description of the input and output image, as well as the input and output buffer.
acldvppDestroyPicDesc(resizeInputDesc_);
acldvppDestroyPicDesc(resizeOutputDesc_);
if (runMode == ACL_HOST) {
//In this mode, the processing result is on the device. Therefore, you need to call the memory copy API to transfer the result data, and then free the device buffer.
//Allocate host buffer vpcOutHostBuffer.
void* vpcOutHostBuffer = nullptr;
vpcOutHostBuffer = malloc(resizeOutBufferSize_);
//Transfer the processing result of the device to the host by using the aclrtMemcpy call.
aclRet = aclrtMemcpy(vpcOutHostBuffer, resizeOutBufferSize_, resizeOutBufferDev_, resizeOutBufferSize_, ACL_MEMCPY_DEVICE_TO_HOST);
//Free the input and output buffer on the device.
(void)acldvppFree(resizeInDevBuffer_);
(void)acldvppFree(resizeOutBufferDev_);
//Free the buffer after the data is used.
free(vpcOutHostBuffer);
} else {
//The process is running on the device, and the processing result is also on the device. Free the device buffer when the data is no longer needed.
(void)acldvppFree(resizeInDevBuffer_);
(void)acldvppFree(resizeOutBufferDev_);
}
//12. Release runtime resources including the stream, context, and device in sequence.
aclrtDestroyStream(stream_);
aclrtDestroyContext(context_);
aclrtResetDevice(0);
//13. Deinitialize AscendCL.
aclRet = aclFinalize();
// ....