Skip to content
Snippets Groups Projects
Commit d580d2c1 authored by Ben Riggan's avatar Ben Riggan
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
LICENSE 0 → 100644
BSD 3-Clause License
Copyright (c) 2020, Regents of the University of Nebraska
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Nebraska nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Homework 1
This repository cotains:
* utils/display_images.py: subroutine for displaying images
* cifar10.py: subroutines for loading and downloading cifar10 data
* zca.py: STARTER CODE for zca whitening subroutine
* ica.py: STARTER CODE for ICA subroutine
## Instructions
### 1. Complete zca.py:
`zca.py` contains the following subroutine
```python
def zca_white(x):
""" perform zca whitening
inputs:
x: numpy array of images
outputs:
y: numpy array of whitened images
"""
# *** put code for zca whitening here ***
return y
```
which need to completed using only the `numpy` package.
**No other packages should be used**
### 2. Complete ica.py
`ica.py` includes the following subroutine
```python
def ica(x, **args):
''' perform independent component analysis (ICA)
inputs:
x: numpy array of images
args:
lr: learning rate (default 1e-3)
nsteps: maximum iterations (default 100)
k: number of latent variables
returns:
L: numpy array of loss function value for all iterations
W: numpy array of ICA basis vectors
'''
# default parameters
if not len(args):
args['lr'] = 1e-4
args['nsteps'] = 1000
args['k'] = 20
lr=args['lr']
nsteps = args['nsteps']
k = args['k']
# ***initialize variables here***
'''training loop using graident descent'''
for step in range(nsteps):
# ***insert gradient descent code here***
# print loss
print('step: {} / {}, L: {}'.format(step, nsteps, L[step]))
return L, W
```
which needs to completed only the following packages:
* `numpy`
* `scipy.linalg`
**No other packages should be used**
### Parameters
`ica.py` also provides a sample main that loads cifar10 using `cifar10.py`,
whitens the images using `zca.py`, performs ICA using the `ica(x,**args)`, and displays
and displays the learned basis images `W`.
**Note that values of parameters such as the learning rate `lr`, number of basis
images `k`, and number of optimization steps `nsteps` may need to be changed**
### Example
The following image show an example result of applying ICA to whitened cifar10 data
![Test Image 1](cifar_ica_16.png)
File added
File added
import pickle
import tarfile
import wget
import logging
import os
import numpy as np
def unpickle(batch):
''' open batch filename '''
with open(batch, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
def download_cifar10(path, url):
''' download cifar10 dataset '''
filename = os.path.join(path,os.path.basename(url))
print('Downloading cifar10 from %s...' % url)
try:
wget.download(url, filename)
except:
raise Exception('Error downloading cifar10 from %s' % url)
def cifar10(path='./data', url='https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz', batch='data_batch_1'):
''' return cifar10 data batch dictonary containing images and labels '''
logging.basicConfig(level=logging.INFO)
try:
os.makedirs(path)
print('making data directory: %s' % os.path.dirname(os.path.abspath(__file__)))
except:
logging.info('directory already exists: %s' % path)
pass
filename = os.path.join(path,os.path.basename(url))
# download cifar if necessary
if not os.path.exists(filename):
download_cifar10(path,url)
""" load cifar data batch dictionary """
# extract archive
tf = tarfile.open(filename)
files = tf.getnames()
imglist = []
lablist = []
for i in range(len(batch)):
assert os.path.join(files[0],batch[i]) in files, 'invalid batch: %s' % batch[i]
# update path
if os.path.basename(path) != files[0]:
path = os.path.join(path,files[0])
if not os.path.exists(path):
tf.extractall(path)
# load data
batch_filename = os.path.join(path, batch[i])
data = unpickle(batch_filename)
imglist.append(data[b'data'])
lablist.append(data[b'labels'])
x = np.concatenate(imglist, axis=0)
y = np.concatenate(lablist, axis=0)
return x, y
cifar_ica_16.png

62.8 KiB

ica.py 0 → 100644
import numpy as np
import argparse
import scipy.linalg
from zca import zca_white
from cifar10 import cifar10
from utils.display_images import display_images
def ica(x, **args):
''' perform independent component analysis (ICA)
inputs:
x: numpy array of images
args:
lr: learning rate (default 1e-3)
nsteps: maximum iterations (default 1000)
k: number of latent variables (defualt 20)
returns:
L: numpy array of loss function value for all iterations
W: numpy array of ICA basis vectors
'''
# default parameters
if not len(args):
args['lr'] = 1e-4
args['nsteps'] = 1000
args['k'] = 20
lr=args['lr']
nsteps = args['nsteps']
k = args['k']
# ***initialize variables here***
'''training loop using graident descent'''
for step in range(nsteps):
# ***insert gradient descent code here***
# print loss
print('step: {} / {}, L: {}'.format(step, nsteps, L[step]))
return L, W
if __name__ == '__main__':
# command line arg parser
parser = argparse.ArgumentParser(description='Perform ICA on cifar-10')
parser.add_argument('-b',
'--batch',
required=True,
nargs='+',
help="cifar10 file(s) data_batch_1, ..., data_batch_5, or test_batch")
parser.add_argument('--k',
type=int,
required=False,
default=20,
help="number of latent variables / basis images")
parser.add_argument('--lr',
type=float,
required=False,
default=1e-3,
help="learning rate")
parser.add_argument('--nsteps',
type=int,
required=False,
default=1000,
help="number of iterations")
args = parser.parse_args()
batch = args.batch
k = args.k
lr = args.lr
nsteps = args.nsteps
# load cifar10 data
images, labels = cifar10(batch=batch)
# perform zca whitening
# ***complete zca_white function in zca.py***
images_ = zca_white(images)
# perform ICA
# *** complete ica function in ica.py ***
L, W = ica(images_, lr=lr, nsteps=nsteps, k=k)
# display ICA basis images
display_images(W.T)
File added
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
import numpy as np
def display_images(x, figsize=(8,8), rows=5, cols=4, reshape=True, shuffle=False, normalize=True):
''' display images in a grid of rows and cols '''
n = x.shape[0]
if reshape:
# unravel vector to channel first image
x = x.reshape((-1,3,32,32))
# channel last image
x = np.moveaxis(x,1,-1)
#shuffle images to show different images
if shuffle:
idx = np.random.permutation(n)
x = x[idx]
# grid of images
fig=plt.figure(figsize=figsize)
for i in range(rows*cols):
# break if too few images exist
if i>=n:
break
fig.add_subplot(rows, cols, i+1)
if not normalize:
plt.imshow(x[i])
else:
x_ = x[i]-x[i].min()
x_ = x_ / x_.max()
plt.imshow(x_)
plt.show()
zca.py 0 → 100644
import numpy as np
def zca_white(x):
""" perform zca whitening
inputs:
x: numpy array of images
outputs:
y: numpy array of whitened images
"""
# *** put code for zca whitening here ***
return y
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment