Hi,
I get error that network has implicit batch size
If you correctly specified the explicit batch flag like this line: https://github.com/NVIDIA/TensorRT/blob/f5e8b8c55060447c4d3a45e7379bc25374edc93f/samples/opensource/sampleDynamicReshape/sampleDynamicReshape.cpp#L126, then you shouldn’t be getting this error. Please do share the full error if you are.
please use executeV2
This is correct. If creating a network with the explicit batch flag (which is required for ONNX models), you should be using executeV2 (which doesn’t require a batchSize parameter since it’s explicit in the model).
If your explicit batch network has fixed shape (batch size >= 1), then you should be able to just use executeV2() similar to how you used execute() in previous TensorRT versions.
If your explicit batch network has dynamic shape (batch size == -1), (which it does in this case), then you need to create an optimization profile for it as you’ve described above. Then you set this optimization profile for your execution context. But also before doing inference, you’ll need to specify the shape at inference time based on the input, such as something like this for an input of shape (4, 3, 384, 1280):
context->setOptimizationProfile(0); // 0 is the first profile, 1 is the second profile, etc.
context->setBindingShape(0, Dims3(4, 3, 384, 1280)); // 0 is the first input binding, you may have multiple input bindings
context->executeV2(...)
See a dynamic shape sample here: https://github.com/NVIDIA/TensorRT/blob/572d54f91791448c015e74a4f1d6923b77b79795/samples/opensource/sampleDynamicReshape/README.md#running-inference
See more info in the docs here: Developer Guide :: NVIDIA Deep Learning TensorRT Documentation
export from pytorch with explicit batch size
modify onnx model to replace all batch dimensions with -1 (Python with onnx library)
Can you clarify what you mean by “export from pytorch with explicit batch size”. Do you mean dynamic axes? I have a minimal example of exporting Alexnet from PyTorch with dynamic batch size here: alexnet_onnx.py · GitHub
If you specified dynamic shape when exporting to ONNX with pytorch, you shouldn’t have to modify the onnx model to have -1 batch dimension after exporting, it should already be -1 if exported correctly. (or something like “unk” or “unknown” when viewing your model in a tool like Netron: Netron)
Here’s an example of how you’d parse and create an engine with roughly your sample optimization profile above using trtexec on the alexnet model for simplicity:
trtexec --explicitBatch --onnx=alexnet_dynamic.onnx \
--minShapes=actual_input_1:1x3x224x224 \ # kMIN shape
--optShapes=actual_input_1:3x3x224x224 \ # kOPT shape
--maxShapes=actual_input_1:5x3x224x224 \ # kMAX shape
--shapes=actual_input_1:3x3x224x224 \ # Inference shape - this is like context->setBindingShape(3,3,224,224)
--saveEngine=alexnet_dynamic.engine
You can also load and test other input shapes within the range of the optimization profile
using a saved engine with trtexec:
trtexec --loadEngine=alexnet_dynamic.engine \
--shapes=actual_input_1:5x3x224x224 # Inference shape - this is like context->setBindingShape(5,3,224,224)
There’s some more info on trtexec
on this page: https://github.com/NVIDIA/TensorRT/tree/master/samples/opensource/trtexec
Altogether I did something like this:
# Start TensorRT 7 container
nvidia-docker run -it -v ${PWD}:/mnt --workdir=/mnt nvcr.io/nvidia/tensorrt:20.02-py3
# Create ONNX model with dynamic batch dimension
pip install torch==1.4 torchvision
wget https://gist.githubusercontent.com/rmccorm4/b72abac18aed6be4c1725db18eba4930/raw/3919c883b97a231877b454dae695fe074a1acdff/alexnet_onnx.py
python alexnet_onnx.py
# Parse ONNX model, create a TensorRT engine with an optimization profile, do some inference, and save the engine
trtexec --explicitBatch --onnx=alexnet_dynamic.onnx \
--minShapes=actual_input_1:1x3x224x224 \
--optShapes=actual_input_1:3x3x224x224 \
--maxShapes=actual_input_1:5x3x224x224 \
--shapes=actual_input_1:3x3x224x224 \
--saveEngine=alexnet_dynamic.engine
# Load our saved engine and try inference with a different shape
trtexec --loadEngine=alexnet_dynamic.engine \
--shapes=actual_input_1:5x3x224x224
Hope this helps.