Documented the return value and added percentile to argparser
This commit is contained in:
parent
38737849e6
commit
f8add5ad42
|
@ -1,17 +1,3 @@
|
||||||
# Copyright 2023 The MediaPipe Authors.
|
|
||||||
#
|
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
# you may not use this file except in compliance with the License.
|
|
||||||
# You may obtain a copy of the License at
|
|
||||||
#
|
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
#
|
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
# See the License for the specific language governing permissions and
|
|
||||||
# limitations under the License.
|
|
||||||
"""Benchmark for the image classifier task."""
|
|
||||||
import argparse
|
import argparse
|
||||||
import time
|
import time
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
@ -22,16 +8,21 @@ from mediapipe.tasks.python import vision
|
||||||
_IMAGE_FILE = 'burger.jpg'
|
_IMAGE_FILE = 'burger.jpg'
|
||||||
|
|
||||||
|
|
||||||
def run(model: str, n_iterations: int, delegate: python.BaseOptions.Delegate):
|
def run(model: str, n_iterations: int, delegate: python.BaseOptions.Delegate,
|
||||||
|
percentile: float):
|
||||||
"""Run asynchronous inference on images and benchmark.
|
"""Run asynchronous inference on images and benchmark.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
model: Path to the TFLite model.
|
model: Path to the TFLite model.
|
||||||
n_iterations: Number of iterations to run the benchmark.
|
n_iterations: Number of iterations to run the benchmark.
|
||||||
delegate: CPU or GPU delegate for inference.
|
delegate: CPU or GPU delegate for inference.
|
||||||
|
percentile: Percentage for the percentiles to compute. Values must be
|
||||||
|
between 0 and 100 inclusive.
|
||||||
|
Returns:
|
||||||
|
The n-th percentile of the inference times.
|
||||||
"""
|
"""
|
||||||
inference_times = []
|
inference_times = []
|
||||||
|
|
||||||
# Initialize the image classifier
|
# Initialize the image classifier
|
||||||
base_options = python.BaseOptions(model_asset_path=model, delegate=delegate)
|
base_options = python.BaseOptions(model_asset_path=model, delegate=delegate)
|
||||||
options = vision.ImageClassifierOptions(
|
options = vision.ImageClassifierOptions(
|
||||||
|
@ -48,7 +39,7 @@ def run(model: str, n_iterations: int, delegate: python.BaseOptions.Delegate):
|
||||||
inference_times.append((end_time_ns - start_time_ns) / 1_000_000)
|
inference_times.append((end_time_ns - start_time_ns) / 1_000_000)
|
||||||
|
|
||||||
classifier.close()
|
classifier.close()
|
||||||
return np.percentile(inference_times, 95)
|
return np.percentile(inference_times, percentile)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -60,15 +51,22 @@ def main():
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--iterations', help='Number of iterations for benchmarking.', type=int,
|
'--iterations', help='Number of iterations for benchmarking.', type=int,
|
||||||
default=100)
|
default=100)
|
||||||
|
parser.add_argument(
|
||||||
|
'--percentile', help='Percentile for benchmarking statistics.',
|
||||||
|
type=float, default=95.0)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Run benchmark on CPU
|
# Run benchmark on CPU
|
||||||
cpu_time = run(args.model, args.iterations, python.BaseOptions.Delegate.CPU)
|
cpu_time = run(args.model, args.iterations, python.BaseOptions.Delegate.CPU,
|
||||||
print(f"95th Percentile Inference Time on CPU: {cpu_time:.6f} milliseconds")
|
args.percentile)
|
||||||
|
print(f"{args.percentile}th Percentile Inference Time on CPU: "
|
||||||
|
f"{cpu_time:.6f} milliseconds")
|
||||||
|
|
||||||
# Run benchmark on GPU
|
# Run benchmark on GPU
|
||||||
gpu_time = run(args.model, args.iterations, python.BaseOptions.Delegate.GPU)
|
gpu_time = run(args.model, args.iterations, python.BaseOptions.Delegate.GPU,
|
||||||
print(f"95th Percentile Inference Time on GPU: {gpu_time:.6f} milliseconds")
|
args.percentile)
|
||||||
|
print(f"{args.percentile}th Percentile Inference Time on GPU: "
|
||||||
|
f"{gpu_time:.6f} milliseconds")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user