In [2]:
import sys, getopt

import argparse

import sys
import os
import time

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.pyplot  as pyplot
import gzip

import theano
import theano.tensor as T

import lasagne
import nolearn


Using gpu device 0: Tesla P100-PCIE-16GB (CNMeM is disabled, cuDNN 5105)

In [3]:
def load_dataset_mnist():
	# We first define a download function, supporting both Python 2 and 3.
	if sys.version_info[0] == 2:
		from urllib import urlretrieve
	else:
		from urllib.request import urlretrieve

	def download(filename, source='http://yann.lecun.com/exdb/mnist/'):
		print("Downloading %s" % filename)
		urlretrieve(source + filename, filename)

	# We then define functions for loading MNIST images and labels.
	# For convenience, they also download the requested files if needed.
	import gzip

	def load_mnist_images(filename):
		if not os.path.exists(filename):
			download(filename)
		# Read the inputs in Yann LeCun's binary format.
		with gzip.open(filename, 'rb') as f:
			data = np.frombuffer(f.read(), np.uint8, offset=16)
		# The inputs are vectors now, we reshape them to monochrome 2D images,
		# following the shape convention: (examples, channels, rows, columns)
		data = data.reshape(-1, 1, 28, 28)
		# The inputs come as bytes, we convert them to float32 in range [0,1].
		# (Actually to range [0, 255/256], for compatibility to the version
		# provided at http://deeplearning.net/data/mnist/mnist.pkl.gz.)
		return (data / np.float32(256))
# 		return 1-(2*(data / np.float32(256)))

	def load_mnist_labels(filename):
		if not os.path.exists(filename):
			download(filename)
		# Read the labels in Yann LeCun's binary format.
		with gzip.open(filename, 'rb') as f:
			data = np.frombuffer(f.read(), np.uint8, offset=8)
		# The labels are vectors of integers now, that's exactly what we want.
		return data

	# We can now download and read the training and test set images and labels.
	X_train = load_mnist_images('train-images-idx3-ubyte.gz')
	y_train = load_mnist_labels('train-labels-idx1-ubyte.gz')
	X_test = load_mnist_images('t10k-images-idx3-ubyte.gz')
	y_test = load_mnist_labels('t10k-labels-idx1-ubyte.gz')

	# We reserve the last 10000 training examples for validation.
# 	X_train, X_val = X_train[:-10000], X_train[-10000:]
# 	y_train, y_val = y_train[:-10000], y_train[-10000:]
	
	
	# We just return all the arrays in order, as expected in main().
	# (It doesn't matter how we do this as long as we can read them again.)
	return X_train, y_train, X_test, y_test

In [4]:
def build_cnn(input_var=None):
	l_in = lasagne.layers.InputLayer(shape=(None, 1, 28, 28), input_var=input_var)
	# print(lasagne.layers.get_output_shape(l_in))
	l_c1 = lasagne.layers.Conv2DLayer(l_in, num_filters=16, filter_size=(5,5), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotNormal() )
	l_c1p = lasagne.layers.MaxPool2DLayer(l_c1, pool_size=(2,2) )
	l_c2 = lasagne.layers.Conv2DLayer( l_c1p, num_filters=16, filter_size=(5,5), nonlinearity=lasagne.nonlinearities.rectify , W=lasagne.init.GlorotNormal() )
	l_c2p = lasagne.layers.MaxPool2DLayer(l_c2, pool_size=(2,2) )
	# l_cclass = lasagne.layers.FlattenLayer(l_cclass, outdim=2, )
	l_outclass = lasagne.layers.DenseLayer(lasagne.layers.dropout(l_c2p, p=0.5), num_units=10, nonlinearity=lasagne.nonlinearities.softmax)
	
	# print("output class:", lasagne.layers.get_output_shape(l_cclass))
	
	# print("output reconstruction:",lasagne.layers.get_output_shape(l_out))
	return l_outclass

