A convolutional layer example with Keras for cifar10

We can now try to use the same network on the cifar10 dataset. In Chapter 3, Deep Learning Fundamentals, we were getting a low 50% accuracy on test data, and to test the new network we have just used for the mnist dataset, we need to just make a couple of small changes to our code: we need to load the cifar10 dataset (without doing any re-shaping, those lines will be deleted):

(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()

And then change the input values for the first convolutional layer:

model.add(Convolution2D(32, (3, 3), input_shape=(32, 32, 3)))

Running this network for 5 epochs will give us around 60% accuracy (up from about 50%) and 66% accuracy after 10 epochs, but then the network starts to overfit and stops improving performance.

Of course the cifar10 images have 32 x 32 x 3 = 3072 pixels, instead of 28 x 28=784 pixels, so we may need to add a couple more convolutional layers, after the first two:

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))

In general, it is better to split large convolutional layers into smaller-sized convolutional layers. For example, if we have two consecutive (3 x 3) convolutional layers, the first layer will have a (3 x 3) view of the input image, and the second layer will have a (5 x 5) view of the input image for each pixel. However, each layer will have non-linear features that will stack up, creating more complex and interesting features of the input than we would get by simply creating a single (5 x 5) filter.

If we run this network for 3 epochs, we are also getting around 60%, but after 20 epochs we are up to 75% accuracy by using a simple network. The state-of-the-art convolutional networks can get around 90% accuracy, but require longer training and are more complicated. We will graphically present the architecture of one important convolutional neural network, called VGG-16, in the next paragraph so that the user can try to implement it using Keras or any other language he or she is comfortable with, such as Theano or TensorFlow (the network was originally created using Caffe, an important deep learning framework developed at Berkeley, see: http://caffe.berkeleyvision.org).

When working with neural networks, it is important to be able to "see" the weights the network has learned. This allows the user to understand what features the network is learning and to allow for better tuning. This simple code will output all the weights for each layer:

index = 0
numpy.set_printoptions(threshold='nan')     
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

If, for example, we are interested in the weights for layer 0, the first convolutional layer, we can apply them to the image to see what features the network is highlighting. If we apply these filters to the image lena, we get:

A convolutional layer example with Keras for cifar10

We can see how each filter is highlighting different features.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset