def test(X_test, y_test): print("Test the AdaBoost Classifier.") clf = AdaBoostClassifier.load(adb_model_path) pre_y = clf.predict(X_test) target_names = ['face', 'non face'] report = classification_report(y_test, pre_y, target_names=target_names) print(report) with open(report_path, "w") as f: f.write(report) f.close() print("report has been wrote into %s" % report_path)
def test_image(): path = 'datasets/original/' face = io.imread_collection(path + 'face/*.jpg') nonface = io.imread_collection(path + 'nonface/*.jpg') labels = ['face', 'nonface'] X = [] y = [] # face_list = [get_features(i) for i in face] # nonface_list = [get_features(i) for i in nonface] # face_list = Parallel(n_jobs=4)(delayed(get_features)(i) for i in face) # nonface_list = Parallel(n_jobs=4)( # delayed(get_features)(i) for i in nonface) # X += face_list # y += list(np.zeros(len(face_list), dtype=int)) # X += nonface_list # y += list(np.ones(len(nonface_list), dtype=int)) # AdaBoostClassifier.save(X, 'X.pkl') # AdaBoostClassifier.save(y, 'y.pkl') X = AdaBoostClassifier.load('X.pkl') y = AdaBoostClassifier.load('y.pkl') X_train, X_test, y_train, y_test = train_test_split( np.array(X), np.array(y), test_size=0.33, random_state=42) print('start training') clf = AdaBoostClassifier(n_weakers_limit=50) clf.fit(X_train, y_train) y_pred = clf.predict(X_test) with open('report.txt', 'w') as f: print(classification_report(y_test, y_pred, target_names=labels), file=f)
def get_features(path): image_paths = [os.path.join(path, f) for f in os.listdir(path)] features = [] # read the images and extract the features for image_path in image_paths: img = cv2.imread(image_path) # convert into gray image gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_reshape = cv2.resize(gray_img, (25, 25), interpolation=cv2.INTER_CUBIC) image = feature.NPDFeature(img_reshape) pre_features = feature.NPDFeature.extract(image) AdaBoostClassifier.save(pre_features, "save.p") face_feature = AdaBoostClassifier.load("save.p") features.append(face_feature) return features
import numpy as np import feature from ensemble import AdaBoostClassifier from sklearn.tree import DecisionTreeClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report import os if __name__ == "__main__": # write your code here features = [] y = np.ones((500, 1)) y = np.append(y, np.ones((500, 1)) * -1).reshape((-1, 1)) if os.path.exists('features.dump'): features = AdaBoostClassifier.load('features.dump') else: pathes = np.array( list( map(lambda s: 'datasets/original/face/' + s, os.listdir('datasets/original/face')))) pathes = np.append( pathes, np.array( list( map(lambda s: 'datasets/original/nonface/' + s, os.listdir('datasets/original/nonface'))))) for index, path in enumerate(pathes): with Image.open(path) as image: print(index, path) image = image.convert('L')
with open('NPD_feature.pkl', 'wb') as f: pickle.dump(NPD_feature_list, f) # 将标签保存起来 data = pd.DataFrame({'label': label}) data.to_csv('label.csv') else: print("preprocessing has been performed") if __name__ == "__main__": # 预处理数据 preprocess_data() # 加载数据 feature = AdaBoostClassifier.load('NPD_feature.pkl') label = pd.read_csv('label.csv')['label'] feature = np.array(feature) label = np.array(label) # 划分训练集和验证集 X_train, X_val, y_train, y_val = train_test_split(feature, label, test_size=0.4, random_state=2019, shuffle=True) print('X_train.shape', X_train.shape) print('X_val.shape', X_val.shape) print('y_train.shape', y_train.shape) print('y_val.shape', y_val.shape)
y_val.append(val_list[i][1]) print("load fea success") # #训练 # print ("train") # temp_cls=AdaBoostClassifier(100) # begin_time =time.time()# # temp_cls.fit(X_train,y_train) # end_time =time.time()# # total_time=(end_time-begin_time) # AdaBoostClassifier.save(temp_cls,"fd.model") # print ("total train time"+str(total_time)) # print ("save trained model success") #加载训练好的模型 temp_cls = AdaBoostClassifier.load("fd.model") print(len(temp_cls.classifier_list)) print(len(temp_cls.alpha)) #测试 acc = 0.0 predict_y = temp_cls.predict_list(X_val) report = classification_report(y_val, predict_y, labels=None, target_names=None, sample_weight=None, digits=2) report_file = open('report.txt', 'w') report_list = report_file.write(report) report_file.close() print("report:" + str(report))
""" errorNum = 0 for i in range(len(y)): if y[i] != predictY[i]: errorNum += 1 rate = 1 - errorNum / len(y) return rate def divideData(faceFileName,nonfaceFileName): ''' 函数作用:切分数据 :param faceFileName:人脸特征文件名 :param nonfaceFileName:非人脸特征文件名 :return: 训练数据,验证数据,训练标签,验证标签 ''' faceImage =AdaBoostClassifier.load(faceFileName) nonfaceIamge = AdaBoostClassifier.load(nonfaceFileName) faceLen = len(faceImage) nonfaceLen = len(nonfaceIamge) faceLabel = [1] * faceLen nonfaceLabel = [-1] * nonfaceLen trainImage = [] trainImage.extend(nonfaceIamge) trainImage.extend(faceImage) trainLabel = [] trainLabel.extend(nonfaceLabel) trainLabel.extend(faceLabel) X_train, X_test, Y_train, Y_test = train_test_split(trainImage, trainLabel, test_size = 0.33) return np.array(X_train), np.array(X_test), np.array(Y_train), np.array(Y_test)
train = np.zeros((1000, 165600)) for i in range(500): img = Image.open(faces_path[i]) img = img.convert('L').resize((24, 24)) nf = NPDFeature(np.array(img)) train[i * 2] = nf.extract() img = Image.open(nonfaces_path[i]) img = img.convert('L').resize((24, 24)) nf = NPDFeature(np.array(img)) train[i * 2 + 1] = nf.extract() AdaBoostClassifier.save(train, 'train.txt') try: X = AdaBoostClassifier.load("train.txt") except IOError: Feature_extract() X = AdaBoostClassifier.load("train.txt") Y = np.zeros((1000, 1)) for i in range(1000): Y[i] = (i + 1) % 2 Y = np.where(Y > 0, 1, -1) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2) booster = AdaBoostClassifier(DecisionTreeClassifier, 15) booster.fit(X_train, Y_train) predict = booster.predict(X_test) wrong_count = 0
#加入label face_features = numpy.concatenate([face_features, face_y], axis=1) nonface_features = numpy.concatenate([nonface_features, nonface_y], axis=1) #将所有数据合并 features = numpy.row_stack((face_features,nonface_features)) #写入缓存 AdaBoostClassifier.save(features,"feature.data") return features if __name__ == "__main__": #Data格式为[X:y] if os.path.exists('feature.data'): #如果预处理过,直接用load()读取数据 Data = AdaBoostClassifier.load('feature.data') else : Data = pre_process() #将X_data与y_data分开 X_data,y_data = Data[:,:-1],Data[:,-1] #切分训练集与验证集 X_train,X_test,y_train,y_test = train_test_split(X_data,y_data,test_size=0.3,random_state=10) print(len(y_train),len(y_test)) #进行AdaBoost训练 mode = tree.DecisionTreeClassifier(max_depth=1) adaboost=AdaBoostClassifier(mode,20) adaboost.fit(X_train,y_train)
for i in range(num_nonface_image): img = Image.open(nonface_path + '/' + nonface_image[i]) img = img.convert('L') img = img.resize((24, 24), Image.ANTIALIAS) img = NPDFeature(np.array(img)) dataset.append(np.concatenate((img.extract(), np.array([-1])))) return dataset if __name__ == "__main__": # write your code here #dataset = load_data() classifier = AdaBoostClassifier(DecisionTreeClassifier(max_depth=3), 6) #classifier.save(dataset,'C:/Users/47864/Desktop/Data/data.txt') dataset = classifier.load('C:/Users/47864/Desktop/Data/data.txt') dataset = np.array(dataset) X = dataset[:, :-1] y = dataset[:, -1:] X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2) #print(len(X),len(X_train)) #print(X_train[0]) classifier.fit(X_train, y_train) y_pred = classifier.predict(X_val) #print(y_pred,y_val) accuracy_rate = np.sum(y_pred.T == y_val.T[0]) / float(len(y_pred)) target_names = ['nonface', 'face'] print("1") print(y_val.T[0])
validate_set = np.array(validate_set) train_img2feature_list = [] validate_img2feature_list = [] for i in range(500): npdFeature_train = NPDFeature(train_set[i]) train_img2feature_list.append(npdFeature_train.extract()) npdFeature_validate = NPDFeature(validate_set[i]) validate_img2feature_list.append(npdFeature_validate.extract()) train_img2feature_list = np.array(train_img2feature_list) validate_img2feature_list = np.array(validate_img2feature_list) AdaBoostClassifier.save(train_img2feature_list, 'train') AdaBoostClassifier.save(validate_img2feature_list, 'validate') if __name__ == '__main__': mk_dataset() train_set_label = np.append(np.ones(250), (-1) * np.ones(250)) validate_set_label = np.append(np.ones(250), (-1) * np.ones(250)) train_img2feature_list = AdaBoostClassifier.load('train') validate_img2feature_list = AdaBoostClassifier.load('validate') adaboostClassifier = AdaBoostClassifier(DecisionTreeClassifier, 20) classifier_list, alpha_list = adaboostClassifier.fit( train_img2feature_list, train_set_label) result_predict = adaboostClassifier.predict(validate_img2feature_list, classifier_list, alpha_list, 0) f = open('report.txt', 'w') f.write(classification_report(validate_set_label, result_predict))