Skip to content

Instantly share code, notes, and snippets.

@Tony607
Created May 19, 2019 12:24
Show Gist options
  • Save Tony607/01de737ce699492f64f64bf51297f637 to your computer and use it in GitHub Desktop.
Save Tony607/01de737ce699492f64f64bf51297f637 to your computer and use it in GitHub Desktop.
How to compress your Keras model x5 smaller with TensorFlow model optimization | DLology
import numpy as np
interpreter = tf.lite.Interpreter(model_path=str(tflite_model_file))
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
def eval_model(interpreter, x_test, y_test):
total_seen = 0
num_correct = 0
for img, label in zip(x_test, y_test):
inp = img.reshape((1, 28, 28, 1))
total_seen += 1
interpreter.set_tensor(input_index, inp)
interpreter.invoke()
predictions = interpreter.get_tensor(output_index)
if np.argmax(predictions) == np.argmax(label):
num_correct += 1
if total_seen % 1000 == 0:
print("Accuracy after %i images: %f" %
(total_seen, float(num_correct) / float(total_seen)))
return float(num_correct) / float(total_seen)
print(eval_model(interpreter, x_test, y_test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment