Skip to content

Instantly share code, notes, and snippets.

@AkiyonKS
Created August 9, 2022 05:16
Show Gist options
  • Save AkiyonKS/86a70523ffb7cc0be7edad3cc44b67e4 to your computer and use it in GitHub Desktop.
Save AkiyonKS/86a70523ffb7cc0be7edad3cc44b67e4 to your computer and use it in GitHub Desktop.
def evaluate_model(interpreter):
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
# Run predictions on ever y image in the "test" dataset.
prediction_digits = []
for i, photo_id in enumerate(df_photo_ids['test'].photo_id):
if i % 1000 == 0:
print('Evaluated on {n} results so far.'.format(n=i))
# Pre-processing: add batch dimension and convert to float32 to match with
# the model's input data format.
test_image = fetch_img_by_photo_id_and_resize(photo_id, (params["img_size"], params["img_size"]))
test_image = np.array([test_image], dtype=np.float32)
interpreter.set_tensor(input_index, test_image)
# Run inference.
interpreter.invoke()
# Post-processing: remove batch dimension and find the digit with highest probability.
output = interpreter.tensor(output_index)
digit = np.argmax(output()[0])
print(i, photo_id, digit)
prediction_digits.append(digit)
print('\n')
# Compare prediction results with ground truth labels to calculate accuracy.
prediction_digits = np.array(prediction_digits)
accuracy = (prediction_digits == df_photo_ids['test'].y).mean()
return accuracy
interpreter = tf.lite.Interpreter(model_content=quantized_and_pruned_tflite_model)
interpreter.allocate_tensors()
test_accuracy = evaluate_model(interpreter)
print('Pruned and quantized TFLite test_accuracy:', test_accuracy)
with open(model_filename + '.tflite', 'wb') as f:
f.write(quantized_and_pruned_tflite_model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment