In [1]:
%matplotlib inline
import math,sys,os,numpy as np
from numpy.linalg import norm
from PIL import Image
from matplotlib import pyplot as plt, rcParams, rc
from scipy.ndimage import imread
from skimage.measure import block_reduce
import cPickle as pickle
from scipy.ndimage.filters import correlate, convolve
from ipywidgets import interact, interactive, fixed
from ipywidgets.widgets import *
rc('animation', html='html5')
rcParams['figure.figsize'] = 3, 6
%precision 4
np.set_printoptions(precision=4, linewidth=100)
In [2]:
"""
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/")
images, labels = mnist.train.images, mnist.train.labels
images = images.reshape((55000,28,28))
np.savez_compressed("MNIST_data/train", images=images, labels=labels)
"""
1
Out[2]:
In [7]:
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data() # saves to /root/.keras/datasets/mnist.pkl.gz
(x_train.shape, y_train.shape, x_test.shape, y_test.shape)
# Alternate:
#import cPickle
#import gzip
#f = gzip.open('/root/.keras/datasets/mnist.pkl.gz', 'rb')
#if sys.version_info < (3,):
# data = cPickle.load(f)
#else:
# data = cPickle.load(f, encoding='bytes')
#f.close()
#(x_train, y_train), (x_test, y_test) = data
Out[7]:
In [8]:
# Add 1 dimension for the color channel.
x_train = np.expand_dims(x_train, 1)
x_test = np.expand_dims(x_test, 1)
# Also need to one-hot encode the training labels
from utils import onehot
y_train = onehot(y_train)
y_test = onehot(y_test)
(x_train.shape, y_train.shape, x_test.shape, y_test.shape)
Out[8]:
In [15]:
def plots(ims, interp=False, titles=None):
ims=np.array(ims)
mn,mx=ims.min(),ims.max()
f = plt.figure(figsize=(12,24))
for i in range(len(ims)):
sp=f.add_subplot(1, len(ims), i+1)
if not titles is None: sp.set_title(titles[i], fontsize=18)
plt.imshow(ims[i], interpolation=None if interp else 'none', vmin=mn,vmax=mx)
def plot(im, interp=False):
f = plt.figure(figsize=(3,6), frameon=True)
plt.imshow(im, interpolation=None if interp else 'none')
plt.gray()
plt.close()
In [26]:
??plt
In [16]:
#data = np.load("MNIST_data/train.npz")
#images=data['images']
#labels=data['labels']
images=x_train
labels=y_train
n=len(images)
images.shape
Out[16]:
In [22]:
inspect_idx = 15
inspect_slice = slice(inspect_idx-5,inspect_idx+5)
#images[inspect_idx]
In [28]:
plot(images[inspect_idx])
#plt.plot(images[inspect_idx])
In [24]:
labels[inspect_idx]
Out[24]:
In [25]:
plots(images[inspect_slice], titles=labels[inspect_slice])
In [10]:
top=[[-1,-1,-1],
[ 1, 1, 1],
[ 0, 0, 0]]
plot(top)
In [11]:
r=(0,28)
def zoomim(x1=0,x2=28,y1=0,y2=28):
plot(images[inspect_idx,y1:y2,x1:x2])
w=interactive(zoomim, x1=r,x2=r,y1=r,y2=r)
w
In [12]:
k=w.kwargs
dims = np.index_exp[k['y1']:k['y2']:1,k['x1']:k['x2']]
images[inspect_idx][dims]
Out[12]:
In [13]:
corrtop = correlate(images[inspect_idx], top)
In [14]:
corrtop[dims]
Out[14]:
In [20]:
plot(corrtop[dims])
In [21]:
plot(corrtop)
In [45]:
np.rot90(top, 1)
Out[45]:
In [46]:
convtop = convolve(images[inspect_idx], np.rot90(top,2))
plot(convtop)
np.allclose(convtop, corrtop)
Out[46]:
In [47]:
straights=[np.rot90(top,i) for i in range(4)]
plots(straights)
In [48]:
br=[[ 0, 0, 1],
[ 0, 1,-1.5],
[ 1,-1.5, 0]]
diags = [np.rot90(br,i) for i in range(4)]
plots(diags)
In [49]:
rots = straights + diags
corrs = [correlate(images[inspect_idx], rot) for rot in rots]
plots(corrs)
In [50]:
def pool(im): return block_reduce(im, (7,7), np.max)
plots([pool(im) for im in corrs])
In [51]:
eights=[images[i] for i in xrange(n) if labels[i]==8]
ones=[images[i] for i in xrange(n) if labels[i]==1]
In [24]:
plots(eights[:5])
plots(ones[:5])
In [25]:
pool8 = [np.array([pool(correlate(im, rot)) for im in eights]) for rot in rots]
In [ ]:
len(pool8), pool8[0].shape
In [26]:
plots(pool8[0][0:5])
In [27]:
def normalize(arr): return (arr-arr.mean())/arr.std()
In [28]:
filts8 = np.array([ims.mean(axis=0) for ims in pool8])
filts8 = normalize(filts8)
In [29]:
plots(filts8)
In [30]:
pool1 = [np.array([pool(correlate(im, rot)) for im in ones]) for rot in rots]
filts1 = np.array([ims.mean(axis=0) for ims in pool1])
filts1 = normalize(filts1)
In [31]:
plots(filts1)
In [32]:
def pool_corr(im): return np.array([pool(correlate(im, rot)) for rot in rots])
In [33]:
plots(pool_corr(eights[0]))
In [35]:
def sse(a,b): return ((a-b)**2).sum()
def is8_n2(im): return 1 if sse(pool_corr(im),filts1) > sse(pool_corr(im),filts8) else 0
In [36]:
sse(pool_corr(eights[0]), filts8), sse(pool_corr(eights[0]), filts1)
Out[36]:
In [37]:
[np.array([is8_n2(im) for im in ims]).sum() for ims in [eights,ones]]
Out[37]:
In [38]:
[np.array([(1-is8_n2(im)) for im in ims]).sum() for ims in [eights,ones]]
Out[38]:
In [ ]:
def n1(a,b): return (np.fabs(a-b)).sum()
def is8_n1(im): return 1 if n1(pool_corr(im),filts1) > n1(pool_corr(im),filts8) else 0
In [ ]:
[np.array([is8_n1(im) for im in ims]).sum() for ims in [eights,ones]]
In [ ]:
[np.array([(1-is8_n1(im)) for im in ims]).sum() for ims in [eights,ones]]
In [8]:
??correlate
In [13]:
correlate([1, 2], [0, 3]) # [1x0 + 1x3, 2x0 + 2x3]
correlate([1,2,3], [0,4,5]) # [1x0 + 1x4 + 1x5, 2x0 + 2x4 + 2x5, 3x0 + 3x4 + 3x5]
Out[13]:
In [ ]: