Based on the great blog by Joel Grus, I implemented LogisticRegression, PCA, and LDA. I'd appreciate feedback as I'm not sure that the logistic classifier is good enough (as it supposed to achieve higher accuracy on training set).
# This code was writen with inspiration from https://github.com/joelgrus/shirts/blob/master/visuals.py and with the help of my friend Jiaher. from PIL import Image from glob import glob from random import shuffle, seed import numpy as np import matplotlib.pyplot as plt from sklearn.decomposition import PCA from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.discriminant_analysis import LinearDiscriminantAnalysis STANDARD_SIZE = (138, 138) HALF_SIZE = (STANDARD_SIZE / 2, STANDARD_SIZE / 2) def img_to_array(filename): """ takes a filename and turns it into a numpy array of RGB pixels """ img = Image.open(filename) img_i = img.resize(STANDARD_SIZE) img.close() img_i = list(img_i.getdata()) img_i = map(list, img_i) img_i = np.array(img_i) s = img_i.shape * img_i.shape img_wide = img_i.reshape(1, s) return img_wide male_files = glob('./images/boys/*') female_files = glob('./images/girls/*') process_file = img_to_array raw_data =  for filename in male_files: print (filename) raw_data.append((process_file(filename), 'male', filename)) for filename in female_files: print (filename) raw_data.append((process_file(filename), 'female', filename)) # randomly order the data seed(0) shuffle(raw_data) # pull out the features and the labels data = np.array([cd for (cd, _y, f) in raw_data]) labels = np.array([_y for (cd, _y, f) in raw_data]) labels = [1 if label == 'male' else 0 for label in labels] X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2) # Simple Linear Model: clf = LogisticRegression(penalty='l2') clf.fit(X_train, y_train) print("linear model test score", clf.score(X_test, y_test)) # find the principal components N_COMPONENTS = 4 pca = PCA(n_components=N_COMPONENTS, random_state=0, svd_solver='randomized') X_train_pca = pca.fit_transform(X_train) X_test_pca = pca.transform(X_test) pcaclf = clf.fit(X_train_pca, y_train) print("pca test score", pcaclf.score(X_test_pca, y_test)) print("pca train score", pcaclf.score(X_train_pca, y_train)) ###LDA: lda = LinearDiscriminantAnalysis(n_components=2) X_train_lda = lda.fit_transform(X_train, y_train) X_test_lda = lda.transform(X_test) ldaclf = clf.fit(X_train_lda, y_train) print("lda train score", ldaclf.score(X_train_lda, y_train)) print("lda test score", ldaclf.score(X_test_lda, y_test))
I'm using the data from ImageNet, nothing fancy.