Skip to content

Instantly share code, notes, and snippets.

@Namburger
Created July 10, 2020 16:30
Show Gist options
  • Save Namburger/39c8da325c154ab126a44ef5e203efb3 to your computer and use it in GitHub Desktop.
Save Namburger/39c8da325c154ab126a44ef5e203efb3 to your computer and use it in GitHub Desktop.
import numpy as np
import sys
from tflite_runtime.interpreter import Interpreter
from tflite_runtime.interpreter import load_delegate
import time
if len(sys.argv) < 2:
print('Usage:', sys.argv[0], 'model_path')
exit()
def main():
"""Runs inference with an input tflite model."""
model_path = str(sys.argv[1])
if model_path.endswith('edgetpu.tflite'):
print('initialized for edgetpu')
delegates = [load_delegate('libedgetpu.so.1.0')]
interpreter = Interpreter(model_path, experimental_delegates=delegates)
else:
print('initialized for cpu')
interpreter = Interpreter(model_path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
images = np.zeros(input_details[0]['shape'], input_details[0]['dtype'])
#print(images)
interpreter.set_tensor(input_details[0]['index'], images)
t0 = time.perf_counter()
for i in range(1000):
interpreter.invoke()
print('time took for 1000 invokes:', time.perf_counter() - t0)
output_details = interpreter.get_output_details()
outputs = interpreter.get_tensor(output_details[0]['index'])
print(outputs)
print('Success.')
if __name__== '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment