In [1]:
import pandas as pd
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from theano.tensor.nnet import softmax
from skimage.color import rgb2gray
In [2]:
%pylab inline
In [54]:
import nolearn
reload(nolearn)
from nolearn.nntools import NeuralNet
from nolearn.nntools import BatchIterator
from nolearn.nntools import FlipBatchIterator
from nolearn.nntools import LabelEncoder
from nntools import layers
import visualize
reload(visualize)
from visualize import PlotLoss
from visualize import PlotWeights
from visualize import PlotWeightChanges
Use Cifar-10 images as provided by kaggle: http://www.kaggle.com/c/cifar-10/data
In [4]:
filepath = '../data/cifar10-train/' # filepath of images
filelabels = '../data/cifar10-trainLabels.csv'
In [5]:
df_labels = pd.read_csv(filelabels, index_col=0)
In [6]:
df_labels['idx'] = np.array(df_labels.index) - 1
df_labels.set_index('idx', drop=True, inplace=True)
In [7]:
encoder = LabelEncoder()
In [8]:
def load_images(n=50000, grayscale=True):
X = np.zeros((n, 32, 32))
for i in range(n):
img = Image.open('{}{}.png'.format(filepath, i + 1))
if grayscale:
X[i] = rgb2gray(np.array(img))
else:
X[i] = np.array(img)
return X
In [9]:
%time X = load_images()
In [10]:
y = encoder.fit_transform(df_labels.values.reshape(-1))
In [11]:
X = X.astype(np.float32)
In [12]:
# use flat images for fully connected layers
X_flat = X.reshape(-1, 32 * 32)
In [13]:
# reshape X to be bc01 compliant
Xr = X.reshape(X.shape[0], 1, X.shape[1], X.shape[2])
In [13]:
def plot_cifar(data, label=None):
plt.figure(figsize=(2, 2))
plt.imshow(data, cmap=plt.get_cmap('gray'), interpolation='bessel')
if label is None:
plt.title(df_labels.ix[i])
else:
if not isinstance(label, tuple):
plt.title(label)
else:
plt.title("pred: {}, true: {}".format(*label))
plt.xticks([])
plt.yticks([])
In [14]:
for i in range(5):
plot_cifar(X[i])
plt.show()
In [14]:
%pylab qt
In [15]:
BATCHSIZE = 128
In [16]:
the_layers = [
('input', layers.InputLayer),
('hidden1', layers.DenseLayer),
('hidden2', layers.DenseLayer),
('output', layers.DenseLayer),
]
In [44]:
nn = NeuralNet(
layers=the_layers,
update_learning_rate=0.02,
update_momentum=0.9,
input_shape=(BATCHSIZE, X.shape[1] ** 2),
hidden1_num_units=200,
hidden2_num_units=200,
output_num_units=10,
output_nonlinearity=softmax,
max_epochs=5,
verbose=1,
on_epoch_finished=PlotLoss(update_every=1),
)
In [45]:
%time nn.fit(X_flat, y)
Out[45]:
In [55]:
nn = NeuralNet(
layers=the_layers,
update_learning_rate=0.02,
update_momentum=0.9,
input_shape=(BATCHSIZE, X.shape[1] ** 2),
hidden1_num_units=200,
hidden2_num_units=200,
output_num_units=10,
output_nonlinearity=softmax,
max_epochs=5,
verbose=1,
on_epoch_finished=PlotWeightChanges(update_every=1),
)
In [56]:
%time nn.fit(X_flat, y)
Out[56]:
nrows x ncols will be the number of visualized units shown in a grid. figsize is the size of the figure
In [18]:
nn = NeuralNet(
layers=the_layers,
update_learning_rate=0.02,
update_momentum=0.9,
input_shape=(BATCHSIZE, X.shape[1] ** 2),
hidden1_num_units=200,
hidden2_num_units=200,
output_num_units=10,
output_nonlinearity=softmax,
max_epochs=5,
verbose=1,
on_epoch_finished=PlotWeights(nrows=10, ncols=10, figsize=(10, 10)),
)
In [19]:
%time nn.fit(X_flat, y)
Out[19]:
In [20]:
plt.close()
In [25]:
%pylab qt
In [21]:
BATCHSIZE = 100
In [22]:
the_layers = [
('input', layers.InputLayer),
('dropout1', layers.DropoutLayer),
('conv2d', layers.Conv2DLayer),
('dropout2', layers.DropoutLayer),
('hidden1', layers.DenseLayer),
('output', layers.DenseLayer),
]
In [23]:
nn = NeuralNet(
layers=the_layers,
update_learning_rate=0.02,
update_momentum=0.9,
batch_iterator=BatchIterator(BATCHSIZE),
input_shape=(BATCHSIZE, Xr.shape[1], Xr.shape[2], Xr.shape[3]),
hidden1_num_units=100,
conv2d_num_filters=9,
conv2d_filter_size=(8, 8),
output_num_units=10,
output_nonlinearity=softmax,
max_epochs=5,
verbose=1,
on_epoch_finished=PlotWeights(nrows=3, ncols=3, figsize=(3, 3)),
)
In [24]:
%time nn.fit(Xr, y)
In [39]:
the_layers = [
('input', layers.InputLayer),
('conv2d1', layers.Conv2DLayer),
('maxpool1', layers.MaxPool2DLayer),
('conv2d2', layers.Conv2DLayer),
('maxpool2', layers.MaxPool2DLayer),
('dropout1', layers.DropoutLayer),
('hidden1', layers.DenseLayer),
('output', layers.DenseLayer),
]
In [76]:
nn = NeuralNet(
layers=the_layers,
update_learning_rate=0.02,
update_momentum=0.9,
batch_iterator=BatchIterator(BATCHSIZE),
input_shape=(BATCHSIZE, Xr.shape[1], Xr.shape[2], Xr.shape[3]),
conv2d1_num_filters=25,
conv2d1_filter_size=(9, 9),
maxpool1_ds=(2, 2),
conv2d2_num_filters=16,
conv2d2_filter_size=(8, 8),
maxpool2_ds=(2, 2),
hidden1_num_units=100,
output_num_units=10,
output_nonlinearity=softmax,
max_epochs=25,
verbose=1,
on_epoch_finished=PlotWeights(nrows=4, ncols=4, figsize=(4, 4),
vis_layer='conv2d2', nchannel=24),
)
You can also visualize a specific layer by indicating the vis_layer argument as the layer's name. Furthermore, indicate which channel you like to visualize by giving nchannel.
In [63]:
%time nn.fit(Xr, y)
In [32]:
%pdb on
In [34]:
BATCHSIZE = 128
In [37]:
the_layers = [
('input', layers.InputLayer),
('conv1', layers.cuda_convnet.Conv2DCCLayer),
('maxpool1', layers.cuda_convnet.MaxPool2DCCLayer),
('conv2', layers.cuda_convnet.Conv2DCCLayer),
('maxpool2', layers.cuda_convnet.MaxPool2DCCLayer),
('hidden1', layers.DenseLayer),
]
In [38]:
nn = NeuralNet(
layers=the_layers,
update_learning_rate=0.02,
update_momentum=0.9,
batch_iterator=BatchIterator(BATCHSIZE),
input_shape=(BATCHSIZE, Xr.shape[1], Xr.shape[2], Xr.shape[3]),
conv1_num_filters=32,
conv1_filter_size=(8, 8),
maxpool1_ds=(2, 2),
conv2_num_filters=16,
conv2_filter_size=(8, 8),
maxpool2_ds=(2, 2),
hidden1_num_units=100,
output_num_units=10,
output_nonlinearity=softmax,
max_epochs=5,
verbose=1,
# on_epoch_finished=PlotLoss(update_every=1),
on_epoch_finished=PlotWeights(nrows=2, ncols=2, figsize=(6, 6)),
)
In [127]:
%time nn.fit(Xr, y)
In [44]:
%pdb on
In [109]:
the_layers = [
('input', layers.InputLayer),
('conv2d', layers.Conv1DLayer),
('maxpool2d', layers.MaxPool2DLayer),
('hidden1', layers.DenseLayer),
('output', layers.DenseLayer),
]
In [32]:
nn = NeuralNet(
layers=the_layers,
update_learning_rate=0.02,
update_momentum=0.9,
update_weight_decay=0.001,
input_shape=(BATCHSIZE, X.shape[1] ** 2),
hidden1_num_units=512,
conv2d_num_filter=4,
conv2d_filter_size=(4, 4),
maxpool2d_ds=(2, 2),
output_num_units=10,
output_nonlinearity=softmax,
max_epochs=5,
verbose=1,
# on_epoch_finished=PlotLoss(update_every=1),
on_epoch_finished=PlotWeights(nrows=10, ncols=10, figsize=(10, 10)),
)
In [ ]:
%time nn.fit(X_flat, y)
In [162]:
preds = encoder.inverse_transform(nn.predict(Xr[:1000]))
trues = list(df_labels.values.reshape(-1))
In [157]:
%pylab inline
In [158]:
ranges = range(10, 15)
for i in ranges:
plot_cifar(Xr[i], (preds[i], trues[i]))
In [160]:
def plot_confusion_matrix(y_true, y_pred, encoder, figsize=(8, 8), colors='Blues'):
n_labels = len(np.unique(y_true))
fig, ax = plt.subplots(figsize=figsize)
ax.imshow(confusion_matrix(trues, preds), interpolation='nearest', cmap=plt.get_cmap(colors))
ax.grid(False)
labels = encoder.inverse_transform(range(10))
plt.xticks(range(n_labels), labels, rotation='vertical')
plt.yticks(range(n_labels), labels)
plt.title('confusion matrix')
return fig, ax
In [164]:
plot_confusion_matrix(trues, preds, encoder)
In [ ]: