首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >[python]tensorrt安装后测试代码

[python]tensorrt安装后测试代码

作者头像
git clone firc-dataset
发布于 2025-07-17 11:11:43
发布于 2025-07-17 11:11:43
4630
举报
测试环境:

windows x64

tensorrt==8.6.1.6

cuda==11.8

cudnn==8.9.7

学习内容

1. ONNX模型格式介绍

  • 定义: Open Neural Network Exchange (ONNX) 是一个用于表示深度学习模型的开放标准格式。
  • 用途: 它允许模型在不同的深度学习框架之间进行转换和共享。
  • 文件结构: ONNX模型通常保存在一个.onnx文件中,这个文件包括了模型的结构(层、操作等)和参数(权重和偏置)。

2. 如何将PyTorch、TensorFlow模型转换为ONNX

  • PyTorch: 使用torch.onnx.export()函数。
  • TensorFlow: 使用tf2onnx.convert或TensorFlow的SavedModel格式和ONNX转换器。

3. 使用TensorRT优化器进行初步优化

  • 步骤: 导入ONNX模型、创建一个TensorRT builder和网络,然后使用这个builder和网络进行模型优化。
实操项目:模型转换与初步优化

将一个简单的PyTorch图像分类模型转换为ONNX格式

代码示例(Python):

代码语言:javascript
复制
import torch
import torchvision.models as models


# Initialize model
model = models.resnet18(pretrained=True)
model.eval()


# Dummy input
x = torch.randn(1, 3, 224, 224)


# Export to ONNX format
torch.onnx.export(model, x, "resnet18.onnx", verbose=True)
  • 执行步骤:
    • 将上述代码保存为export_pytorch_to_onnx.py。

在命令行中运行:

代码语言:javascript
复制
python export_pytorch_to_onnx.py 
    • 查看生成的resnet18.onnx文件。

使用TensorRT进行模型优化,并进行初步的推理测试

代码示例(Python):

代码语言:javascript
复制
import os
import pycuda.autoinit
import pycuda.driver as cuda
import tensorrt as trt
import torch
import time
from PIL import Image
import cv2, os
import torchvision
import numpy as np
from scipy.special import softmax

### get_img_np_nchw h和postprocess_the_output函数根据需要进行修改
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
TRT_LOGGER = trt.Logger()


def softmax(out_np, dim):
    s_value = np.exp(out_np) / np.sum(np.exp(out_np), axis=dim, keepdims=True)
    return s_value


def get_img_np_nchw(img_path):
    image = Image.open(img_path).convert('RGB')
    image = np.asarray(image, dtype='float32')
    image = cv2.resize(np.array(image), (224, 224), interpolation=cv2.INTER_CUBIC)
    img_in = np.transpose(image, (2, 0, 1)).astype(np.float32)  # (3, 224 224)
    img_in /= 255.0  # 归一化[0, 1]
    miu = np.array([0.485, 0.456, 0.406]).reshape(3, 1, 1)
    std = np.array([0.229, 0.224, 0.225]).reshape(3, 1, 1)
    img_in -= miu
    img_in /= std
    img_in = img_in[np.newaxis]
    image = np.tile(img_in, (max_batch_size, 1, 1, 1))
    return image


