def evaluate_valid(data_op=1, op=1, reduction=1, rate=0.2): """ 评估函数,在三个数据集上进行评估 :param op: 1-all 2-part :param data_op: 1-CK 2-Fer 3-Jaffe :return: """ import preprocess from tqdm import tqdm from data import CK, Fer2013, Jaffe filters = Gabor().build_filters() if data_op == 1: _, x, y = CK().gen_train_no() if data_op == 2: _, x, y = Fer2013().gen_train_no() if data_op == 3: _, x, y = Jaffe().gen_train_no() train = [] if op == 1: for i in tqdm(np.arange(0, x.shape[0], 1)): x[i] = preprocess.gray_norm(x[i]) x[i] = preprocess.adaptive_histogram_equalization(x[i]) res = Gabor().getGabor(x[i], filters, False, reduction) res = np.array(res).reshape(-1) res = np.append(res, y[i]) train.append(res) train = np.array(train) if data_op != 2: # 需要划分 from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(train, train, random_state=2019, test_size=rate) Classifier().SVM(x_train, x_test) test1 = [] test2 = [] if data_op == 2: _, x, y = Fer2013().gen_valid_no(1) for i in tqdm(np.arange(0, x.shape[0], 1)): x[i] = preprocess.gray_norm(x[i]) x[i] = preprocess.adaptive_histogram_equalization(x[i]) res = Gabor().getGabor(x[i], filters, False, reduction) res = np.array(res).reshape(-1) res = np.append(res, y[i]) test1.append(res) _, x, y = Fer2013().gen_valid_no(2) for i in tqdm(np.arange(0, x.shape[0], 1)): x[i] = preprocess.gray_norm(x[i]) x[i] = preprocess.adaptive_histogram_equalization(x[i]) res = Gabor().getGabor(x[i], filters, False, reduction) res = np.array(res).reshape(-1) res = np.append(res, y[i]) test2.append(res) test1 = np.array(test1) test2 = np.array(test2) print("Public") Classifier().SVM(train, test1) print("Pirvate") Classifier().SVM(train, test2)
def evaluate1_lbp(): filters = Gabor().build_filters() from tqdm import tqdm from data import CK, Fer2013, Jaffe _, x, y = Fer2013().gen_train_no() train = [] for i in tqdm(np.arange(0, x.shape[0], 1)): res = LBP().get_lbp(x[i]) res = np.array(res).reshape(-1) res = np.append(res, y[i]) train.append(res) train = np.array(train) test = [] _, x, y = Jaffe().gen_train_no() for i in tqdm(np.arange(0, x.shape[0], 1)): res = LBP().get_lbp(x[i]) res = np.array(res).reshape(-1) res = np.append(res, y[i]) test.append(res) test = np.array(train) Classifier().SVM(train, test) test = [] _, x, y = CK().gen_train_no() for i in tqdm(np.arange(0, x.shape[0], 1)): x[i] = preliminary_treatment.gray_norm(x[i]) x[i] = preliminary_treatment.adaptive_histogram_equalization(x[i]) res = Gabor().getGabor(x[i], filters, False, 6) res = np.array(res).reshape(-1) res = np.append(res, y[i]) test.append(res) test = np.array(train) Classifier().SVM(train, test)
def evaluate_test(): """ 在未训练的数据集上进行测试 :return: """ filters = Gabor().build_filters() from tqdm import tqdm from data import CK, Fer2013, Jaffe _, x, y = Fer2013().gen_train_no() train = [] for i in tqdm(np.arange(0, x.shape[0], 1)): x[i] = preprocess.gray_norm(x[i]) x[i] = preprocess.adaptive_histogram_equalization(x[i]) res = Gabor().getGabor(x[i], filters, False, 6) res = np.array(res).reshape(-1) res = np.append(res, y[i]) train.append(res) train = np.array(train) test = [] _, x, y = Jaffe().gen_train_no() for i in tqdm(np.arange(0, x.shape[0], 1)): x[i] = preprocess.gray_norm(x[i]) x[i] = preprocess.adaptive_histogram_equalization(x[i]) res = Gabor().getGabor(x[i], filters, False, 6) res = np.array(res).reshape(-1) res = np.append(res, y[i]) test.append(res) test = np.array(train) Classifier().SVM(train, test) test = [] _, x, y = CK().gen_train_no() for i in tqdm(np.arange(0, x.shape[0], 1)): x[i] = preprocess.gray_norm(x[i]) x[i] = preprocess.adaptive_histogram_equalization(x[i]) res = Gabor().getGabor(x[i], filters, False, 6) res = np.array(res).reshape(-1) res = np.append(res, y[i]) test.append(res) test = np.array(train) Classifier().SVM(train, test)
from visualize import plot_loss, plot_acc parser = argparse.ArgumentParser() parser.add_argument("--dataset", type=str, default="fer2013", help="dataset to train, fer2013 or jaffe or ck+") parser.add_argument("--epochs", type=int, default=200) parser.add_argument("--batch_size", type=int, default=32) parser.add_argument("--plot_history", type=bool, default=True) opt = parser.parse_args() his = None print(opt) if opt.dataset == "fer2013": expressions, x_train, y_train = Fer2013().gen_train() _, x_valid, y_valid = Fer2013().gen_valid() _, x_test, y_test = Fer2013().gen_test() # target编码 y_train = to_categorical(y_train).reshape(y_train.shape[0], -1) y_valid = to_categorical(y_valid).reshape(y_valid.shape[0], -1) # 为了统一几个数据集,必须增加一列为0的 y_train = np.hstack((y_train, np.zeros((y_train.shape[0], 1)))) y_valid = np.hstack((y_valid, np.zeros((y_valid.shape[0], 1)))) print( "load fer2013 dataset successfully, it has {} train images and {} valid iamges" .format(y_train.shape[0], y_valid.shape[0])) model = CNN3(input_shape=(48, 48, 1), n_classes=8) sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(optimizer=sgd,
def evaluate_lbp(data_op=1, op=1, reduction=1, rate=0.2): filters = Gabor().build_filters() from tqdm import tqdm from data import CK, Fer2013, Jaffe _, x, y = CK().gen_train_no() if data_op == 2: _, x, y = Fer2013().gen_train_no() if data_op == 3: _, x, y = Jaffe().gen_train_no() train = [] if op == 1: for i in tqdm(np.arange(0, x.shape[0], 1)): x[i] = preliminary_treatment.gray_norm(x[i]) x[i] = preliminary_treatment.adaptive_histogram_equalization(x[i]) res = Gabor().getGabor(x[i], filters, False, reduction) res = np.array(res).reshape(-1) res = np.append(res, y[i]) train.append(res) train = np.array(train) if op == 2: for i in tqdm(np.arange(0, x.shape[0], 1)): img, dets, shape_list, img_list, pt_post_list = preliminary_treatment.deal(x[i]) res1 = Gabor().getGabor(img, filters, 0, 1) res1 = np.array(res1) res = [] if len(shape_list) == 0: continue for _, pt in enumerate(shape_list[0].parts()): px, py = min(max(pt.x, 0), 47), min(max(pt.y, 0), 47) im = res1[0] cv2.circle(im, (px, py), 2, (255, 0, 0), 1) res.append(res1[:, px, py]) res = np.array(res) res = np.append(res, y[i]) train.append(res) train = np.array(train) if op == 3: for i in tqdm(np.arange(0, x.shape[0], 1)): res = LBP().get_lbp(x[i]) res = np.array(res).reshape(-1) res = np.append(res, y[i]) train.append(res) train = np.array(train) if data_op != 2: from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(train, train, random_state=2019, test_size=rate) Classifier().SVM(x_train, x_test) test1 = [] test2 = [] if data_op == 2: _, x, y = Fer2013().gen_valid_no(1) for i in tqdm(np.arange(0, x.shape[0], 1)): res = LBP().get_lbp(x[i]) res = np.array(res).reshape(-1) res = np.append(res, y[i]) test1.append(res) _, x, y = Fer2013().gen_valid_no(2) for i in tqdm(np.arange(0, x.shape[0], 1)): res = LBP().get_lbp(x[i]) res = np.array(res).reshape(-1) res = np.append(res, y[i]) test2.append(res) test1 = np.array(test1) test2 = np.array(test2) print("Public") Classifier().SVM(train, test1) print("Pirvate") Classifier().SVM(train, test2)