Jetson Nano( 五) TensorRT yolov4 yolov4-tiny yolov5 实测

TensorRT yolov4 yolov4-tiny yolov5 长期更新

软硬件环境

Jetson Nano 4G
JP 4.4.1
CUDA 10.2
TensorRT 7.1.3.0
Pytorch 1.6.0

参考大佬项目

https://github.com/enazoe/yolo-tensorrt作者封装了模型的转换
https://github.com/wang-xinyu/tensorrtx
https://github.com/jkjung-avt/tensorrt_demos

简要流程

使用模型框架不同主要使用了pytorch和darknet。

Darknet → ONNX → TensorRT
PyTorch → ONNX/WTS → TensorRT

注意要点

1.CMakelist.txt中TensorRT链接地址。
2.CMakelist.txt中CUDA链接地址。
3.CUDA TensorRT环境变量。
4.Yolov4/v4-tiny 中cfg中batch设置(batch 1或4)。
5.Yolov5 版本和PyTorch版本的对应(Yolov5 v3.1 pytorch 1.6)。

操作流程

只要环境正常,操作上不复杂,使用大佬的封装好了的方法。

git clone https://github.com/enazoe/yolo-tensorrt.git
cd yolo-tensorrt/
mkdir build
cd build/
cmake ..
make
./yolo-trt

注意:目前主版本支持的yolov5为3.0版本,编译之前修改samples/sample_detector.cpp内需要推理的模型如下:
在这里插入图片描述

附上yolov4-tiny运行图片,大约推理耗时36—40ms左右:
在这里插入图片描述
在这里插入图片描述

记录遇坑

在这里插入图片描述
本人把下面nvcc目录添加进/etc/environment文件下,就可以了。

CUDACXX=/usr/local/cuda-10.2/bin/nvcc

推理速度记录

系统模型框架输入精度时间(ms)
JP4.3yolov5s v1.0PyTorch 1.4416*41660~80
JP4.4.1yolov4Tensorrt 7.1.3.0416*416FP16280
JP4.4.1yolov4Tensorrt 7.1.3.0416*416FP32380
JP4.4.1yolov4-tiny(batch 1)Tensorrt 7.1.3.0416*416FP1645
JP4.4.1yolov4-tiny(batch 4)Tensorrt 7.1.3.0416*416FP1638
JP4.4.1yolov5s v3.1Tensorrt 7.1.3.0608*608FP16110~130
JP4.4.1yolov5s v6.0Tensorrt 7.1.3.0608*608FP1670-78
JP4.4.1yolov5n v6.0Tensorrt 7.1.3.0608*608FP1635-47
JP4.4.1yolov5s v3.1PyTorch 1.6608*608170~210
JP4.4.1yolov5s v6.0PyTorch 1.8608*608140~175
JP4.4.1yolov5s v3.1PyTorch 1.6416*416120~140

