VisionWorks学习之OpenVX到VPI的迁移


本文主要介绍一下openVX迁移到VPI的一些注意事项,主要包含一些数据结构,以及如何初始化这些数据结构。数据结构包含object,images,pyramids,arrays

第一部分 Data Object Differences

OpenVx objects是半透明的,应用程序可以获取对不透明数据对象的引用,openvx实现管理object memory (数据内容可以在给定时间驻留在系统中的任何位置),并且显式地请求的对象的数据内容可以直接访问。
VPI objects是以C结构体的方式对外展示的。应用程序分配和初始化object控制结构和Cuda内存,应用程序管理 CPU-GPU之间的同步。
下面显示VX-VPI数据类型对应关系。
在这里插入图片描述
下面看一下VPI API 复杂数据对象的方法

Data ObjectDescription
nvxcu__t是基类,包含一些共有的信息
nvxcu_<object> _<property>_t子类,我们要实例化的就是这些,

cv基元函数使用指向基结构的指针作为参数。不需要前缀/后缀原语函数,因为单个函数可以支持同一类的多种类型的数据对象。原语很容易扩展。说白了就是定义的函数使用的参数都是父类,使用的时候可以传入子类作为参数。
例如:

<primitive>(nvxcu_<object>_t *input, nvxcu_<object>_t *output)

VPI Images

下面来看一下nvxcu_image_t这个基类里面包含哪些共有的数据参数:

参数描述
image_type指定image变体类型
format指定image类型
width,height指定图像的大小

nv_cv_pitch_linear_image_t是使用pitch linear的cuda buffer实现的一个image,有以下成员:

成员描述
baseThe base image structure (nvxcu_image_t)base.image.type must be NVXCU_PITCH_LINEAR_IMAGE.
planesPlan descriptor array. Must be consistent with base.format.
dev_ptrCUDA buffer的指针
pitch_in_bytesSpecifies the pitch of the CUDA buffer, in bytes.

nvxcu__uniform_image_t实现所有像素值相同的图像。它有以下成员:

MemberDescription
baseThe base image structure (nvxcu_image_t). base.image.type must be NVXCU_UNIFORM_IMAGE.
uniform_valueSpecifies the value of the pixel (nvxcu_pixel_value_t). Must be consistent with base.format.

创建一个Image

使用openVX创建:

vx_image im = vxCreateImage(context, 1920,1080, VX_DF_IMAGE_U8);

使用VPI创建一个image:

void *dev_ptr = NULL;
size_t pitch =0;
cudaMallocPitch(&dev_ptr,&pitch, 1920*sizeof(uint8_t),1080);
nvxcu_pitch_linear_image_t image;
image.base.image_type = NVXCU_PITCH_LINEAR_IMAGE;
image.base.format = NVXCU_DF_IMAGE_U8;
image.base.width = 1920;
image.base.height = 1080;
image.planes[0].dev_ptr = dev_ptr;
image.planes[0].pitch_in_bytes = pitch;

看一下VPI创建还是比较麻烦的,不过为了效率也不要怕麻烦,毕竟人家快。

image使用示例

下面比较OpenVX和VPI之间的图像使用情况OpenVX中的图像使用

vx_image in = vxCreateImage(...);
vx_image_out = vxCreateImage(...);
vxuBox3x3(context, in, out);

使用VPI使用image usage

nvxcu_pitch_linear_image_t in = { ... } ; 
nvxcu_pitch_linear_image_t out = { ... } ; 
nvxcu_border_t border_mode = { ... } ; 
nvxcu_stream_exec_target_t target =  { ... } ;
nvxcuBox3x3(&in.base, &out.base, &border_mode, &target.base);

VPI pyramids

nvxcu_pyramid_t 包含以下参数:

ParameterDescription
pyramid_type指定金字塔变体类型.
num_levels指定 金字塔有几层.
scale指定层级间的缩放因子.

nvxcu_pitch_linear_pyramid_t 使用pitch linear的cuda buffer实现的一个pyramid.

MemberDescription
baseThe base pyramid structure (nvxcu_pyramid_t). base.pyramid_type must be NVXCU_PITCH_LINEAR_PYRAMID.
levelsA pointer to an array of base.num_levels image descriptors of type nvxcu_pitch_linear_image_t. Images must be consistent in terms of type and dimensions. levels[0] is the base of the pyramid (largest dimension).

创建一个pyramid

使用openVX创建一个pyramid

vx_pyramid pyr = vxCreatePyramid(context, num_levels, VX_SCALE_PYRAMID_HALF, 1920, 1080, VX_DF_IMAGE_U8);

使用VPI创建一个VPI:

nvxcu_pitch_linear_pyramid_t pyr;
pyr.base.pyramid_type = NVXCU_PITCH_LINEAR_PYRAMID;
pyr.base.num_levels = num_levels;
pyr.base.scale = NVXCU_SCALE_PYRAMID_HALF;
pyr.levels = malloc(num_levels * sizeof(nxcu_pitch_linear_image_t));
uint32_t cur_width = width, cur_height = height; float cur_scale = NVXCU_SCALE_PYRAMID_HALF;
for (uint32_t i = 0; i < num_levels; ++i) {
    cudaMallocPitch(&pyr.levels[i].planes[0].dev_ptr,
                &pyr.levels[i].planes[0].pitch_in_bytes,
                cur_width * sizeof(uint8_t), cur_height);
    pyr.levels[i].base.image_type = NVXCU_PITCH_LINEAR_IMAGE;
pyr.levels[i].base.format = NVXCU_DF_IMAGE_U8;
pyr.levels[i].base.width = cur_width;
pyr.levels[i].base.height = cur_height;
    cur_scale *= pyr.base.scale; 
cur_width = (uint32_t)ceilf(width * cur_scale);
cur_height = (uint32_t)ceilf(height * cur_scale);
}

VPI arrays

nvxcu_array_t is the base image structure. It includes the following parameters:

ParameterDescription
array_typeSpecifies the variant of the array.
item_typeSpecifies the type of elements in the array.
capacitySpecifies the maximum number of elements in the array.

nvxcu_plan_array_t array implementation uses plain linear CUDA memory. It has the following members:

MemberDescription
baseThe base array structure (nvxcu_array_t). base.array_type must be NVXCU_PLAIN_ARRAY.
dev_ptrA pointer to a CUDA buffer that can store at least base.capacity elements.
num_items_dev_ptrA pointer to the element counter in CUDA memory.

创建一个array

使用openVX:

vx_array array = vxCreateArray(context, NVX_TYPE_POINT2F, 1000);

使用VPI:

void * dev_ptr = NULL;
cudaMalloc(&dev_ptr, 1000 * sizeof(nvxcu_point2f_t));
uint32_t * num_items_dev_ptr = NULL;
cudaMalloc((void **)&num_items_dev_ptr, sizeof(uint32_t));
cudaMemset(num_items_dev_ptr, 0, sizeof(uint32_t));
nvxcu_plain_array_t array;
array.base.array_type = NVXCU_PLAIN_ARRAY;
array.base.item_type = NVXCU_TYPE_POINT2F;
array.base.capacity = 1000;
array.dev_ptr = dev_ptr;
array.num_items_dev_ptr = num_items_dev_ptr;

第二部分 OpenVX和VPI Primitives

提供一个OpenVX和VPI执行模型,target assignments和border mode

执行模型比较

下面看一下OpenVX和VPI执行模型的比较

OpenVX

openVX提供两个可选的执行模型。

  1. 立即执行模式
    立即模式有一个类似于opencv的同步执行模型。这一切都是在运行时完成的,包括临时内存分配。
vx_status status = vxu<Primitive>(context, <params>);
  1. Graph模式
    在图形模式下,先把如参数检查、内存分配和优化等先于执行。它执行异步节点执行和同步图形执行。
// Ahead of time
vx_graph vxCreateCraph(context);
vx_node node = vx<Primitive>Node(graph, <params>);
vx_status verif_status = vxVerifyGraph(graph);
// Data process time
vx_status exec_status = vxProcessGraph(graph);

VPI模式

VPI执行模型包含三步:

  1. 查询临时内存中的需求(仅限复杂原语)。
nvxcu_tmp_buf_size_t tmp_size;
tmp_size = nvxcu<Primitive> GetBuff(<param metadata>, const struct cudaDeviceProp*);
  1. 分配临时内存这可以是CUDA和主机内存,并且可以提前完成