In [5]:
def iterate_minibatches(inputs, targets, batchsize, shuffle=False):
	assert len(inputs) == len(targets)
	if shuffle:
		indices = np.arange(len(inputs))
		np.random.shuffle(indices)
	for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
		if shuffle:
			excerpt = indices[start_idx:start_idx + batchsize]
		else:
			excerpt = slice(start_idx, start_idx + batchsize)
		yield inputs[excerpt], targets[excerpt]

In [6]:
X_train, y_train, X_test, y_test = load_dataset_mnist()

In [7]:
Y_train = np.repeat(y_train,28*28).reshape((len(y_train), 1, 28, 28))
Y_test = np.repeat(y_test,28*28).reshape((len(y_test), 1, 28, 28))

In [8]:
plt.imshow(X_train[np.random.randint(0, len(X_train)), 0, :,:])
plt.gray()
plt.show()



In [9]:
num_epochs=1
prop_valid=20
size_minibatch = 100 

print("Set network")
input_var = T.tensor4('inputs')
target_var = T.tensor4('targets')
class_var = T.ivector('classes')
seg_var = T.tensor4('segmentations')
network_cnn = build_cnn(input_var)


Set network

In [10]:
out_cnn = lasagne.layers.get_output(network_cnn)
ce_cnn = lasagne.objectives.categorical_crossentropy(out_cnn, class_var)
ace_cnn = lasagne.objectives.aggregate(ce_cnn)
params_cnn = lasagne.layers.get_all_params(network_cnn, trainable=True)
updates_cnn = lasagne.updates.nesterov_momentum(ace_cnn, params_cnn, learning_rate=0.1, momentum=0.9)
train_fn_cnn = theano.function([input_var, class_var], ace_cnn, updates=updates_cnn)

cnn = lasagne.layers.get_output(network_cnn, deterministic=True)
cnn_ce = lasagne.objectives.categorical_crossentropy(cnn, class_var)
cnn_ace = lasagne.objectives.aggregate(cnn_ce)
cnn_acc = T.mean( T.eq( T.argmax( cnn, axis=1 ), class_var ), dtype=theano.config.floatX )
eval_cnn = theano.function([input_var, class_var], [cnn_ace, cnn_acc] )

In [14]:
train_ace = 0
train_batches = 0
start_time = time.time()
for batch in iterate_minibatches(X_train, y_train, 500, shuffle=True):
    inputs, classes = batch
    train_ace += train_fn_cnn( inputs, classes )
    train_batches += 1
    print(train_ace / train_batches)
test_ace = 0
test_acc = 0
test_batches = 0
for batch in iterate_minibatches(X_test, y_test, 500, shuffle=True):
    inputs, classes = batch
    ace, acc = eval_cnn( inputs, classes )
    test_ace += ace
    test_acc += acc
    test_batches += 1
print("Ace :", test_ace / test_batches)
print("ACC:\t\t{:.2f} %".format( 100*(test_acc / test_batches) ) )
#print("Acc :", test_acc / test_batches)
print("time:", time.time() - start_time , "s")