不知道为何yolov5s v1.0版本直接在nano JP4.3上能跑100ms以内,如有有了解的大佬请告知一下哈。

  • 7
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
检测教程 1. 安装依赖库 我们需要在Jetson Nano上安装一些必要的依赖库,包括OpenCV、numpy和TensorRT。可以使用以下命令进行安装: ``` sudo apt-get update sudo apt-get install libopencv-dev python3-opencv python3-numpy ``` 安装TensorRT的过程较为复杂,可以参考NVIDIA的官方文档进行安装:https://docs.nvidia.com/deeplearning/tensorrt/install-guide/index.html 2. 下载模型 我们需要下载YoloV7模型并将其转换为TensorRT模型。模型可以从GitHub上的以下链接中下载: https://github.com/WongKinYiu/yolov7 我们可以使用以下命令将模型转换为TensorRT模型: ``` python3 yolov7_to_onnx.py --model yolov7.pt --output yolov7.onnx --input-size 608 608 python3 onnx_to_tensorrt.py --model yolov7.onnx --output yolov7.engine --input-shape 3 3 608 608 --max-batch-size 1 --fp16 ``` 以上命令将模型从PyTorch模型转换为ONNX模型,然后将ONNX模型转换为TensorRT模型。在转换过程中,我们可以指定输入图像的大小、最大批量大小和精度等参数。 3. 编写代码 我们可以使用以下代码来加载TensorRT模型并使用摄像头进行实时检测: ```python import cv2 import numpy as np import time import tensorrt as trt import pycuda.driver as cuda import pycuda.autoinit # 加载TensorRT模型 engine_file_path = 'yolov7.engine' with open(engine_file_path, 'rb') as f: engine_data = f.read() runtime = trt.Runtime(trt.Logger(trt.Logger.WARNING)) engine = runtime.deserialize_cuda_engine(engine_data) context = engine.create_execution_context() # 定义输入和输出张量 input_binding = [] output_binding = [] for i in range(engine.num_bindings): if engine.binding_is_input(i): input_shape = engine.get_binding_shape(i) input_dtype = engine.get_binding_dtype(i) input_size = trt.volume(input_shape) * engine.max_batch_size * np.dtype(input_dtype).itemsize input_array = cuda.mem_alloc(input_size) input_binding.append({'engine_idx': i, 'array': input_array, 'shape': input_shape, 'dtype': input_dtype}) else: output_shape = engine.get_binding_shape(i) output_dtype = engine.get_binding_dtype(i) output_size = trt.volume(output_shape) * engine.max_batch_size * np.dtype(output_dtype).itemsize output_array = cuda.mem_alloc(output_size) output_binding.append({'engine_idx': i, 'array': output_array, 'shape': output_shape, 'dtype': output_dtype}) # 定义预处理和后处理函数 def preprocess(image): image = cv2.resize(image, (input_shape[3], input_shape[2])) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = image.transpose((2, 0, 1)).astype(np.float32) image /= 255.0 return image def postprocess(outputs, conf_th=0.5, iou_th=0.5): class_ids = [] confidences = [] boxes = [] for output in outputs: output = output.reshape((-1, 85)) for o in output: class_id = np.argmax(o[5:]) confidence = o[4] * o[class_id + 5] if confidence > conf_th: cx, cy, w, h = o[:4] x1 = int((cx - w / 2) * input_shape[3]) y1 = int((cy - h / 2) * input_shape[2]) x2 = int((cx + w / 2) * input_shape[3]) y2 = int((cy + h / 2) * input_shape[2]) class_ids.append(class_id) confidences.append(confidence) boxes.append([x1, y1, x2, y2]) indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_th, iou_th) results = [] for i in indices: i = i[0] class_id = class_ids[i] confidence = confidences[i] box = boxes[i] results.append((class_id, confidence, box)) return results # 打开摄像头并进行检测 cap = cv2.VideoCapture(0) while True: ret, image = cap.read() image = preprocess(image) cuda.memcpy_htod_async(input_binding[0]['array'], image, cuda.Stream()) context.execute_async_v2(bindings=[int(i['array']) for i in input_binding] + [int(i['array']) for i in output_binding], stream_handle=cuda.Stream()) outputs = [cuda.memcpy_dtoh_async(output['array'], cuda.Stream()) for output in output_binding] results = postprocess(outputs) for result in results: class_id, confidence, box = result x1, y1, x2, y2 = box cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(image, f'{class_id}: {confidence:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.imshow('image', image) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() ``` 在代码中,我们首先使用TensorRT API加载了TensorRT模型,并定义了输入和输出张量。然后,我们定义了预处理和后处理函数,用于将输入图像转换为模型需要的格式,并将模型输出转换为物体检测结果。最后,我们使用OpenCV打开摄像头并进行实时检测,将检测结果显示在屏幕上。 4. 运行程序 保存代码文件并在Jetson Nano上运行它。程序将打开摄像头并实时检测摄像头中的物体。可以使用以下命令运行程序: ``` python3 detect.py ``` 注意,在首次运行程序时,由于需要编译TensorRT模型,程序可能会需要一些时间来启动。运行程序后,可以通过按下“q”键来退出程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值