Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit f7571a2

Browse files
author
DEKHTIARJonathan
committed
[TF-TRT] Adding TF-Hub YAMNet
1 parent ba4865d commit f7571a2

File tree

4 files changed

+287
-0
lines changed

4 files changed

+287
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The given SavedModel SignatureDef contains the following input(s):
2+
inputs['waveform'] tensor_info:
3+
dtype: DT_FLOAT
4+
shape: (-1)
5+
name: serving_default_waveform:0
6+
The given SavedModel SignatureDef contains the following output(s):
7+
outputs['output_0'] tensor_info:
8+
dtype: DT_FLOAT
9+
shape: (-1, 521)
10+
name: StatefulPartitionedCall:0
11+
outputs['output_1'] tensor_info:
12+
dtype: DT_FLOAT
13+
shape: (-1, 1024)
14+
name: StatefulPartitionedCall:1
15+
outputs['output_2'] tensor_info:
16+
dtype: DT_FLOAT
17+
shape: (-1, 64)
18+
name: StatefulPartitionedCall:2
19+
Method name is: tensorflow/serving/predict
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 os
19+
import sys
20+
21+
import numpy as np
22+
import tensorflow as tf
23+
24+
# Allow import of top level python files
25+
import inspect
26+
27+
currentdir = os.path.dirname(
28+
os.path.abspath(inspect.getfile(inspect.currentframe()))
29+
)
30+
parentdir = os.path.dirname(currentdir)
31+
parentdir = os.path.dirname(parentdir)
32+
33+
sys.path.insert(0, parentdir)
34+
35+
from benchmark_args import BaseCommandLineAPI
36+
from benchmark_runner import BaseBenchmarkRunner
37+
38+
39+
class CommandLineAPI(BaseCommandLineAPI):
40+
41+
def __init__(self):
42+
super(CommandLineAPI, self).__init__()
43+
44+
self._parser.add_argument(
45+
"--frame_length",
46+
type=int,
47+
default=1,
48+
help="Input audio frame length in seconds."
49+
)
50+
51+
52+
class BenchmarkRunner(BaseBenchmarkRunner):
53+
54+
def get_dataset_batches(self):
55+
"""Returns a list of batches of input samples.
56+
57+
Each batch should be in the form [x, y], where
58+
x is a numpy array of the input samples for the batch, and
59+
y is a numpy array of the expected model outputs for the batch
60+
61+
Returns:
62+
- dataset: a TF Dataset object
63+
- bypass_data_to_eval: any object type that will be passed unmodified to
64+
`evaluate_result()`. If not necessary: `None`
65+
66+
Note: script arguments can be accessed using `self._args.attr`
67+
"""
68+
# Input is a numpy array of arbitrary length
69+
70+
# generates a wave of 16kHz for frame_length number of seconds
71+
wave = np.array(
72+
np.sin(np.linspace(-np.pi, np.pi, 16000 * self._args.frame_length)),
73+
dtype=np.float32
74+
)
75+
76+
waves = np.expand_dims(wave, axis=0)
77+
78+
dataset = tf.data.Dataset.from_tensor_slices(waves)
79+
dataset = dataset.repeat()
80+
81+
dataset = dataset.prefetch(tf.data.AUTOTUNE)
82+
return dataset, None
83+
84+
def preprocess_model_inputs(self, data_batch):
85+
"""This function prepare the `data_batch` generated from the dataset.
86+
Returns:
87+
x: input of the model
88+
y: data to be used for model evaluation
89+
90+
Note: script arguments can be accessed using `self._args.attr`
91+
"""
92+
93+
x = data_batch
94+
return x, None
95+
96+
def postprocess_model_outputs(self, predictions, expected):
97+
"""Post process if needed the predictions and expected tensors. At the
98+
minimum, this function transforms all TF Tensors into a numpy arrays.
99+
Most models will not need to modify this function.
100+
101+
Note: script arguments can be accessed using `self._args.attr`
102+
"""
103+
104+
# NOTE : DO NOT MODIFY FOR NOW => We do not measure accuracy right now
105+
106+
return predictions.numpy(), expected.numpy()
107+
108+
def evaluate_model(self, predictions, expected, bypass_data_to_eval):
109+
"""Evaluate result predictions for entire dataset.
110+
111+
This computes overall accuracy, mAP, etc. Returns the
112+
metric value and a metric_units string naming the metric.
113+
114+
Note: script arguments can be accessed using `self._args.attr`
115+
"""
116+
return None, "Top-10 Accuracy"
117+
118+
119+
if __name__ == '__main__':
120+
121+
cmdline_api = CommandLineAPI()
122+
args = cmdline_api.parse_args()
123+
124+
runner = BenchmarkRunner(args)
125+
runner.execute_benchmark()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
4+
5+
BASE_BENCHMARK_DATA_EXPORT_DIR="${BASE_DIR}/benchmark_data"
6+
rm -rf ${BASE_BENCHMARK_DATA_EXPORT_DIR}
7+
mkdir -p ${BASE_BENCHMARK_DATA_EXPORT_DIR}
8+
9+
# Default Argument Values
10+
BYPASS_ARGUMENTS=""
11+
MODEL_DIR="/models/tf_hub/YAMNet"
12+
DATA_DIR="/tmp/"
13+
FRAME_LENGTH="1"
14+
15+
# List of models. YAMNet is a single implementation
16+
MODELS=(
17+
"YAMNet"
18+
)
19+
20+
RUN_ARGS="--data_dir=${DATA_DIR} --input_saved_model_dir=${MODEL_DIR} --display_every=50 --frame_length=${FRAME_LENGTH}"
21+
TF_TRT_ARGS="--use_tftrt --use_dynamic_shape --num_calib_batches=10"
22+
TF_XLA_ARGS="--use_xla_auto_jit"
23+
24+
export TF_TRT_SHOW_DETAILED_REPORT=1
25+
26+
for model_name in "${MODELS[@]}"; do
27+
echo "Processing Model: ${model_name} ..."
28+
29+
MODEL_DATA_EXPORT_DIR="${BASE_BENCHMARK_DATA_EXPORT_DIR}/${model_name}"
30+
mkdir -p ${MODEL_DATA_EXPORT_DIR}
31+
32+
# ============================ TF NATIVE ============================ #
33+
# TF Native - FP32
34+
echo "Running ${BASE_DIR}/run_inference.sh"
35+
script -q -c "${BASE_DIR}/run_inference.sh ${RUN_ARGS} --precision=FP32" /dev/null | tee ${MODEL_DATA_EXPORT_DIR}/inference_tf_native_fp32.log
36+
37+
# TF Native - FP16
38+
script -q -c "${BASE_DIR}/run_inference.sh ${RUN_ARGS} --precision=FP16" /dev/null | tee ${MODEL_DATA_EXPORT_DIR}/inference_tf_native_fp16.log
39+
40+
# ============================ TF XLA ============================ #
41+
# TF XLA - FP32
42+
script -q -c "${BASE_DIR}/run_inference.sh ${RUN_ARGS} --precision=FP32 ${TF_XLA_ARGS}" /dev/null | tee ${MODEL_DATA_EXPORT_DIR}/inference_tf_xla_fp32.log
43+
44+
# TF XLA - FP16
45+
script -q -c "${BASE_DIR}/run_inference.sh ${RUN_ARGS} --precision=FP16 ${TF_XLA_ARGS}" /dev/null | tee ${MODEL_DATA_EXPORT_DIR}/inference_tf_xla_fp16.log
46+
47+
# ============================ TF-TRT ============================ #
48+
# TF-TRT - FP32
49+
script -q -c "TF_TRT_EXPORT_GRAPH_VIZ_PATH=${MODEL_DATA_EXPORT_DIR}/tftrt_fp32.dot ${BASE_DIR}/run_inference.sh ${RUN_ARGS} --precision=FP32 ${TF_TRT_ARGS}" /dev/null | tee ${MODEL_DATA_EXPORT_DIR}/inference_tftrt_fp32.log
50+
51+
# TF-TRT - FP16
52+
script -q -c "TF_TRT_EXPORT_GRAPH_VIZ_PATH=${MODEL_DATA_EXPORT_DIR}/tftrt_fp16.dot ${BASE_DIR}/run_inference.sh ${RUN_ARGS} --precision=FP16 ${TF_TRT_ARGS}" /dev/null | tee ${MODEL_DATA_EXPORT_DIR}/inference_tftrt_fp16.log
53+
54+
# TF-TRT - INT8
55+
script -q -c "TF_TRT_EXPORT_GRAPH_VIZ_PATH=${MODEL_DATA_EXPORT_DIR}/tftrt_int8.dot ${BASE_DIR}/run_inference.sh ${RUN_ARGS} --precision=INT8 ${TF_TRT_ARGS}" /dev/null | tee ${MODEL_DATA_EXPORT_DIR}/inference_tftrt_int8.log
56+
done
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
3+
nvidia-smi
4+
5+
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
6+
7+
BATCH_SIZE="1"
8+
OUTPUT_TENSOR_NAMES="output_0,output_1,output_2"
9+
NUM_ITERATIONS="1000"
10+
11+
# Loop through arguments and process them
12+
for arg in "$@"
13+
do
14+
case $arg in
15+
--batch_size=*)
16+
shift # Remove --batch_size= from processing
17+
;;
18+
--num_iterations=*)
19+
NUM_ITERATIONS="${arg#*=}"
20+
shift # Remove --num_iterations= from processing
21+
;;
22+
--output_tensors_name=*)
23+
OUTPUT_TENSOR_NAMES="${arg#*=}"
24+
shift # Remove --output_tensors_name= from processing
25+
;;
26+
--data_dir=*)
27+
DATA_DIR="${arg#*=}"
28+
shift # Remove --data_dir= from processing
29+
;;
30+
--input_saved_model_dir=*)
31+
MODEL_DIR="${arg#*=}"
32+
shift # Remove --input_saved_model_dir= from processing
33+
;;
34+
--total_max_samples=*)
35+
shift # Remove --total_max_samples= from processing
36+
;;
37+
--frame_length=*)
38+
FRAME_LENGTH="${arg#*=}"
39+
shift # Remove --frame_length= from processing
40+
;;
41+
*)
42+
BYPASS_ARGUMENTS="${BYPASS_ARGUMENTS} ${arg}"
43+
;;
44+
esac
45+
done
46+
47+
# Trimming front and back whitespaces
48+
BYPASS_ARGUMENTS=$(echo ${BYPASS_ARGUMENTS} | tr -s " ")
49+
50+
echo -e "\n********************************************************************"
51+
echo "[*] DATA_DIR: ${DATA_DIR}"
52+
echo "[*] MODEL_DIR: ${MODEL_DIR}"
53+
echo ""
54+
echo "[*] BATCH_SIZE: ${BATCH_SIZE}"
55+
echo "[*] NUM_ITERATIONS: ${NUM_ITERATIONS}"
56+
echo ""
57+
echo "[*] FRAME_LENGTH: ${FRAME_LENGTH}"
58+
echo "[*] OUTPUT_TENSOR_NAMES: ${OUTPUT_TENSOR_NAMES}"
59+
echo ""
60+
echo "[*] BYPASS_ARGUMENTS: ${BYPASS_ARGUMENTS}"
61+
echo -e "********************************************************************\n"
62+
63+
64+
if [[ ! -d ${DATA_DIR} ]]; then
65+
echo "ERROR: \`--data_dir=/path/to/directory\` does not exist. [Received: \`${DATA_DIR}\`]"
66+
exit 1
67+
fi
68+
69+
if [[ ! -d ${MODEL_DIR} ]]; then
70+
echo "ERROR: \`--input_saved_model_dir=/path/to/directory\` does not exist. [Received: \`${MODEL_DIR}\`]"
71+
exit 1
72+
fi
73+
74+
set -x
75+
76+
python ${BASE_DIR}/infer.py \
77+
--data_dir=${DATA_DIR} \
78+
--calib_data_dir=${DATA_DIR} \
79+
--input_saved_model_dir=${MODEL_DIR} \
80+
--output_tensors_name=${OUTPUT_TENSOR_NAMES} \
81+
--batch_size=${BATCH_SIZE} \
82+
--frame_length=${FRAME_LENGTH} \
83+
`# The following is set because we will be running synthetic benchmarks` \
84+
--use_synthetic_data \
85+
--num_iterations=${NUM_ITERATIONS} \
86+
--total_max_samples=1 \
87+
${BYPASS_ARGUMENTS}

0 commit comments

Comments
 (0)