Objective:
To introduce you about convolutional neural network.
Description:
Convolutional Neural Networks (CNN) are complex feed forward neural networks. CNNs are used for image classification and recognition because of its high accuracy There are three types of layers in a convolutional neural network: i. Convolutional layer ii. Pooling layer iii. Fully connected layer Each of these layers has different parameters that can be optimized and performs a different task on the input data.
What is Pooling Layer?
Pooling layer is responsible for reducing the spatial size of the Convolved Feature. This is to decrease the computational power required to process the data through dimensionality reduction. There are two types of Pooling
i. Average Pooling.
ii. Max Pooling
Max Pooling returns the maximum value from the portion of the image covered by the Kernel. On the other hand, Average Pooling returns the average of all the values from the portion of the image covered by the Kernel. Max Pooling also performs as a Noise Suppressant. It discards the noisy activations altogether and also performs de-noising along with dimensionality reduction. On the other hand, Average Pooling simply performs dimensionality reduction as a noise suppressing mechanism. Hence, we can say that Max Pooling performs a lot better than Average Pooling.
What is Convolutional Layer?
Convolutional layers are the major building blocks used in convolutional neural networks. A convolution is the simple application of a filter to an input that results in an activation. Repeated application of the same filter to an input results in a map of activations called a feature map, indicating the locations and strength of a detected feature in an input, such as an image. A convolutional layer contains a set of filters whose parameters need to be learned. The height and weight of the filters are smaller than those of the input volume. Each filter is convolved with the input volume to compute an activation map made of neurons.
What is Fully Connected Layer?
A fully connected layer that takes the output of convolution/pooling and predicts the best label to describe the image We have three layers in the full connection step
i. Input layer
ii. Fully-connected layer
iii. Output layer
Input Layer:
It takes the output of the previous layers, “flattens” them and turns them into a single vector that can be an input for the next stage.
Fully Connected Layer:
It takes the inputs from the feature analysis and applies weights to predict the correct label.
Output Layer:
It gives the final probabilities for each label.
ReLU Layer:
ReLU is an activation function. Rectified Linear Unit (ReLU) transform function only activates a node if the input is above a certain quantity, while the input is below zero, the output is zero, but when the input rises above a certain threshold, it has a linear relationship with the dependent variable. The main aim is to remove all the negative values from the convolution. All the positive values remain the same but all the negative values get changed to zero.
CNN Architectures:
CNN architecture is formed by the stack of distinct layers eg: conv layer pooling and relu.. Each architecture has its own number of layers and convolutional block. In the year 1994, LENET5 is one of the very first convolutional neural networks[2] with the passage of time different architectures are introduced with reduced error rate. Below is the comparison of different CNN architectures. Each architecture has its own no of layers and parameters.
Machine Learning with Tensor Flow: Machine learning is a complex discipline but with help of Tensor Flow it is quite easy to do. Tensor Flow
is an open source machine learning frame work. It gives us ease the process of acquiring data, training
models, serving predictions, and refining future results.
Installation of Machine Learning Library
i. Keras
ii. Tensorflow
You can install these packages from command prompt by using below commands
i. Python –m pip install keras
ii. Python –m pip install tensorflow
Some Useful Commands:
Several utilities/classes need to be imported first: from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D
An instance of Sequential class is then initiated (our model):
my_model = Sequential()
Now, the layers can be added as we please:
my_model.add(Conv2D(64, kernel_size = 3, activation = “relu”, input_shape=(len,width,depth)))
Input Image 512*512 Convolutional layer (3*3*3) Convolutional layer (3*3*3)
Relu layer Convolutional block (3*3*5) Pooling layer (down sample by 2) Pooling layer (down sample by 2)
Fully Connected layer 5 my_model.add(Conv2D (32, kernel_size = 3, activation = “relu”)) my_model.add(MaxPooling2D(pool_size=(2,2)))
my_model.add(Flatten()) my_model.add(Dense(3, activation = “softmax)) For compilation, training and making new predictions: my_model.compile(loss = “categorical_crossentropy”,
optimizer = optimizers.Adam(lr = 1e-4), metrics = [‘acc’]) my_model.fit(training_data, training_labels, epochs = 10, batch_size = 32) my_predictions = my_model.predict(test_data
Here is the task to understand this topic in more effective way:
You are given a basic CNN architecture implement this architecture in python with the help of tensor flow library. In this architecture you have 3 convolutional layers,2 pooling layers and 1 relu layer and at the end there is fully connected layer.
Code:
from keras.models import Sequential
import numpy as np
import pandas as pd
import os
import csv
import tensorflow as tf
import cv2 as cv
from sklearn.model_selection import train_test_split
training_data = []
labels = []
#load images,
resize them and store them in an array
def load_images():
main_path = ‘freestudystrom.site/training_set/' #main path of the sub folders
categories = ['01','02'] #folder 01 for cats and 02 for
dogs
for c in categories:
path =
os.path.join(main_path,c)
#go into each
folder
for img in os.listdir(path):
img_read =
cv.imread(os.path.join(path,img),0) #read all images in the folder
in grayscale
img_read = cv.resize(img_read,(300,300)) #resize them to ensure they all
are of same size
training_data.append(np.array(img_read)) #keep adding the images to
training_data
#load labels and store them in an array
def load_labels():
labels = pd.read_csv('freestudystrom.site/datas.csv') #read input file from path and
store in labels array
labels = np.array(labels) #convert them to array
return labels
load_images() #call load_images function
labels =
load_labels() #store labels
training_data
= np.array(training_data) #convert training_data to an array
labels =
np.array(labels) #convert labels to an array
#print images array and labels array original shapes
print('Read Images Shape : ',training_data.shape)
print('Read Lables Shape : ',np.shape(labels))
#split the
read images to train and test images and labels
train_img,test_img,train_label,test_label
= train_test_split(training_data,labels,test_size=0.2,random_state=2)
train_img = np.expand_dims(train_img, axis=3)
test_img = np.expand_dims(test_img, axis=3)
train_label = np.array(train_label) #convert training labels to an
array
test_label =
np.array(test_label) #convert testing labels to an
array
#print shapes of test and train images array and labels array
print('Train Images Shape : ',train_img.shape)
print('Test Images Shape : ',test_img.shape)
print('Train Label Shape : ',train_label.shape)
print('Test Label Shape : ',test_label.shape)
#create
sequential model
model =
tf.keras.models.Sequential()
#layer 1 :
Convolutional 3x3x3
model.add(tf.keras.layers.Conv2D(3,kernel_size=3 , strides=1 , padding='valid' , input_shape=(300,300,1)))
#layer 2 :
Convolutional 3x3x3
model.add(tf.keras.layers.Conv2D(3,kernel_size=3 , strides=1 , padding='valid'))
#layer 3 :
Pooling by 2x2
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
#layer 4 :
Convolutional 3x3x5
model.add(tf.keras.layers.Conv2D(5,kernel_size=3 , strides=1 , padding='valid'))
#layer 5 :
ReLU
model.add(tf.keras.layers.ReLU())
#layer 6 :
Pooling by 2x2
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
#Flatten i.e
make a 1D array
model.add(tf.keras.layers.Flatten())
#Hidden layers
model.add(tf.keras.layers.Dense(1024,activation='relu'))
model.add(tf.keras.layers.Dense(2,activation='relu')) #for 2 classes : cats and dogs
model.summary() #model summary
model.compile(loss= 'binary_crossentropy', optimizer = "adam", metrics=['accuracy']) #compile model
model.fit(train_img
, train_label , verbose=1,epochs = 10 , batch_size=10) #fit model with training images
and labels
mypredict =
model.predict(test_img) #predict model with test images
for accuracy prediction
print(mypredict)
OUTPUT:
No comments:
Post a Comment