def preprocess_ml_data(): data = pd.read_csv(path) pixel_list = data['pixels'].tolist() emotions = data['emotion'].tolist() faces = [] for pixels in pixel_list: face = string_to_nparray(pixels) face = face / float(255) faces.append(face) save_pck(faces, './ml_data/X') save_pck(emotions, './ml_data/y')
def support_vector(): svc = svm.SVC(kernel='linear') svc.fit(X_train, y_train) save_pck(svc, './ml_model_saves/svm') # svc = load('./ml_model_saves/svm.pck') svc_predict = svc.predict(X_val) print(classification_report(y_val, svc_predict)) cnf_matrix = (confusion_matrix(y_val, svc_predict)) plt.figure() plot_confusion_matrix(cnf_matrix, classes=EMOTIONS, normalize=True, title='Normalized confusion matrix SVM') plt.show()
def naive_bayes(): gnb = GaussianNB() gnb.fit(X_train, y_train) save_pck(gnb, './ml_model_saves/gnb') # gnb = load('./ml_model_saves/gnb.pck') gnb_predict = gnb.predict(X_val) print(classification_report(y_val, gnb_predict)) cnf_matrix = (confusion_matrix(y_val, gnb_predict)) plt.figure() plot_confusion_matrix(cnf_matrix, classes=EMOTIONS, normalize=True, title='Normalized confusion matrix NB') plt.show()
def linear_disc(): lda = LinearDiscriminantAnalysis() lda.fit(X_train, y_train) save_pck(lda, './ml_model_saves/lda') # lda = load('./ml_model_saves/lda.pck') lda_predict = lda.predict(X_val) print(classification_report(y_val, lda_predict)) cnf_matrix = (confusion_matrix(y_val, lda_predict)) plt.figure() plot_confusion_matrix(cnf_matrix, classes=EMOTIONS, normalize=True, title='Normalized confusion matrix LDA') plt.show()
def random_forests(): rfc = RandomForestClassifier() rfc.fit(X_train, y_train) save_pck(rfc, './ml_model_saves/rfc') # rfc = load('./ml_model_saves/rfc.pck') rfc_predict = rfc.predict(X_val) print(classification_report(y_val, rfc_predict)) cnf_matrix = (confusion_matrix(y_val, rfc_predict)) plt.figure() plot_confusion_matrix(cnf_matrix, classes=EMOTIONS, normalize=True, title='Normalized confusion matrix RF') plt.show()
def ada_boost(): adc = AdaBoostClassifier() adc.fit(X_train, y_train) save_pck(adc, './ml_model_saves/adc') # adc = load('./ml_model_saves/adc.pck') adc_predict = adc.predict(X_val) print(classification_report(y_val, adc_predict)) cnf_matrix = (confusion_matrix(y_val, adc_predict)) plt.figure() plot_confusion_matrix(cnf_matrix, classes=EMOTIONS, normalize=True, title='Normalized confusion matrix AB') plt.show()
def preprocess_cnn_data(): data = pd.read_csv(path) pixel_list = data['pixels'].tolist() emotions = data['emotion'].tolist() faces = [] for pixels in pixel_list: face = string_to_nparray(pixels) face = face.reshape(48, 48) face = face / float(255) faces.append(face) # # to flip the image (commenting out since no significant increase in accuracy). # for pixels in pixel_list: # face = string_to_nparray(pixels) # face = face.reshape(48, 48) # face = face / float(255) # faces.append(cv2.flip(face, 1)) # emotions = emotions + emotions save_pck(faces, './cnn_data/X') save_pck(emotions, './cnn_data/y')