|
| 1 | +''' |
| 2 | +============================ |
| 3 | +Principal Component Analysis |
| 4 | +============================ |
| 5 | +
|
| 6 | +''' |
| 7 | + |
| 8 | +from sklearn import svm |
| 9 | +import numpy as np |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import math |
| 12 | +from matplotlib import cm |
| 13 | +from mpl_toolkits.mplot3d import Axes3D |
| 14 | +from matplotlib.ticker import LinearLocator, FormatStrFormatter |
| 15 | +import matplotlib.animation as animation |
| 16 | +import random |
| 17 | +import glob |
| 18 | +import os |
| 19 | +import sys, getopt |
| 20 | +from time import clock |
| 21 | +from scipy import ndimage |
| 22 | +from scipy import misc |
| 23 | +from PIL import Image |
| 24 | +from scipy.misc import toimage |
| 25 | +from scipy.interpolate import interp1d |
| 26 | +from sklearn.model_selection import cross_val_score |
| 27 | + |
| 28 | +width = 0 |
| 29 | +hight = 0 |
| 30 | + |
| 31 | +def readX(file): |
| 32 | + global width |
| 33 | + global height |
| 34 | + inputs = [] |
| 35 | + labels = [] |
| 36 | + class_label = -1; |
| 37 | + for dirName, subdirList, fileList in os.walk(file): |
| 38 | + print dirName, subdirList, fileList |
| 39 | + class_label += 1 |
| 40 | + for filename in fileList: |
| 41 | + if dirName != file and filename != ".DS_Store": |
| 42 | + image = Image.open(os.path.join(dirName, filename)).convert('L') |
| 43 | + width, height = image.size |
| 44 | + inputs.append(np.asarray(image).flatten().tolist()) |
| 45 | + labels.append(class_label) |
| 46 | + return np.matrix(inputs), labels |
| 47 | + |
| 48 | +def display_face(face, name): |
| 49 | + data = face.copy() |
| 50 | + data.resize(height, width) |
| 51 | + toimage(data).save('{}.png'.format(name)) |
| 52 | + |
| 53 | +def map_gray(eigenface): |
| 54 | + interpolate = interp1d([eigenface.min(), eigenface.max()],[0,255]) |
| 55 | + return interpolate(eigenface) |
| 56 | + |
| 57 | +def convert_to_list(inputs): |
| 58 | + train_data = [] |
| 59 | + for i in xrange(inputs.shape[0]): |
| 60 | + param = [] |
| 61 | + for j in xrange(inputs.shape[1]): |
| 62 | + param.append(inputs.item(i,j)) |
| 63 | + train_data.append(param) |
| 64 | + return train_data |
| 65 | + |
| 66 | +inputs, labels = readX(str(sys.argv[1])) |
| 67 | +print labels |
| 68 | + |
| 69 | +# Part A |
| 70 | +average_face = np.mean(inputs, axis=0) |
| 71 | +mean_faces = np.subtract(inputs, average_face) |
| 72 | +normalized_faces = np.divide(mean_faces, np.std(mean_faces, axis=0)).T |
| 73 | +print normalized_faces.shape |
| 74 | +display_face(average_face, "average_face") |
| 75 | + |
| 76 | +# Part B |
| 77 | +U, s, V = np.linalg.svd(normalized_faces, full_matrices=False) |
| 78 | +best_50 = np.matrix(s[:50]) |
| 79 | +with open('principal_components.txt', 'wb') as f: |
| 80 | + for line in best_50: |
| 81 | + np.savetxt(f, line, fmt='%.2f') |
| 82 | + |
| 83 | +# Part C |
| 84 | +for i in range(0,5): |
| 85 | + eigenface = U.T[i] |
| 86 | + display_face(map_gray(eigenface), 'eigenface{}'.format(i)) |
| 87 | + |
| 88 | +# Part D |
| 89 | +eigenfaces = [] |
| 90 | +for i in range(0,50): |
| 91 | + eigenfaces.append(U.T[i][0].tolist()[0]) |
| 92 | +with open('eigenfaces.txt', 'wb') as f: |
| 93 | + count = 0 |
| 94 | + for line in eigenfaces: |
| 95 | + count += 1 |
| 96 | + f.write("eigenvector {}\n".format(count)) |
| 97 | + np.savetxt(f, line, fmt='%.6f') |
| 98 | + |
| 99 | +new_proj = np.matrix(eigenfaces) * normalized_faces |
| 100 | +proj = new_proj / np.linalg.norm(new_proj, axis=0) |
| 101 | +mean_proj = np.subtract(proj, np.mean(proj, axis=1)) |
| 102 | +normalized_proj = np.divide(mean_proj, np.std(mean_proj, axis=1)) |
| 103 | + |
| 104 | +reduced_images = [] |
| 105 | +for row in proj.T: |
| 106 | + image = map_gray(np.dot(row,eigenfaces)) |
| 107 | + image += average_face |
| 108 | + image = map_gray(image) |
| 109 | + reduced_images.append(image.tolist()[0]) |
| 110 | +reduced_images = np.matrix(reduced_images) |
| 111 | +with open('reduced_images.txt', 'wb') as f: |
| 112 | + count = 0 |
| 113 | + for line in reduced_images: |
| 114 | + count += 1 |
| 115 | + f.write("image {}\n".format(count)) |
| 116 | + np.savetxt(f, line, fmt='%.2f') |
| 117 | + |
| 118 | +# Part E |
| 119 | +clf = svm.SVC(decision_function_shape='ovo') |
| 120 | +start = clock() |
| 121 | +scores = np.mean(cross_val_score(clf, convert_to_list(normalized_proj.T), labels, cv=10)) |
| 122 | +end = clock() |
| 123 | +print "score1 = ", scores, 'in {} secs'.format(end - start) |
| 124 | + |
| 125 | +clf = svm.SVC(decision_function_shape='ovo') |
| 126 | +start = clock() |
| 127 | +scores = np.mean(cross_val_score(clf, convert_to_list(normalized_faces.T), labels, cv=10)) |
| 128 | +end = clock() |
| 129 | +print "score2 = ", scores, 'in {} secs'.format(end - start) |
| 130 | + |
| 131 | +# Part F |
| 132 | +for i in range(1,4): |
| 133 | + j = random.randint(1, inputs.shape[0]) |
| 134 | + display_face(inputs[j], "input{}".format(i)) |
| 135 | + display_face(reduced_images[j], "output{}".format(i)) |
| 136 | + |
0 commit comments