基于tensorRT-Alpha,部署YOLOV8模型

github项目;
https://github.com/FeiYull/TensorRT-Alpha

tensorrt部署yolov8的流程;;
https://www.cnblogs.com/feiyull/p/17066486.html

环境:
>>> print(torch.__version__)
2.1.1+cu118
>>> print(torchvision.__version__)
0.16.1+cu118

cuda:11.3
tensorrt 的版本为8.4.2.4;;
opencv==4.5.5

'''
针对新场景下训练模型,利用tensorrt加速;需要修改的模块;
'''

# 全过程语句
conda activate py310
## 第一步.训练

## 第2步..pt转onnx
cd ${YOLO_PROJ}/ultralytics
run pt2onnx.py
# 文件里onnx输出位置就在.pt同一目录下

## 第三步.onnx转tensorRT engine
# 设置tensorRT的环境变量,最好用export设置这个窗口的临时环境变量,不要在系统的~/.bashrc和/cat/profile里添加永久环境变量,
export PATH=${TENSORRT_PATH}/TensorRT-8.4.2.4/bin:$PATH
export LD_LIBRARY_PATH=${TENSORRT_PATH}/TensorRT-8.4.2.4/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=${TENSORRT_PATH}/TensorRT-8.4.2.4/lib:$LIBRARY_PATH

cd ${TENSORRT_ALPHA_PATH}/tensorrt_alp/yolov8/build2
# 将onnx文件复制到编译目录下
trtexec --onnx=best.onnx  --saveEngine=best.trt


## 第4步: 在目录下编译运行

#需要修改可执行文件的头文件,修改目录如下:
$ cd tensorrt-alpha/yolov8

##第5步:修改文件
$ vim app_yolov8.cpp
#修改第6行的类别名,改成自己训练集的类别名,例如:
initParameters.class_names = utils::dataSets::mask2;
#修改第10行的类别数,改成自己训练集的类别数,例如:
initParameters.num_class = 2;

#第6步:
#修改用到的头文件及函数
$ cd tensorrt-alpha/untils
$ vim untils.h

#修改untils.h文件中的第37行,换成自己的类别,如:
const std::vector<std::string> mask2 = {"non-mask", "mask"};
#在untils.h文件的第80行之后,添加自己的类别颜色,如:
const std::vector<cv::Scalar> color2{
            cv::Scalar(148, 80, 189),cv::Scalar(105, 52, 238)
        };

#第7步:
#修改untils.cpp文件  
$ vim untils.cpp

#修改untils.cpp文件中的show函数,在第170行后添加自己类别对应的代码,如:
if (classNames.size() == 2) // mask2
{
    color = Colors::color2[box.label];
}
#修改untils.cpp文件中的save函数,在第224行后添加自己类别对应的代码,如:
if (classNames.size() == 2) // mask2
{
    color = Colors::color2[box.label];
}
#第8步;
修改好以后,进行编译
cmake ..
make -j
./app_yolov8  --model=newbest0402.trt  --size=640 --batch_size=1  --img='raw.jpg'   --savePath='res0407.jpg'


#常见错误;
  The CUDA compiler

    "/usr/bin/nvcc"

  is not able to compile a simple test program.

#解答;
#需要在cmakelists.txt文件当中添加cuda路径;并且前置;;
set(CMAKE_CUDA_COMPILER "/usr/local/cuda-11.3/bin/nvcc")

#以下位置 具体位置代码为:

${TENSORRT_PATH}/tensorrt_alp/yolov8

cat CMakeLists.txt 

cmake_minimum_required(VERSION 3.10)

#set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CUDA_COMPILER "/usr/local/cuda-11.3/bin/nvcc")
PROJECT(yolov8 VERSION 1.0.0 LANGUAGES C CXX CUDA)

CMake Error at /home/sniper/anaconda3/envs/yolov8/lib/python3.8/site-packages/cmake/data/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message): The C compiler "/usr/bin/gcc" is not able to compile a simple test program. It fails with the following output: Change Dir: /opt/projects/tensorrt-alpha/yolov8/build/CMakeFiles/CMakeScratch/TryCompile-TrXrKM Run Build Command(s):/usr/bin/make -f Makefile cmTC_7d457/fast && /usr/bin/make -f CMakeFiles/cmTC_7d457.dir/build.make CMakeFiles/cmTC_7d457.dir/build make[1]: 进入目录“/opt/projects/tensorrt-alpha/yolov8/build/CMakeFiles/CMakeScratch/TryCompile-TrXrKM” Building C object CMakeFiles/cmTC_7d457.dir/testCCompiler.c.o /usr/bin/gcc --sysroot=/usr/ -march=x86-64 -o CMakeFiles/cmTC_7d457.dir/testCCompiler.c.o -c /opt/projects/tensorrt-alpha/yolov8/build/CMakeFiles/CMakeScratch/TryCompile-TrXrKM/testCCompiler.c Linking C executable cmTC_7d457 /home/sniper/anaconda3/envs/yolov8/lib/python3.8/site-packages/cmake/data/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7d457.dir/link.txt --verbose=1 /usr/bin/gcc --sysroot=/usr/ -march=x86-64 -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,--disable-new-dtags -Wl,--gc-sections -Wl,-rpath,/home/sniper/anaconda3/lib -Wl,-rpath-link,/home/sniper/anaconda3/lib -L/home/sniper/anaconda3/lib CMakeFiles/cmTC_7d457.dir/testCCompiler.c.o -o cmTC_7d457 /usr/bin/ld: 找不到 /lib/x86_64-linux-gnu/libc.so.6 于 /usr/ 内部 /usr/bin/ld: 找不到 /usr/lib/x86_64-linux-gnu/libc_nonshared.a 于 /usr/ 内部 /usr/bin/ld: 找不到 /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 于 /usr/ 内部 collect2: error: ld returned 1 exit status make[1]: *** [CMakeFiles/cmTC_7d457.dir/build.make:99:cmTC_7d457] 错误 1 make[1]: 离开目录“/opt/projects/tensorrt-alpha/yolov8/build/CMakeFiles/CMakeScratch/TryCompile-TrXrKM” make: *** [Makefile:127:cmTC_7d457/fast] 错误 2 CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:25 (PROJECT) -- Configuring incomplete, errors occurred!
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值