def __init__(self, neutral, smiling, batch_size=300, epochs=1, alpha=1): d = neutral.shape[1] self.W = 0 self.b = 0 lasso = Lasso(alpha=alpha, warm_start=True) lasso.coef_ = np.eye(d, d) for _ in range(epochs): print "epoch", _ js = np.random.random_integers(0, neutral.shape[0] - 1, size=batch_size) X = lemur_util.distort(neutral[js]) Y = lemur_util.distort(smiling[js]) model = lasso.fit(X, Y) self.W += model.coef_ self.b += model.intercept_ print self.W.shape, self.b.shape self.W /= epochs self.b /= epochs self.b.reshape((1, d))
neutral.append(scipy.ndimage.imread(filename, flatten=True).flatten() / 255) neutral = np.array(neutral) # Load (and repeat) smiling faces with LemurTimer("loading smiling faces"): smiling = [] for filename in sorted(glob.glob(SMILING_FACES_GLOB)): smiling.append(scipy.ndimage.imread(filename, flatten=True).flatten() / 255) smiling = np.array(smiling) # Approximate PCA by the lemur method :P with LemurTimer("approximating PCA"): pca = LemurPCA(PCA_BATCH_SIZE, PCA_EPOCHS) # Perform Kernel Ridge Regression using pca as the kernel neutral_training = lemur_util.distort(neutral[:N_TRAINING].repeat(REPEAT, axis=0)) smiling_training = smiling[:N_TRAINING].repeat(REPEAT, axis=0) with LemurTimer("performing KRR"): krr = LemurKRR(pca, neutral_training, smiling_training) # Predict the smiling faces from the neutral ones with LemurTimer("predicting smiling faces"): prediction = krr(neutral[-N_TEST:]).T # Display the neutral, smiling and predicted faces side to side # one row per test image f = plt.figure() for i in range(N_TEST): k = N_TRAINING + i f.add_subplot(N_TEST, 3, 3*i + 1)