Convolution Neural Network


Convolution Neural Network 

In neural networks, Convolutional neural networks is one of the main categories to do image recognition, images classifications. Object detections, recognition faces etc..

Convolution neural networks are deep artificial neural networks that are used primarily to classify images.

Algorithm for CNN

Step 1: Convolution layer

Provide input image into convolution layer.

Choose parameters, apply filters with strides, padding if required.

Step 2: ReLU layer

Perform convolution on image and apply ReLU activation to the matrix.

Step 3: Pooling layer

Perform pooling  to reduce dimensionality size.

Step 4: Flattening 

Flatten the output and feed into fully connected layer.

Step 5: Fully connected layer

Convolution layer

This layer performs a dot product between two matrices, where one matrix is the set of learnable parameters otherwise known as a kernel.During the forward pass, kernel slides across the height and width of the image. This produces of two-dimensional representation of the image known as activation map that gives the response of the kernel at each spatial position of the image. The sliding size of the kernel is called stride.

convolution accepts large array of pixels as input to the network. The hidden layer of the network carry out feature extraction of the image.

Feature extraction:

A method for feature extraction which makes use of feed forward neural networks with single hidden layer is presented. The hidden activations of the pruned network are the features extracted from the original data set.

ReLU operation:

  • Sets all negative pixels to positive.
  • Introduces non linearity to the network.
  • Performs element wise operation.
  • The output is rectified feature map.
Pooling layer:
The pooling layer replaces the output of the network at certain locations by deriving a summary statistic of the near by outputs. This helps in reducing the spatial size of the representation, which decreases the required amount computation and weights.
Convolution neural networks consists of alternating convolution layers and pooling layers.
The pooling layer is obtained by applying pooling operator to aggregate into within small region of the input feature and them down sampling the results.
Convolution networks include local or global pooling layers.
Pooling layers reduce dimensions of the data by combining the outputs of neuron clusters at one layer into single neuron in the next layer. 


Flattening:
Flattening is the process of converting all the resultant two dimension arrays from pooled feature map into a single long continues linear vector.

Fully connected layer:
Neurons in this layer have full connectivity with neurons in the preceding and succeeding layer as seen in regular FCNN.
the flattened matrix from the pooling layer id feed as input to the fully connected layer to classify the the image. 
The Fully Connected layer helps map the representation between the input and the output.

We train a convolution Neural Network (CNN) on furniture's image. CNNs are regularized versions of multiplayer perceptrons. Multiplayer perceptrons usually refer to fully connected networks. that is, each neuron is one layer is connected to all neurons in the next layer. our trained model learns different furniture images and recognizes which particular furniture is the input image.

Code:
Load furniture data set
from keras.applications import VGG16
conv_base=VGG16(weights='imagenet', include_top=Fase, input_shape=(150, 150, 3))
from keras import models
from keras import layers
model=models.sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(4, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.RMSprop(lr=2e-5), metrics=['acc'])
history=model.fit_generator(train_generator, steps_per_epoch=10, epochs=20, validation_data=validation_generator, validation_steps=6)
conv_base.trainable=True
set_trainable=False
for layer in conv_base.layers:
    if layer.name=='block5_conv1:
        set_trainable=True
        if set_trainabe:
            layer.trainable=True
        else:
             layer.trainable=False
from keras.models import load_model
model=load_model('epoch10.h5')
mc=keras.callbacks.ModelCheckpoint('weights.{epoch:03d}-{val_loss:.2f}.hdf5',save_weights_only=False,period=5)
model.compile(loss='categorical_crossentropy', optimizer=optimizers.RMSprop(lr=le-5),metrics=['acc'])
history=model.fit_generator(train_generator, steps_per_epoch=15, epochs=5, validation_data=validation_generator,validation_steps=6)

The inputs of different images are given one by one into CNN then the layers of CNN will perform their respective tasks at the end it will predict the image.