-
Notifications
You must be signed in to change notification settings - Fork 0
/
evPMS.py
54 lines (39 loc) · 1.72 KB
/
evPMS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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: "Angry", 1: "Disgusted", 2: "Discomfort", 3: "Happy", 4: "Neutral", 5: "Sad", 6: "Pain"}
# 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')
# do prediction on test data
predictions = emotion_model.predict_generator(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=emotion_dict)
cm_display.plot(cmap=plt.cm.Blues)
plt.show()
# Classification report
print("-----------------------------------------------------------------")
print(classification_report(test_generator.classes, predictions.argmax(axis=1)))