# TensorRT engine gives garbage output

**URL:** <https://forums.developer.nvidia.com/t/tensorrt-engine-gives-garbage-output/111663>\
**Category:** TensorRT\
**Created:** [February 8, 2020, 1:41pm UTC](https://forums.developer.nvidia.com/t/tensorrt-engine-gives-garbage-output/111663 "2020-02-08T13:41:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinstephen99](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/kevinstephen99/32/30310_2.png) [@kevinstephen99](https://forums.developer.nvidia.com/u/kevinstephen99)\
**Post date:** [February 8, 2020, 1:41pm UTC](https://forums.developer.nvidia.com/t/tensorrt-engine-gives-garbage-output/111663/1 "2020-02-08T13:41:39Z")

</div>

Hello!

I am currently working with a pre created ONNX model, The ONNX model was possibly created using an input shape of (10, 3, 32, 32). Does this mean my created engine for the said model will only work work on (10, 3, 32, 32).

If so, how do I generalise my shapes.

Another issue I am facing is that my engine on inference returns garbage values.  
The values returned are -4.3160208e+08 or 0.

Here is the code that I am using.

TRT\_LOGGER = trt.Logger(trt.Logger.WARNING)  
runtime = trt.Runtime(TRT\_LOGGER)

model\_path = ‘model.onnx’

def print\_network(network):  
for i in range(network.num\_layers):  
layer = network.get\_layer(i)

```
    print("\nLAYER {}".format(i))
    print("===========================================")
    layer_input = layer.get_input(0)
    if layer_input:
        print("\tInput Name: {}".format(layer_input.name))
        print("\tInput Shape: {}".format(layer_input.shape))

    layer_output = layer.get_output(0)
    if layer_output:
        print("\tOutput Name: {}".format(layer_output.name))
        print("\tOutput Shape: {}".format(layer_output.shape))
    print("===========================================")

```

def build\_engine(model\_path):  
with trt.Builder(TRT\_LOGGER) as builder, builder.create\_network(flags = 1) as network,   
trt.OnnxParser(network, TRT\_LOGGER) as parser:  
builder.max\_workspace\_size = 1\<\<32  
builder.max\_batch\_size = 1  
builder.fp16\_mode = 0

with open(model\_path, ‘rb’) as f:  
value = parser.parse(f.read())  
print("Parser: ", value)

```
    engine = builder.build_cuda_engine(network)
    print_network(network)

    print(engine)
    return engine

```

engine = build\_engine(model\_path)

# buf = engine.serialize()

# with open(“ssh.engine”, ‘wb’) as f:

# f.write(buf)

# create buffer

print(imu.dtype, “DTYPE”)

context = engine.create\_execution\_context()

print(engine.get\_binding\_shape(0),engine.get\_binding\_shape(1),engine.get\_binding\_shape(2), engine.get\_binding\_shape(3), engine.get\_binding\_shape(4), “binding shape”)  
#print(imu[0].shape,“imu shape”)  
h\_input = cuda.pagelocked\_empty(trt.volume((10, 3, 32, 32)), dtype = np.float32)  
h\_output\_hmap = cuda.pagelocked\_empty(trt.volume((1, 1, 8, 8)), dtype=np.float32)  
h\_output\_scale = cuda.pagelocked\_empty(trt.volume(( 1, 2, 8, 8)), dtype=np.float32)  
h\_output\_offset = cuda.pagelocked\_empty(trt.volume(( 1, 2, 8, 8)), dtype=np.float32)

# Allocate device memory for inputs and outputs.

print(h\_input.nbytes, “Hinput”)  
print(h\_output\_hmap.nbytes, “Houtput\_hmap”)  
print(h\_output\_scale.nbytes, “Houtput\_scale”)  
print(h\_output\_offset.nbytes, “Houtput\_offset”)

d\_input = cuda.mem\_alloc(h\_input.nbytes)  
d\_output\_hmap = cuda.mem\_alloc(h\_output\_hmap.nbytes)  
d\_output\_scale = cuda.mem\_alloc(h\_output\_scale.nbytes)  
d\_output\_offset = cuda.mem\_alloc(h\_output\_offset.nbytes)  
bindings = [int(d\_input), int(d\_output\_hmap), int(d\_output\_scale), int(d\_output\_offset)]

# Create a stream in which to copy inputs/outputs and run inference.

stream = cuda.Stream()

print(np.ascontiguousarray(np.array(imu[0])).shape)

with engine.create\_execution\_context() as context:  
# Transfer input data to the GPU.  
cuda.memcpy\_htod\_async(d\_input,np.ascontiguousarray(np.array(imu)), stream)  
# Run inference.  
context.execute\_async(bindings=bindings, stream\_handle=stream.handle)  
# Transfer predictions back from the GPU.  
cuda.memcpy\_dtoh\_async(h\_output\_hmap, d\_output\_hmap, stream)  
cuda.memcpy\_dtoh\_async(h\_output\_scale, d\_output\_scale, stream)  
cuda.memcpy\_dtoh\_async(h\_output\_offset, d\_output\_offset, stream)  
# Synchronize the stream  
stream.synchronize()  
# Return the host output.  
print(h\_output\_hmap.shape,  
h\_output\_scale.shape,  
h\_output\_offset.shape)

```
    # print(h_output_hmap, "HMAP")

    print(h_output_hmap)
    # print(h_output_scale)
    # print(h_output_offset)

```

---

<div class="post-metadata">

**Author:** ![SunilJB](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/suniljb/32/14043_2.png) [@SunilJB](https://forums.developer.nvidia.com/u/SunilJB)\
**Post date:** [February 10, 2020, 4:10am UTC](https://forums.developer.nvidia.com/t/tensorrt-engine-gives-garbage-output/111663/2 "2020-02-10T04:10:50Z")

</div>

Hi,

Since ONNX model has static input shape of (10, 3, 32, 32), by default TensorRT engine will be optimized of that specific input size.

You can update your ONNX model for dynamic input and use TRT dynamic shape feature to support the dynamic input.  
Please refer below link for more details:  
[https://docs.nvidia.com/deeplearning/sdk/tensorrt-archived/tensorrt-700/tensorrt-developer-guide/index.html#work\_dynamic\_shapes](https://docs.nvidia.com/deeplearning/sdk/tensorrt-archived/tensorrt-700/tensorrt-developer-guide/index.html#work_dynamic_shapes)

Torch to ONNX (dynamic\_axes):  
[https://pytorch.org/tutorials/advanced/super\_resolution\_with\_onnxruntime.html#](https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html#)

Regarding accuracy, could you please check the output of ONNX model and TRT engine using FP32?

Also, can you provide the following information along with model file so we can better help?  
Provide details on the platforms you are using:  
o Linux distro and version  
o GPU type  
o Nvidia driver version  
o CUDA version  
o CUDNN version  
o Python version [if using python]  
o Tensorflow and PyTorch version  
o TensorRT version

Thanks
