本文分享自华为云社区《CANN DVPP进行图片的等比例缩放-云社区-华为云》,作者:马城林 。
首先没有任何的规定表示我们必须进行等比例的缩放,因为实际上即使图像上的物体因为缩放而变形,物体本身的特征还是存在,神经网络依旧可以提取对应的特征进而预测出物体的位置,通过计算实际的宽高与模型之间的宽高的比例依旧可以将模型预测到的候选框的映射到实际的原图上。这样看来等比例的缩放存在感好低!
但是事情真的是这样吗?肯定不是呀,要不我为啥会写这篇博客呢?
首先有以下几点考虑:
如果在我们的训练服务器,或者我们自己的个人计算机上,因为计算能力的相对充沛,我们可以借助很多开源图像库例如opencv,pillow等进行图像的等比例缩放。但是一旦道路端侧部署时这些开源图像库的方法往往成为整个推理过程最耗时的部分,因为他们主要通过CPU进行运算,而在诸如200DK这样的环境上,进行这样的变换让本就不充沛的CPU算力雪上加霜。而昇腾AI处理器上拥有专门处理数字图像的硬件单元。所以如何借助这些专用的硬件单元是十分重要的。话不多说开始吧!
主要从以下几个方面进行讲解
详细的约束对齐规则
JPEG 解码
(https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/51RC2alpha007/infacldevg/aclcppdevg/aclcppdevg_03_0176.html)
专有网络
(https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/51RC2alpha007/infacldevg/aclcppdevg/aclcppdevg_03_0157.html)
JPEG 编码
(https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/51RC2alpha007/infacldevg/aclcppdevg/aclcppdevg_03_0183.html)
很多朋友和我一样属于一听就会,一动手就废的类型,在脑海里勾勒出万里江山,实际一上手,我是个傻子。。。。。
实际上是我们还没有掌握做事情的诀窍,就是把事情分解成可以很方便用代码描述出的步骤;计算机编程让人感到头皮发麻的一个重要原因就是计算机它只会一步一步来,不会跳步。所以我们脑海里面很简单的步骤在编程实现后也可能代码很长。
1、将常见的JPEG图片解码成YUV格式的图片
aclError acldvppJpegDecodeAsync(acldvppChannelDesc *channelDesc, const void *data, uint32_t size, acldvppPicDesc *outputDesc,aclrtStream stream)
2、因为JPEG解码有128x16的对齐要求,所以解码后的数据边缘会出现无效数据,需要进行裁剪
aclError acldvppVpcCropAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc, acldvppPicDesc *outputDesc, acldvppRoiConfig *cropArea, aclrtStream stream)
3、进行resize的操作,没有对应的等比例缩放的接口,但是如果我们最后指定的图像大小相对于原图是等比例的,就是等比例缩放
aclError acldvppVpcResizeAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc, acldvppPicDesc *outputDesc, acldvppResizeConfig *resizeConfig, aclrtStream stream)
4、进行等比例缩放时需要一个,与模型大小一致的纯色背景图片作为贴图的背景,我们可以申请对应大小的一些区域然后,将对应区域全部赋值为同一个数。
aclError aclrtMemset (void *devPtr, size_t maxCount, int32_t value, size_t count)
5、进行贴图
aclError acldvppVpcCropAndPasteAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc, acldvppPicDesc *outputDesc, acldvppRoiConfig *cropArea, acldvppRoiConfig *pasteArea, aclrtStream stream)
6、进行模型推理或编码保存
acldvppChannelDesc *channelDescchannelDesc = acldvppCreateChannelDesc();aclError ret = acldvppCreateChannel(channelDesc);// 销毁与创建的顺序相反acldvppDestroyChannel(channelDesc);acldvppDestroyChannelDesc(channelDesc);
acldvppPicDesc *picDescpicDesc = acldvppCreatePicDesc();acldvppSetPicDescData(picDesc, decodeOutDevBuffer_); // 图像的内存地址acldvppSetPicDescSize(picDesc, decodeOutBufferSize); // 图像所占的内存大小acldvppSetPicDescFormat(picDesc, PIXEL_FORMAT_YUV_SEMIPLANAR_420); // 图像的格式acldvppSetPicDescWidth(picDesc, inputWidth_); // 图像的实际宽acldvppSetPicDescHeight(picDesc, inputHeight_); // 图像的实际高acldvppSetPicDescWidthStride(picDesc, decodeOutWidthStride); // 图像在内存中对齐后的宽acldvppSetPicDescHeightStride(picDesc, decodeOutHeightStride); // 图像在内存中对齐后的高// 销毁acldvppDestroyPicDesc(picDesc);
// device侧,200DK等只有device内存的也用此接口,且只用此接口aclError aclrtMalloc(void **devPtr, size_t size, aclrtMemMallocPolicy policy)aclError aclrtFree(void *devPtr)// host侧aclError aclrtMallocHost(void **hostPtr, size_t size)aclError aclrtFreeHost(void *hostPtr)// dvpp 内存(在device侧,因为需要内存地址128对齐,所以拥有单独的接口)aclError acldvppMalloc(void **devPtr, size_t size)aclError acldvppFree(void *devPtr)
acldvppRoiConfig *acldvppCreateRoiConfig(uint32_t left, uint32_t right, uint32_t top, uint32_t bottom)// 销毁aclError acldvppDestroyRoiConfig(acldvppRoiConfig *roiConfig)
// 通过此接口可以方便的获取JPEG图像的宽,高,色彩通道数,编码格式,只需要传入对应的JPEG源数据地址以及大小aclError acldvppJpegGetImageInfoV2(const void *data, uint32_t size, uint32_t *width, uint32_t *height, int32_t *components, acldvppJpegFormat *format)// 获取JPEG图片解码后的数据大小,因为解码后会对齐到128,所以不能任何时候都简单地依据原图大小计算解码后占用的内存大小。aclError acldvppJpegPredictDecSize(const void *data, uint32_t dataSize, acldvppPixelFormat outputPixelFormat, uint32_t *decSize)// 获取PNG图像的宽和高aclError acldvppPngGetImageInfo(const void *data, uint32_t dataSize, uint32_t *width, uint32_t *height, int32_t *components)// 预测PNG图片解码后占用内存空间aclError acldvppPngPredictDecSize(const void *data, uint32_t dataSize, acldvppPixelFormat outputPixelFormat, uint32_t *decSize)// 获取JPEG图像编码后的占用内存空间aclError acldvppJpegPredictEncSize(const acldvppPicDesc *inputDesc, const acldvppJpegeConfig *config, uint32_t *size)
// 缩放接口,单一接口,将缩放后的图片作为输出aclError acldvppVpcResizeAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc, acldvppPicDesc *outputDesc, acldvppResizeConfig *resizeConfig, aclrtStream stream)// 抠图接口,单一接口,从原图中根据ROI区域抠出一部分作为输出aclError acldvppVpcCropAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc, acldvppPicDesc *outputDesc, acldvppRoiConfig *cropArea, aclrtStream stream)// 组合接口(抠图+缩放),应用场景:JPEG 解码后的图片中可能有无效的数据,直接缩放到模型需要的大小会把无效数据一起输入,可以先把真实图像的数据抠出来然后进行缩放,减少无效的数据干扰。aclError acldvppVpcCropResizeAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc, acldvppPicDesc *outputDesc, acldvppRoiConfig *cropArea, acldvppResizeConfig *resizeConfig, aclrtStream stream)// 组合接口(抠图+贴图 / 抠图+缩放+贴图)如果抠图与贴图的大小不一致会进行一步缩放操作。aclError acldvppVpcCropAndPasteAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc, acldvppPicDesc *outputDesc, acldvppRoiConfig *cropArea, acldvppRoiConfig *pasteArea, aclrtStream stream)// 组合接口(抠图+贴图 / 抠图+缩放+贴图)此接口功能与上一个是一致的,但是此接口可以指定不同的缩放算法aclError acldvppVpcCropResizePasteAsync(acldvppChannelDesc *channelDesc, acldvppPicDesc *inputDesc, acldvppPicDesc *outputDesc, acldvppRoiConfig *cropArea, acldvppRoiConfig *pasteArea, acldvppResizeConfig *resizeConfig, aclrtStream stream)
效果展示:1024x688 =》 416x416
// 定义一些全局变量namespace { std::string aclConfig = "../src/acl.json"; const std::string image_path = "../data/dog2.jpg"; const std::string image_save_path = "../out/dog2.jpg"; int32_t deviceId = 0; aclrtContext context; aclrtStream stream; aclrtRunMode runMode_ = ACL_DEVICE; uint32_t model_width = 416; // 模型需要的图像宽 uint32_t model_height = 416; // 模型需要的图像高 uint32_t image_width = 0; // 图像的真实宽 uint32_t image_height = 0; // 图像的真实高 acldvppChannelDesc *dvppChannelDesc = nullptr;}
开始进行解码操作
// 将图片读入内存,如果运行模式为(ACL_HOST)模式,需要申请host侧内存存放我们的原始图片数据,然后申请相同大小的device内存拷贝过去。如果运行在(ACL_DEVICE)模式,例如200DK这样的场景,直接申请device内存,不需要进行内存的拷贝搬运。// 经过读取图片的步骤我们应该能获得如下变量void* JpegImagePtr; // 原始图像在device侧的内存地址(不管运行在那种方式下的昇腾软件栈都是如此)uint32_t JpegImageSize; // 原始图像的大小// 我们不了解图像的编码以及真实宽高,还有颜色通道数,所以需要执行获取图像信息的接口// 图像的原始宽高变量我们前面已经定义过, 所以我们现在只需要定义图像的通道数与编码方式还有图像解码后所占用的内存的大小int32_t components; // 图像的通道数acldvppJpegFormat format; // 图像的编码方式uint32_t JpegdOutSize;void* JpegdOutPtr;aclError ret = acldvppJpegGetImageInfoV2(JpegImagePtr, JpegImageSize, &image_width, &height, &components, &format)aclError ret = acldvppJpegPredictDecSize(JpegImagePtr, JpegImageSize, format, &JpegdOutSize)// 申请存放解码后YUV图像的内存aclError ret = acldvppMalloc(&JpegdOutPtr, JpegdOutSize);// 设置JPEG解码后图像的描述信息(注意JPEG解码后内存宽高对齐到128x16)acldvppPicDesc* JpegdOutputPicDesc = acldvppCreatePicDesc();acldvppSetPicDescData(out_pic_desc, JpegdOutPtr);acldvppSetPicDescSize(out_pic_desc, JpegdOutSize);acldvppSetPicDescFormat(out_pic_desc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);acldvppSetPicDescWidth(out_pic_desc, image_width);acldvppSetPicDescHeight(out_pic_desc, image_height);acldvppSetPicDescWidthStride(out_pic_desc, ALIGN_UP128(image_width));acldvppSetPicDescHeightStride(out_pic_desc, ALIGN_UP16(image_height));// 执行解码接口ret = acldvppJpegDecodeAsync(dvppChannelDesc, JpegImagePtr, JpegImageSize, JpegdOutputPicDesc, stream);ret = aclrtSynchronizeStream(stream_);
开始进行抠图的操作,因为我们这次需要执行抠图+缩放+贴图三个动作所以我们选择组合API进行编程
// 首先声明原图抠图的区域// 抠图的区域左偏移和上偏移都是偶数// 抠图的区域右偏移和下偏移都是奇数uint32_t left = 0;uint32_t top = 0;uint32_t right = image_width % 2 == 0 ? image_width-1 : image_width;uint32_t bottom = image_height % 2 == 0 ? image_height-1 : image_height;acldvppRoiConfig* cropArea = cropConfig = acldvppCreateRoiConfig(left, right, top, bottom);// 计算等比例缩放后的新的宽高(等比例缩放后可能不满足16x2的对齐规则,需要手动进行对齐的操作)void LargeSizeAtLeast(uint32_t W, uint32_t H, uint32_t &newInputWidth, uint32_t &newInputHeight){ INFO_LOG("W:%u H:%u nw:%u, nh:%u", W, H, newInputWidth, newInputHeight); float scaleRatio = 0.0; float inputWidth = 0.0; float inputHeight = 0.0; float resizeMax = 0.0; bool maxWidthFlag = false; inputWidth = (float)W; inputHeight = (float)H; resizeMax = (float)(416); maxWidthFlag = (W >= H) ? true : false; if (maxWidthFlag == true) { newInputWidth = resizeMax; scaleRatio = resizeMax / W; // 高度2对齐 newInputHeight = scaleRatio * H; newInputHeight = ALIGN_UP2(newInputHeight); INFO_LOG("scaleRatio: %.3f, modelInputWidth: %u, modelInputHeight: %d, newInputWidth: %d, newInputHeight: %d", scaleRatio, W, H, newInputWidth, newInputHeight); } else { scaleRatio = resizeMax / H; // 如果高度是长边,建议宽度在等比例缩放后再做一次16对齐。因为vpc在输出时宽有16字节对齐约束,当贴图的宽非16对齐时,会导致在贴图的时候, // 芯片会自动进行16字节对齐,导致每次写入数据的时候都会引入部分无效数据,从而导致精度下降。 newInputWidth = scaleRatio * W; newInputWidth = ALIGN_UP16(newInputWidth); newInputHeight = resizeMax; INFO_LOG("scaleRatio: %.3f, modelInputWidth: %u, modelInputHeight: %d, newInputWidth: %d, newInputHeight: %d", scaleRatio, W, H, newInputWidth, newInputHeight); }}// 设置贴图的范围左偏移要求16对齐acldvppRoiConfig *InitVpcOutConfig(uint32_t width, uint32_t height, uint32_t modelInputWidth, uint32_t modelInputHeight){ uint32_t right = 0; uint32_t bottom = 0; uint32_t left = 0; uint32_t top = 0; uint32_t left_stride; acldvppRoiConfig *cropConfig; uint32_t small = width < height ? width : height; uint32_t padded_size_half; if (small == width) { padded_size_half = (modelInputWidth - width) / 2; // 贴图区域距离左边界的距离 left = padded_size_half; left_stride = ALIGN_UP16(left); right = (left_stride + width) % 2 == 0 ? (left_stride + width - 1) : (left_stride + width); if (left_stride + right > modelInputWidth) { while (true) { left_stride = left_stride - 16; right = (left_stride + width) % 2 == 0 ? (left_stride + width - 1) : (left_stride + width); if (left_stride + right < modelInputWidth) break; } } right = (left_stride + width) % 2 == 0 ? (left_stride + width - 1) : (left_stride + width); bottom = (modelInputHeight % 2 == 0 ? modelInputHeight - 1 : modelInputHeight); top = bottom - height + 1; } else { padded_size_half = (modelInputHeight - height) / 2; right = (modelInputWidth % 2 == 0 ? modelInputWidth - 1 : modelInputWidth); left = right + 1 - width; left_stride = ALIGN_UP16(left); top = (padded_size_half % 2 == 0 ? padded_size_half : padded_size_half + 1); bottom = (height + top - 1) % 2 == 0 ? (height + top - 2) : (height + top - 1); } INFO_LOG("left_stride=%d, right=%d, top=%d, bottom=%d
", left_stride, right, top, bottom); cropConfig = acldvppCreateRoiConfig(left_stride, right, top, bottom); if (cropConfig == nullptr) { ERROR_LOG("acldvppCreateRoiConfig failed"); return nullptr; } return cropConfig;}// 设置最后完成等比例缩放后的图像的信息void* output_ptr = nullptr;uint32_t output_size = YUV420SP_SIZE(ALIGN_UP16(model_width), ALIGN_UP2(model_height));ret = acldvppMalloc(&output_ptr, output_size); // 申请模板内存if (ret != ACL_SUCCESS) {ERROR_LOG("acl malloc output is failed");}ret = aclrtMemset(output_ptr, output_size, 128, output_size);if (ret != ACL_SUCCESS) {ERROR_LOG("mem set 128 is failed");}acldvppPicDesc *output_Desc = acldvppCreatePicDesc();acldvppSetPicDescData(output_Desc, output_ptr);acldvppSetPicDescSize(output_Desc, output_size);acldvppSetPicDescFormat(output_Desc, PIXEL_FORMAT_YUV_SEMIPLANAR_420);acldvppSetPicDescWidth(output_Desc, model_width);acldvppSetPicDescHeight(output_Desc, model_height);acldvppSetPicDescWidthStride(output_Desc, ALIGN_UP16(model_width));acldvppSetPicDescHeightStride(output_Desc, ALIGN_UP2(model_height));// 执行裁剪加贴图的操作ret = acldvppVpcCropAndPasteAsync(dvppChannelDesc, JpegdOutputPicDesc, output_Desc, cropArea, pasteArea, stream);
完成了一系列的操作
点击下方,第一时间了解华为云新鲜技术~
华为云博客_大数据博客_AI博客_云计算博客_开发者中心-华为云
留言与评论(共有 0 条评论) “” |