# Path: benchmarks/benchmark_utils.py
# def parse_args():
# def check_cpu_scaling_governor_status():
# def machine_info():
# def test_data_path(path, *paths):
# def reference_path(path, *paths):
# def benchmark_result_path(path, *paths):
# def test_image(path, *paths):
# def read_reference(file_name):
# def check_result(reference, result_list, enable_assertion):
# def save_as_csv(file_name, result):
# TEST_DATA_DIR = os.path.join(
#     os.path.dirname(os.path.realpath(__file__)), '..', 'test_data')
# REFERENCE_DATA_DIR = os.path.join(
#     os.path.dirname(os.path.realpath(__file__)), 'reference')
# BENCHMARK_RESULT_DIR = os.path.join(tempfile.gettempdir(), 'result')
# 
# Path: pycoral/utils/edgetpu.py
# _EDGETPU_SHARED_LIB = {
#   'Linux': 'libedgetpu.so.1',
#   'Darwin': 'libedgetpu.1.dylib',
#   'Windows': 'edgetpu.dll'
# }[platform.system()]
# def load_edgetpu_delegate(options=None):
# def make_interpreter(model_path_or_content, device=None, delegate=None):
# def _is_valid_ctypes_input(input_data):
# def _gst_buffer_map(buffer):
# def _check_input_size(input_size, expected_input_size):
# def run_inference(interpreter, input_data):
# class _GstMapInfo(ctypes.Structure):


import sys
import time
import timeit
import numpy as np
from benchmarks import benchmark_utils
from pycoral.utils import edgetpu
def run_benchmark(model, delegate):
  """Returns average inference time in ms on specified model with random input."""

  print('Benchmark for [%s]' % model)
  print('model path = %s' % benchmark_utils.test_data_path(model))
  interpreter = edgetpu.make_interpreter(
      benchmark_utils.test_data_path(model), delegate=delegate)
  interpreter.allocate_tensors()
  iterations = 200 if 'edgetpu' in model else 20

  input_tensor = interpreter.tensor(interpreter.get_input_details()[0]['index'])
  np.random.seed(12345)
  input_tensor()[0] = np.random.randint(
      0, 256, size=input_tensor().shape[1:], dtype=np.uint8)
  result = 1000 * timeit.timeit(
      interpreter.invoke, number=iterations) / iterations

  print('%.2f ms (iterations = %d)' % (result, iterations))
  return result


def main():
  print('Python version: ', sys.version)
  args = benchmark_utils.parse_args()
  machine = benchmark_utils.machine_info()
  benchmark_utils.check_cpu_scaling_governor_status()
  models, reference = benchmark_utils.read_reference(
      'inference_reference_%s.csv' % machine)

  results = [('MODEL', 'INFERENCE_TIME')]

# Path: benchmarks/benchmark_utils.py
# def parse_args():
# def check_cpu_scaling_governor_status():
# def machine_info():
# def test_data_path(path, *paths):
# def reference_path(path, *paths):
# def benchmark_result_path(path, *paths):
# def test_image(path, *paths):
# def read_reference(file_name):
# def check_result(reference, result_list, enable_assertion):
# def save_as_csv(file_name, result):
# TEST_DATA_DIR = os.path.join(
#     os.path.dirname(os.path.realpath(__file__)), '..', 'test_data')
# REFERENCE_DATA_DIR = os.path.join(
#     os.path.dirname(os.path.realpath(__file__)), 'reference')
# BENCHMARK_RESULT_DIR = os.path.join(tempfile.gettempdir(), 'result')
# 
# Path: pycoral/utils/edgetpu.py
# _EDGETPU_SHARED_LIB = {
#   'Linux': 'libedgetpu.so.1',
#   'Darwin': 'libedgetpu.1.dylib',
#   'Windows': 'edgetpu.dll'
# }[platform.system()]
# def load_edgetpu_delegate(options=None):
# def make_interpreter(model_path_or_content, device=None, delegate=None):
# def _is_valid_ctypes_input(input_data):
# def _gst_buffer_map(buffer):
# def _check_input_size(input_size, expected_input_size):
# def run_inference(interpreter, input_data):
# class _GstMapInfo(ctypes.Structure):


# Path: benchmarks/inference_benchmarks.py
import sys
import time
import timeit
import numpy as np
from benchmarks import benchmark_utils
from pycoral.utils import edgetpu

def run_benchmark(model, delegate):
  """Returns average inference time in ms on specified model with random input."""

  print('Benchmark for [%s]' % model)
  print('model path = %s' % benchmark_utils.test_data_path(model))
  interpreter = edgetpu.make_interpreter(
      benchmark_utils.test_data_path(model), delegate=delegate)
  interpreter.allocate_tensors()
  iterations = 200 if 'edgetpu' in model else 20

  input_tensor = interpreter.tensor(interpreter.get_input_details()[0]['index'])
  np.random.seed(12345)
  input_tensor()[0] = np.random.randint(
      0, 256, size=input_tensor().shape[1:], dtype=np.uint8)
  result = 1000 * timeit.timeit(
      interpreter.invoke, number=iterations) / iterations

  print('%.2f ms (iterations = %d)' % (result, iterations))
  return result


def main():
  print('Python version: ', sys.version)
  args = benchmark_utils.parse_args()
  machine = benchmark_utils.machine_info()
  benchmark_utils.check_cpu_scaling_governor_status()
  models, reference = benchmark_utils.read_reference(
      'inference_reference_%s.csv' % machine)

  results = [('MODEL', 'INFERENCE_TIME')]

  delegate = edgetpu.load_edgetpu_delegate()