CV-CUDA NVIDIA GPU前后处理库入门

CV-CUDA分类和分割任务中数据加载与预处理介绍

CV-CUDA是NVIDIA开发出的一个用于图像预处理的库,将大部分的预处理和后处理都迁移到GPU上进行,提高对输入输出图像的处理效率,目前该库才刚开源不久,本文使用的是v0.2.1这个版本。官方声明所提供的CV-CUDA的相关处理函数与Opencv相一致。Yeah!

导入相关的python库

  • samples中classification代码分析:
import torch    
import torchnvjpeg   # 加速图像JPEG解码预处理
import cvcuda

注意:记得在加载torchnvjpeg之前导入torch的包,不然会报某些动态库找不到的错。

如之前出现的问题

- ImportError: libc10.so: cannot open shared object file: No such file or directory
- 说明:libc10.so是基于pytorch生成的,因此需要先导入torch包,然后再导入依赖于torch的包

使用torchnvjpeg包进行数据解码:

# Decode in batch using torchnvjpeg decoder on the GPU.
decoder = torchnvjpeg.Decoder(
    0,
    0,
    True,
    self.device_id,
    effective_batch_size,
    8,  # this is max_cpu_threads parameter. Not used internally.
    max_image_size,
    torch.cuda.current_stream(self.device_id),
)
image_tensor_list = decoder.batch_decode(data_batch)

图像分类预处理相关步骤(Preprocessing)

Preprocessing includes the following sequence of operations.
Resize -> DataType Convert(U8->F32) -> Normalize
(Apply mean and std deviation) -> Interleaved to Planar

从官方的图像处理步骤上看,cvcuda针对的数据格式为"NHWC",而非pytorch的"NCHW"

cvcuda的预处理主要包括resize,数据类型转换,标准化(均值和方差)

(1)把数据转换为张量(image_tensors);

(2)cvcuda加载tensor数据 cvcuda_input_tensor = cvcuda.as_tensor(image_tensors, "NHWC")

(3)数据尺度一致化:cvcuda_resize_tensor = cvcuda.resize(cvcuda_input_tensor, (batch_size, resize_img_height, resize_img_width, channels), cvcuda.Interp.LINEAR)

  • 第一个参数要求输入cvcuda.as_tensor转换得到的张量;
  • 第二个参数是一个数据格式(元组)要求:(批数据大小,输入图像高度,输入图像宽度,输入图像通道数);
  • 第三个参数是resize图像的插值方式。

(4)uint8图像数据类型转换convert:cvcuda_convert_tensor = cvcuda.convertto(cvcuda_resize_tensor, np.float32, scale=1/255)
将resize之后的图像cvcuda_resize_tensor进行convert,图像类型要求使用numpy数据类型,对图像尺度进行归一化操作(即乘上1/255)

(5)对数据进行标准化

A 定义均值张量和方差张量:

mean_tensor = torch.Tensor([0.485, 0.456, 0.406])   # [R, G, B]
std_tensor = torch.Tensor([0.229, 0.224, 0.225])

B 对均值和方差张量进行维度变换,并将张量数据迁移到GPU上

mean_tensor = torch.reshape(mean_tensor, (1, 1, 1, 3)).cuda()
std_tensor = torch.reshape(std_tensor, (1, 1, 1, 3)).cuda()

C 转换为CVCUDA 张量

cvcuda_mean_tensor = cvcuda.as_tensor(mean_tensor, "NHWC")
cvcuda_std_tensor = cvcuda.as_tensor(std_tensor, "NHWC")

D 应用标准化

cvcuda_norm_tensor = cvcuda.normalize(cvcuda_convert_tensor, 
						base=cvcuda_mean_tensor,
						scale=cvcuda_scale_tensor,
						flags=cvcuda.NormalizeFlags.SCALE_IS_STDDEV)

cvcuda.NormalizeFlags.SCALE_IS_STDDEV代表所使用的标准化操作模式
注意上述使用的张量格式都是[N, H, W, C]

(6)张量维度变换

cvcuda_preprocessed_tensor = cvcuda.reformat(cvcuda_norm_tensor, "NCHW")

 # Run inference on the preprocessed input
torch_preprocessed_tensor = torch.as_tensor(cvcuda_preprocessed_tensor.cuda(), device="cuda")

模型推理相关步骤(Inference)

使用pytorch搭建的模型,这里将模型定义为model。

(1)torch张量化数据

torch_preprocessed_tensor = torch.as_tensor(cvcuda_preprocessed_tensor.cuda(), device="cuda")

(2)模型推理

with torch.no_grad():
	infer_input = model(torch_preprocessed_tensor)

samples中segmentation代码分析:(支持pytorch和tensorrt)

from trt_utils import convert_onnx_to_tensorrt, setup_tensort_bindings  # noqa: E402
from vpf_utils import nvencoder, nvdecoder  # noqa: E402 用于处理视频数据

与分类的操作步骤相似,只是在处理视频数据时,需要使用vpf对视频数据以一定帧数切分为图像。

(1)示例中直接使用了pytorch的分割模型(model),这里不做过多介绍。

(2)所有的预处理操作与上述图像分类预处理相关步骤大同小异。

cvcuda中其他的图像处理API:

(1)cvcuda.gaussian(cvcuda_input_tensor, kernel_size=(27, 27), sigma=(25, 25))

  • cvcuda_input_tensor:由cvcuda加载tensor数据得到 [N, H, W, C]

  • kernel_size:高斯核的大小

  • sigma:核参数大小

注意:输入网络前需进行维度转换和放到cuda上,具体代码操作如下:

cvcuda_blurred_input_imgs = cvcuda.reformat(cvcuda_blurred_input_imgs, "NCHW")
blurred_input_imgs = torch.as_tensor(cvcuda_blurred_input_imgs.cuda(), device=torch.device("cuda", self.device_id), )

Reference

[1] https://github.com/itsliupeng/torchnvjpeg

[2] https://github.com/CVCUDA/CV-CUDA

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IRevers

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值