Ticker

6/recent/ticker-posts

Convolutional Neural Networks - Keras

 


In This post, we are gonna make a Convolutional Neural Network(CNN) using Keras and Tensorflow for image recognition. Here we are building a neural network with Convolution, MaxPool, Dense and Flatten layers explained in detailed in the comments.

You can download the dataset from this link here  -  CNN Dataset

This is a very interesting program and the neural network in this program promises accurate predictions and its efficient too. You should try this fun project , just install Keras and Tensorflow and start making neural networks.

    # convolutional neural network

    # part 1- Building the CNN
    from keras.models import Sequential
    from keras.layers import MaxPool2D
    from keras.layers import Dense
    from keras.layers import Convolution2D
    from keras.layers import Flatten

    # initializing the CNN
    classifier = Sequential()

    #step-1  add the convolution layer
    # here 32 is the no. of feature detector or same no. of feature maps would be formed, 3,3 is the shape of the feature detector that is the no. of rows and columns. 3,3 can also be called as kernel size.In the input shape 64,64 gives the no. of rows and columns in each image array and 3 means that there are 3 layers that is  red, green and blue. so it represents that the image is coloured so there are 3 colour layers.

    classifier.add(Convolution2D(32,3,3, activation="relu", input_shape=(64,64,3)))

    # step2 is Pooling
    #pooling is done on the feature maps and a matrix of 2by2 or a stride of 2 is applied on the feature maps which   gives us the maximum values can be also called features in the images and this process also reduces the addition data from the feature map and gives us pooled feature map.
    # this step with pool size of 2,2 is just gonna reduce the size of feature map by 2 and create a pooled feature  map with less irrelivant data
    classifier.add(MaxPool2D(pool_size=(2,2)))

    # i am just gonna create another convolution layer for better accuracy
    classifier.add(Convolution2D(64,3,3, activation="relu"))
    classifier.add(MaxPool2D(pool_size=(2,2)))
    classifier.add(Convolution2D(128,3,3, activation="relu"))
    classifier.add(MaxPool2D(pool_size=(2,2)))

    #now we are gonna do the flattening process that we are gonna convert the pooled feature map into an array, row     # wise, and the elements of array are gonna act as indivisual input or node in the ANN.
    classifier.add(Flatten())

    # now we will add the ANN with fully connected dense layers
    # we used sigmoid in the output layer as there could be just 2 outputs- dog or cat. so a binary output could  satisfy aur model result. sigmoid is used for binary output and softmax is just like sigmoid but is used when we have more that 2 output, so it gives us the probabilities. 

    classifier.add(Dense(128, activation="relu"))#first hidden layer or input layer
    classifier.add(Dense(1, activation="sigmoid"))#output layer


    # compiling the model
    # we are gonna use adam optimizer which is used for statistical gradient descent and loss function is the   binary crossentropy as we have binary output here and if we would have more than 2 output than we would have used categorical crossentropy

    classifier.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])


    # fitting the CNN to the images
    # in this step we use the process called image augmentation which creates the preprocessed training images in  which are somewhat tilted or zoomed or horizontally flipped etc. so in this way our model doesnt get overfitted and our models accuracy somewhat also increases with these preprocesses images as they are totally new for our model. we do this step as for gaining high accuracy for a image detection model, we have to   train our model on a lot of images, a lot more than we using that is 8000, so we generally use this function.

    from keras.preprocessing.image import ImageDataGenerator

    train_datagen = ImageDataGenerator( rescale=1./255,
                                        shear_range=0.2,
                                        zoom_range=0.2,
                                        horizontal_flip=True)
    test_datagen = ImageDataGenerator(rescale=1./255)
    training_set = train_datagen.flow_from_directory(
            'dataset/training_set',
            target_size=(64, 64),
            batch_size=1,
            class_mode='binary')
    test_set = test_datagen.flow_from_directory(
            'dataset/test_set',
            target_size=(64, 64),
            batch_size=1,
            class_mode='binary')
    classifier.fit(
            training_set,
            steps_per_epoch=8000,
            epochs=25,
            validation_data=test_set,
            validation_steps=2000)


    # Making a single prediction

    import numpy as np
    from keras.preprocessing import image
    test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg', target_size = (64, 64))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis = 0)
    result = cnn.predict(test_image)
    training_set.class_indices
    if result[0][0] == 1:
        prediction = 'dog'
    else:
        prediction = 'cat'
    print(prediction)

Post a Comment

0 Comments