def run_SVC(): print('# ======================= SVC ========================= #') svc = SVC(kernel='linear', C=1) svc.fit(train_x, train_y) pred_y = svc.predict(test_x) score, confusion_matrix, report = report_results(test_y, np.array(pred_y), cmat_flag=True) print()
def run_kNN(): print('# ======================= K-Nearest Neighbors ========================= #') acc_list = [] for k in K_LIST: knn = KNeighborsClassifier(n_neighbors=k) knn.fit(train_x, train_y) knn_pred_y = knn.predict(test_x) print('k-neighbors: %d' % k) acc, cmat, rpt = report_results(test_y, knn_pred_y) acc_list.append(acc) print()
def run_RandomForest(): print('# ======================= Random Forest ========================= #') acc_list_rf = [] for n in [25]: rand_forest = RandomForestClassifier(n_estimators=n) rand_forest.fit(train_x, train_y) rf_pred_y = rand_forest.predict(test_x) print('Tree count: %d' % n) acc, cm, rpt = report_results(test_y, rf_pred_y, cmat_flag=True) acc_list_rf.append(acc) task5(cm, 'Random Forest (25 trees)') print()
# for img, lbl in zip(test_faces, test_labels): # key_points, descriptors = extract_sift(img) # pred_list = rand_forest.predict(descriptors) # counts = np.bincount(pred_list) # bin counting for upcoming argmax # pred = np.argmax(counts) # pred_y.append(pred) # test_y.append(lbl) # score, confusion_matrix, report = report_results_face(test_y, pred_y) # acc_list_rf.append(score) # print('Tree count: %d \t Accuracy: %.3f' % (n, score)) # print() print('# ======================= SVC ========================= #') c = list(zip(train_x, train_y)) random.shuffle(c) train_x, train_y = zip(*c) svc = SVC(kernel='rbf', C=10, gamma=0.00001) svc.fit(train_x, train_y) test_y, pred_y = [], [] for img, lbl in zip(test_faces, test_labels): key_points, descriptors = extract_sift(img) pred_list = svc.predict(descriptors) counts = np.bincount(pred_list) # bin counting for upcoming argmax pred = np.argmax(counts) pred_y.append(pred) test_y.append(lbl) score, confusion_matrix, report = report_results(test_y, np.array(pred_y), cmat_flag=True)
pred_gait = gait_model.predict_proba(gait_test_x) pred_gait_avg = np.zeros((0, 10)) for i in range(10): t2 = pred_gait[i * 8:(i + 1) * 8, :] # pick 8 rows pred_gait_avg = np.vstack((pred_gait_avg, t2.mean(axis=0))) alpha_list = [x / 10.0 for x in range(7, 8)] acc_list = [] for alpha in alpha_list: print('alpha=%.2f' % alpha) acc_count = 0 pred_y = [] for i in range( face_test_x.shape[1]): # loop through all test face images true_y = face_test_y[i] pf = pred_face[i, :] # face prediction vector of shape (10,) gf = pred_gait_avg[ true_y, :] # gait prediction vector of shape (10,) fused_pred_vector = alpha * pf + (1 - alpha) * gf fused_pred = np.argmax(fused_pred_vector) pred_y.append(fused_pred) acc_count += 1 if fused_pred == true_y else 0 acc, cm, rpt = report_results(face_test_y, pred_y, cmat_flag=True) task8(cm) acc = acc_count / face_test_x.shape[1] acc_list.append(acc) print('\tAccuracy: %.2f' % acc) # question5(alpha_list, acc_list)
# test_y, pred_y = [], [] # for img, lbl in zip(test_faces, test_labels): # key_points, descriptors = extract_surf(img) # pred_list = knn.predict(descriptors) # counts = np.bincount(pred_list) # bin counting for upcoming argmax # pred = np.argmax(counts) # pred_y.append(pred) # test_y.append(lbl) # # knn_pred_y = knn.predict(test_x) # print('k-neighbors: %d' % k) # score, confusion_matrix, report = report_results_face(test_y, pred_y) # acc_list.append(score) # print() print('# ======================= Random Forest ========================= #') acc_list_rf = [] for n in N_ESTIMATORS: rand_forest = RandomForestClassifier(n_estimators=n) rand_forest.fit(train_x, train_y) test_y, pred_y = [], [] for img, lbl in zip(test_faces, test_labels): key_points, descriptors = extract_surf(img) pred_list = rand_forest.predict(descriptors) counts = np.bincount(pred_list) # bin counting for upcoming argmax pred = np.argmax(counts) pred_y.append(pred) test_y.append(lbl) score, confusion_matrix, report = report_results(test_y, pred_y) acc_list_rf.append(score) print('Tree count: %d \t Accuracy: %.3f' % (n, score)) print()
# # plt.savefig(os.path.join(RESULTS_HOME, 'question4_confusion_matrix_D_'+str(d)+'.png'), dpi=300) # # # plt.show() # question3(D_LIST, acc_list) print( '# ======================= K-Nearest Neighbors ========================= #' ) acc_list = [] cmat_k5 = None for k in K_LIST: knn = KNeighborsClassifier(n_neighbors=k) knn.fit(train_x.T, train_y) knn_pred_y = knn.predict(test_x.T) print('k-neighbors: %d' % k) acc, cmat, rpt = report_results(test_y, knn_pred_y) if k != 5 \ else report_results(test_y, knn_pred_y, cmat_flag=True) cmat_k5 = cmat if k == 5 else cmat_k5 acc_list.append(acc) print() print( '# ======================= Random Forest ========================= #') acc_list_rf = [] cmat_n100 = None for n in N_ESTIMATORS: rand_forest = RandomForestClassifier(n_estimators=n) rand_forest.fit(train_x.T, train_y) rf_pred_y = rand_forest.predict(test_x.T) score = metrics.accuracy_score(test_y, rf_pred_y) acc, cmat, rpt = report_results(test_y, rf_pred_y) if n != 100 \
clsfr_titles = ['RandomForest %d' % n for n in N_ESTIMATORS_LIST] clsfr_titles.extend('kNearestNeighbors %d' % k for k in K_LIST) clsfr_titles.append('DecisionTree default') clsfr_titles.append('SupportVector rbf') clsfr_titles.append('DecisionTree linear') return clsfr, clsfr_titles if __name__ == '__main__': dataset = read_data(GAIT_DATA_HOME, ids, action, sensor) # 1. GAIT visualizing # task6(dataset[0]) # 2. Train classifiers train_x, train_y, test_x, test_y = get_train_test_features(dataset) print(train_x.shape, train_y.shape, test_x.shape, test_y.shape) classifiers, classifiers_title = get_classifiers() # Performing classification using each classifier for i in range(len(classifiers)): print('========================================================') print('\tRunning:', classifiers_title[i]) print('========================================================') classifiers[i].fit(train_x, train_y) # train pred_y = classifiers[i].predict(test_x) score, confusion_matrix, report = report_results(test_y, pred_y, cmat_flag=True) np.save(os.path.join(RESULTS_HOME, 'gait', get_safe_string(classifiers_title[i]) + '_cmat.npy'), confusion_matrix) print(score)