<jupyter_start><jupyter_text><NAME>  18L-1811 Project Part C  Import Libraries<jupyter_code>import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from sklearn.model_selection import train_test_split
import numpy as np
import cv2
from time import time
import matplotlib.pyplot as plt
import matplotlib
from keras.callbacks import TensorBoard
import os
from keras.optimizers import SGD
from os import listdir
from os.path import join, basename
from keras.preprocessing.image import ImageDataGenerator<jupyter_output><empty_output><jupyter_text>Enable Intellisense<jupyter_code>%config IPCompleter.greedy=True<jupyter_output><empty_output><jupyter_text>Global Variable<jupyter_code>trainCats='CatsandDogs/cats'
trainDogs='CatsandDogs/dogs'
labels=[]
data=[]
im_width=64
im_height=64
batchsize=400
epochs=5<jupyter_output><empty_output><jupyter_text>images Name Directory<jupyter_code>trainImageFilesOfCats=[f for f in  os.listdir(trainCats) if os.path.isfile(os.path.join(trainCats,f))]
trainImageFilesOfDogs=[f for f in  os.listdir(trainDogs) if os.path.isfile(os.path.join(trainDogs,f))]<jupyter_output><empty_output><jupyter_text>PreProcessing Cat Data<jupyter_code>progress=0
for fileNameOfCat in trainImageFilesOfCats:
    imageFileOfCat = str(trainCats+'/'+fileNameOfCat)
    imageOfCat=cv2.imread(imageFileOfCat,cv2.IMREAD_GRAYSCALE)
    newImage=cv2.resize(imageOfCat,(im_width,im_height))
    data.append(newImage)
    labels.append(0)
    progress=progress+1
    if progress % 1000==0:
        print('progress= ' + str(progress)+' <<Imges done')<jupyter_output>progress= 1000 <<Imges done
progress= 2000 <<Imges done
progress= 3000 <<Imges done
progress= 4000 <<Imges done
progress= 5000 <<Imges done
progress= 6000 <<Imges done
progress= 7000 <<Imges done
progress= 8000 <<Imges done
progress= 9000 <<Imges done
progress= 10000 <<Imges done
progress= 11000 <<Imges done
progress= 12000 <<Imges done<jupyter_text>PreProcessing Dog Data<jupyter_code>progress=0
for fileNameOfDog in trainImageFilesOfDogs:
    imageFileOfDog = str(trainDogs+'/'+fileNameOfDog)
    imageOfDog=cv2.imread(imageFileOfDog,cv2.IMREAD_GRAYSCALE)
    newImage=cv2.resize(imageOfDog,(im_width,im_height))
    data.append(newImage)
    labels.append(1)
    progress=progress+1
    if progress % 1000==0:
        print('progress= ' + str(progress)+' <<Imges done')
