Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import numpy as np import os from PIL import Image def load _ faces ( path , ext = . pgm ) :
import numpy as np
import os
from PIL import Image
def loadfacespath extpgm:
Load faces into an array N M
where N is the number of face images and M is the dimensionality
heightwidth for greyscale
Hint: oswalk supports recursive listing of files
and directories in a path
Args:
path: path to the directory with face images
ext: extension of the image files you can assume pgm only
Returns:
x: N M array
hw: H W tuple
#
# You code here
#
def PCAmcq:
Which method will be a more reasonable choice in your implementation
: SVD
: Eigendecomposition
Why?
: It is more computationally efficient for our problem
: It allows to compute eigenvectors and eigenvalues of any matrix
: It can be applied to any matrix and is more numerically stable
: We can find the eigenvalues we need for our problem from the singular values
: We can find the singular values we need for our problem from the eigenvalues
Return your answer as a tuple, eg return means that the more reasonable
method is SVD because it is more computationally efficient for our problem, and
allows to compute eigenvector and eigenvalues of any matrix.
return
def computepcaX:
PCA implementation
Args:
X: N M an array with N Mdimensional features
Returns:
u: M M bases with principal components
var: N corresponding variance
#
# You code here
#
def basisu var, p:
Return the minimum number of basis vectors from matrix U such
that they account for at least p percent of total variance.
Hint: Do the singular values really represent the variance?
Args:
u: M M numpy array containing principal components.
For example, i'th vector is u: i
var: N numpy array containing the variance along the principal components.
p: percent of total variance that should be contained.
Returns:
v: M D numpy array that contains M principal components containing at most
p percentile of the variance.
#
# You code here
#
def reconstructfaceimage, meanface, u:
Reconstructs the face image with respect to the first D
principal components u
Args:
faceimage: M numpy array MHW of the face.
meanface: M numpy array MHW mean face.
u: M D matrix containing D principal components.
Returns:
reconstructedimg: M numpy array of reconstructed face image
#
# You code here
#
def componentsmcq:
Select the right answer only one option:
: The first principal components mostly correspond to local features, eg nose, mouth, eyes
: The first principal components predominantly contain global structure, eg complete face
: The fewer principal components we use, the smaller is the reprojection error
: The more principal components we use, the sharper is the image
: The variations in the last principal components is perceptually insignificant; these bases can be neglected in the projection
return
def searchY x u meanface, topn:
Search for the top most similar images based on a given number of
components in their PCA decomposition.
Args:
Y: N M numpy array with N Mdimensional features
x: M numpy array image we would like to retrieve
u: M D numpy arrray, bases vectors. Note, we already assume D has been selected
meanface: M numpy array, mean face as a vector
topn: integer, number of n closest images using L distance to return
Returns:
Y: topn M
#
# You code here
#
def interpolatex x u meanface, n:
Interpolates from x to x
Args:
x: M numpy array, the first image
x: M numpy array, the second image
u: M D numpy array, bases vectors. Note, we already assume D has been selected.
meanface: M numpy array, mean face as a vector
n: number of interpolation steps including x and x
Hint: you can use nplinspace to generate N equallyspaced points on a line
SEE Returns:
Y: n M numpy arrray, interpolated results.
The first dimension is in the index into corresponding
image; Y reconstructx meanface, u; Y reconstructx meanface, u
#
# You code here
#
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started