Hi All
I want to calculate Area Under The Curve using trained model (.lite), before calculate it, i try to calculate accuracy using this files, but i think the accuracy value is not match with edge impulse dashboard result, my code is give result: 32.47% and edge impulse dashboard give 62.5%.
This is my code
import numpy as np
import tensorflow as tf
import json
# Load the TFLite model
model_path = '128/ei-driver-drowsiness-detection-3-object-detection-tensorflow-lite-float32-model.lite'
interpreter = tf.lite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()
# Get input and output details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Load the test data
X_test = np.load('ei-driver-drowsiness-detection-3-object-detection-1/data/X_split_test.npy').astype('float32')
# Making predictions
predictions = []
for i in range(len(X_test)):
interpreter.set_tensor(input_details[0]['index'], X_test[i:i+1])
interpreter.invoke()
prediction = interpreter.get_tensor(output_details[0]['index'])
predictions.append(prediction)
predictions = np.array(predictions).squeeze()
# Load the ground truth labels as JSON
with open('ei-driver-drowsiness-detection-3-object-detection-1/data/Y_split_test.json', 'r') as f:
Y_test = json.load(f)
# Extract the labels from the bounding boxes
Y_test_labels = []
for entry in Y_test:
labels = [box['label'] for box in entry['boundingBoxes']]
Y_test_labels.append(labels)
Y_test_labels = np.array(Y_test_labels)
# Assuming the last dimension of predictions corresponds to class probabilities
# Extract the predicted class by taking argmax over the class probabilities
# Typically, class probabilities are the last value, so we extract them.
predicted_class_labels = np.argmax(predictions[..., -1], axis=-1)
# Since Y_test_labels contains multiple bounding boxes per sample, we will flatten
Y_test_labels_flat = Y_test_labels.flatten()
# Compare the most confident class from the predictions
predicted_class_labels_flat = predicted_class_labels.flatten()
# Ensure both are the same length by truncating the longer one
min_len = min(len(predicted_class_labels_flat), len(Y_test_labels_flat))
predicted_class_labels_flat = predicted_class_labels_flat[:min_len]
Y_test_labels_flat = Y_test_labels_flat[:min_len]
# Set a threshold for classification (you can skip thresholding with argmax)
threshold = 0.5
predicted_labels = (predicted_class_labels_flat > threshold).astype(int)
# Calculate accuracy for multilabel classification (Hamming Loss)
hamming_loss = np.mean(np.not_equal(predicted_labels, Y_test_labels_flat))
accuracy = 1 - hamming_loss # accuracy is 1 - Hamming Loss
print(f'Hamming Loss: {hamming_loss}')
print(f'Accuracy: {accuracy * 100:.2f}%')
Anyone can help me to take a look that code, where the code must adjust?
Thank you