I’m trying to make an introduction to tensorrt and my first goal is to import an ONNX model and save the engine. However I ran into the below problem:
File "onnx2_tensorrt.py", line 13, in <module>
with builder.create_builder_config() as config, builder.build_cuda_engine(network,config) as engine:
TypeError: build_cuda_engine(): incompatible function arguments. The following argument types are supported:
1. (self: tensorrt.tensorrt.Builder, network: tensorrt.tensorrt.INetworkDefinition) -> tensorrt.tensorrt.ICudaEngine
Invoked with: <tensorrt.tensorrt.Builder object at 0x7f79ecd90bc8>, <tensorrt.tensorrt.INetworkDefinition object at 0x7f79ecd90c00>, <tensorrt.tensorrt.IBuilderConfig object at 0x7f79ecdb37a0>
My code is like:
import tensorrt as trt
TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
model_path = 'export.onnx'
max_batch_size = 32
builder = trt.Builder(TRT_LOGGER)
with builder.create_network() as network, trt.OnnxParser(network, TRT_LOGGER) as parser:
with open(model_path, 'rb') as model:
parser.parse(model.read())
builder.max_batch_size = max_batch_size
builder.max_workspace_size = 1 << 50 # This determines the amount of memory available to the builder when building an optimized engine and should generally be set as high as possible.
with builder.create_builder_config() as config, builder.build_cuda_engine(network,config) as engine:
with open('sample.engine', 'wb') as f:
f.write(engine.serialize())
What’s the point I’m missing?
BTW, my system is
-Ubuntu 18.04
-CUDA 10.0 /CuDNN 7.4.2
-TensorRT 6.0
-Python 3.6.8
-GTX1050 Max-Q
import tensorrt as trt
TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
model_path = 'export.onnx'
max_batch_size = 32
builder = trt.Builder(TRT_LOGGER)
with builder.create_network() as network, trt.OnnxParser(network, TRT_LOGGER) as parser:
with open(model_path, 'rb') as model:
parser.parse(model.read())
builder.max_batch_size = max_batch_size
builder.max_workspace_size = 1 << 1000000 # This determines the amount of memory available to the builder when building an optimized engine and should generally be set as high as possible.
with builder.create_builder_config() as config, builder.build_cuda_engine(network=network) as engine:
with open('/home/orcun/PyProjects/yolov3_onnx/sample.engine', 'wb') as f:
f.write(engine.serialize())
and received bunch of random number as output. But nothing is saved.
This is taken from the tensorrt samples - /usr/src/tensorrt/samples/python/
def build_engine():
"""Takes an ONNX file and creates a TensorRT engine to run inference with"""
with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.OnnxParser(network, TRT_LOGGER) as parser:
builder.max_workspace_size = 1 << 28 # 256MiB
builder.max_batch_size = 1
# Parse model file
if not os.path.exists(onnx_file_path):
print('ONNX file {} not found, please run yolov3_to_onnx.py first to generate it.'.format(onnx_file_path))
sys.exit(0)
print('Loading ONNX file from path {}...'.format(onnx_file_path))
with open(onnx_file_path, 'rb') as model:
print('Beginning ONNX file parsing')
parser.parse(model.read())
print('Completed parsing of ONNX file')
print('Building an engine from file {}; this may take a while...'.format(onnx_file_path))
engine = builder.build_cuda_engine(network)
print("Completed creating Engine")
with open(engine_file_path, "wb") as f:
f.write(engine.serialize())
return engine
Loading ONNX file from path /home/orcun/PyProjects/yolov3_onnx/my_yolo.onnx...
Beginning ONNX file parsing
Completed parsing of ONNX file
Building an engine from file /home/orcun/PyProjects/yolov3_onnx/my_yolo.onnx; this may take a while...
[TensorRT] ERROR: Network must have at least one output
Completed creating Engine
Traceback (most recent call last):
File "onnx2_tensorrt.py", line 29, in <module>
build_engine('/home/orcun/PyProjects/yolov3_onnx/my_yolo.onnx', '/home/orcun/PyProjects/yolov3_onnx/sample.engine')
File "onnx2_tensorrt.py", line 26, in build_engine
f.write(engine.serialize())
AttributeError: 'NoneType' object has no attribute 'serialize'
Why doesnt it find the outputs, which causes it to fail the writing part? How to solve this?