Saturday, May 9, 2020

Deep Learning 2-Classification with Custom CNN

Learn A-Z Deep Learning in 15 Days


In this Lecture, will learn about Classification with Custom CNN

Imp:-Use Kaggle Kernel Or Google Colab to Run Code. Download the Cat Vs Dog dataset from the link below.

import cv2
import os 
import numpy as np


Unzip  the Dataset

!unzip ../input/train.zip

!unzip ../input/test1.zip


Import the Train 

IMAGE_WIDTH = 128
IMAGE_HEIGHT = 128
IMAGE_CHANNELS = 1
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)

directory = "/kaggle/working/train"
data = []
label = []

for filename in os.listdir(directory):

    image = cv2.imread(directory+r'/'+filename,0)
    
    if image is None:
        continue
    image = cv2.resize(image,IMAGE_SIZE)
    
    category = filename.split('.')[0]
    if category == 'dog':
        label.append(1)
    else:
        label.append(0)

    data.append(image/255)


List to Array Conversion

data=np.array(data)
data=data.reshape((data.shape)[0],(data.shape)[1],(data.shape)[2],1)
label=np.array(label)
print(data.shape)
print(label.shape)


Train Test Split

from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(data, label, test_size=0.3, random_state=42) 


One hot encoding

from keras.utils import np_utils
y_train = np_utils.to_categorical(y_train,num_classes=2)
y_val = np_utils.to_categorical(y_val,num_classes=2)


Custom CNN Architecture

### 1st Network
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization

model = Sequential()

model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax')) 

model.summary()
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

OR

### 2nd Network
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Activation, Dropout, Conv2D, MaxPooling2D, AveragePooling2D, Input, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers 


img_input  = Input(shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS))

x = Conv2D(32, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Conv2D(64, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Conv2D(128, (3, 3),activation='relu',padding='same')(img_input)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Dropout(0.25)(x)

x = Flatten()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(2, activation='softmax', name='predictions')(x)

model = Model(img_input , x)
model.summary()
model.compile(loss='categorical_crossentropy',optimizer=optimizers.adam(lr=1e-8),metrics=['acc'])


Callbacks

from keras.callbacks import ModelCheckpoint, EarlyStopping

filepath = "./cp-{epoch:02d}.h5"

checkpoint = ModelCheckpoint(filepath,
                             monitor="val_loss",
                             mode="min",
                             save_best_only = True,
                             verbose=1)


earlystop = EarlyStopping(monitor = 'val_loss', 
                          min_delta = 0, 
                          patience =4,
                          verbose = 1,
                          restore_best_weights = True)

# put our callbacks into a callback list
callbacks = [earlystop, checkpoint]


Start the Training

model.fit(x_train,y_train,validation_data=(x_val,y_val),epochs=50,batch_size=64, callbacks = callbacks)


Visualize Graph

### Graph Epoch vs acc AND Epoch vs Loss

import matplotlib.pyplot as plt

acc = model.history.history['acc']
val_acc = model.history.history['val_acc']
loss = model.history.history['loss']
val_loss = model.history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'blue', label='Training acc')
plt.plot(epochs, val_acc, 'red', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'blue', label='Training loss')
plt.plot(epochs, val_loss, 'red', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()



Import test dataset

IMAGE_WIDTH = 128
IMAGE_HEIGHT = 128
IMAGE_CHANNELS = 1
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)

directory = "/kaggle/working/test1"
data = []


for filename in os.listdir(directory):

    image = cv2.imread(directory+r'/'+filename,0)
    if image is None:
        continue
    image = cv2.resize(image,IMAGE_SIZE)
    
    data.append(image/255)


List to Array Conversion

data = np.array(data)
test_image = np.array(data.reshape((data.shape)[0],(data.shape)[1],(data.shape)[2],1))


Make the Prediction

predictions = model.predict(test_image)
results = np.argmax(predictions, axis = 1)


Convert the label into the category

key={0:'cat',1:'dog'}
label_prediction=[key[r] for r in results]


Display the Result

import matplotlib.image as img
import matplotlib.pyplot as plt

nb_rows = 3
nb_cols = 3
fig, axs = plt.subplots(nb_rows, nb_cols, figsize=(6, 6), dpi =100)

n = 0
for i in range(0, nb_rows):
    for j in range(0, nb_cols):
        axs[i,j].set_title(label_prediction[n])
        axs[i,j].imshow(data[n],cmap = "gray")
        n += 1  
        
plt.tight_layout()
plt.show()


Create the Dataframe

import pandas as pd

df=pd.DataFrame(data={'imagename':os.listdir(directory), 'predicted_labels': label_prediction})
df.head()


Save the dataframe in csv format

df.to_csv('submission_new_model.csv', index=False, header=True)



In the next blog, we will start  Deep Learning Classification with Data Augmentation.
https://sngurukuls247.blogspot.com/2020/05/deep-learning-3-classification-with_9.html

                                                                                                                                                    

Follow the link below to access Free Python Lectures-
https://www.youtube.com/sngurukul

Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment