#!/usr/bin/env python
# -*- coding: utf-8 -*-
from keras.models import load_model
from sklearn.metrics import confusion_matrix
from marcos import exp_dir
import matplotlib.pyplot as plt
def plot_confusion_matrix(cm, classes,
title='Confusion matrix',
cmap=plt.cm.jet):
"""
This function prints and plots the confusion matrix.
"""
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, '{:.2f}'.format(cm[i, j]), horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
def main():
model_path = os.path.join(exp_dir,store_path,'model.h5')
emotion_classifier = load_model(model_path)
np.set_printoptions(precision=2)
dev_feats = read_dataset('valid')
predictions = emotion_classifier.predict_classes(dev_feats)
te_labels = get_labels('valid')
conf_mat = confusion_matrix(te_labels,predictions)
plt.figure()
plot_confusion_matrix(conf_mat, classes=["Angry","Disgust","Fear","Happy","Sad","Surprise","Neutral"])
plt.show()