def mk_dataset(): face_dir = "/Users/zoushuai/Python/lab/datasets/original/face" nonface_dir = "/Users/zoushuai/Python/lab/datasets/original/nonface" face_list = resize_image(face_dir) nonface_list = resize_image(nonface_dir) train_set = face_list[0:250] train_set.extend( nonface_list[0:250]) # trainset contains 250 faces and 250 nonfaces train_set = np.array(train_set) validate_set = face_list[250:500] validate_set.extend(nonface_list[250:500] ) # validateset contains 250 faces and 250 nonfaces 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')
def train(X_train, y_train): clf = AdaBoostClassifier(DecisionTreeClassifier(max_depth=1), n_weakers_limit=20) print("Training a AdaBoost Classifier.") clf.fit(X_train, y_train) # If model directories don't exist, create them if not os.path.isdir(os.path.split(adb_model_path)[0]): os.makedirs(os.path.split(adb_model_path)[0]) clf.save(clf, adb_model_path)
def organizeData(inputFileName, outputFileName): ''' 函数作用:读取图片,转化成灰度格式图片,抽取NPD特征,保存到本地 :param outputFileName:输出文件名 :param inputFileName:输入文件名 :return: ''' ImageList = getAllImage(inputFileName) grayList = convertImage(ImageList, 24) featureList = getNPDFeature(grayList) AdaBoostClassifier.save(featureList, outputFileName)
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
def pre_process(): face_features = numpy.array([]) nonface_features = numpy.array([]) features = numpy.array([]) #转化图片 process_image(path_face_origin, path_face_purpose) process_image(path_nonface_origin, path_nonface_purpose) num_face = len(os.listdir('datasets/original/face')) num_nonface = len(os.listdir('datasets/original/nonface')) #获取特征 face_features = get_feature(path_face_purpose) nonface_features = get_feature(path_nonface_purpose) print(face_features.shape,nonface_features.shape) print(num_face,num_nonface) #改变特征形状 face_features.shape = num_face, 165600 nonface_features.shape = num_nonface, 165600 #准备加入label face_y = numpy.ones(shape=(num_face, 1)) nonface_y = numpy.zeros(shape=(num_nonface, 1)) nonface_y -= numpy.ones(shape=(num_nonface, 1)) #加入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
def Feature_extract(): #提取特征 face_path = '.\\datasets\\original\\face\\face_%03d.jpg' faces_path = [] for i in range(500): faces_path.append(face_path % i) nonface_path = '.\\datasets\\original\\nonface\\nonface_%03d.jpg' nonfaces_path = [] for i in range(500): nonfaces_path.append(nonface_path % i) 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')
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') image = image.resize((24, 24)) imageData = np.array(image) npd = feature.NPDFeature(imageData) features.append(npd.extract()) AdaBoostClassifier.save(features, 'features.dump') features = np.array(features) print(features.shape) X_train, X_val, y_train, y_val = train_test_split(features, y, test_size=0.25) classifier = AdaBoostClassifier(DecisionTreeClassifier, 5) classifier.fit(X_train, y_train) score = classifier.predict_scores(X_val, y_val) predict = classifier.predict(X_val) y_val = np.array(list(map(lambda x: int(x), y_val.reshape(1, -1)[0])))
def split_dataset(dataset, train_ratio=0.8): """ :return: X_train, y_train, X_valid, y_valid """ pivot = int(2 * SAMPLES_N * train_ratio) train_set = dataset[0][:pivot], dataset[1][:pivot] valid_set = dataset[0][pivot:], dataset[1][pivot:] return train_set + valid_set if __name__ == "__main__": X_train, y_train, X_valid, y_valid = split_dataset(load_dataset()) adaBoost = AdaBoostClassifier(DecisionTreeClassifier, WEAKERS_LIMIT) accs = adaBoost.fit(X_train, y_train, X_valid, y_valid) plt.figure(figsize=[8, 5]) plt.title('Accuracy') plt.xlabel('Num of weak classifiers') plt.ylabel('Accuracy') plt.plot(accs[0], '--', c='b', linewidth=3, label='train') plt.plot(accs[1], c='r', linewidth=3, label='valid') plt.legend() plt.grid() plt.savefig('AdaBoost-accuracy.png') plt.show() AdaBoostClassifier.save(adaBoost, 'AdaBoost-Model.pkl')
img_nonface=mpimg.imread(currentpath2+"{:0>3d}".format(i)+".jpg") img_nonface_=rgb2gray(img_nonface) f=NPDFeature(img_nonface_) feature_=f.extract() feature.append(feature_) label.append(-1) if __name__ == "__main__": # write your code here readimg() train_feature,validation_feature,train_label,validation_label=train_test_split(feature,label,test_size=0.3) #adaboost adaboostClassifier=AdaBoostClassifier(DecisionTreeClassifier(max_depth = 1, random_state = 1),20) adaboostClassifier.save(train_feature,'train_feature') adaboostClassifier.save(train_label,'train_label') adaboostClassifier.save(validation_feature,'validation_feature') adaboostClassifier.save(validation_label,'validation_label') adaboostClassifier.fit(train_feature,train_label) adaboostClassifier.drawPic() ''' #debug adaboostClassifier=AdaBoostClassifier(DecisionTreeClassifier(max_depth = 1, random_state = 1),20) train_feature=adaboostClassifier.load("train_feature") train_label=adaboostClassifier.load("train_label") adaboostClassifier.fit(train_feature,train_label) adaboostClassifier.drawPic()
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 for j in range(predict.shape[0]): if predict[j] != Y_test[j]: wrong_count += 1 AdaBoostClassifier.save(classification_report(Y_test, predict), "classifier_report.txt") pass