Running the TensorFlow Official ResNet with TensorRT
TensorRT is NVIDIA's inference optimizer for deep learning. Briefly, TensorRT rewrites parts of the execution graph to allow for faster prediction times.
Here we provide a sample script that can:
Convert a TensorFlow SavedModel to a Frozen Graph.
Load a Frozen Graph for inference.
Time inference loops using the native TensorFlow graph.
Time inference loops using FP32, FP16, or INT8 precision modes from TensorRT.
We provide some results below, as well as instructions for running this script.
How to Run This Script
Step 1: Install Prerequisites
We use the image processing functions from the Official version of ResNet. Please checkout the Models repository if you haven't
already, and add the Official Models to your Python path:
Step 2: Get a model to test
The provided script runs with the Official version of ResNet trained with ImageNet data, but can be used for other models as well, as long as you have a SavedModel or a Frozen Graph.
You can download the ResNetv2-ImageNet SavedModel or Frozen Graph, or, if you want to train the model yourself, pass --export_dir
to the Official ResNet imagenet_main.py.
When running this script, you can pass in a SavedModel directory containing the Protobuf MetaGraphDef and variables directory to savedmodel_dir
, or pass in a Protobuf frozen graph file directly to frozen_graph
. If you downloaded the SavedModel linked above, note that you should untar it before passing in to the script.
Step 3: Get an image to test
The script can accept a JPEG image file to use for predictions. If none is provided, random data will be generated. We provide a sample image.jpg
here which can be passed in with the --image_file
flag.
Step 4: Run the model
You have TensorFlow, TensorRT, a graph def, and a picture. Now it's time to time.
For the full set of possible parameters, you can run python tensorrt.py --help
. Assuming you used the files provided above, you would run:
This will print the predictions for each of the precision modes that were run (native, which is the native precision of the model passed in, as well as the TensorRT version of the graph at precisions of fp32, fp16 and int8):
The script will generate or append to a file in the output_dir, log.txt
, which includes the timing information for each of the models:
The script will also output the GraphDefs used for each of the modes run, for future use and inspection:
Troubleshooting and Notes
GPU/Precision Compatibility
Not all GPUs support the ops required for all precisions. For example, P100s cannot currently run INT8 precision.
Label Offsets
Some ResNet models represent 1000 categories, and some represent all 1001, with the 0th category being "background". The models provided are of the latter type. If you are using a different model and find that your predictions seem slightly off, try passing in the --ids_are_one_indexed
arg, which adjusts the label alignment for models with only 1000 categories.
Model Links
ResNet-v2-ImageNet Frozen Graph
Last updated