class HostDeviceMem(object):
    def __init__(self, host_mem, device_mem):
        """host_mom指代cpu内存,device_mem指代GPU内存
        """
        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__()


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 get_engine(max_batch_size=1, onnx_file_path="", engine_file_path="", fp16_mode=False, int8_mode=False,
               save_engine=False):
    """
    params max_batch_size:      预先指定大小好分配显存
    params onnx_file_path:      onnx文件路径
    params engine_file_path:    待保存的序列化的引擎文件路径
    params fp16_mode:           是否采用FP16
    params int8_mode:           是否采用INT8
    params save_engine:         是否保存引擎
    returns:                    ICudaEngine
    """
    # 如果已经存在序列化之后的引擎,则直接反序列化得到cudaEngine
    if os.path.exists(engine_file_path):
        print("Reading engine from file: {}".format(engine_file_path))
        with open(engine_file_path, 'rb') as f, \
                trt.Runtime(TRT_LOGGER) as runtime:
            return runtime.deserialize_cuda_engine(f.read())  # 反序列化
    else:  # 由onnx创建cudaEngine

        # 使用logger创建一个builder
        # builder创建一个计算图 INetworkDefinition
        explicit_batch = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
        # In TensorRT 7.0, the ONNX parser only supports full-dimensions mode, meaning that your network definition must be created with the explicitBatch flag set. For more information, see Working With Dynamic Shapes.

        with trt.Builder(TRT_LOGGER) as builder, \
                builder.create_network(explicit_batch) as network, \
                trt.OnnxParser(network, TRT_LOGGER) as parser, \
                builder.create_builder_config() as config:  # 使用onnx的解析器绑定计算图,后续将通过解析填充计算图
            profile = builder.create_optimization_profile()
            # profile.set_shape("input", (1, 3, 224, 224),(4,3,224,224),(8,3,224,224))
            # config.add_optimization_profile(profile)

            config.max_workspace_size = 1 << 28  # 预先分配的工作空间大小,即ICudaEngine执行时GPU最大需要的空间
            builder.max_batch_size = max_batch_size  # 执行时最大可以使用的batchsize
            #builder.fp16_mode = fp16_mode
            # 解析onnx文件,填充计算图
            if not os.path.exists(onnx_file_path):
                quit("ONNX file {} not found!".format(onnx_file_path))
            print('loading onnx file from path {} ...'.format(onnx_file_path))
            # with open(onnx_file_path, 'rb') as model: # 二值化的网络结果和参数
            #     print("Begining onnx file parsing")
            #     parser.parse(model.read())  # 解析onnx文件
            parser.parse_from_file(onnx_file_path)  # parser还有一个从文件解析onnx的方法

            print("Completed parsing of onnx file")
            # 填充计算图完成后,则使用builder从计算图中创建CudaEngine
            print("Building an engine from file{}' this may take a while...".format(onnx_file_path))

            #################
            # import pdb;pdb.set_trace()
            print(network.get_layer(network.num_layers - 1).get_output(0).shape)
            # network.mark_output(network.get_layer(network.num_layers -1).get_output(0))
            engine = builder.build_engine(network, config)  # 注意,这里的network是INetworkDefinition类型,即填充后的计算图
            print("Completed creating Engine")
            if save_engine:  # 保存engine供以后直接反序列化使用
                with open(engine_file_path, 'wb') as f:
                    f.write(engine.serialize())  # 序列化
            return engine


def do_inference(context, bindings, inputs, outputs, stream, batch_size=1):
    # Transfer data from CPU to the GPU.
    [cuda.memcpy_htod_async(inp.device, inp.host, stream) for inp in inputs]
    # Run inference.
    context.execute_async(batch_size=batch_size, 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]


def postprocess_the_outputs(outputs, shape_of_output):
    outputs = outputs.reshape(*shape_of_output)
    out = np.argmax(softmax(outputs, axis=1)[0, ...], axis=0)
    # import pdb;pdb.set_trace()
    return out


# 验证TensorRT模型是否正确
onnx_model_path = 'resnet18.onnx'
max_batch_size = 1
img_path = 'data/bus.jpg'

image = get_img_np_nchw(img_path)
fp16_mode = False

trt_engine_path = 'resnet18.engine'
# Build an engine
engine = get_engine(max_batch_size, onnx_model_path, trt_engine_path, fp16_mode)
# Create the context for this engine
context = engine.create_execution_context()

# Allocate buffers for input and output
inputs, outputs, bindings, stream = allocate_buffers(engine)  # input, output: host # bindings

# Do inference
inputs[0].host = image.reshape(-1)
shape_of_output = (max_batch_size, 3)
# inputs[1].host = ... for multiple input
t1 = time.time()
trt_outputs = do_inference(context, bindings=bindings, inputs=inputs, outputs=outputs, stream=stream)  # numpy data
t2 = time.time()
# feat = postprocess_the_outputs(trt_outputs[0], shape_of_output)
labels_sm = softmax(trt_outputs, dim=1)
print(labels_sm)
labels_max = np.argmax(labels_sm, axis=1)
print(labels_max)
print('TensorRT ok')
  • 执行步骤:
    • 将上述代码保存为optimize_with_tensorrt.py。

在命令行中运行:

代码语言:javascript
复制
python optimize_with_tensorrt.py 
    • 如果一切正常,你应该看到输出“TensorRT ok”
常见问题与解决方案
    1. ONNX模型导出失败:确保PyTorch版本与ONNX版本兼容。
    2. 检查模型是否在推理模式下(model.eval())。
    3. TensorRT优化失败:确保已分配足够的工作空间:builder.max_workspace_size。
    4. 检查ONNX模型是否包含TensorRT不支持的操作或层。

完整代码:https://download.csdn.net/download/FL1623863129/90392065

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-02-16,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 测试环境:
  • 学习内容
  • 实操项目:模型转换与初步优化
  • 常见问题与解决方案
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档