Jetson学习笔记(三):多种模型文件的调用部署

简介: 文章介绍了如何在Jetson平台上使用torch2trt和onnx2trt工具来部署和调用TensorRT模型。

在这里插入图片描述

1.torch2trt–trt模型调用

通过torch2trt的官方代码找到加载这个trt文件封装好了的函数TRTModule,可直接通过model_trt.load_state_dict(torch.load(‘mode.trt’))得到。

from torch import TRTModule
engine_path='./trt模型地址'
def read_model():
    model_trt=TRTModule()
    model_trt.load_State_dict(torch.load(engine_path))
    return model_trt

2.onnx2trt–trt模型调用

import pycuda.driver as cuda
import pycuda.autoinit
import cv2,time
import numpy as np
import os
import tensorrt as trt

TRT_LOGGER = trt.Logger()
engine_file_path = "/home/z/Documents/face_detect_yolov4_yolov4tiny_ssd-master/yolov4-tiny.trt"

class HostDeviceMem(object):
    def __init__(self, host_mem, device_mem):
        self.host = host_mem
        self.device = device_mem

    def __str__(self):
        return "Host:\n" + str(self.host) + "\nDevice:\n" + str(self.device)

    def __repr__(self):
        return self.__str__()
# Allocates all buffers required for an engine, i.e. host/device inputs/outputs. 分配引擎所需的所有缓冲区
def allocate_buffers(engine):
    inputs = []
    outputs = []
    bindings = []
    stream = cuda.Stream()
    for binding in engine:
        size = trt.volume(engine.get_binding_shape(binding)) * engine.max_batch_size
        dtype = trt.nptype(engine.get_binding_dtype(binding))
        # Allocate host and device buffers
        host_mem = cuda.pagelocked_empty(size, dtype)
        device_mem = cuda.mem_alloc(host_mem.nbytes)
        # Append the device buffer to device bindings.
        bindings.append(int(device_mem))
        # Append to the appropriate list.
        if engine.binding_is_input(binding):
            inputs.append(HostDeviceMem(host_mem, device_mem))
        else:
            outputs.append(HostDeviceMem(host_mem, device_mem))
    return inputs, outputs, bindings, stream

def do_inference_v2(context, bindings, inputs, outputs, stream):
    # Transfer input data to the GPU.
    [cuda.memcpy_htod_async(inp.device, inp.host, stream) for inp in inputs]
    # Run inference.
    context.execute_async_v2(bindings=bindings, stream_handle=stream.handle)
    # Transfer predictions back from the GPU.
    [cuda.memcpy_dtoh_async(out.host, out.device, stream) for out in outputs]
    # Synchronize the stream
    stream.synchronize()
    # Return only the host outputs.
    return [out.host for out in outputs]

with open(engine_file_path, "rb") as f, trt.Runtime(TRT_LOGGER) as runtime,\
runtime.deserialize_cuda_engine(f.read()) as engine, engine.create_execution_context() as context:
    inputs, outputs, bindings, stream = allocate_buffers(engine)
    #print('Len of inputs:',len(inputs))
    #print('Len of outputs:',len(outputs))

    image = cv2.imread('4.jpg',cv2.IMREAD_GRAYSCALE)
    image=cv2.resize(image,(28,28))
    print(image.shape)
    image=image[np.newaxis,np.newaxis,:,:].astype(np.float32)
    inputs[0].host = image
    print('开始推理')
    start = time.time()
    trt_outputs =do_inference_v2(context, bindings=bindings, \
        inputs=inputs, outputs=outputs, stream=stream)
    finish = time.time()
    #print('inference time {} sec'.format(finish - start))
    print(trt_outputs)
目录
相关文章
|
计算机视觉 Python
Jetson 学习笔记(六):cv2调用CSI摄像头(jetson nx/nano)、打开海康摄像头、打开电脑摄像头
这篇文章介绍了在不同平台上接入并显示摄像头视频流的方法,包括海康摄像头的RTSP连接、电脑内置摄像头的直接读取、Jetson NX/Nano通过CSI接口和USB接口的操作,以及Jetson Nano通过Gstreamer管道和jetcam库的使用,并提供了相应的代码示例。
1690 1
|
存储 传感器 自动驾驶
几种常见的点云格式数据解析与在线预览
3D模型在线转换网站支持pcd、pts、xyz、las、laz、asc、ply等点云格式文件在线预览,同时支持将点云格式在线转换为ply、xyz等模型格式。
9021 1
|
小程序 安全 网络协议
Nginx配置小程序域名(HTTPS
Nginx配置小程序域名(HTTPS
Nginx配置小程序域名(HTTPS
Jetson学习笔记(二):TensorRT 查看模型的输入输出
这篇博客介绍了如何使用TensorRT查看模型的输入输出,并通过代码示例展示了如何获取和验证模型的输入输出信息。
795 5
|
4月前
|
缓存 API 数据库
Python性能优化利器:lru_cache装饰器详解
Python性能优化利器:lru_cache装饰器详解
|
PyTorch 算法框架/工具
Jetson学习笔记(四):pth(torch模型文件)转trt(tensorrt引擎文件)实操
关于如何使用torch2trt工具将PyTorch模型转换为TensorRT引擎文件的实操指南。
942 1
Jetson学习笔记(四):pth(torch模型文件)转trt(tensorrt引擎文件)实操
|
监控 Java Linux
Jetson 学习笔记(十二):CSI摄像头实现rtsp流的传输并对动态获取多路流进行探索
本文是关于如何在Jetson设备上使用CSI摄像头实现RTSP流传输的详细教程,包括安装依赖、编译gst-rtsp-server、测试、源代码介绍以及如何动态获取多路流的RTSP服务器。
1660 2
Jetson 学习笔记(十二):CSI摄像头实现rtsp流的传输并对动态获取多路流进行探索
|
机器学习/深度学习 PyTorch 算法框架/工具
深度学习之格式转换笔记(一):模型文件pt转onnx转tensorrt格式实操成功
关于如何将深度学习模型从PyTorch的.pt格式转换为ONNX格式,然后再转换为TensorRT格式的实操指南。
3522 1
深度学习之格式转换笔记(一):模型文件pt转onnx转tensorrt格式实操成功
|
编解码 Ubuntu 应用服务中间件
Jetson 环境安装(三):jetson nano配置ffmpeg和nginx(亲测)
本文介绍了在NVIDIA Jetson Nano上配置FFmpeg和Nginx的步骤,包括安装、配置和自启动设置。
955 1
Jetson 环境安装(三):jetson nano配置ffmpeg和nginx(亲测)
|
并行计算 Ubuntu 开发工具
Jetson学习笔记(一):jetson 系列镜像下载、烧写、设置散热风扇、中文包、pip、中转英目录、软件源、显示CSI摄像头
关于NVIDIA Jetson系列设备的入门学习笔记,涵盖了从下载镜像、烧录、设置散热风扇、安装中文语言包、配置环境变量、安装CUDA和OpenCV,到显示CSI摄像头和增加Swap交换空间的详细步骤。
2334 0
Jetson学习笔记(一):jetson 系列镜像下载、烧写、设置散热风扇、中文包、pip、中转英目录、软件源、显示CSI摄像头