ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Ubuntu下 u2net tensorrt模型部署

Ubuntu下 u2net tensorrt模型部署 TensorRT系列之 Windows10下yolov8 tensorrt模型加速部署TensorRT系列之 Linux下 yolov8 tensorrt模型加速部署TensorRT系列之 Linux下 yolov7 tensorrt模型加速部署TensorRT系列之 Linux下 yolov6 tensorrt模型加速部署TensorRT系列之 Linux下 yolov5 tensorrt模型加速部署TensorRT系列之 Linux下 yolox tensorrt模型加速部署TensorRT系列之 Linux下 u2net tensorrt模型加速部署更多点我进去…文章目录ubuntu下u2net tensorrt模型部署一、Ubuntu18.04环境配置1.1 安装工具链和opencv1.2 安装Nvidia相关库1.2.1 安装Nvidia显卡驱动1.2.2 安装 cuda11.31.2.3 安装 cudnn8.21.2.4 下载 tensorrt8.4.2.41.2.5 下载仓库TensorRT-Alpha并设置二、从u2net源码中导出onnx文件三、利用tensorrt编译onnx模型四、编译执行u2net-tensorrt工程五、结束语ubuntu下u2net tensorrt模型部署U-2-Net是一种基于显著对象检测SOD的卷积神经网络其核心思想是探索比场景或图像周围区域更专注的物体或区域因此非常适合于做抠图应用。这种算法主要利用由AlexNetVGGResNetResNeXtDenseNet等骨干网络提取的深度特征进行显著物体检测。U-2-Net在设计上进行了一些创新。首先它采用了编码器-解码器的结构这种结构在许多分割模型中都有广泛应用如U-Net。其次U-2-Net在编码器和解码器之间添加了跳跃连接skip connection即在每个编码器阶段都会将对应的特征图与解码器中的相应特征图相加。这种跳跃连接有助于将编码器的低级特征与解码器的高级特征相结合从而提高分割的准确性。U-2-Net的应用场景非常广泛除了显著对象检测外还可以应用于生物医学图像分割、语义分割等方向。由于U-2-Net具有较好的分割性能和较低的计算复杂度因此在实际应用中具有较高的实用价值。u2net对物体分割的边缘细节把控非常到位如下图是检测效果。本文提供u2net-tensorrt加速方法。有源码有源码有源码不要慌哈哈哈。一、Ubuntu18.04环境配置如果您对tensorrt不是很熟悉请务必保持下面库版本一致。请注意: Linux系统安装以下库务必去进入系统bios下关闭安全启动(设置 secure boot 为 disable)1.1 安装工具链和opencvsudoapt-getupdatesudoapt-getinstallbuild-essentialsudoapt-getinstallgitsudoapt-getinstallgdbsudoapt-getinstallcmakesudoapt-getinstalllibopencv-dev# pkg-config --modversion opencv1.2 安装Nvidia相关库注Nvidia相关网站需要注册账号。1.2.1 安装Nvidia显卡驱动ubuntu-drivers devicessudoadd-apt-repository ppa:graphics-drivers/ppasudoaptupdatesudoaptinstallnvidia-driver-470-server# for ubuntu18.04nvidia-smi1.2.2 安装 cuda11.3进入链接: https://developer.nvidia.com/cuda-toolkit-archive选择CUDA Toolkit 11.3.0(April 2021)选择[Linux] - [x86_64] - [Ubuntu] - [18.04] - [runfile(local)]在网页你能看到下面安装命令我这里已经拷贝下来wgethttps://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.runsudoshcuda_11.3.0_465.19.01_linux.runcuda的安装过程中需要你在bash窗口手动作一些选择这里选择如下select[continue] - [accept] - 接着按下回车键取消Driver和465.19.01这个选项如下图(it is important!) - [Install]bash窗口提示如下表示安装完成## Summary ##Driver: Not Selected#Toolkit: Installed in /usr/local/cuda-11.3/#......把cuda添加到环境变量vim~/.bashrc把下面拷贝到 .bashrc里面# cuda v11.3exportPATH/usr/local/cuda-11.3/bin${PATH::${PATH}}exportLD_LIBRARY_PATH/usr/local/cuda-11.3/lib64${LD_LIBRARY_PATH::${LD_LIBRARY_PATH}}exportCUDA_HOME/usr/local/cuda-11.3刷新环境变量和验证source~/.bashrc nvcc-Vbash窗口打印如下信息表示cuda11.3安装正常nvcc: NVIDIA(R)Cuda compiler driverbrCopyright(c)2005-2021 NVIDIA CorporationbrBuilt on Sun_Mar_21_19:15:46_PDT_2021brCuda compilation tools, release11.3, V11.3.58brBuild cuda_11.3.r11.3/compiler.29745058_0br1.2.3 安装 cudnn8.2进入网站https://developer.nvidia.com/rdp/cudnn-archive选择 Download cuDNN v8.2.0 (April 23rd, 2021), for CUDA 11.x选择 cuDNN Library for Linux (x86_64)你将会下载这个压缩包: “cudnn-11.3-linux-x64-v8.2.0.53.tgz”# 解压tar-zxvfcudnn-11.3-linux-x64-v8.2.0.53.tgz将cudnn的头文件和lib拷贝到cuda11.3的安装目录下sudocpcuda/include/cudnn.h /usr/local/cuda/include/sudocpcuda/lib64/libcudnn* /usr/local/cuda/lib64/sudochmodar /usr/local/cuda/include/cudnn.hsudochmodar /usr/local/cuda/lib64/libcudnn*1.2.4 下载 tensorrt8.4.2.4本教程中tensorrt只需要下载\、解压即可不需要安装。进入网站 https://developer.nvidia.com/nvidia-tensorrt-8x-download把这个打勾 I Agree To the Terms of the NVIDIA TensorRT License Agreement选择: TensorRT 8.4 GA Update 1选择: TensorRT 8.4 GA Update 1 for Linux x86_64 and CUDA 11.0, 11.1, 11.2, 11.3, 11.4, 11.5, 11.6 and 11.7 TAR Package你将会下载这个压缩包: “TensorRT-8.4.2.4.Linux.x86_64-gnu.cuda-11.6.cudnn8.4.tar.gz”# 解压tar-zxvfTensorRT-8.4.2.4.Linux.x86_64-gnu.cuda-11.6.cudnn8.4.tar.gz# 快速验证一下tensorrtcudacudnn是否安装正常cdTensorRT-8.4.2.4/samples/sampleMNISTmakecd../../bin/导出tensorrt环境变量(it is important!)注将LD_LIBRARY_PATH:后面的路径换成你自己的后续编译onnx模型的时候也需要执行下面第一行命令exportLD_LIBRARY_PATH$LD_LIBRARY_PATH:/home/xxx/temp/TensorRT-8.4.2.4/lib ./sample_mnistbash窗口打印类似如下图的手写数字识别表明cudacudnntensorrt安装正常1.2.5 下载仓库TensorRT-Alpha并设置gitclone https://github.com/FeiYull/tensorrt-alphagitfetch origingitcheckout legacy设置您自己TensorRT根目录:gitclone https://github.com/FeiYull/tensorrt-alphagitfetch origingitcheckout legacycdtensorrt-alpha/cmakevimcommon.cmake# 在文件common.cmake中的第20行中设置成你自己的目录别和我设置一样的路径eg:# set(TensorRT_ROOT /root/TensorRT-8.4.2.4)二、从u2net源码中导出onnx文件可以直接从网盘下载onnx文件[weiyun]:weiyun or google driver 你也可以自己下载仓库然后按照下面指令手动导出onnx文件:# 下载u2net源码https://github.com/xuebinqin/U-2-Net安装 u2net环境cdU-2-Net-master pipinstall-rrequirements.txt在u2net官方git页面下载pth格式模型你将得到文件u2net.pth和u2netp.pth其中u2netp.pth是小模型。然后使用tensorrt-alpha中提供的python脚本导出onnx脚本路径TensorRT-Alpha/u2net/alpha_export.py具体导出指令如下cpalpha_export.py U-2-Net-master python alpha_export.py--netu2net--weightssaved_models/u2net/u2net.pth python alpha_export.py--netu2netp--weightssaved_models/u2netp/u2netp.pth三、利用tensorrt编译onnx模型将你的onnx模型放到这个路径:tensorrt-alpha/data/u2net。cdtensorrt-alpha/data/u2net# 设置环境变量exportLD_LIBRARY_PATH$LD_LIBRARY_PATH:~/TensorRT-8.4.2.4/lib使用以下命令编译onnx模型# put your onnx file in this path:tensorrt-alpha/data/u2netcdtensorrt-alpha/data/u2netexportLD_LIBRARY_PATH$LD_LIBRARY_PATH:~/TensorRT-8.4.2.4/lib../../../../TensorRT-8.4.2.4/bin/trtexec--onnxu2net.onnx--saveEngineu2net.trt--buildOnly--minShapesimages:1x3x320x320--optShapesimages:4x3x320x320--maxShapesimages:8x3x320x320../../../../TensorRT-8.4.2.4/bin/trtexec--onnxu2netp.onnx--saveEngineu2netp.trt--buildOnly--minShapesimages:1x3x320x320--optShapesimages:4x3x320x320--maxShapesimages:8x3x320x320四、编译执行u2net-tensorrt工程使用命令行编译下代码gitclone https://github.com/FeiYull/tensorrt-alphagitfetch origingitcheckout legacycdtensorrt-alpha/u2netmkdirbuildcdbuild cmake..make-j10按照需求执行推理支持推理一张图片、在线推理视频文件或者在线从摄像头获取视频流并推理。## 320# infer image./app_u2net--model../../data/u2net/u2net.trt--size320--batch_size1--img../../data/sailboat3.jpg--show--savePath# infer video./app_u2net--model../../data/u2net/u2net.trt--size320--batch_size2--video../../data/people.mp4--show# infer camera./app_u2net--model../../data/u2net/u2net.trt--size320--batch_size2--cam_id0--show例如以下是u2net 部署后小模型的分割人像效果。再看一组发丝级别的分割效果五、结束语都看到这里了觉得可以请点赞收藏有条件的去仓库点个star仓库https://github.com/FeiYull/TensorRT-Alpha/tree/legacy
返回列表