NVIDIA / NVIDIA/TensorRT

onnx2tensorrt

Open
#4,064 5 comments 0 reactions 1 assignee View on GitHub

@kevinch-nv is already working on this.

Since Feb 11, 2025.

Module:Runtime triaged
Dominant language
C++
Stars
13.4k
Forks
2.4k
Avg merge
5d 3h
Merged PRs (30d)
2

Description

import torch

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model1_trained = torch.load('MyClassify', map_location=device)
model1_trained.eval()
dummy_input = torch.randn(1, 1, 224, 224, device=device)
onnx_model_path = 'resnet182.onnx'
try:
torch.onnx.export(
model1_trained,
dummy_input,
onnx_model_path,
export_params=True,
opset_version=11,
do_constant_folding=True,
input_names=['input'],
output_names=['output'],
dynamic_axes={'input': {0: 'batch_size'},
'output': {0: 'batch_size'}}
)
print(f"模型已成功转换为 ONNX 并保存在 {onnx_model_path}")
except Exception as e:
print(f"转换为 ONNX 时出错: {e}")

C++ :onnx2tensorrt

  IOptimizationProfile* profile = builder->createOptimizationProfile();
  auto input = network->getInput(0);
  auto inputDims = input->getDimensions();
  profile->setDimensions(input->getName(), OptProfileSelector::kMIN, Dims4{ 1, inputDims.d[1], inputDims.d[2], inputDims.d[3] });
  profile->setDimensions(input->getName(), OptProfileSelector::kOPT, Dims4{ 4, inputDims.d[1], inputDims.d[2], inputDims.d[3] });
  profile->setDimensions(input->getName(), OptProfileSelector::kMAX, Dims4{ 8, inputDims.d[1], inputDims.d[2], inputDims.d[3] });
  config->addOptimizationProfile(profile);

When infering, the minimum size, optimal size, and maximum size should be set to the same before the error will be reported, otherwise the error will be reported
samplesCommon::BufferManager buffers(mEngine);
errormsg:System.Runtime.InteropServices.SEHException:“External component has thrown an exception.”

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.