|
| 1 | +#!# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Copyright 2021 The TensorFlow Authors. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# ============================================================================= |
| 17 | + |
| 18 | +import math |
| 19 | +import os |
| 20 | +import sys |
| 21 | + |
| 22 | +import numpy as np |
| 23 | +import tensorflow as tf |
| 24 | + |
| 25 | +# Allow import of top level python files |
| 26 | +import inspect |
| 27 | + |
| 28 | +currentdir = os.path.dirname( |
| 29 | + os.path.abspath(inspect.getfile(inspect.currentframe())) |
| 30 | +) |
| 31 | +parentdir = os.path.dirname(currentdir) |
| 32 | +parentdir = os.path.dirname(parentdir) |
| 33 | + |
| 34 | +sys.path.insert(0, parentdir) |
| 35 | + |
| 36 | +from benchmark_args import BaseCommandLineAPI |
| 37 | +from benchmark_runner import BaseBenchmarkRunner |
| 38 | + |
| 39 | + |
| 40 | +class CommandLineAPI(BaseCommandLineAPI): |
| 41 | + |
| 42 | + def __init__(self): |
| 43 | + super(CommandLineAPI, self).__init__() |
| 44 | + |
| 45 | + self._parser.add_argument( |
| 46 | + '--input_size', |
| 47 | + type=int, |
| 48 | + default=384, |
| 49 | + help='Size of input images expected by the model' |
| 50 | + ) |
| 51 | + |
| 52 | + def _validate_args(self, args): |
| 53 | + super(CommandLineAPI, self)._validate_args(args) |
| 54 | + |
| 55 | + # TODO: Remove when proper dataloading is implemented |
| 56 | + if not args.use_synthetic_data: |
| 57 | + raise ValueError( |
| 58 | + "This benchmark does not currently support non-synthetic data " |
| 59 | + "--use_synthetic_data" |
| 60 | + ) |
| 61 | + # This model requires that the batch size is 1 |
| 62 | + if args.batch_size != 1: |
| 63 | + raise ValueError( |
| 64 | + "This benchmark does not currently support " |
| 65 | + "--batch_size != 1" |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +class BenchmarkRunner(BaseBenchmarkRunner): |
| 70 | + |
| 71 | + def get_dataset_batches(self): |
| 72 | + """Returns a list of batches of input samples. |
| 73 | +
|
| 74 | + Each batch should be in the form [x, y], where |
| 75 | + x is a numpy array of the input samples for the batch, and |
| 76 | + y is a numpy array of the expected model outputs for the batch |
| 77 | +
|
| 78 | + Returns: |
| 79 | + - dataset: a TF Dataset object |
| 80 | + - bypass_data_to_eval: any object type that will be passed unmodified to |
| 81 | + `evaluate_result()`. If not necessary: `None` |
| 82 | +
|
| 83 | + Note: script arguments can be accessed using `self._args.attr` |
| 84 | + """ |
| 85 | + |
| 86 | + tf.random.set_seed(10) |
| 87 | + |
| 88 | + inputs = tf.random.uniform( |
| 89 | + shape=(1, self._args.input_size, self._args.input_size, 3), |
| 90 | + maxval=255, |
| 91 | + dtype=tf.int32 |
| 92 | + ) |
| 93 | + |
| 94 | + dataset = tf.data.Dataset.from_tensor_slices(inputs) |
| 95 | + |
| 96 | + dataset = dataset.map( |
| 97 | + lambda x: {"inputs": tf.cast(x, tf.uint8)}, num_parallel_calls=tf.data.AUTOTUNE |
| 98 | + ) |
| 99 | + |
| 100 | + dataset = dataset.repeat() |
| 101 | + dataset = dataset.batch(self._args.batch_size) |
| 102 | + |
| 103 | + dataset = dataset.prefetch(tf.data.AUTOTUNE) |
| 104 | + return dataset, None |
| 105 | + |
| 106 | + def preprocess_model_inputs(self, data_batch): |
| 107 | + """This function prepare the `data_batch` generated from the dataset. |
| 108 | + Returns: |
| 109 | + x: input of the model |
| 110 | + y: data to be used for model evaluation |
| 111 | +
|
| 112 | + Note: script arguments can be accessed using `self._args.attr` """ |
| 113 | + |
| 114 | + return data_batch, None |
| 115 | + |
| 116 | + def postprocess_model_outputs(self, predictions, expected): |
| 117 | + """Post process if needed the predictions and expected tensors. At the |
| 118 | + minimum, this function transforms all TF Tensors into a numpy arrays. |
| 119 | + Most models will not need to modify this function. |
| 120 | +
|
| 121 | + Note: script arguments can be accessed using `self._args.attr` |
| 122 | + """ |
| 123 | + |
| 124 | + # NOTE : DO NOT MODIFY FOR NOW => We do not measure accuracy right now |
| 125 | + |
| 126 | + return predictions.numpy(), expected.numpy() |
| 127 | + |
| 128 | + def evaluate_model(self, predictions, expected, bypass_data_to_eval): |
| 129 | + """Evaluate result predictions for entire dataset. |
| 130 | +
|
| 131 | + This computes overall accuracy, mAP, etc. Returns the |
| 132 | + metric value and a metric_units string naming the metric. |
| 133 | +
|
| 134 | + Note: script arguments can be accessed using `self._args.attr` |
| 135 | + """ |
| 136 | + return None, "Raw Pitch Accuracy" |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + |
| 141 | + cmdline_api = CommandLineAPI() |
| 142 | + args = cmdline_api.parse_args() |
| 143 | + |
| 144 | + runner = BenchmarkRunner(args) |
| 145 | + runner.execute_benchmark() |
0 commit comments