data=np.array(data)
print(data.shape)
data=data.reshape((data.shape)[0],(data.shape)[1],(data.shape)[2],1)
print(data.shape)
labels=np.array(labels)
print(labels.shape)<jupyter_output>(25000,)<jupyter_text>split  training and test data<jupyter_code>X_train, X_val, Y_train, Y_val = train_test_split(data,labels, test_size=0.2, random_state=42)<jupyter_output><empty_output><jupyter_text>Building Convolutional Neural Network  Model 1 batch size 100 , epoch 30  , Adam optimizer Default Learning Rate Provided Architecture<jupyter_code>model1 = Sequential()
model1.add(Conv2D(kernel_size=(3,3),filters=3,input_shape=(im_width, im_height,1),activation="relu",padding="valid"))
model1.add(Conv2D(kernel_size=(3,3),filters=10,activation="relu",padding="same"))
model1.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model1.add(Conv2D(kernel_size=(3,3),filters=3,activation="relu",padding="same"))
model1.add(Conv2D(kernel_size=(5,5),filters=5,activation="relu",padding="same"))
model1.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))
model1.add(Conv2D(kernel_size=(2,2),strides=(2,2),filters=10))
model1.add(Flatten())
model1.add(Dropout(0.2))
model1.add(Dense(100,activation="sigmoid"))
model1.add(Dense(1,activation="sigmoid"))
model1.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 62, 62, 3)         30        
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 62, 62, 10)        280       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 31, 31, 10)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 31, 31, 3)         273       
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 31, 31, 5)         380       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 15, 15, 5)         0         
_________________________________________________________________
conv2d_5 ([...]<jupyter_text>compile Model<jupyter_code>model1.compile(optimizer = 'Adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
batchsize=100
epochs=30
HistoryOfModel1 = model1.fit(X_train,Y_train, batch_size=batchsize, epochs=epochs, validation_data=(X_val,Y_val), verbose=1)
score1 = model1.evaluate(X_val, Y_val, verbose=0)
print('Test loss:', score1[0])
print('Test accuracy:', score1[1])<jupyter_output>Train on 20000 samples, validate on 5000 samples
Epoch 1/30
20000/20000 [==============================] - 25s 1ms/step - loss: 0.6791 - acc: 0.5669 - val_loss: 0.6478 - val_acc: 0.6198
Epoch 2/30
20000/20000 [==============================] - 17s 865us/step - loss: 0.6361 - acc: 0.6373 - val_loss: 0.6149 - val_acc: 0.6622
Epoch 3/30
20000/20000 [==============================] - 18s 889us/step - loss: 0.6036 - acc: 0.6741 - val_loss: 0.6015 - val_acc: 0.6754
Epoch 4/30
20000/20000 [==============================] - 17s 866us/step - loss: 0.5799 - acc: 0.6962 - val_loss: 0.5791 - val_acc: 0.6934
Epoch 5/30
20000/20000 [==============================] - 18s 907us/step - loss: 0.5600 - acc: 0.7115 - val_loss: 0.5670 - val_acc: 0.7016
Epoch 6/30
20000/20000 [==============================] - 18s 879us/step - loss: 0.5461 - acc: 0.7234 - val_loss: 0.5711 - val_acc: 0.7026
Epoch 7/30
20000/20000 [==============================] - 18s 894us/step - loss: 0.5307 - acc: 0.7338 - val_loss: 0.559[...]<jupyter_text>Graph<jupyter_code>train_loss=HistoryOfModel1.history['loss']
val_loss=HistoryOfModel1.history['val_loss']
train_acc=HistoryOfModel1.history['acc']
val_acc=HistoryOfModel1.history['val_acc']
xc=range(epochs)
plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])<jupyter_output><empty_output><jupyter_text>Building Convolutional Neural Network   Model 2 Increase batch size 512 , epoch 36 , Change First Conv2D Filter Size 32 Adam optimizer Default Learning Rate<jupyter_code>model1 = Sequential()
model1.add(Conv2D(kernel_size=(3,3),filters=32,input_shape=(im_width, im_height,1),activation="relu",padding="valid"))
model1.add(Conv2D(kernel_size=(3,3),filters=10,activation="relu",padding="same"))
model1.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model1.add(Conv2D(kernel_size=(3,3),filters=3,activation="relu",padding="same"))
model1.add(Conv2D(kernel_size=(5,5),filters=5,activation="relu",padding="same"))
model1.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))
model1.add(Conv2D(kernel_size=(2,2),strides=(2,2),filters=10))
model1.add(Flatten())
model1.add(Dropout(0.2))
model1.add(Dense(122,activation="sigmoid"))
model1.add(Dense(1,activation="sigmoid"))
model1.summary()
model1.compile(optimizer = 'Adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
batchsize=512
epochs=36
historydata2 = model1.fit(data,labels, batch_size=512, epochs=36, validation_split = 0.25, verbose=1)

(loss, accuracy) = model1.evaluate(data,labels, batch_size=512, verbose=1)

train_loss=historydata2.history['loss']
val_loss=historydata2.history['val_loss']
train_acc=historydata2.history['acc']
val_acc=historydata2.history['val_acc']
xc=range(epochs)
plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])<jupyter_output><empty_output><jupyter_text>Building Convolutional Neural Network  Model  Architecture 3 batch size 512 , epoch 15  ,Change Optimize   rmsprop (full-batch optimization) Default Learning Rate<jupyter_code>model3 = Sequential()
model3.add(Conv2D(kernel_size=(3,3),filters=3,input_shape=(im_width, im_height,1),activation="relu",padding="valid"))
model3.add(Conv2D(kernel_size=(3,3),filters=10,activation="relu",padding="same"))
model3.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model3.add(Conv2D(kernel_size=(3,3),filters=3,activation="relu",padding="same"))
model3.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))
model3.add(Conv2D(kernel_size=(2,2),strides=(2,2),filters=10))
model3.add(Flatten())
model3.add(Dropout(0.2))
model3.add(Dense(100,activation="sigmoid"))
model3.add(Dense(1,activation="sigmoid"))
model3.summary()
model3.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['accuracy'])
batchsize=512
epochs=15
HistoryOfModel3 = model3.fit(X_train,Y_train, batch_size=batchsize, epochs=epochs, validation_data=(X_val,Y_val), verbose=1)

score1 = model3.evaluate(X_val, Y_val, verbose=0)
print('Test loss:', score1[0])
print('Test accuracy:', score1[1])
#accuracy
plt.plot(HistoryOfModel3.history['acc'])
plt.plot(HistoryOfModel3.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
#loss
plt.plot(HistoryOfModel3.history['loss'])
plt.plot(HistoryOfModel3.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Building Convolutional Neural Network  Model  Architecture 4 batch size 512 , epoch 15 ,Change 2nd last Dense layer activation function to Add more Linearity ,Change Optimize  Adam Default Learning Rate<jupyter_code>model4 = Sequential()
model4.add(Conv2D(kernel_size=(3,3),filters=32,input_shape=(im_width, im_height,1),activation="relu",padding="valid"))
model4.add(Conv2D(kernel_size=(3,3),filters=10,activation="relu",padding="same"))
model4.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

model4.add(Conv2D(kernel_size=(5,5),filters=5,activation="relu",padding="same"))
model4.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))
model4.add(Conv2D(kernel_size=(2,2),strides=(2,2),filters=10))
model4.add(Flatten())
model4.add(Dropout(0.2))
model4.add(Dense(100,activation="relu"))
model4.add(Dense(1,activation="sigmoid"))
model4.summary()
model4.compile(optimizer = 'Adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
batchsize=512
epochs=15
HistoryOfModel4 = model4.fit(X_train,Y_train, batch_size=batchsize, epochs=epochs, validation_data=(X_val,Y_val), verbose=1)

score1 = model4.evaluate(X_val, Y_val, verbose=0)
print('Test loss:', score1[0])
print('Test accuracy:', score1[1])
#accuracy
plt.plot(HistoryOfModel4.history['acc'])
plt.plot(HistoryOfModel4.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
#loss
plt.plot(HistoryOfModel4.history['loss'])
plt.plot(HistoryOfModel4.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Building Convolutional Neural Network  Model  Architecture 5 batch size 512 ,Change 2nd last Dense layer sigmoid activation function ,Change Optimize  rmsprop Default Learning Rate<jupyter_code>model5 = Sequential()
model5.add(Conv2D(kernel_size=(3,3),filters=3,input_shape=(im_width, im_height,1),activation="relu",padding="valid"))
model5.add(Conv2D(kernel_size=(3,3),filters=10,activation="relu",padding="same"))
model5.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model5.add(Conv2D(kernel_size=(3,3),filters=3,activation="relu",padding="same"))
model5.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))
model5.add(Conv2D(kernel_size=(2,2),strides=(2,2),filters=10))
model5.add(Flatten())
model5.add(Dropout(0.5))
model5.add(Dense(100,activation="sigmoid"))
model5.add(Dense(1,activation="sigmoid"))
model5.summary()


#model5.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['accuracy'])
model5.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
epochs=10
batchsize=512
HistoryOfModel5 = model5.fit(X_train,Y_train, batch_size=batchsize, epochs=epochs, validation_data=(X_val,Y_val), verbose=1)

score = model5.evaluate(X_val, Y_val, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
train_loss=HistoryOfModel5.history['loss']
val_loss=HistoryOfModel5.history['val_loss']
train_acc=HistoryOfModel5.history['acc']
val_acc=HistoryOfModel5.history['val_acc']
xc=range(epochs)

plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])

