Skip to content

Instantly share code, notes, and snippets.

@rmccorm4
Last active May 11, 2020 19:11
Show Gist options
  • Save rmccorm4/8a2111306f6f7caad215ab3012d6de93 to your computer and use it in GitHub Desktop.
Save rmccorm4/8a2111306f6f7caad215ab3012d6de93 to your computer and use it in GitHub Desktop.
# Export sample Alexnet model to ONNX with a dynamic batch dimension
wget https://gist.githubusercontent.com/rmccorm4/b72abac18aed6be4c1725db18eba4930/raw/3919c883b97a231877b454dae695fe074a1acdff/alexnet_onnx.py
python3 alexnet_onnx.py
# Emulate "maxBatchSize" behavior from implicit batch engines by setting
# an optimization profile with min=(1, *shape), opt=max=(maxBatchSize, *shape)
MAX_BATCH_SIZE=32
INPUT_NAME="actual_input_1"
# Convert dynamic batch ONNX model to TRT Engine with optimization profile defined
trtexec --explicitBatch --onnx=alexnet_dynamic.onnx \
--minShapes=${INPUT_NAME}:1x3x224x224 \ # kMIN shape
--optShapes=${INPUT_NAME}:${MAX_BATCH_SIZE}x3x224x224 \ # kOPT shape
--maxShapes=${INPUT_NAME}:${MAX_BATCH_SIZE}x3x224x224 \ # kMAX shape
--shapes=${INPUT_NAME}:1x3x224x224 \ # Inference shape - this is like context.set_binding_shape(0, (1,3,224,224))
--saveEngine=alexnet_dynamic.engine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment