{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":false},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sea","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"code","source":"from keras.datasets.mnist import load_data\nfrom keras.layers import Dense,Embedding,Conv2D,MaxPooling2D,Flatten,Dropout\nfrom keras.models import Sequential\nfrom keras.losses import binary_crossentropy\nfrom keras.optimizers import SGD,rmsprop,RMSprop\nfrom keras.metrics import binary_accuracy\nfrom keras.utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We will use ImageDataGenerator for generating data from the image set.\n\nWe already know that small the size the size of computational data, faster is the processing of neural network. So we will scale the image by factor of 1/255 to bring all the RGB values in range [0,1]."},{"metadata":{"trusted":false},"cell_type":"code","source":"trainDataGen=ImageDataGenerator(rescale=1./255)\ntestDataGen=ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets generate the image data for training set and test set."},{"metadata":{"trusted":false},"cell_type":"code","source":"trainData=trainDataGen.flow_from_directory(\"/kaggle/input/cat-and-dog/training_set/training_set\",batch_size=20,class_mode='binary')\ntestData=testDataGen.flow_from_directory(\"/kaggle/input/cat-and-dog/test_set/test_set\",batch_size=20,class_mode='binary')","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"trainData.image_shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So from above block of code we found out that image size is 256x256 and in 3 channel. We can resize the images to smaller size lets say 150x150 so as to decrease the size of feature map generated by CovNets. We can pass **target_size** parameter to flow_from_directory to get the desired size.\n\n\nHere lets continue with the original size i.e 256x256"},{"metadata":{"trusted":false},"cell_type":"code","source":"Y_train=to_categorical(trainData.classes)\nY_test=to_categorical(testData.classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"Y_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"sea.countplot(trainData.classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"sea.countplot(testData.classes)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So both training set and test set have equal number of samples for both category."},{"metadata":{},"cell_type":"markdown","source":"Now lets begin with network creation.\n\nWe will use CovNet with 3x3 window,128 channel and activation function as 'Relu'"},{"metadata":{"trusted":false},"cell_type":"code","source":"network=Sequential()\nnetwork.add(Conv2D(128,(3,3),activation='relu',input_shape=(256,256,3)))\nnetwork.add(MaxPooling2D((2,2)))\nnetwork.add(Conv2D(128,(3,3),activation='relu'))\nnetwork.add(MaxPooling2D((2,2)))\nnetwork.add(Conv2D(128,(3,3),activation='relu'))\nnetwork.add(MaxPooling2D((2,2)))\nnetwork.add(Conv2D(128,(3,3),activation='relu'))\nnetwork.add(MaxPooling2D((2,2)))\nnetwork.add(Conv2D(128,(3,3),activation='relu'))\nnetwork.add(Flatten())\nnetwork.add(Dropout(0.9))\nnetwork.add(Dense(256,activation='relu'))\nnetwork.add(Dropout(0.5))\nnetwork.add(Dense(256,activation='relu'))\nnetwork.add(Dropout(0.5))\nnetwork.add(Dense(256,activation='relu'))\nnetwork.add(Dense(1,activation='sigmoid'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets see the summary of our network."},{"metadata":{"trusted":false},"cell_type":"code","source":"network.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Here we will use RMSprop as optimizers. Its default learning rate is 0.001. I had set it to 0.0001.\n\nYou can also use SGD and set the learning rate and momentum."},{"metadata":{"trusted":false},"cell_type":"code","source":"network.compile(loss='binary_crossentropy',optimizer=RMSprop(lr=0.0001),metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Here we will use fit_generator instead of fit as input to network is going too be generator.\n\nsteps_per_epoch is 250 and epochs is 40. This is going to be highly computationally expensive."},{"metadata":{"trusted":false},"cell_type":"code","source":"network.fit_generator(trainData,steps_per_epoch=250,epochs=40)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"network.evaluate_generator(testData,steps=250)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"I have added few dropout layers in model to handle overfitting. This has decreased the training accuracy but increased the testing accuracy. \n\nFeel free to change the dropout values to see the affect on the accuracy of model.\n\nOverfitting can also be handlled by bringing in more data which can be done by Data Augmentation. This involves generating more data from existing data.\n\nFor example, a sample image can be rotated,flipped,zoomed or transformed to generate more data."},{"metadata":{},"cell_type":"markdown","source":"This can be done by adding more parameter to the ImageGenerator function to generated image with transformation\n\nfor example:-\n\nImageDataGenerator(rescale=1./255,zca_whitening=True,rotation_range=30,vertical_flip=True)"},{"metadata":{},"cell_type":"markdown","source":"****\n****"},{"metadata":{},"cell_type":"markdown","source":"Lets see how to visualize the output of layers of CovNet"},{"metadata":{"trusted":false},"cell_type":"code","source":"filename=\"/kaggle/input/cat-and-dog/training_set/training_set/cats/cat.1.jpg\"\nplt.imshow(plt.imread(filename))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets create the 4D tensor for image"},{"metadata":{"trusted":false},"cell_type":"code","source":"from keras.preprocessing import image\n\nimg=image.load_img(filename,target_size=(256,256))\nimgArray=image.img_to_array(img)\nimgTensor=np.expand_dims(imgArray,axis=0)/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"imgTensor.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets generate the output of model for our input\n\nTaking output of first 4 layer"},{"metadata":{"trusted":false},"cell_type":"code","source":"from keras import models\nlayer_output=[layer.output for layer in network.layers[:4]]\nactivationModel=models.Model(inputs=network.input,output=layer_output)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"For 3rd layer or 2nd CovNet layer"},{"metadata":{"trusted":false},"cell_type":"code","source":"activation=activationModel.predict(imgTensor)[2]\nactivation.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"activation output have 128 channel and with size of 254x254"},{"metadata":{},"cell_type":"markdown","source":"Plotting the output of 4th channel"},{"metadata":{"trusted":false},"cell_type":"code","source":"plt.matshow(activation[0,:,:,4])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Plotting output for 50th channel"},{"metadata":{"trusted":false},"cell_type":"code","source":"plt.matshow(activation[0,:,:,50])","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":1}