Pytorch多batch导出ONNX模型

b, h, w, c = model.shape
str_w = str(w)
str_h = str(h)
str_c = str(c)
dynamic_axes = {'input': {0: 'batch', 1: str_h, 2: str_w, 3: str_c}}                   
torch.onnx.export(model,  							# model being run
                  x,  								# model input (or a tuple for multiple inputs)
                  "model.onnx",  					# where to save the model (can be a file or file-like object)
                  export_params=True,  				# store the trained parameter weights inside the model file
                  opset_version=11,  				# the ONNX version to export the model to
                  do_constant_folding=True, 	    # whether to execute constant folding for optimization
                  input_names=['input'],  			# the model's input names
                  output_names=['output'],  		# the model's output names
                  dynamic_axes=dynamic_axes)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pytorch 模型导出ONNXTensorRT 格式的具体步骤如下: ### 导出ONNX 格式 1. 安装 onnx 包:`pip install onnx` 2. 加载 Pytorch 模型并将其转换为 ONNX 模型: ```python import torch import torchvision import onnx # 加载 Pytorch 模型 model = torchvision.models.resnet18(pretrained=True) # 转换为 ONNX 模型 dummy_input = torch.randn(1, 3, 224, 224) input_names = ["input"] output_names = ["output"] onnx_path = "resnet18.onnx" torch.onnx.export(model, dummy_input, onnx_path, verbose=True, input_names=input_names, output_names=output_names) ``` 3. 导入 ONNX 模型: ```python import onnx # 加载 ONNX 模型 onnx_path = "resnet18.onnx" model = onnx.load(onnx_path) ``` ### 导出TensorRT 格式 1. 安装 TensorRT 并设置环境变量: ```python # 安装 TensorRT !pip install nvidia-pyindex !pip install nvidia-tensorrt # 设置 TensorRT 环境变量 import os os.environ["LD_LIBRARY_PATH"] += ":/usr/local/cuda/lib64:/usr/lib/x86_64-linux-gnu" ``` 2. 加载 Pytorch 模型并将其转换为 TensorRT 模型: ```python import tensorrt as trt import pycuda.driver as cuda import torch import torchvision # 加载 Pytorch 模型 model = torchvision.models.resnet18(pretrained=True) # 转换为 TensorRT 模型 TRT_LOGGER = trt.Logger(trt.Logger.WARNING) trt_runtime = trt.Runtime(TRT_LOGGER) with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.OnnxParser(network, TRT_LOGGER) as parser: builder.max_workspace_size = 1 << 30 builder.max_batch_size = 1 # 加载 ONNX 模型 onnx_path = "resnet18.onnx" with open(onnx_path, "rb") as f: parser.parse(f.read()) # 构建 TensorRT 引擎 engine = builder.build_cuda_engine(network) # 保存 TensorRT 引擎 with open("resnet18.trt", "wb") as f: f.write(engine.serialize()) ``` 3. 导入 TensorRT 模型: ```python import tensorrt as trt # 加载 TensorRT 模型 trt_path = "resnet18.trt" with open(trt_path, "rb") as f, trt.Runtime(trt.Logger(trt.Logger.WARNING)) as runtime: engine = runtime.deserialize_cuda_engine(f.read()) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值