Table Of Contents
This sample, sampleDynamicReshape, demonstrates how to use dynamic input dimensions in TensorRT. It creates an engine that takes a dynamically shaped input and resizes it to be consumed by an ONNX MNIST model that expects a fixed size input. For more information, see Working With Dynamic Shapes in the TensorRT Developer Guide.
This sample creates an engine for resizing an input with dynamic dimensions to a size that an ONNX MNIST model can consume.
Specifically, this sample:
First, create a network with full dims support: auto preprocessorNetwork = makeUnique(builder->createNetworkV2(1U << static_cast<int32_t>(NetworkDefinitionCreationFlag::kEXPLICIT_BATCH)));
Next, add an input layer that accepts an input with a dynamic shape, followed by a resize layer that will reshape the input to the shape the model expects:
The -1 dimensions denote dimensions that will be supplied at runtime.
First, create an empty full-dims network, and parser:
Next, parse the model file to populate the network:
When building the preprocessor engine, also provide an optimization profile so that TensorRT knows which input shapes to optimize for:
OptProfileSelector::kOPT
specifies the dimensions that the profile will be optimized for, whereas OptProfileSelector::kMIN
and OptProfileSelector::kMAX
specify the minimum and maximum dimensions for which the profile will be valid:
Create an optimization profile for calibration:
Prepare and set int8 calibrator if running in int8 mode:
Run engine build with config:
For the MNIST model, attach a Softmax layer to the end of the network, set softmax axis to 1 since network output has shape [1, 10] in full dims mode and replace the existing network output with the Softmax:
A calibrator and a calibration profile are set the same way as above for the preprocessor engine config. calibBatchSize
is set to 1 for the prediction engine as ONNX model has an explicit batch.
Finally, build as normal: mPredictionEngine = makeUnique(builder->buildEngineWithConfig(*network, *config));
During inference, first copy the input buffer to the device:
Since the preprocessor engine accepts dynamic shapes, specify the actual shape of the current input to the execution context: mPreprocessorContext->setBindingDimensions(0, inputDims);
Next, run the preprocessor using the executeV2
function. The example writes the output of the preprocessor engine directly to the input device buffer of the MNIST engine:
Then, run the MNIST engine:
Finally, copy the output back to the host:
In this sample, the following layers are used. For more information about these layers, see the TensorRT Developer Guide: Layers documentation.
Resize layer The IResizeLayer implements the resize operation on an input tensor.
Compile this sample by running make
in the <TensorRT root directory>/samples/sampleDynamicReshape
directory. The binary named sample_dynamic_reshape
will be created in the <TensorRT root directory>/bin
directory. ``` cd <TensorRT root directory>/samples/sampleDynamicReshape make ```
Where <TensorRT root directory>
is where you installed TensorRT.
Input filename: ../../../../../data/samples/mnist/mnist.onnx ONNX IR version: 0.0.3 Opset version: 8 Producer name: CNTK Producer version: 2.5.1 Domain: ai.cntk Model version: 1
[W] [TRT] onnx2trt_utils.cpp:214: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32. [W] [TRT] onnx2trt_utils.cpp:214: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32. [I] [TRT] Detected 1 inputs and 1 output network tensors. [I] [TRT] Detected 1 inputs and 1 output network tensors. [I] Profile dimensions in preprocessor engine: [I] Minimum = (1, 1, 1, 1) [I] Optimum = (1, 1, 28, 28) [I] Maximum = (1, 1, 56, 56) [I] Input: @@@@@@@@@@@@@@ @@@@@@@@@@@@@@ @@@@@@@@@@@@@@ @@@@@@@@@@@@@@ @@@@@@*. .*@@@@@@ @@@@@*. +@@@@@ @@@@@. :#+ %@@@@@ @@@@@.:@+ +@@@@@ @@@@@.:@@: +@@@@@ @@@@@=%@@: +@@@@@ @@@@@@@@# +@@@@@ @@@@@@@@* +@@@@@ @@@@@@@@: +@@@@@ @@@@@@@@: +@@@@@ @@@@@@@@* .@@@@@ @@@@@%**%. @@@@@ @@@@%+. .: .@@@@@@ @@@@= .. :@@@@@@ @@@@: *@: :@@@@@@ @@@% %@* *@@@@@ @@@% ++ ++ .%@@@@@ @@@@- +@- +@@@@@ @@@@= :@# .%@@@@ @@@@+*@@%. %@@@@ @@@@@@@@@@@@@@ @@@@@@@@@@@@@@ @@@@@@@@@@@@@@ @@@@@@@@@@@@@@
[I] Output: [I] Prob 0 0.0000 Class 0: [I] Prob 1 0.0000 Class 1: [I] Prob 2 1.0000 Class 2: ********** [I] Prob 3 0.0000 Class 3: [I] Prob 4 0.0000 Class 4: [I] Prob 5 0.0000 Class 5: [I] Prob 6 0.0000 Class 6: [I] Prob 7 0.0000 Class 7: [I] Prob 8 0.0000 Class 8: [I] Prob 9 0.0000 Class 9: &&&& PASSED TensorRT.sample_dynamic_reshape # ./sample_dynamic_reshape ```
This output shows that the sample ran successfully; PASSED
.
--help
optionsTo see the full list of available options and their descriptions, use the -h
or --help
command line option.
The following resources provide a deeper understanding of dynamic shapes.
ONNX
Models
Documentation
For terms and conditions for use, reproduction, and distribution, see the TensorRT Software License Agreement documentation.
February 2020 This is the second release of the README.md
file and sample.
There are no known issues in this sample.