I am using the tensorflow function below to evaluate the accuracy of tflite models. Can you please add more evaluation metrics (precision, recall, f1-score).
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
def evaluate_tflite(model_path, dataset):
# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Define a list to store the predictions and labels
predictions = []
labels = []
# Loop through the entire evaluation dataset
for images, labels_batch in dataset:
print('-', end=' ')
# Loop through each image in the batch
for image in images:
# Add a batch dimension
image = tf.expand_dims(image, axis=0)
# Set the tensor to point to the input data to be inferred
interpreter.set_tensor(input_details[0]['index'], image)
# Run inference
interpreter.invoke()
# Get the output tensor
output_data = interpreter.get_tensor(output_details[0]['index'])
# Append the prediction to the list
predictions.append(output_data)
# Append the labels to the list
labels.extend(labels_batch)
# Convert the predictions and labels to numpy arrays
predictions = np.array(predictions)
#predictions = predictions.astype(int)
predictions = predictions.reshape(256, 23)
labels = np.array(labels)
#labels = labels.astype(int)
# Calculate the accuracy using categorical accuracy
accuracy = tf.keras.metrics.categorical_accuracy(labels, predictions)
# Calculate the average accuracy across all batches
average_accuracy = tf.reduce_mean(accuracy)
# Print the average accuracy
print(f"Average accuracy: {average_accuracy}")
Assistant
Here is the updated code with additional metrics:
python
def evaluate_tflite(model_path, dataset):
# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Define lists to store the predictions and labels
predictions = []
labels = []
# Loop through the entire evaluation dataset
for images, labels_batch in dataset:
print('-', end=' ')
# Loop through each image in the batch
for image in images:
# Add a batch dimension
image = tf.expand_dims(image, axis=0)
# Set the tensor to point to the input data to be inferred
interpreter.set_tensor(input_details[0]['index'], image)
# Run inference
interpreter.invoke()
# Get the output tensor
output_data = interpreter.get_tensor(output_details[0]['index'])
# Append the prediction to the list
predictions.append(output_data)
# Append the labels to the list
labels.extend(labels_batch)
# Convert the predictions and labels to numpy arrays
predictions = np.array(predictions)
predictions = predictions.astype(int)
predictions = predictions.reshape(256, 23)
labels = np.array(labels)
labels = labels.astype(int)
# Calculate the accuracy, precision, recall and f1-score
accuracy = tf.keras.metrics.categorical_accuracy(labels, predictions)
precision = tf.keras.metrics.Precision(labels, predictions)
recall = tf.keras.metrics.Recall(labels, predictions)
f1_score = tf.keras.metrics.F1Score(labels, predictions)
# Calculate the average metrics across all batches
average_accuracy = tf.reduce_mean(accuracy)
average_precision = tf.reduce_mean(precision)
average_recall = tf.reduce_mean(recall)
average_f1_score = tf.reduce_mean(f1_score)
# Print the metrics
print(f"Average Accuracy: {average_accuracy}")
print(f"Average Precision: {average_precision}")
print(f"Average Recall: {average_recall}")
print(f"Average F1 Score: {average_f1_score}")