from keras.regularizers import l2
from keras import regularizers<jupyter_output><empty_output><jupyter_text>Building Convolutional Neural Network  Model  Architecture 6 batch size 512 ,Change epochs 30,Change First Dense layer relu activation function and neuron 128 ,Change Filter Size 64 64 16 16 64<jupyter_code>model5 = Sequential()
model5.add(Conv2D(kernel_size=(3,3),filters=64,input_shape=(64, 64,1),activation="relu",padding="valid"))
model5.add(Conv2D(kernel_size=(3,3),filters=64,activation="relu",padding="same"))
model5.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model5.add(Conv2D(kernel_size=(3,3),filters=16,activation="relu",padding="same"))
model5.add(Conv2D(kernel_size=(3,3),filters=16,activation="relu",padding="same"))
model5.add(MaxPooling2D(pool_size=(3,3),strides=(2,2)))
model5.add(Conv2D(kernel_size=(2,2),strides=(2,2),filters=64))
model5.add(Flatten())
model5.add(Dropout(0.6))
model5.add(Dense(128,activation="relu",kernel_regularizer=regularizers.l2(0.09)))
model5.add(Dense(1,activation="sigmoid"))
#model3.compile(optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = ['accuracy'])
batchsize=64
epochs=30
model5.compile(loss=keras.losses.mean_squared_error,optimizer=keras.optimizers.SGD(lr=0.009,momentum=0.7),metrics=['accuracy'])
HistoryOfModel5 = model5.fit(X_train,Y_train, batch_size=batchsize, epochs=epochs, validation_data=(X_val,Y_val), verbose=1)
score = model5.evaluate(X_val, Y_val, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
plt.plot(HistoryOfModel5.history['acc'])
plt.plot(HistoryOfModel5.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
plt.plot(HistoryOfModel5.history['loss'])
plt.plot(HistoryOfModel5.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Change fit_generator(Trains the model on data generated batch-by-batch)  Building Convolutional Neural Network  Model  Architecture 7<jupyter_code>trainDataGenerate = ImageDataGenerator(rescale = 1./255,shear_range = 0.22,zoom_range = 0.20,horizontal_flip = True)
testDataGenerate = ImageDataGenerator(rescale = 1./255)
trainingSetData = trainDataGenerate.flow_from_directory('TrainSetData', target_size = (im_width, im_height),batch_size = 32,class_mode = 'binary')
testintSetData = testDataGenerate.flow_from_directory('TestingSetData',target_size = (im_width, im_height),batch_size = 32,class_mode = 'binary')
model6 = Sequential()
model6.add(Conv2D(32, (3, 3), input_shape = (im_width, im_height, 3), activation = 'relu'))
model6.add(MaxPooling2D(pool_size = (2, 2)))
model6.add(Conv2D(kernel_size=(5,5),filters=5,activation="relu",padding="same"))
model6.add(Conv2D(kernel_size=(3,3),filters=32,activation="relu",padding="same"))
model6.add(Conv2D(32, (3, 3), activation = 'relu'))
model6.add(MaxPooling2D(pool_size = (2, 2)))
model6.add(Flatten())
model6.add(Dense(128,activation="relu"))
model6.add(Dense(1,activation="sigmoid"))
model6.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
model6.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_112 (Conv2D)          (None, 62, 62, 32)        896       
_________________________________________________________________
max_pooling2d_55 (MaxPooling (None, 31, 31, 32)        0         
_________________________________________________________________
conv2d_113 (Conv2D)          (None, 31, 31, 5)         4005      
_________________________________________________________________
conv2d_114 (Conv2D)          (None, 31, 31, 32)        1472      
_________________________________________________________________
conv2d_115 (Conv2D)          (None, 29, 29, 32)        9248      
_________________________________________________________________
max_pooling2d_56 (MaxPooling (None, 14, 14, 32)        0         
_________________________________________________________________
flatten_28[...]<jupyter_text>numberOfSamples=10002,testingSample=2000,stepsPerEpoch=800,epochs=40,batchsize=256<jupyter_code>numberOfSamples=10002
testingSample=2000
stepsPerEpoch=800
epochs=40
batchsize=256
historyData6=model6.fit_generator(trainingSetData,steps_per_epoch = numberOfSamples // batchsize ,epochs = epochs,validation_data = testintSetData,validation_steps = testingSample // batchsize)
print(len(trainingSetData))
[loss,accuracy] = model6.evaluate_generator(testintSetData, steps=313, max_queue_size=10, workers=0, use_multiprocessing=False)
print(accuracy)
#accuracy
plt.plot(historyData6.history['acc'])
plt.plot(historyData6.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
#loss
plt.plot(historyData6.history['loss'])
plt.plot(historyData6.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Building Convolutional Neural Network  Model  Architecture 8<jupyter_code>trainDataGenerate = ImageDataGenerator(rescale = 1./255,shear_range = 0.22,zoom_range = 0.20,horizontal_flip = True)
testDataGenerate = ImageDataGenerator(rescale = 1./255)

trainingSetData = trainDataGenerate.flow_from_directory('TrainSetData', target_size = (im_width, im_height),batch_size = 32,class_mode = 'binary')
testintSetData = testDataGenerate.flow_from_directory('TestingSetData',target_size = (im_width, im_height),batch_size = 32,class_mode = 'binary')
model7 = Sequential()
model7.add(Conv2D(32, (3, 3), input_shape = (im_width, im_height, 3), activation = 'relu'))
model7.add(MaxPooling2D(pool_size = (2, 2)))
#model7.add(Conv2D(kernel_size=(5,5),filters=5,activation="relu",padding="same"))
model7.add(Conv2D(kernel_size=(3,3),filters=32,activation="relu",padding="same"))
model7.add(Conv2D(32, (3, 3), activation = 'relu'))
model7.add(MaxPooling2D(pool_size = (2, 2)))
model7.add(Flatten())
model7.add(Dense(128,activation="relu"))
model7.add(Dense(1,activation="sigmoid"))
model7.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
model7.summary()
numberOfSamples=10002
testingSample=2000
stepsPerEpoch=800
epochs=40
batchsize=128
historyData=model7.fit_generator(trainingSetData,steps_per_epoch = numberOfSamples // batchsize ,epochs = epochs,validation_data = testintSetData,validation_steps = testingSample // batchsize)
print(len(trainingSetData))
[loss,accuracy] = model7.evaluate_generator(testintSetData, steps=313, max_queue_size=10, workers=0, use_multiprocessing=False)
print("Acuracy:"+ str(accuracy))
print("loss:"+ str(loss))
train_loss=historyData.history['loss']
val_loss=historyData.history['val_loss']
train_acc=historyData.history['acc']
val_acc=historyData.history['val_acc']
xc=range(epochs)

plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])


#accuracy
plt.plot(historyData.history['acc'])
plt.plot(historyData.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
#loss
plt.plot(historyData.history['loss'])
plt.plot(historyData.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup