Eigenfaces 25 points We will now use the SVD and orthogonal matrices to build a
ID: 3739325 • Letter: E
Question
Eigenfaces 25 points We will now use the SVD and orthogonal matrices to build a simple classifier that detects whether a given image contains a face or not. We will do this by constructing a subspace of images of faces in the vector space of all gray-scale images using low-rank approximation. We can then use a distance measure to decide if an image shows a face or not Review: Facts about ortho To start, let's briefly review some facts about orthogonality and the SVD Approximation with low-dimensional subspaces Suppose you have a matrix A whose columns span an m-dimensional space V. Then A has rank m. Consider the SVD A-UEVT. The columns of U corresponding to non- zero singular values form the span of V. The subspace V of rank n (where nExplanation / Answer
import numpy as np
from io import BytesIO
import scipy.misc
from numpy.random import randint
import numpy.linalg as la
import matplotlib.pyplot as plt
bio = lambda image_path: BytesIO(data_files[image_path])
face_images = []
maybe_face_images = []
for i in range(20):
image_path = "image.png" % i
face_images.append(scipy.misc.imread(bio(image_path)))
face_images = np.array(face_images).astype(np.float64)
index = randint(0,20) #randomly swapping first image
temp = face_images[0].copy()
face_images[0] = face_images[index].copy()
face_images[index] = temp.copy()
from PIL import Image, ImageOps
maybe_face_images = []
for i in range(10):
image_path = "data/maybe-faces/image.png" % i
maybe_face_images.append(np.array(ImageOps.grayscale(Image.open(bio(image_path)))))
maybe_face_images = np.array(maybe_face_images).astype(np.float64)
#CODE
N = face_images.shape[0]
h = face_images.shape[1]
w = face_images.shape[2]
#print(N, h, w)
face_vectors = np.zeros((h*w,N))
for i in range(N):
face_vectors[:,i] = face_images[i].flatten()
U, sigma, VT = la.svd(face_vectors, full_matrices=False)
print("b. Full U shape: (31374, 31374)", " Reduced U shape: ", np.shape(U), " Too large of an array if kept full size")
plt.figure(1)
plt.title("Singular Values")
plt.plot(np.linspace(0,len(sigma),num=len(sigma)),sigma)
plt.plot(np.linspace(0,len(sigma),num=len(sigma)),sigma, "o")
plt.xlabel("Index")
plt.ylabel("Value")
print("c. Rank: 20")
vec = U[:,:5] @(U[:,:5].T @ face_vectors[:,0])
face0_approx = vec.reshape((189,166))
print("d. Used left-singular vectors")
plt.figure(2)
plt.imshow(face_images[0])
plt.figure(3)
plt.imshow(face0_approx)
face0_ratio = (la.norm(face0_approx.flatten(), 2)**2)/(la.norm(face_vectors[:,0])**2)
print("f. the ratio should become closer to 1 as more singular vectors are used")
M = maybe_face_images.shape[0]
maybe_face_vectors = np.zeros((h*w,M))
for i in range(M):
maybe_face_vectors[:,i] = maybe_face_images[i].flatten()
maybe_face_faceliness = []
for i in range(M):
vec = U[:,:] @(U[:,:].T @ maybe_face_vectors[:,i])
approx = vec.reshape((189,166))
ratio = (la.norm(approx.flatten(), 2)**2)/(la.norm(maybe_face_vectors[:,i])**2)
maybe_face_faceliness.append(ratio)
print("h. If the ratio is above a certain threshold, then it probably contains a face")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.