def init_algorithm( self, save_training_file='datasets/faces_training.pkl', load_preprocessed_data='datasets/faces_preprocessed.pkl', load_mlp='params/mlp.xml', face_casc='params/haarcascade_frontalface_default.xml', left_eye_casc='params/haarcascade_lefteye_2splits.xml', right_eye_casc='params/haarcascade_righteye_2splits.xml'): self.data_file = save_training_file self.faces = FaceDetector(face_casc, left_eye_casc, right_eye_casc) self.head = None # load preprocessed dataset to access labels and PCA params if path.isfile(load_preprocessed_data): (_, y_train), (_, y_test), V, m = homebrew.load_from_file( load_preprocessed_data) self.pca_V = V self.pca_m = m self.all_labels = np.unique(np.hstack((y_train, y_test))) # load pre-trained multi-layer perceptron if path.isfile(load_mlp): layer_sizes = np.array([self.pca_V.shape[1], len(self.all_labels)]) self.MLP = MultiLayerPerceptron(layer_sizes, self.all_labels) self.MLP.load(load_mlp) else: print "Warning: Testing is disabled" print "Could not find pre-trained MLP file ", load_mlp self.testing.Disable() else: print "Warning: Testing is disabled" print "Could not find data file ", load_preprocessed_data self.testing.Disable()
def init_algorithm( self, save_training_file='datasets/faces_training.pkl', load_preprocessed_data='datasets/faces_preprocessed.pkl', load_mlp='params/mlp.xml', face_casc='params/haarcascade_frontalface_default.xml', left_eye_casc='params/haarcascade_lefteye_2splits.xml', right_eye_casc='params/haarcascade_righteye_2splits.xml'): """Initializes face detector and facial expression classifier This method initializes both the face detector and the facial expression classifier. :param save_training_file: filename for storing the assembled training set :param load_preprocessed_data: filename for loading a previously preprocessed dataset (for classification in Testing Mode) :param load_mlp: filename for loading a pre-trained MLP classifier (use the script train_test_mlp.py) :param face_casc: path to a face cascade :param left_eye_casc: path to a left-eye cascade :param right_eye_casc: path to a right-eye cascade """ self.data_file = save_training_file self.faces = FaceDetector(face_casc, left_eye_casc, right_eye_casc) self.head = None # load preprocessed dataset to access labels and PCA params if path.isfile(load_preprocessed_data): (_, y_train), ( _, y_test), V, m = homebrew.load_from_file(load_preprocessed_data) self.pca_V = V self.pca_m = m self.all_labels = np.unique(np.hstack((y_train, y_test))) # load pre-trained multi-layer perceptron if path.isfile(load_mlp): layer_sizes = np.array( [self.pca_V.shape[1], len(self.all_labels)]) self.MLP = MultiLayerPerceptron(layer_sizes, self.all_labels) self.MLP.load(load_mlp) else: print "Warning: Testing is disabled" print "Could not find pre-trained MLP file ", load_mlp self.testing.Disable() else: print "Warning: Testing is disabled" print "Could not find data file ", load_preprocessed_data self.testing.Disable()
def main(): # load training data # training data can be recorded using chapter7.py in training mode (X_train, y_train), (X_test, y_test), _, _ = homebrew.load_data( "datasets/faces_training.pkl", num_components=50, test_split=0.2, save_to_file="datasets/faces_preprocessed.pkl", seed=42) if len(X_train) == 0 or len(X_test) == 0: print "Empty data" raise SystemExit # convert to numpy X_train = np.squeeze(np.array(X_train)).astype(np.float32) y_train = np.array(y_train) X_test = np.squeeze(np.array(X_test)).astype(np.float32) y_test = np.array(y_test) # find all class labels labels = np.unique(np.hstack((y_train, y_test))) # prepare training num_features = len(X_train[0]) num_classes = len(labels) params = dict(term_crit=(cv2.TERM_CRITERIA_COUNT, 300, 0.01), train_method=cv2.ANN_MLP_TRAIN_PARAMS_BACKPROP, bp_dw_scale=0.001, bp_moment_scale=0.9) saveFile = 'params/mlp.xml' # find best MLP configuration print "---" print "1-hidden layer networks" best_acc = 0.0 # keep track of best accuracy for l1 in xrange(10): # gradually increase the hidden-layer size layerSizes = np.int32( [num_features, (l1 + 1) * num_features / 5, num_classes]) MLP = MultiLayerPerceptron(layerSizes, labels) print layerSizes MLP.fit(X_train, y_train, params=params) (acc, _, _) = MLP.evaluate(X_train, y_train) print " - train acc = ", acc (acc, _, _) = MLP.evaluate(X_test, y_test) print " - test acc = ", acc if acc > best_acc: # save best MLP configuration to file MLP.save(saveFile) best_acc = acc
def main(): (X_train, y_train), (X_test, y_test), _, _ = homebrew.load_data( load_from_folder="/datasets", num_components=50, test_split=0.2, save_to_file="datasets/faces_preprocessed.pkl", seed=42) if len(X_train) == 0 or len(X_test) == 0: print "Empty data" raise SystemExit X_train = np.squeeze(np.array(X_train)).astype(np.float32) y_train = np.array(y_train) X_test = np.squeeze(np.array(X_test)).astype(np.float32) y_test = np.array(y_test) labels = np.unique(np.hstack((y_train, y_test))) print X_train.shape num_features = len(X_train[0]) print num_features num_classes = len(labels) print num_classes params = dict(term_crit=(cv2.TERM_CRITERIA_COUNT, 300, 0.01), train_method=cv2.ANN_MLP_TRAIN_PARAMS_BACKPROP, bp_dw_scale=0.001, bp_moment_scale=0.9) saveFile = 'params/mlp.xml' print "---" print "1-hidden layer networks" best_acc = 0.0 for l1 in xrange(20): layerSizes = np.int32([ num_features, (l1 + 1) * num_features / 5, (l1 + 1) * num_features / 10, num_classes ]) MLP = MultiLayerPerceptron(layerSizes, labels) print layerSizes MLP.fit(X_train, y_train, params=params) (acc, _, _) = MLP.evaluate(X_train, y_train) print " - train acc = ", acc (acc, _, _) = MLP.evaluate(X_test, y_test) print " - test acc = ", acc if acc > best_acc: MLP.save(saveFile) best_acc = acc
def InitAlgorithm(self, faceCasc, leftEyeCasc, rightEyeCasc, loadPreprocessedData, loadMLP): # load pre-trained cascades self.faceCasc = cv3.CascadeClassifier(faceCasc) if self.faceCasc.empty(): print 'Warning: Could not load face cascade:', faceCasc raise SystemExit self.leftEyeCasc = cv3.CascadeClassifier(leftEyeCasc) if self.leftEyeCasc.empty(): print 'Warning: Could not load left eye cascade:', leftEyeCasc raise SystemExit self.rightEyeCasc = cv3.CascadeClassifier(rightEyeCasc) if self.rightEyeCasc.empty(): print 'Warning: Could not load right eye cascade:', rightEyeCasc raise SystemExit # load preprocessed dataset to access labels and PCA params if path.isfile(loadPreprocessedData): (_, y_train), ( _, y_test), self.pca_V, self.pca_m = homebrew.load_from_file( loadPreprocessedData) self.allLabels = np.unique(np.hstack((y_train, y_test))) # load pre-trained multi-layer perceptron if path.isfile(loadMLP): self.MLP = MultiLayerPerceptron( np.array([self.pca_V.shape[1], len(self.allLabels)]), self.allLabels) self.MLP.load(loadMLP) else: print "Warning: Testing is disabled" print "Could not find pre-trained MLP file ", loadMLP self.testing.Disable() else: print "Warning: Testing is disabled" print "Could not find preprocessed data file ", loadPreprocessedData self.testing.Disable()
load_mlp = 'params/mlp.xml' light1 = 0 light2 = 0 if path.isfile(load_preprocessed_data): (_, y_train), (_, y_test), V, m = homebrew.load_from_file(load_preprocessed_data) pca_V = V pca_m = m all_labels = np.unique(np.hstack((y_train, y_test))) # load pre-trained multi-layer perceptron if path.isfile(load_mlp): layer_sizes = np.array([pca_V.shape[1], len(all_labels)]) MLP = MultiLayerPerceptron(layer_sizes, all_labels) MLP.load(load_mlp) ############################################################################# import cv2 import numpy as np import copy import math # Environment: # OS : Mac OS EL Capitan # python: 3.5 # opencv: 2.4.13 def single_play(): # parameters