Question: Using single value decomposition in Python 3 to locate faces. Setup Code: import numpy as np from io import BytesIO import scipy.misc from numpy.random import
Using single value decomposition in Python 3 to locate faces.
Setup Code:
import numpy as np from io import BytesIO import scipy.misc from numpy.random import randint bio = lambda image_path: BytesIO(data_files[image_path]) face_images = [] maybe_face_images = [] for i in range(20): image_path = "data/faces/img%02d.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/img%02d.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)

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. ace Review: Facts about orthogonality and the SVD (dick to view) You are given a 3D array face_images, in which each 2D subarray is of shape (h,w). Each subarray represents an image(pixel matrix of size h x w) of a face. You are also given another array maybe face images. Each 2D subarray is again of shape (h,w). It is an image, which may or may not be a face Currently we have a vector space of images which is of dimension hw (e.g. 10,000 dimensional if h 100). Each pixel is a dimension. So first we will construct a subspace of much lower dimension. This will help us in building an image classifier. We can classify whether each image in maybeface images is a face or not using the following steps a Convert each image in face_images to a 1D array by joining rows one after the another. Store each of these 1D arrays as a column in a 2D array face_vectors. If there are N images of size h x w, face_vectors will be of shape (hw, N). This provides you with a matrix, whose columns form a lower dimensional vector space of face images b. Calculate the SVD UEV of face vectors. The columns of U span the vector space of face images. Specify full matrices-False in numpy.1inalg.svd . What is the size of c. Plot the singular values from part (b). Make the z-axis the index of the singular value and the y-axis the magnitude of the singular value. What do you observe? What is the d Approximate the first face, i.e. face images[e] using the first five singular vectors. Store your approximation as a 2D array of size h x w in facee_approx. Note that this is a e. Plot face images[e] and facee approx on two separate plots using matplotlib.pyplot.imshow Hint array.reshape(..) can help you here. Look up its documentation. You may also need to transpose the reshaped array. obtained U? If you don't use this parameter, what will be the size of U? Why are we specifying it? Use print) to show your (very short) answers for these questions (approximate) rank of your vector space? Use print) to present your observation briefly projection onto a subspace of rank 5. Which singular vectors will you use, left or right? Again use print) for your answer 1 Calculate 'how much' of face images[e] is left over after approximating, by measuring face0 approx.flattenOl2 llface vectors[0] face0 ratio NOTE: Here face-vectors [e] means the first face vector which is the 0'h column of face-vectors . In Python syntax this column is referenced with face-vectors( : How will this ratio change as you use more singular vectors? Again use print for your answer g. Now we will do the beginning stages of image classification. First convert each image in maybe face images to a 1D array by joining rows as above. Store each of these 1D arrays as a column in the 2D array maybe face_vectors. If there are M images then it's shape will be (hw, M). We will deal with each image here separately h. For each image vector (we'll all it v) in maybe,face vectors, computeiprojection of v onto the space of face.vectors. Note that this projection is onto the entire |v1 space, not only a projection onto a rank 5 subspace as done previously. Store it in list maybe face faceliness. By looking at this ratio, how could you classify an image in maybe face_images as a face (or not a face)? You may plot and see what images there are in maybe_face_images. Use print) to answer this question. HINTS You don't need to compute v explicitly in part (h). Instead directly compute the square of its 2-norm by using inner products of v and singular vectors. Refer to the introduction. . (x, y)-x:y INPUT: . face_images: 3D numpy array of shape (N, h, w) It has Nimages of size h x w maybe_face_images: 3D array of shape (M,h, w). It has M images of size h x w OUTPUT .facee approx 2D numpy array of shape (h, w) . facee_ratio scalar of type float. . maybe face faceliness: list of length M. Each element will be a float value
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