nvxcu_tmp_buf_t tmp_buf = {NULL, NULL};
cudaMalloc(&tmp_buf.dev_ptr, tmp_size.dev_buf_size);
cudaMallocHost(&tmp_buf.host_ptr, tmp_size.host_buf_size);
  1. 异步原语执行
nvxcu_tmp_buf_size_t tmp_size;
tmp_size = nvxcu<Primitive>(<params, including tmp bufs>);

VPI执行模型例子

nvxcu_border_t              border = { ... } ; 
nvxcu_stream_exec_target_t  target = { ... } ;
// Query for needed temporary memory
nvxcu_tmp_buf_size_t gauss_pyr_buf_size_ = 
          nvxcuGaussianPyramid_GetBufSize(width, height, nb_levels, &border, &exec_target_.dev_prop);
// Allocate required buffers
nvxcu_tmp_buf_t tmp_buf = {NULL, NULL};
if (tmp_size.dev_buf_size > 0)
    cudaMalloc(&tmp_buf.dev_ptr, tmp_size.dev_buf_size);
if (tmp_size.host_buf_size > 0)
   cudaMallocHost(&tmp_buf.host_ptr, tmp_size.host_buf_size)
// Process data
nvxcu_pitch_linear_pyramid_t pyr = { /* must be width x height with nb_levels*/ }
nvxcuGaussianPyramid(&pyr.base, tmp_buf, &border, &exec_target_.base) );
// Synchronize the stream to get results
cudaStreamSynchronize(exec_target_.stream)

目标分配比较

Border Mode 比较

openvx中的目标分配是可选的,默认情况下是自动分配的。您可以使用以下选项手动将原语分配给GPU或CPU:

vxSetNodeTarget, vxSetImmediateModeTarget(...)

VPI中的运行在哪个设备上是固定好的,必须在每个基元执行调用(nvxcu_exec_target_t参数)处提供目标CUDA流

VPI 目标指定

nvxcu_exec_target_t是基本执行目标结构,包括以下参数:

ParameterDescription
exec_target_type这是当前支持的唯一CUDA流目标。

nvxcu_stream_exec_target_t array实现使用普通线性CUDA内存它包括下列成员:

MemberDescription
baseThe base target structure (nvxcu_exec_target_t). base.exec_target_type must be NVXCU_STREAM_EXEC_TARGET.
streamSpecifies the CUDA stream.
dev_propSpecifies the CUDA device property for the stream (cudaDeviceProp).
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JetPack 4.5是Linux for Tegra (L4T)软件套件的一个版本,特别适用于NVIDIA的Jetson系列嵌入式计算平台。该软件套件是一个开发工具包,为开发者提供了构建、部署和优化AI、机器学习和计算机视觉应用程序所需的一切工具和库。 JetPack 4.5基于Linux for Tegra (L4T) R32.5版本,L4T是一个优化的嵌入式操作系统,专为NVIDIA的Tegra ARM处理器设计。它提供了一个稳定和高效的平台,用于在NVIDIA Jetson平台上开发和运行各种应用程序。 JetPack 4.5引入了一些重要的更新和改进。首先,它集成了CUDA 10.2、cuDNN 8.0等重要的深度学习和机器学习库,这为用户开发和部署高性能的AI应用程序提供了更好的支持。 其次,JetPack 4.5引入了TensorRT 7.1.3,这是一个可用于优化和加速深度学习推理的重要工具。TensorRT可将训练好的深度学习模型转换为高效的推理模型,从而在嵌入式设备上实现低延迟的高性能推理。 此外,JetPack 4.5还提供了各种开发工具和示例代码,帮助用户更轻松地构建和优化他们的应用程序。它还支持嵌入式视觉库(EGLStream和VisionWorks)以及多个传感器的接口,如CSI相机和IMU传感器。 总之,JetPack 4.5是一个功能强大的嵌入式开发工具包,为开发者提供了丰富的资源和工具,以便更轻松地构建和优化他们的AI和机器学习应用程序。同时,它也为用户提供了一个稳定和高效的平台,用于在Jetson嵌入式计算平台上开发和部署应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值