Homework 2
This repository cotains:
- utils/display_images.py: subroutine for displaying images
- cifar10.py: subroutines for loading and downloading cifar10 data
- omp.py: STARTER CODE for OMP whitening subroutine
- dictlearn.py: STARTER CODE for dictlearn subroutine
Instructions
1. Complete omp.py:
omp.py
contains the following subroutine
def omp(D,x,a,L=None, tol=1e-6):
""" perform orthogonal matching pursuit
inputs:
D: dictionary (k x d numpy array s.t. k>d)
x: image/data vector (1 x d numpy array)
L: maximum sparse coefficients (default: 0.5*k)
a: sparse vector (1 x k numpy array)
outputs:
a: updated sparse vector (k x 1 numpy array)
"""
# dims
k, d = D.shape
# default parameter
if L is None:
L = 0.5 * k
# *** init parameters here
# *** put code for omp here ***
return a
which need to completed using only the numpy
package.
No other packages should be used
2. Complete dictlearn.py
dictlearn.py
includes the following subroutine that needs to be completed.
def ksvd(X, **args):
''' k-svd algorithm
inputs:
x: images (n x d array)
D: optional dictionary. If D is None, then dictionary is learned. Otherwise,
only sparse coefficients are learned. (default None)
args:
k: number of latent variables (defualt 20)
L: sparsity parameter
nsteps: maximum number of iterations (default 200)
tol: toleraance for convergence (default 1e-3)
returns:
J: objective error
D: dictionary (k x d numpy array)
A: sparse coefficients (n x k numpy array)
'''
# default parameters
if not len(args):
args['k'] = 1024
args['nsteps'] = 200
args['L'] = 100
args['tol'] = 1e-3
nsteps = args['nsteps']
k = args['k']
L = args['L']
tol = args['tol']
# dims
n, d = X.shape
# ***initialize variables here***
# *** start main loop ***
for step in range(nsteps):
# *** test for convergence here ***
# display progress
print('{} / {} - J: {}'.format(step, nsteps, J))
# *** sparse coding using orthogonal matching pursuit here ***
# *** dictionary update here ***
return J, D, A
ksvd
needs to completed only using the following packages:
numpy
-
scipy.linalg
No other packages should be used
Parameters
dictlearn.py
also provides a sample main that loads cifar10 using cifar10.py
, performs dictionary learning using omp
and ksvd
subroutines, and displays the learned dictionary D
.
Note that values of parameters such as the tolerance tol
, number of dictionary atoms k
, and number of optimization steps nsteps
may need to be changed
Example
The following image shows an example dictionary learned with 8 x 8 images patches (assuming k=512 and L=25).