CNN KeRas (TensorFlow) Example with Cifar10 & Quick CNN in Theano

We will use cifar10 dataset from Toronto Uni for another Keras example. We used this dataset for another CNN model with more detailed process here.

You can find more details on Valentino Zocca, Gianmario Spacagna, Daniel Slater’s book Python Deep Learning.

###the cifar10 dataset is comprised of 10 classes of objects: airplanes, automobiles, 
###birds, cats, deers, dogs, frogs, horses, ships, and trucks. 

import numpy as np 
###1. import data             
from keras.datasets import cifar10
from keras.models import Sequential 
from keras.layers.core import Dense, Activation
from keras.layers import Convolution2D, MaxPooling2D, Flatten
from keras.layers import Dropout
from keras.utils import np_utils

###2. Observe, preprocess and split data
classes = 10 
(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
Y_train = np_utils.to_categorical(Y_train, classes)     
Y_test = np_utils.to_categorical(Y_test, classes)

###3. Set up parameters
batch_size = 100     
hidden_neurons = 200    
epochs = 20

###4. Build the CNN 
model = Sequential()
model.add(Convolution2D(32, (3, 3), input_shape=(32, 32, 3)))
model.add(Activation('relu'))
model.add(Convolution2D(32, (3, 3)))  
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))   

model.add(Convolution2D(64, (3, 3))) 
model.add(Activation('relu'))     
model.add(Convolution2D(64, (3, 3)))     
model.add(Activation('relu'))     
model.add(MaxPooling2D(pool_size=(2, 2)))     
model.add(Dropout(0.25))
               
model.add(Flatten())
 
model.add(Dense(hidden_neurons)) 
model.add(Activation('relu')) 
model.add(Dropout(0.5))      
model.add(Dense(classes)) 
model.add(Activation('softmax'))
     
model.compile(loss='categorical_crossentropy', 
	metrics=['accuracy'], optimizer='adadelta')
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, 
	validation_split = 0.1, verbose=1)

###5. Test and evaluate
score = model.evaluate(X_test, Y_test, verbose=1)
print('Test accuracy:', score[1]) 
###Test accuracy: 0.7314

np.set_printoptions(threshold='nan')  
index = 0   
for layer in model.layers:       
  filename = "conv_layer_" + str(index)       
  f1 = open(filename, 'w+')       
  f1.write(repr(layer.get_weights()))       
  f1.close()       
  print (filename + " has been opened and closed")     
  index = index+1
#SKIPPED

We have a significantly improved result with this new model(0.7314) in this article, compared with the result of this one: 0.45.

We used different combination of Convolution2D, Activation, Pooling layers, regularization methods, with different loss/optimization functions and more epochs of course.

#################################################################
The following is the example for CNN in Theano, which I am not so familiar with.
We use the CNN model to reconstruct the image of astronaut.


import numpy as np
import theano  
import matplotlib.pyplot as plt 
import theano.tensor as T
from theano.tensor.nnet import conv
import skimage.data

import matplotlib.cm as cm
###Theano works by  rst creating a symbolic representation of the operations we define
###We de ne the variables needed and the neural network operations, by de ning the 
###number of feature maps (the depth of the convolutional layer) and the size of the  
###filter, then we symbolically de ne the input using the Theano tensor class.

depth = 4
filter_shape = (3, 3) 
input = T.tensor4(name='input')  
   
w_shape = (depth, 3, filter_shape[0], filter_shape[1]) 
dist = np.random.uniform(-0.2, 0.2, size=w_shape)
W = theano.shared(np.asarray(dist, dtype=input.dtype), name = 'W')                             
conv_output = conv.conv2d(input, W)   
output = T.nnet.sigmoid(conv_output)
f = theano.function([input], output)

###The skimage module we imported can be used to load an image, we will import an 
###image called lena, then after having reshaped the image to be passed in to the 
###Theano function we defined

astronaut = skimage.data.astronaut()
img = np.asarray(astronaut, dtype='float32') / 255
filtered_img = f(img.transpose(2, 0, 1).reshape(1, 3, 512, 512))


plt.axis('off') 
plt.imshow(img) 
plt.show()  
for img in range(depth):
  fig = plt.figure()   
  plt.axis( 'off')   
  plt.imshow(filtered_img[0, img, :, :, ], cmap = cm.gray)
  plt.show()

  filename = "astro" + str(img)
  fig.savefig(filename, bbox_inches='tight')
  
print (W.get_value())
###visualize the weights used
#[[[[-0.16301878 -0.18621896 -0.18338209]
#   [ 0.06357719 -0.07383809  0.19255888]
#   [ 0.03818481 -0.17782268  0.08764949]]#

#  [[-0.01760012  0.14943243 -0.02379293]
#   [-0.11184585 -0.19627694  0.13048806]
#   [ 0.10442394 -0.116303   -0.02064335]]#

#  [[-0.08437459  0.13528617 -0.13373102]
#   [ 0.12458314 -0.11737537  0.07734527]
#   [ 0.06268637 -0.0772257  -0.17128495]]]#
#

# [[[ 0.05853633  0.15095849 -0.14762383]
#   [-0.18264049  0.16169083 -0.12765901]
#   [ 0.13266272 -0.16784693 -0.16796944]]#

#  [[-0.02076725  0.18425296  0.12996845]
#   [ 0.1407634  -0.12032619 -0.0843996 ]
#   [-0.07204886  0.0729924   0.17851035]]#

#  [[-0.14401533  0.0768466  -0.12737689]
#   [ 0.08582157  0.12218379  0.14989744]
#   [-0.03143492  0.04522111 -0.07469705]]]#
#

# [[[ 0.15633859 -0.14617934  0.18642434]
#   [-0.06511963 -0.1936396   0.04354378]
#   [-0.0719605  -0.00252774 -0.09780363]]#

#  [[ 0.17214265 -0.05812361  0.1683185 ]
#   [-0.18314059  0.18575455 -0.00335069]
#   [ 0.09025795 -0.09443732  0.05989196]]#

#  [[ 0.16441579  0.0127632  -0.08911109]
#   [ 0.16661518 -0.19495967 -0.02467694]
#   [-0.1596653   0.14749465  0.16887643]]]#
#

# [[[-0.01298296  0.05963887 -0.07402569]
#   [ 0.15802026  0.15277311 -0.17868301]
#   [ 0.02154541 -0.00411402 -0.00611873]]#

#  [[-0.01512309  0.01985545  0.09706898]
#   [ 0.14609261  0.16392157  0.05649868]
#   [-0.03687655  0.0952949  -0.11755887]]#

#  [[-0.15505062  0.19448228 -0.05702192]
#   [-0.03315583 -0.14914799  0.15471155]
#   [ 0.04977158  0.15516876 -0.03267578]]]]

Screen Shot 2017-06-20 at 3.11.12 PM.png
Screen Shot 2017-06-20 at 3.11.33 PM.jpg

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s