Created
February 23, 2020 11:18
-
-
Save TheBojda/84da5a81025ca2b5ea3a2e2be2af8b79 to your computer and use it in GitHub Desktop.
Load and run a TFLite model in Python from https://www.tensorflow.org/lite/guide/inference#load_and_run_a_model_in_python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import tensorflow as tf | |
# Load TFLite model and allocate tensors. | |
interpreter = tf.lite.Interpreter(model_path="converted_model.tflite") | |
interpreter.allocate_tensors() | |
# Get input and output tensors. | |
input_details = interpreter.get_input_details() | |
output_details = interpreter.get_output_details() | |
# Test model on random input data. | |
input_shape = input_details[0]['shape'] | |
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32) | |
interpreter.set_tensor(input_details[0]['index'], input_data) | |
interpreter.invoke() | |
# The function `get_tensor()` returns a copy of the tensor data. | |
# Use `tensor()` in order to get a pointer to the tensor. | |
output_data = interpreter.get_tensor(output_details[0]['index']) | |
print(output_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment