emotion-reg / EvaluateEmotionDetector.py
geeknix's picture
Upload 6 files
cdd7ccc verified
import numpy as np
from keras.models import model_from_json
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix, classification_report,ConfusionMatrixDisplay
emotion_dict = {0: "Happy", 1: "Neutral", 2: "Sad"}
# load json and create model
json_file = open('model/emotion_model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
emotion_model = model_from_json(loaded_model_json)
# load weights into new model
emotion_model.load_weights("model/emotion_model.h5")
print("Loaded model from disk")
# Initialize image data generator with rescaling
test_data_gen = ImageDataGenerator(rescale=1./255)
# Preprocess all test images
test_generator = test_data_gen.flow_from_directory(
'data/test',
target_size=(48, 48),
batch_size=64,
color_mode="grayscale",
class_mode='categorical',
classes=['Happy', 'Neutral', 'Sad'])
# do prediction on test data
predictions = emotion_model.predict(test_generator)
# see predictions
for result in predictions:
max_index = int(np.argmax(result))
print(emotion_dict[max_index])
print("-----------------------------------------------------------------")
# confusion matrix
c_matrix = confusion_matrix(test_generator.classes, predictions.argmax(axis=1))
print(c_matrix)
cm_display = ConfusionMatrixDisplay(confusion_matrix=c_matrix, display_labels=list(emotion_dict.values()))
cm_display.plot(cmap=plt.cm.Blues)
plt.show()
# Classification report
print("-----------------------------------------------------------------")
print(classification_report(test_generator.classes, predictions.argmax(axis=1)))