{"cells":[{"metadata":{"_uuid":"5e30d84e1d82809b4a0c61d2b75cad9625f5a72b"},"cell_type":"markdown","source":"## Cats vs Dogs ConvNet Keras"},{"metadata":{"_uuid":"cbd85fe8ed496f06efaad04a4eaf36c39a54bba3"},"cell_type":"markdown","source":"## [Please star/upvote in case u like it.]"},{"metadata":{"_uuid":"2841b32576e96ff0c5cc2226d9fd8ecc5830b006"},"cell_type":"markdown","source":"## TABLE OF CONTENTS::"},{"metadata":{"_uuid":"bed99c10944c38d5513684ffd584d1edfbf1bdb8"},"cell_type":"markdown","source":"[ **1 ) Importing Various Modules**](#content1)"},{"metadata":{"_uuid":"9d1328364de913dc56f989b0e40fa5ed141df020"},"cell_type":"markdown","source":"[ **2 ) Prepare the Data**](#content2)"},{"metadata":{"_uuid":"4f6abc45bf9e5b9167fa9d3a6f0c99e1314d5d8d"},"cell_type":"markdown","source":"[ **3 ) Modelling**](#content3)"},{"metadata":{"_uuid":"397faf592474bcaf9552bc912865c34e550aebc3"},"cell_type":"markdown","source":"[ **4 )  Evaluating the Model Performance**](#content4)"},{"metadata":{"_uuid":"d1a432cdca7d056a7a7f9cec78b594015bb0a4f6"},"cell_type":"markdown","source":"[ **5 ) Visualizing Predictons on the Validation Set**](#content5)"},{"metadata":{"_uuid":"af2b859d6a64f2821f146e648d0225fc69642ecc"},"cell_type":"markdown","source":"[ **6 ) Making Predictions on the Test Set**](#content6)"},{"metadata":{"_uuid":"1ea3ab0e4002dafca3c4b48f68eec53b8e8384a1"},"cell_type":"markdown","source":"[ **7 ) Saving Submissions onto a CSV**](#content7)"},{"metadata":{"_uuid":"a65a3feca5ac38be5d87e89377a625d528875b4a"},"cell_type":"markdown","source":"<a id=\"content1\"></a>\n## 1 ) Importing Various Modules"},{"metadata":{"colab":{},"colab_type":"code","id":"t1Tdup-d3ITX","trusted":true,"_uuid":"6a7bdb829ad1b3a035df5bb633475128e2d520d5"},"cell_type":"code","source":"# Ignore  the warnings\nimport warnings\nwarnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\n\n# data visualisation and manipulation\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib import style\nimport seaborn as sns\n \n#configure\n# sets matplotlib to inline and displays graphs below the corressponding cell.\n%matplotlib inline  \nstyle.use('fivethirtyeight')\nsns.set(style='whitegrid',color_codes=True)\n\n#model selection\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score,confusion_matrix,roc_curve,roc_auc_score\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.preprocessing import LabelEncoder,OneHotEncoder\n\n#preprocess.\nfrom keras.preprocessing.image import ImageDataGenerator\n\n#dl libraraies\nfrom keras import backend as K\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.optimizers import Adam,SGD,Adagrad,Adadelta,RMSprop\nfrom keras.utils import to_categorical\n\n# specifically for cnn\nfrom keras.layers import Dropout, Flatten\nfrom keras.layers import Conv2D, MaxPooling2D, BatchNormalization\n \nimport tensorflow as tf\nimport random as rn\n\n# specifically for manipulating zipped images and getting numpy arrays of pixel values of images.\nimport cv2                  \nimport numpy as np         \nimport os                   \nfrom random import shuffle  \nfrom zipfile import ZipFile\nfrom PIL import Image\nfrom tqdm import tqdm ","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"56e888c4d949139f15e0b8d3068da3cc27aba8f0"},"cell_type":"markdown","source":"<a id=\"content2\"></a>\n## 2 ) Prepare the Data"},{"metadata":{"_uuid":"3a7c4aa79304934268a4c281eb462175782d5be4"},"cell_type":"markdown","source":"## 2.1) Making the functions to get the training ,testing and validation set from the Images"},{"metadata":{"_uuid":"3facbdd28d0fbe22510145048e582d4a8a02eea4"},"cell_type":"markdown","source":"I have created the training and validation sets. Much of this section is inspired from the [**sentdex** ](https://www.kaggle.com/sentdex/full-classification-example-with-convnet)work."},{"metadata":{"colab":{},"colab_type":"code","id":"dqdBaZPx3ITj","trusted":true,"_uuid":"e93de0f3ded0dbf7ed33f11d120a2f75860ce51d"},"cell_type":"code","source":"TRAIN_DIR = '../input/train'\nTEST_DIR = '../input/test'\nIMG_SIZE=100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97a3b02a07935c1ba0c02083f22b9721017347c1"},"cell_type":"code","source":"def label_img(img):\n    word_label = img.split('.')[0]\n    return word_label","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"3sn9AvHjjJPP","trusted":true,"_uuid":"bca9a7fae741de019fbe43e0de564249ba54fb18"},"cell_type":"code","source":"def create_train_data():\n    training_data = []\n    for img in tqdm(os.listdir(TRAIN_DIR)):\n        label=label_img(img)\n        path = os.path.join(TRAIN_DIR,img)\n        img_num = img.split('.')[0]\n        img = cv2.imread(path,cv2.IMREAD_COLOR)\n        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n        training_data.append([np.array(img),str(label)])\n        \n    shuffle(training_data)\n    return training_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"793dcaa47cfcc5b0a9d457df64336237b12d9aef","scrolled":true},"cell_type":"code","source":"train_data=create_train_data()\ntrain_data=np.array(train_data)\nprint(train_data.shape)\nX= np.array([i[0] for i in train_data]).reshape(-1,IMG_SIZE,IMG_SIZE,3)\nY= np.array([i[1] for i in train_data])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"569c8b4b8b0377280dfc650074cab60ff78e0025"},"cell_type":"code","source":"def create_test_data():\n    testing_data = []\n    for img in tqdm(os.listdir(TEST_DIR)):\n        path = os.path.join(TEST_DIR,img)\n        img_num = img.split('.')[0]\n        img = cv2.imread(path,cv2.IMREAD_COLOR)\n        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n        testing_data.append([np.array(img)])\n        \n    shuffle(testing_data)\n    return testing_data","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f9674a9f3816ba32f54ff34e1cff5557cbe03018"},"cell_type":"markdown","source":"## 2.2 ) Visualizing some Random Images"},{"metadata":{"_uuid":"72064372bc4968512f69b594160228a542e1964b"},"cell_type":"markdown","source":"In this section I have visualized the dataset with just some random images. This confirms that dataset has been loaded correctly."},{"metadata":{"trusted":true,"_uuid":"2abb997d3e900a35b04ae717ee4973ed798e7537"},"cell_type":"code","source":"fig,ax=plt.subplots(6,2)\nfig.set_size_inches(15,15)\nfor i in range(6):\n    for j in range (2):\n        l=rn.randint(0,len(Y))\n        ax[i,j].imshow(X[l])\n        ax[i,j].set_title('Pet: '+Y[l])\n        \nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6a36acf8b9d47cd71dd645b8a229108819e228bb"},"cell_type":"markdown","source":"## 2.3 ) One Hot Encoding"},{"metadata":{"_uuid":"b24a315f28bb4ae14cffc0ef911c16535cf8fc74"},"cell_type":"markdown","source":"Encoding the target as usual."},{"metadata":{"trusted":true,"_uuid":"865dc8509cd6eab1fbb1f9ee285029c803e7b903"},"cell_type":"code","source":"le=LabelEncoder()\nZ=Y\nY=le.fit_transform(Y)\nY=to_categorical(Y)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a2c144337bb96da3d4648adcd8c1773a57af8bf1"},"cell_type":"markdown","source":"## 2.4 ) Splitting into Training and Validation Sets"},{"metadata":{"trusted":true,"_uuid":"9e43b13f9f85c22a33f90e44bf31b99d228e8c52"},"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.2,random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"28bcd4b943f715579ba6ac8b62c5a13012dd334d"},"cell_type":"markdown","source":"## 2.5 ) Setting the Random seeds"},{"metadata":{"_uuid":"90a68aaa2617a5d11e8c9861d0b2e5bd36825ab3"},"cell_type":"markdown","source":"Setting the seeds for ensuring reproducability. the seeds are of numpy,tensorflow and the python."},{"metadata":{"colab":{},"colab_type":"code","id":"H3gtbUAJK2uS","trusted":true,"_uuid":"5cb2fc798541c9e02b40dfc71b1141520c543592"},"cell_type":"code","source":"np.random.seed(42)\nrn.seed(42)\ntf.set_random_seed(42)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fe6f6972339265c93005baeb66e1b40a22d479ca"},"cell_type":"markdown","source":"<a id=\"content3\"></a>\n## 3 ) Modelling"},{"metadata":{"_uuid":"d7e9dcb475828d2fc425d486ef8822125cd9a6bc"},"cell_type":"markdown","source":"## 3.1 ) Building the ConvNet Model"},{"metadata":{"colab":{},"colab_type":"code","id":"oE_psUhqTLA7","trusted":true,"_uuid":"00d0436a504fecf82e4ab1320546fae1f2ca784a"},"cell_type":"code","source":"# # modelling starts using a CNN.\n\nmodel = Sequential()\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu', input_shape = (100,100,3)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n \nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n \n\nmodel.add(Conv2D(filters = 96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(Conv2D(filters = 96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n \nmodel.add(Conv2D(filters =96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(Conv2D(filters = 96, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n \n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dense(2, activation = \"sigmoid\"))\n ","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"Y3WoOgt_lqIA","trusted":true,"_uuid":"f839e6fccc6ef204e517196c5a936baa23de7f01"},"cell_type":"code","source":"batch_size=128\nepochs=20\n\nfrom keras.callbacks import ReduceLROnPlateau\nred_lr= ReduceLROnPlateau(monitor='val_acc',patience=2,verbose=1,factor=0.1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"015103abf07d6cd425d0cc71874ade612646f7c7"},"cell_type":"markdown","source":"## 3.2 ) Data Augmentation to prevent Overfitting"},{"metadata":{"colab":{},"colab_type":"code","id":"h0m-lmFnL75i","trusted":true,"_uuid":"564bddb5fdbf9fb0d3e45e9904f4639cd68472ad"},"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rescale=1. / 255,\n    zoom_range=0.1,\n    rotation_range=10,\n    horizontal_flip=True)\ndatagen.fit(x_train)\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ed8f5af06d41935be6a636c7af85919c71bc80c0"},"cell_type":"markdown","source":"## 3.3 ) Compiling the Keras Model"},{"metadata":{"colab":{},"colab_type":"code","id":"p66UVSmMd2EL","trusted":true,"_uuid":"f5df296911f7081f484612b27327c1a61b9ec82a"},"cell_type":"code","source":"model.compile(optimizer=RMSprop(lr=0.001),loss='binary_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"aa3176962930854115e3e01ea185d85fd91eddd4"},"cell_type":"markdown","source":"## 3.4 ) Summary of the Model"},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":646},"colab_type":"code","id":"3efXy4G_l1YI","outputId":"49a38e4b-c70d-412a-b966-50277edbd0e4","trusted":true,"_uuid":"d8602c76aaf858121558ab18511786eabec6fca8"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ddf3212452344d109b87ab6cebdb790773ec1d95"},"cell_type":"markdown","source":"## 3.5 ) Fitting on the Training set and making predcitons on the Validation set"},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":697},"colab_type":"code","id":"LLyD8DGNL79V","outputId":"0885e5a8-2bec-4546-cade-cbf1f2f482ff","trusted":true,"_uuid":"1f2ce703dd26187e5510c8243aa36b520263a90d","scrolled":true},"cell_type":"code","source":"History = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (x_test,y_test),\n                              verbose = 1, steps_per_epoch=x_train.shape[0] // batch_size,callbacks=[red_lr])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5eb608df8c82b79fd792f8bdab043249238e7936"},"cell_type":"markdown","source":"####  The  final validation accuracy is close to 89 or 90 % which is quite decent.           \nTuning in can definitely get an accuracy better than this,"},{"metadata":{"_uuid":"7266487d2e708dfbeb6c8bea73b99e4f51d5957b"},"cell_type":"markdown","source":"<a id=\"content4\"></a>\n## 4 ) Evaluating the Model Performance"},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":398},"colab_type":"code","id":"7g42t1xp9fIF","outputId":"ca3e0e9d-65c3-4e0b-e75d-5be9399dbabd","trusted":true,"_uuid":"e271492122b6c4e9c0df1a0cc974d567e27ca510"},"cell_type":"code","source":"plt.plot(History.history['loss'])\nplt.plot(History.history['val_loss'])\nplt.title('Model Loss')\nplt.ylabel('Loss')\nplt.xlabel('Epochs')\nplt.legend(['train', 'test'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":398},"colab_type":"code","id":"DOhtMZVdfTE2","outputId":"bf6d4ea0-afff-4290-ec56-b098625f7e5e","trusted":true,"_uuid":"a019139df6d275b522ea6d8d06a0d1778cf0a71c"},"cell_type":"code","source":"plt.plot(History.history['acc'])\nplt.plot(History.history['val_acc'])\nplt.title('Model Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epochs')\nplt.legend(['train', 'test'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cd4ad73f1acf25934388f631b9461bc6176f79c8"},"cell_type":"markdown","source":"<a id=\"content5\"></a>\n## 5) Visualizing Predictons on the Validation Set"},{"metadata":{"_uuid":"cd3579a22b558e13b54379519aaa832545e6839b"},"cell_type":"markdown","source":"**Just writing some functions to see some correct results and also misclassified images. This just makes it a bit more clearer and attractive.;)**"},{"metadata":{"trusted":true,"_uuid":"c967019b4bdff024951564d63c147536b2814583"},"cell_type":"code","source":"# getting predictions on val set.\npred=model.predict(x_test)\npred_pet=np.argmax(pred,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"99b62209032113cd8362fb3639213e09187e0b9a"},"cell_type":"code","source":"pred_pet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ee16dc59a33300347a34b047a46bf39266723e7"},"cell_type":"code","source":"# now storing some properly as well as misclassified indexes'.\ni=0\nprop_class=[]\nmis_class=[]\n\nfor i in range(len(y_test)):\n    if(np.argmax(y_test[i])==pred_pet[i]):\n        prop_class.append(i)\n    if(len(prop_class)==8):\n        break\n\ni=0\nfor i in range(len(y_test)):\n    if(not np.argmax(y_test[i])==pred_pet[i]):\n        mis_class.append(i)\n    if(len(mis_class)==8):\n        break","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"bf4d099ce566d03c50fdff0e1a9dbe16078ffbfe"},"cell_type":"markdown","source":"#### PROPERLY CLASSIFIED IMAGES"},{"metadata":{"trusted":true,"_uuid":"33acb902f6ac801c440c97c2942820b953aad8f9"},"cell_type":"code","source":"warnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\n\ncount=0\nfig,ax=plt.subplots(4,2)\nfig.set_size_inches(15,15)\nfor i in range (4):\n    for j in range (2):\n        ax[i,j].imshow(x_test[prop_class[count]])\n        ax[i,j].set_title(\"Predicted Pet : \"+str(le.inverse_transform([pred_pet[prop_class[count]]]))+\"\\n\"+\"Actual Pet : \"+str(le.inverse_transform([np.argmax([y_test[prop_class[count]]])])))\n        plt.tight_layout()\n        count+=1","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"fcdb2aa445a5f392e32e08c475bfbdfdb759f741"},"cell_type":"markdown","source":"#### MISCLASSIFIED PET IMAGES"},{"metadata":{"trusted":true,"_uuid":"2350038ff21a55d99c0202841bdb9d159ad74a2a"},"cell_type":"code","source":"warnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\n\ncount=0\nfig,ax=plt.subplots(4,2)\nfig.set_size_inches(15,15)\nfor i in range (4):\n    for j in range (2):\n        ax[i,j].imshow(x_test[mis_class[count]])\n        ax[i,j].set_title(\"Predicted Pet : \"+str(le.inverse_transform([pred_pet[mis_class[count]]]))+\"\\n\"+\"Actual Pet : \"+str(le.inverse_transform([np.argmax([y_test[mis_class[count]]])])))\n        plt.tight_layout()\n        count+=1","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"694d002d6620966fe300077465a56f8ee3efc414"},"cell_type":"markdown","source":"<a id=\"content6\"></a>\n## 6 ) Making predictions on the Test Set"},{"metadata":{"trusted":true,"_uuid":"45ade67940e4f65938ee748c5ae8ac05e9bc7bcb"},"cell_type":"code","source":"test_data=create_test_data()\ntest_data=np.array(test_data)\nprint(test_data.shape)\ntest= np.array([i[0] for i in test_data]).reshape(-1,IMG_SIZE,IMG_SIZE,3)","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"C-9fn6i1fVtH","trusted":true,"_uuid":"7b3dbc8590c88b6e1af2dc8e3ee89e6cc779a7ca"},"cell_type":"code","source":"pred=model.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"16bb17528e8e5a5f8d8c7303a93de4eb74505ea1"},"cell_type":"markdown","source":"<a id=\"content7\"></a>\n## 7 ) Saving Submissions onto a CSV"},{"metadata":{"_uuid":"bf575bd16dd16bbedbd7bea5c1c88a0105c1c542"},"cell_type":"markdown","source":"Generating a csv file to make predictions onto Kaggle."},{"metadata":{"colab":{},"colab_type":"code","id":"YawSlPJrPUqF","trusted":true,"_uuid":"c00f225478e1ad0162cbd594b8166caa3e0521c2"},"cell_type":"code","source":"imageid=[]\npred_prob=[]\nfor i in range(len(test)):\n    imageid.append(i+1)\n    pred_prob.append(pred[i,1])\n   \nd={'id':imageid,'label':pred_prob}\nans=pd.DataFrame(d)\nans.to_csv('predictions.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"-PuKvEoPSkSa","trusted":true,"_uuid":"f8cbd3d12414d04cf2cf42c663b384966089ed4c"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"colab":{},"colab_type":"code","id":"EbLw7QZwTVBs","_uuid":"c065c906f8ec60249d6f325cdbfae8818f0c3b89"},"cell_type":"markdown","source":"## THE END!!!"},{"metadata":{"_uuid":"3e9dcf5828432764e00e1f56b0ad2b9f7ec0952f"},"cell_type":"markdown","source":"## [Please star/upvote if u liked it.]"},{"metadata":{"trusted":false,"_uuid":"93c061c289f10c67b3915e40e3c8cade55432011"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"accelerator":"GPU","colab":{"collapsed_sections":[],"name":"Cats-vs-Dogs-1.ipynb","provenance":[],"version":"0.3.2"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}