MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT / main

Function main

samples/python/efficientnet/compare_tf.py:88–143  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

86
87
88def main(args):
89 # Initialize TRT and TF infer objects.
90 tf_infer = TensorFlowInfer(args.saved_model)
91 trt_infer = TensorRTInfer(args.engine)
92
93 batcher = ImageBatcher(
94 args.input, *trt_infer.input_spec(), max_num_images=args.num_images, preprocessor=args.preprocessor
95 )
96
97 # Make sure both systems use the same input spec, so we can use the exact same image batches with both
98 tf_shape, tf_dtype = tf_infer.input_spec()
99 trt_shape, trt_dtype = trt_infer.input_spec()
100 if trt_dtype != tf_dtype:
101 print("Input datatype does not match")
102 print("TRT Engine Input Dtype: {} {}".format(trt_dtype))
103 print("TF Saved Model Input Dtype: {} {}".format(tf_dtype))
104 print("Please use the same TensorFlow saved model that the TensorRT engine was built with")
105 sys.exit(1)
106
107 if (tf_shape[1] and trt_shape[1] != tf_shape[1]) or (tf_shape[2] and trt_shape[2] != tf_shape[2]):
108 print("Input shapes do not match")
109 print("TRT Engine Input Shape: {} {}".format(trt_shape[1:]))
110 print("TF Saved Model Input Shape: {} {}".format(tf_shape[1:]))
111 print("Please use the same TensorFlow saved model that the TensorRT engine was built with")
112 sys.exit(1)
113
114 match = 0
115 error = 0
116 for batch, images in batcher.get_batch():
117 # Run inference on the same batch with both inference systems
118 tf_classes, tf_scores, _ = tf_infer.infer(batch)
119 trt_classes, trt_scores, _ = trt_infer.infer(batch)
120
121 # The last batch may not have all image slots filled, so limit the results to only the amount of actual images
122 tf_classes = tf_classes[0 : len(images)]
123 tf_scores = tf_scores[0 : len(images)]
124 trt_classes = trt_classes[0 : len(images)]
125 trt_scores = trt_scores[0 : len(images)]
126
127 # Track how many images match on top-1 class id predictions
128 match += np.sum(trt_classes == tf_classes)
129 # Track the mean square error in confidence score
130 error += np.sum((trt_scores - tf_scores) * (trt_scores - tf_scores))
131
132 print(
133 "Processing {} / {} images: {:.2f}% match ".format(
134 batcher.image_index, batcher.num_images, (100 * (match / batcher.image_index))
135 ),
136 end="\r",
137 )
138
139 print()
140 pc = 100 * (match / batcher.num_images)
141 print("Matching Top-1 class predictions for {} out of {} images: {:.2f}%".format(match, batcher.num_images, pc))
142 avgerror = np.sqrt(error / batcher.num_images)
143 print("RMSE between TensorFlow and TensorRT confidence scores: {:.3f}".format(avgerror))
144
145

Callers 1

compare_tf.pyFile · 0.70

Calls 10

input_specMethod · 0.95
input_specMethod · 0.95
get_batchMethod · 0.95
inferMethod · 0.95
inferMethod · 0.95
TensorRTInferClass · 0.90
ImageBatcherClass · 0.90
printFunction · 0.85
TensorFlowInferClass · 0.70
sqrtMethod · 0.45

Tested by

no test coverage detected