win yolov5.7 tensorRT部署推理

安装TensorRT

下载tensorrt8.xx版本,适用于Windows的cuda11.x的版本


把tensorRT里面的bin、include、lib添加到本机CUDA中,CUDA需要加入环境变量中

 配置虚拟环境

torch的版本要和CUDA一致,

CUDA是11.7,

torch:                  torch-2.0.0+cu117-cp39-cp39-win_amd64.whl

torchvision:         torchvision-0.15.0+cu117-cp39-cp39-win_amd64.whl

  找到TensorRT-8.6.1.6\python/python/tensorrt-8.6.1-cp39-none-win_amd64.whl

pip install nvidia-pyindex 
pip install tensorrt-8.6.1-cp39-none-win_amd64.whl

pip install -r requirements.txt

在YOLOv5.7的export.py中修改要转换的模型和导出格式

 编译opencv:

Windows 下编译 OpenCV 和 OpenCV-contrib_windows编译opencv-CSDN博客

【VS2019+OpenCV4.5.1+OpenCV_contrib4.5.1安装+配置保姆式教程】_Jay_z在造梦的博客-CSDN博客

在debug下报错:错误    LNK1104    无法打开文件“python310_d.lib”环境中没有安装python的debug版本,换成release环境。

GitHub - Monday-Leo/Yolov5_Tensorrt_Win10: A simple implementation of tensorrt yolov5 python/c++🔥icon-default.png?t=N7T8https://github.com/Monday-Leo/Yolov5_Tensorrt_Win10

报错: No module named ‘utils‘,untils库的安装,不是pip和conda指令,特别可以的教程,还有为什么不能的原因,绝对良心。_no module named 'utils-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要了解 YOLOv5.7 分割网络的后处理过程。YOLOv5.7 分割网络的输出结果是一个张量,包含了每个像素点属于每个类别的概率,以及每个像素点对应的 bounding box 的位置信息。后处理过程主要包括以下几个步骤: 1. 对每个像素点的概率进行阈值处理,得到每个类别的二值图像。 2. 对每个类别的二值图像进行连通域分析,得到每个连通域的 bounding box。 3. 对每个 bounding box 进行非极大值抑制(NMS),去除重叠的 bounding box。 接下来是使用 C++ 和 CUDA 的代码实现过程: 1. 阈值处理 ``` __global__ void threshold_kernel(float* input, float* output, int size, float threshold) { int tid = blockIdx.x * blockDim.x + threadIdx.x; if (tid < size) { output[tid] = (input[tid] >= threshold) ? 1.0f : 0.0f; } } void threshold(float* input, float* output, int size, float threshold) { int block_size = 256; int grid_size = (size + block_size - 1) / block_size; threshold_kernel<<<grid_size, block_size>>>(input, output, size, threshold); } ``` 2. 连通域分析 ``` __global__ void connected_components_kernel(float* input, int* labels, int width, int height) { int tid = blockIdx.x * blockDim.x + threadIdx.x; if (tid < width * height) { int x = tid % width; int y = tid / width; int label = 0; if (input[tid] == 1.0f && labels[tid] == 0) { label++; labels[tid] = label; int idx = tid; while (idx >= 0) { int left = idx % width == 0 ? -1 : idx - 1; int right = idx % width == width - 1 ? -1 : idx + 1; int up = idx < width ? -1 : idx - width; int down = idx >= width * (height - 1) ? -1 : idx + width; if (left >= 0 && input[left] == 1.0f && labels[left] == 0) { labels[left] = label; idx = left; continue; } if (right >= 0 && input[right] == 1.0f && labels[right] == 0) { labels[right] = label; idx = right; continue; } if (up >= 0 && input[up] == 1.0f && labels[up] == 0) { labels[up] = label; idx = up; continue; } if (down >= 0 && input[down] == 1.0f && labels[down] == 0) { labels[down] = label; idx = down; continue; } break; } } } } void connected_components(float* input, int* labels, int width, int height) { int size = width * height; int block_size = 256; int grid_size = (size + block_size - 1) / block_size; int* d_labels; cudaMalloc(&d_labels, size * sizeof(int)); cudaMemcpy(d_labels, labels, size * sizeof(int), cudaMemcpyHostToDevice); connected_components_kernel<<<grid_size, block_size>>>(input, d_labels, width, height); cudaMemcpy(labels, d_labels, size * sizeof(int), cudaMemcpyDeviceToHost); cudaFree(d_labels); } ``` 3. 非极大值抑制 ``` __global__ void nms_kernel(float* input, float* output, int width, int height, int num_classes, float threshold) { int tid = blockIdx.x * blockDim.x + threadIdx.x; if (tid < num_classes * width * height) { int c = tid / (width * height); int i = tid % (width * height); int x = i % width; int y = i / width; int idx = y * width + x; float score = input[tid]; if (score >= threshold) { bool keep = true; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dx == 0 && dy == 0) { continue; } int nx = x + dx; int ny = y + dy; if (nx >= 0 && nx < width && ny >= 0 && ny < height) { int nidx = ny * width + nx; float nscore = input[c * width * height + nidx]; if (score < nscore) { keep = false; break; } } } if (!keep) { break; } } if (keep) { output[c * width * height + idx] = score; } } } } void nms(float* input, float* output, int width, int height, int num_classes, float threshold) { int size = num_classes * width * height; int block_size = 256; int grid_size = (size + block_size - 1) / block_size; nms_kernel<<<grid_size, block_size>>>(input, output, width, height, num_classes, threshold); } ``` 以上就是使用 C++ 和 CUDA 的代码实现 YOLOv5.7 分割网络的后处理过程的方法。需要注意的是,这些代码只是实现了基本的功能,具体的实现需要根据具体的场景进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值