19 CheXNet – Inference with Nvidia T4 on Dell EMC PowerEdge R7425
4 Inference with TensorRT™
NVIDIA TensorRT™ is a C++ based library aimed to perform high performance inference on
GPUs. After a model is trained and saved, TensorRT™ transforms it by applying graph
optimization and layer fusion for a faster implementation, so it can be deployed in an
inference context.
TensorRT™ provides three tools to optimize the models for inference: TensorFlow-TensorRT
integrated (TF-TRT), TensorRT C++ API, and TensorRT Python API. The last two tools
include parsers for importing existing models from Caffe, ONNX, or TensorFlow. Also, C++
and Python API’s include methods for building models programmatically. It is important to
note that TF-TRT is the Nvidia’s preferred method for importing TensorFlow models to
TensorRT™ [11].
In this project we will show how to implement using TensorRT™ C++ API, as well as TF-TRT
integrated with the parser (UFF for TensorFlow). Below is a brief description of each method
applied to CheXNet model.
4.1 TensorRT™ using TensorFlow-TensorRT (TF-TRT) Integrated
With TF-TRT integrated, TensorRT™ will parse the model and apply optimizations to the
portions of the graph wherever possible, allowing TensorFlow to execute the remaining graph
that couldn’t be optimized. TF-TRT integration workflow includes importing a TensorFlow
model, creating an optimized graph with TensorRT™, importing it back as the default graph,
and running inference. After importing the model TensorRT™ optimizes the TensorFlow's
subgraphs, then replaces each supported subgraph with a TensorRT™ optimized node,
producing a frozen graph that runs in TensorFlow for inference. TensorFlow executes the
graph for all supported areas and calls TensorRT™ to execute TensorRT™ optimized nodes.
In this section we present the general steps to work with the custom model CheXNet and TF-
TRT integration. For step-by-step instructions on how to use TensorRT™ with the
TensorFlow framework, see “Accelerating Inference In TensorFlow With TensorRT™-User
Guide”[11].
4.1.1 TF-TRT Workflow with a Frozen Graph
There are three workflows for creating a TensorRT™ inference graph from a TensorFlow
model depending of the format: for saved model, frozen graph, and separate MetaGraph with
checkpoint files.
In this project we will focus on the workflow using a frozen graph file. Figure 8. shows the
specific workflow for creating a TensorRT™ inference graph from a TensorFlow model in
frozen graph format file as an input. For more information about the other two methods, refer
to the following Nvidia documentation: “Accelerating Inference in TensorFlow With
TensorRT™ - User Guide” [12].