Jetson nano TensorRT环境搭建(jetson-inference)

TensorRT

使用TensorRT

Jetson Nano的官方文档中给我们推荐了二个例子,其中一个使用Tensor RT做物品识别的例子。具体的可以参考英伟达jetson-inference例子。跑通这个例子需要的模型就大概1G以上,所以这个例子的大部分并没有放到SD卡上(SD卡上只有运行这个模型所需要的TensorRT)。悲剧的是存放这些模型的服务器被墙了,所以只能将之前下载好的包远程传输到对应的下载目录下。
首先如果您没有安装git和cmake,先安装它们

sudo apt-get install git cmake

接着从git上克隆jetson-inference 库

git clone https://github.com/dusty-nv/jetson-inference

进入文件夹jetson-inference

cd jetson-inference

当使用git clone下来的工程中带有submodule时,初始的时候,submodule的内容并不会自动下载下来的,此时,只需执行如下命令:git submodule update --init --recursive,即可将子模块内容下载下来后工程才不会缺少相应的文件。

git submodule update --init  

配置cmake,cmake的介绍可以参考我的博文。这里我没有用科学上网的方式下载模型,我直接把模型用FileZilla远程传入nano。操作如下:
1)编辑jetson-inference/CMakePrebuild.sh。把./download-models.sh注释掉,(前面加个#注释)
在这里插入图片描述

2)把模型远程传输到data/networks目录。
在这里插入图片描述
新建build文件夹,用来存放编译文件

mkdir build    #创建build文件夹

进入

cd build       #进入build

运行cmake

cmake ../      #运行cmake,它会自动执行上一级目录下面的 CMakePrebuild.sh

然后在此目录执行解压:

for tar in *.tar.gz; do tar xvf $tar; done

在这里插入图片描述
cmake成功后,就需要编译了
进入build文件夹

cd jetson-inference/build	

开始编译

make (或者make -j4)    //注意:(在build目录下)
// 这里的 make 不用 sudo
// 后面 -j4 使用 4 个 CPU 核同时编译,缩短时间

如果编译成功,会生成下列文件夹结构
在这里插入图片描述

|-build
   \aarch64		    (64-bit)
      \bin			where the sample binaries are built to
      \include		where the headers reside
      \lib			where the libraries are build to
   \armhf           (32-bit)
      \bin			where the sample binaries are built to
      \include		where the headers reside
      \lib			where the libraries are build to

测试

cd jetson-inference/build/aarch64/bin
./imagenet-console ./images/bird_0.jpg output_wyk.jpg

执行等待许久后出现如下(第一次需要很长时间,后面执行就会很快):
在这里插入图片描述

识别结果

当然nano中的文件也可以传到pc上查看
在这里插入图片描述
更多学习请看官方文档:
官方Demo
官方TensorRT教程

Jetson Nano上使用TensorRT加速Yolov5的推理可以按照以下步骤进行: 1. 安装TensorRT和Yolov5:首先确保你已经在Jetson Nano上安装了JetPack SDK,该包中包含了TensorRT和CUDA等必要的组件。你可以从NVIDIA官方网站下载并安装JetPack SDK。然后,你可以从Yolov5的GitHub页面获取Yolov5的代码。 2. 将Yolov5模型转换为TensorRT引擎:在Yolov5代码的根目录下,有一个`yolov5s.yaml`文件,它定义了模型的结构和超参数。你可以使用`convert.py`脚本将模型转换为TensorRT引擎。具体命令如下: ``` python convert.py --weights yolov5s.pt --cfg yolov5s.yaml --output yolov5s.engine ``` 这将生成一个名为`yolov5s.engine`的TensorRT引擎文件。 3. 编写推理代码:使用TensorRT引擎进行推理,可以使用Jetson Inference库。首先,确保你已经在Jetson Nano上安装了Jetson Inference库。然后,创建一个新的Python文件,并添加以下代码: ```python import ctypes import numpy as np import cv2 import jetson.inference import jetson.utils ctypes.CDLL('libnvinfer_plugin.so', mode=ctypes.RTLD_GLOBAL) engine_path = 'yolov5s.engine' input_width = 640 input_height = 640 # 加载TensorRT引擎 trt_yolov5 = jetson.inference.detectNet(engine_path, threshold=0.5) # 加载输入图像 input_image = jetson.utils.loadImage('input.jpg') input_image = jetson.utils.cudaFromNumpy(input_image) # 设置网络的输入尺寸 trt_yolov5.SetInputWidth(input_width) trt_yolov5.SetInputHeight(input_height) # 进行目标检测 detections = trt_yolov5.Detect(input_image, input_width, input_height) # 处理检测结果 for detection in detections: class_name = trt_yolov5.GetClassDesc(detection.ClassID) print(f'Object: {class_name}, Confidence: {detection.Confidence:.2f}') left = int(detection.Left) top = int(detection.Top)
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值