|
| 1 | +# |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (c) 2018 Intel Corporation |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# SPDX-License-Identifier: EPL-2.0 |
| 19 | +# |
| 20 | + |
| 21 | +import time |
| 22 | +from argparse import ArgumentParser |
| 23 | + |
| 24 | +import tensorflow as tf |
| 25 | +from tensorflow.python.tools.optimize_for_inference_lib import optimize_for_inference |
| 26 | +from tensorflow.python.framework import dtypes |
| 27 | + |
| 28 | +import datasets2 as datasets |
| 29 | + |
| 30 | + |
| 31 | +# override by args |
| 32 | +INPUTS = "input" |
| 33 | +OUTPUTS = "predict" |
| 34 | + |
| 35 | +INCEPTION_V3_IMAGE_SIZE = 224 |
| 36 | + |
| 37 | + |
| 38 | +class eval_classifier_optimized_graph: |
| 39 | + """Evaluate image classifier with optimized TensorFlow graph""" |
| 40 | + |
| 41 | + def __init__(self): |
| 42 | + |
| 43 | + arg_parser = ArgumentParser(description='Parse args') |
| 44 | + |
| 45 | + arg_parser.add_argument('-b', "--batch-size", |
| 46 | + help="Specify the batch size. If this " \ |
| 47 | + "parameter is not specified or is -1, the " \ |
| 48 | + "largest ideal batch size for the model will " \ |
| 49 | + "be used.", |
| 50 | + dest="batch_size", type=int, default=-1) |
| 51 | + |
| 52 | + arg_parser.add_argument('-e', "--num-inter-threads", |
| 53 | + help='The number of inter-thread.', |
| 54 | + dest='num_inter_threads', type=int, default=0) |
| 55 | + |
| 56 | + arg_parser.add_argument('-a', "--num-intra-threads", |
| 57 | + help='The number of intra-thread.', |
| 58 | + dest='num_intra_threads', type=int, default=0) |
| 59 | + |
| 60 | + arg_parser.add_argument('-g', "--input-graph", |
| 61 | + help='Specify the input graph for the transform tool', |
| 62 | + dest='input_graph') |
| 63 | + |
| 64 | + arg_parser.add_argument('-i', "--input", |
| 65 | + help='Specify the input of the model', |
| 66 | + dest='input') |
| 67 | + arg_parser.add_argument('-o', "--output", |
| 68 | + help='Specify the output of the model', |
| 69 | + dest='output') |
| 70 | + arg_parser.add_argument('--image_size', dest='image_size', |
| 71 | + help='image size', |
| 72 | + type=int, default=224) |
| 73 | + |
| 74 | + arg_parser.add_argument('-d', "--data-location", |
| 75 | + help='Specify the location of the data. ' |
| 76 | + 'If this parameter is not specified, ' |
| 77 | + 'the benchmark will use random/dummy data.', |
| 78 | + dest="data_location", default=None) |
| 79 | + |
| 80 | + arg_parser.add_argument('-r', "--accuracy-only", |
| 81 | + help='For accuracy measurement only.', |
| 82 | + dest='accuracy_only', action='store_true') |
| 83 | + |
| 84 | + arg_parser.add_argument("--warmup-steps", type=int, default=10, |
| 85 | + help="number of warmup steps") |
| 86 | + arg_parser.add_argument("--steps", type=int, default=50, |
| 87 | + help="number of steps") |
| 88 | + |
| 89 | + arg_parser.add_argument( |
| 90 | + '--data-num-inter-threads', dest='data_num_inter_threads', |
| 91 | + help='number threads across operators', |
| 92 | + type=int, default=16) |
| 93 | + arg_parser.add_argument( |
| 94 | + '--data-num-intra-threads', dest='data_num_intra_threads', |
| 95 | + help='number threads for data layer operator', |
| 96 | + type=int, default=14) |
| 97 | + arg_parser.add_argument( |
| 98 | + '--num-cores', dest='num_cores', |
| 99 | + help='number of cores', |
| 100 | + type=int, default=28) |
| 101 | + arg_parser.add_argument( |
| 102 | + '--env', dest='env', help='specific Tensorflow env', |
| 103 | + default='mkl' |
| 104 | + ) |
| 105 | + |
| 106 | + self.args = arg_parser.parse_args() |
| 107 | + |
| 108 | + # validate the arguments specific for InceptionV3 |
| 109 | + self.validate_args() |
| 110 | + |
| 111 | + def run(self): |
| 112 | + """run benchmark with optimized graph""" |
| 113 | + |
| 114 | + print("Run inference") |
| 115 | + |
| 116 | + data_config = tf.compat.v1.ConfigProto() |
| 117 | + data_config.intra_op_parallelism_threads = self.args.data_num_intra_threads |
| 118 | + data_config.inter_op_parallelism_threads = self.args.data_num_inter_threads |
| 119 | + data_config.use_per_session_threads = 1 |
| 120 | + |
| 121 | + infer_config = tf.compat.v1.ConfigProto() |
| 122 | + if self.args.env == 'mkl': |
| 123 | + print("Set inter and intra for mkl") |
| 124 | + infer_config.intra_op_parallelism_threads = self.args.num_intra_threads |
| 125 | + infer_config.inter_op_parallelism_threads = self.args.num_inter_threads |
| 126 | + infer_config.use_per_session_threads = 1 |
| 127 | + |
| 128 | + data_graph = tf.Graph() |
| 129 | + with data_graph.as_default(): |
| 130 | + if (self.args.data_location): |
| 131 | + print("Inference with real data.") |
| 132 | + dataset = datasets.ImagenetData(self.args.data_location) |
| 133 | + preprocessor = dataset.get_image_preprocessor()( |
| 134 | + self.args.image_size, self.args.image_size, self.args.batch_size, |
| 135 | + num_cores=self.args.num_cores, |
| 136 | + resize_method='bilinear') |
| 137 | + images, labels = preprocessor.minibatch(dataset, subset='validation') |
| 138 | + else: |
| 139 | + print("Inference with dummy data.") |
| 140 | + input_shape = [self.args.batch_size, self.args.image_size, self.args.image_size, 3] |
| 141 | + images = tf.random.uniform(input_shape, 0.0, 255.0, dtype=tf.float32, name='synthetic_images') |
| 142 | + |
| 143 | + infer_graph = tf.Graph() |
| 144 | + with infer_graph.as_default(): |
| 145 | + graph_def = tf.compat.v1.GraphDef() |
| 146 | + with tf.compat.v1.gfile.FastGFile(self.args.input_graph, 'rb') as input_file: |
| 147 | + input_graph_content = input_file.read() |
| 148 | + graph_def.ParseFromString(input_graph_content) |
| 149 | + |
| 150 | + output_graph = optimize_for_inference(graph_def, [self.args.input], |
| 151 | + [self.args.output], dtypes.float32.as_datatype_enum, False) |
| 152 | + tf.import_graph_def(output_graph, name='') |
| 153 | + |
| 154 | + # Definite input and output Tensors for detection_graph |
| 155 | + input_tensor = infer_graph.get_tensor_by_name(self.args.input + ':0') |
| 156 | + output_tensor = infer_graph.get_tensor_by_name(self.args.output + ':0') |
| 157 | + |
| 158 | + data_sess = tf.compat.v1.Session(graph=data_graph, config=data_config) |
| 159 | + infer_sess = tf.compat.v1.Session(graph=infer_graph, config=infer_config) |
| 160 | + |
| 161 | + num_processed_images = 0 |
| 162 | + #num_remaining_images = datasets.IMAGENET_NUM_VAL_IMAGES |
| 163 | + num_remaining_images = 50000 |
| 164 | + |
| 165 | + if (not self.args.accuracy_only): |
| 166 | + iteration = 0 |
| 167 | + warm_up_iteration = self.args.warmup_steps |
| 168 | + total_run = self.args.steps |
| 169 | + total_time = 0 |
| 170 | + |
| 171 | + while num_remaining_images >= self.args.batch_size and iteration < total_run: |
| 172 | + iteration += 1 |
| 173 | + |
| 174 | + data_load_start = time.time() |
| 175 | + image_np = data_sess.run(images) |
| 176 | + data_load_time = time.time() - data_load_start |
| 177 | + |
| 178 | + num_processed_images += self.args.batch_size |
| 179 | + num_remaining_images -= self.args.batch_size |
| 180 | + |
| 181 | + start_time = time.time() |
| 182 | + infer_sess.run([output_tensor], feed_dict={input_tensor: image_np}) |
| 183 | + time_consume = time.time() - start_time |
| 184 | + |
| 185 | + # only add data loading time for real data, not for dummy data |
| 186 | + if self.args.data_location: |
| 187 | + time_consume += data_load_time |
| 188 | + |
| 189 | + print('Iteration %d: %.6f sec' % (iteration, time_consume)) |
| 190 | + if iteration > warm_up_iteration: |
| 191 | + total_time += time_consume |
| 192 | + |
| 193 | + time_average = total_time / (iteration - warm_up_iteration) |
| 194 | + print('Average time: %.6f sec' % (time_average)) |
| 195 | + |
| 196 | + print('Batch size = %d' % self.args.batch_size) |
| 197 | + if (self.args.batch_size == 1): |
| 198 | + print('Latency: %.3f ms' % (time_average * 1000)) |
| 199 | + |
| 200 | + print('Throughput: %.3f images/sec' % (self.args.batch_size / time_average)) |
| 201 | + |
| 202 | + else: # accuracy check |
| 203 | + total_accuracy1, total_accuracy5 = (0.0, 0.0) |
| 204 | + |
| 205 | + while num_remaining_images >= self.args.batch_size: |
| 206 | + # Reads and preprocess data |
| 207 | + np_images, np_labels = data_sess.run([images, labels]) |
| 208 | + num_processed_images += self.args.batch_size |
| 209 | + num_remaining_images -= self.args.batch_size |
| 210 | + |
| 211 | + start_time = time.time() |
| 212 | + # Compute inference on the preprocessed data |
| 213 | + predictions = infer_sess.run(output_tensor, |
| 214 | + {input_tensor: np_images}) |
| 215 | + elapsed_time = time.time() - start_time |
| 216 | + |
| 217 | + with tf.Graph().as_default() as accu_graph: |
| 218 | + accuracy1 = tf.reduce_sum( |
| 219 | + input_tensor=tf.cast(tf.nn.in_top_k(predictions=tf.constant(predictions), |
| 220 | + targets=tf.constant(np_labels), k=1), tf.float32)) |
| 221 | + |
| 222 | + accuracy5 = tf.reduce_sum( |
| 223 | + input_tensor=tf.cast(tf.nn.in_top_k(predictions=tf.constant(predictions), |
| 224 | + targets=tf.constant(np_labels), k=5), tf.float32)) |
| 225 | + with tf.compat.v1.Session() as accu_sess: |
| 226 | + np_accuracy1, np_accuracy5 = accu_sess.run([accuracy1, accuracy5]) |
| 227 | + |
| 228 | + total_accuracy1 += np_accuracy1 |
| 229 | + total_accuracy5 += np_accuracy5 |
| 230 | + |
| 231 | + print("Iteration time: %0.4f ms" % elapsed_time) |
| 232 | + print("Processed %d images. (Top1 accuracy, Top5 accuracy) = (%0.4f, %0.4f)" \ |
| 233 | + % (num_processed_images, total_accuracy1 / num_processed_images, |
| 234 | + total_accuracy5 / num_processed_images)) |
| 235 | + |
| 236 | + def validate_args(self): |
| 237 | + """validate the arguments""" |
| 238 | + |
| 239 | + if not self.args.data_location: |
| 240 | + if self.args.accuracy_only: |
| 241 | + raise ValueError("You must use real data for accuracy measurement.") |
| 242 | + |
| 243 | + |
| 244 | +if __name__ == "__main__": |
| 245 | + |
| 246 | + evaluate_opt_graph = eval_classifier_optimized_graph() |
| 247 | + evaluate_opt_graph.run() |
0 commit comments