0.219732537866
0.204012431204
0.195550054312
0.187306694686
0.195568218827
0.196663849056
0.192231742399
0.199438659474
0.195519602961
0.189558626711
0.188028964129
0.183866693328
0.181874879278
0.184352130762
0.184060737491
0.182929250412
0.181344936876
0.183879615532
0.18255701818
0.181983741373
0.184115691554
0.183506085114
0.18238155803
0.182760215054
0.180337018967
0.179077649346
0.177898709421
0.177232967424
0.178534959925
0.17891043971
0.180125906102
0.180712245405
0.180683350924
0.18146463133
0.179519010442
0.179059215718
0.17973024378
0.178584348999
0.178779516083
0.179051552713
0.178308927431
0.178203150275
0.17770623433
0.178243392232
0.177978550726
0.17722962246
0.176899514934
0.17667624106
0.177259639514
0.177820411026
0.177617434777
0.177139751613
0.176502264614
0.176871438545
0.17639128918
0.176377311615
0.175709353727
0.175382286824
0.175275345222
0.17407467713
0.173758052412
0.173094330536
0.173443572152
0.173081592191
0.172326377378
0.172083426267
0.171697581771
0.170930145199
0.171119158467
0.17120025903
0.171635624808
0.171415968074
0.17093787475
0.171966008641
0.171544755697
0.170908661951
0.17149533999
0.171570664511
0.171937496885
0.17217479283
0.172550420058
0.172757617675
0.172640467534
0.172135932548
0.172212178655
0.17216421863
0.171811369558
0.171736758287
0.171496721382
0.171184695181
0.171112118268
0.170934320225
0.171421787672
0.170637625329
0.17027285946
0.169745512229
0.169488789264
0.169057889937
0.169167844409
0.16890034087
0.169031057691
0.168707845039
0.168152342722
0.168289978487
0.168001753234
0.168331271775
0.167817726453
0.16785308015
0.16806100514
0.167988818207
0.167783789135
0.167704204363
0.16829172924
0.167751419962
0.168130659409
0.168376809179
0.168152683056
0.168077129808
0.167952932605
0.167807798336
Ace : 0.0592968197539
ACC:		98.20 %
time: 1.1186859607696533 s

In [12]:
nn_cnn = lasagne.layers.get_all_param_values(network_cnn)
df_nn_cnn = pd.DataFrame(nn_cnn)
print(df_nn_cnn)


                                                   0
0  [[[[ 0.16502364  0.14663561 -0.08001634 -0.104...
1  [-0.0639891, 0.0929369, -0.0500132, -0.196512,...
2  [[[[-0.06532975 -0.12259582 -0.00795136  0.015...
3  [-0.154948, -0.0746034, 0.0176097, -0.0646097,...
4  [[0.0605964, 0.0554546, 0.0541457, -0.0484306,...
5  [-0.119447, 0.394134, -0.0713351, -0.03419, -0...

In [13]:
nbRow = 2
fig, axes = plt.subplots(nrows=nbRow, ncols=16)
l1 = df_nn_cnn[0][0]
l1 = l1.max(axis=1)
nbCol = l1.shape[0]
for c in range(nbCol):
    f = l1[c]
    axes[0, c].imshow(f)
    axes[0, c].set_axis_off()
l2 = df_nn_cnn[0][2]
l2 = l2.max(axis=1)
nbCol = l2.shape[0]
for c in range(nbCol):
    f = l2[c]
    axes[1, c].imshow(f)
    axes[1, c].set_axis_off()
#plt.gray()
plt.axis('off')
plt.show()



In [119]:
l_in = lasagne.layers.InputLayer(shape=(None, 1, 28, 28), input_var=input_var)
# print(lasagne.layers.get_output_shape(l_in))
l_c1 = lasagne.layers.Conv2DLayer(l_in, num_filters=16, filter_size=(5,5), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotNormal() )
l_c1p = lasagne.layers.MaxPool2DLayer(l_c1, pool_size=(2,2) )
l_c2 = lasagne.layers.Conv2DLayer( l_c1p, num_filters=16, filter_size=(5,5), nonlinearity=lasagne.nonlinearities.rectify , W=lasagne.init.GlorotNormal() )
l_c2p = lasagne.layers.MaxPool2DLayer(l_c2, pool_size=(2,2) )
# l_cclass = lasagne.layers.FlattenLayer(l_cclass, outdim=2, )
l_out = lasagne.layers.DenseLayer(lasagne.layers.dropout(l_c2p, p=0.5), num_units=10, nonlinearity=lasagne.nonlinearities.softmax)

lasagne.layers.set_all_param_values( l_out, lasagne.layers.get_all_param_values(network_cnn) )

In [ ]: