{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# **Import libraries**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport os\nimport glob as gb\nimport cv2\nimport tensorflow as tf\nimport keras\nfrom tensorflow.keras.preprocessing.image import load_img\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom tensorflow.keras.preprocessing.image import array_to_img\nfrom sklearn.utils import shuffle\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom matplotlib import pyplot\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nimport zipfile\nfrom sklearn.utils import shuffle\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:44.588737Z","iopub.execute_input":"2022-09-10T01:45:44.589661Z","iopub.status.idle":"2022-09-10T01:45:44.600813Z","shell.execute_reply.started":"2022-09-10T01:45:44.589620Z","shell.execute_reply":"2022-09-10T01:45:44.599503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Collecting and Precossing**","metadata":{}},{"cell_type":"code","source":"trainpath = '../input/dogs-vs-cats/train.zip'\ntestpath = '../input/dogs-vs-cats/test1.zip'\nfiles_path = \"/kaggle/files/unzipped/\"\n\n\nwith zipfile.ZipFile(trainpath, 'r') as zipp:\n    zipp.extractall(files_path)\n    \nwith zipfile.ZipFile(testpath, 'r') as zipp:\n    zipp.extractall(files_path)\n\n\ntrainpath = \"/kaggle/files/unzipped/train\"\ntestpath = \"/kaggle/files/unzipped/test1\"\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:44.603172Z","iopub.execute_input":"2022-09-10T01:45:44.603949Z","iopub.status.idle":"2022-09-10T01:45:53.812765Z","shell.execute_reply.started":"2022-09-10T01:45:44.603909Z","shell.execute_reply":"2022-09-10T01:45:53.808688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = os.listdir(trainpath)\nprint(f'For Training data , found {len(file)} Photos')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.813630Z","iopub.status.idle":"2022-09-10T01:45:53.814053Z","shell.execute_reply.started":"2022-09-10T01:45:53.813870Z","shell.execute_reply":"2022-09-10T01:45:53.813888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s=[]\nfiles = gb.glob(pathname= str( trainpath + '/*.jpg'))\nfor file in files:\n    image = plt.imread(file)\n    s.append(image.shape)\npd.Series(s).value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.816506Z","iopub.status.idle":"2022-09-10T01:45:53.817051Z","shell.execute_reply.started":"2022-09-10T01:45:53.816785Z","shell.execute_reply":"2022-09-10T01:45:53.816813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = os.listdir(testpath)\nprint(f'For Training data , found {len(file)} Photos')","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.819153Z","iopub.status.idle":"2022-09-10T01:45:53.819689Z","shell.execute_reply.started":"2022-09-10T01:45:53.819414Z","shell.execute_reply":"2022-09-10T01:45:53.819439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s=[]\nfiles = gb.glob(pathname= str( testpath + '/*.jpg'))\nfor file in files:\n    image = plt.imread(file)\n    s.append(image.shape)\npd.Series(s).value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.821796Z","iopub.status.idle":"2022-09-10T01:45:53.822322Z","shell.execute_reply.started":"2022-09-10T01:45:53.822045Z","shell.execute_reply":"2022-09-10T01:45:53.822070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size=180","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.824398Z","iopub.status.idle":"2022-09-10T01:45:53.824920Z","shell.execute_reply.started":"2022-09-10T01:45:53.824643Z","shell.execute_reply":"2022-09-10T01:45:53.824668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = []\nY_train = []\nX_Title=[]\n\nfiles = gb.glob(pathname= str( trainpath + '/*.jpg'))\nfor file in files: \n    image = cv2.imread(file)\n    image_array = cv2.resize(image , (size,size))\n    X_train.append(list(image_array))\n    file=file.split('/')[-1]\n    sep=file.split('.')[0]\n    X_Title.append(sep)\n    if(sep=='dog'):\n        Y_train.append(1)\n    else:\n        Y_train.append(0)\n        \n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.826981Z","iopub.status.idle":"2022-09-10T01:45:53.827514Z","shell.execute_reply.started":"2022-09-10T01:45:53.827231Z","shell.execute_reply":"2022-09-10T01:45:53.827257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = []\n\nfiles = gb.glob(pathname= str( testpath + '/*.jpg'))\nfor file in files: \n    image = cv2.imread(file)\n    image_array = cv2.resize(image , (size,size))\n    X_test.append(list(image_array))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.830466Z","iopub.status.idle":"2022-09-10T01:45:53.831096Z","shell.execute_reply.started":"2022-09-10T01:45:53.830817Z","shell.execute_reply":"2022-09-10T01:45:53.830843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_train)\nX_test = np.array(X_test)\nY_train = np.array(Y_train)\n\nprint(f'X_train shape  is {X_train.shape}')\nprint(f'X_test shape  is {X_test.shape}')\nprint(f'y_train shape  is {Y_train.shape}')\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.832611Z","iopub.status.idle":"2022-09-10T01:45:53.833323Z","shell.execute_reply.started":"2022-09-10T01:45:53.833052Z","shell.execute_reply":"2022-09-10T01:45:53.833078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Size OF Image = ',np.array(X_train[5]).shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.835139Z","iopub.status.idle":"2022-09-10T01:45:53.836146Z","shell.execute_reply.started":"2022-09-10T01:45:53.835881Z","shell.execute_reply":"2022-09-10T01:45:53.835908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Training Data Shuffle and Split**","metadata":{}},{"cell_type":"code","source":"X_train, Y_train= shuffle(X_train, Y_train, random_state=True)        \n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.839642Z","iopub.status.idle":"2022-09-10T01:45:53.840870Z","shell.execute_reply.started":"2022-09-10T01:45:53.840566Z","shell.execute_reply":"2022-09-10T01:45:53.840592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = tf.one_hot(Y_train, 2)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.842080Z","iopub.status.idle":"2022-09-10T01:45:53.843286Z","shell.execute_reply.started":"2022-09-10T01:45:53.843011Z","shell.execute_reply":"2022-09-10T01:45:53.843039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_Train=X_train[:20000]\nY_Train=Y_train[:20000]\nX_val=X_train[20000:]\ny_val=Y_train[20000:]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.844554Z","iopub.status.idle":"2022-09-10T01:45:53.845686Z","shell.execute_reply.started":"2022-09-10T01:45:53.845414Z","shell.execute_reply":"2022-09-10T01:45:53.845440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_Title=np.array(X_Title)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.846982Z","iopub.status.idle":"2022-09-10T01:45:53.848139Z","shell.execute_reply.started":"2022-09-10T01:45:53.847881Z","shell.execute_reply":"2022-09-10T01:45:53.847907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### **Number of Dogs is equal to Number of Cats in Training Data**","metadata":{}},{"cell_type":"code","source":"pd.Series(X_Title).value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.849369Z","iopub.status.idle":"2022-09-10T01:45:53.850505Z","shell.execute_reply.started":"2022-09-10T01:45:53.850234Z","shell.execute_reply":"2022-09-10T01:45:53.850262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Callbacks:**","metadata":{}},{"cell_type":"code","source":"Earlystop = EarlyStopping(patience=10)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.851869Z","iopub.status.idle":"2022-09-10T01:45:53.853010Z","shell.execute_reply.started":"2022-09-10T01:45:53.852737Z","shell.execute_reply":"2022-09-10T01:45:53.852765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            lr=0.0001,\n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.854287Z","iopub.status.idle":"2022-09-10T01:45:53.855394Z","shell.execute_reply.started":"2022-09-10T01:45:53.855121Z","shell.execute_reply":"2022-09-10T01:45:53.855149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [Earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.856831Z","iopub.status.idle":"2022-09-10T01:45:53.858047Z","shell.execute_reply.started":"2022-09-10T01:45:53.857688Z","shell.execute_reply":"2022-09-10T01:45:53.857735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Augmentation**","metadata":{}},{"cell_type":"markdown","source":"# **CNN Model**","metadata":{}},{"cell_type":"code","source":" \n\n\ndata_augmentation_layer = keras.Sequential([                                    \n  keras.layers.RandomFlip('horizontal',input_shape=(size, size, 3)),\n  keras.layers.RandomRotation(0.15),\n  keras.layers.RandomZoom(0.2),\n  keras.layers.RandomWidth(0.1),\n  keras.layers.RandomHeight(0.1),\n], name='data_augmentation')\n\nmodel = keras.Sequential([\ndata_augmentation_layer,\n    \nkeras.layers.Conv2D(filters=16,kernel_size = (3, 3),strides=1),\nkeras.layers.Activation(activation='relu'),\nkeras.layers.MaxPool2D(),\n    \nkeras.layers.Conv2D(filters=32,kernel_size = (3, 3),strides=1),\nkeras.layers.Activation(activation='relu'),\nkeras.layers.MaxPool2D(),    \nkeras.layers.BatchNormalization(),\n    \nkeras.layers.Conv2D(filters=64,kernel_size = (3, 3),strides=1),\nkeras.layers.Activation(activation='relu'),\nkeras.layers.MaxPool2D(),    \n    \nkeras.layers.Conv2D(filters=128,kernel_size =(3, 3), strides=1),\nkeras.layers.Activation(activation='relu'),\nkeras.layers.MaxPool2D(),\n  \nkeras.layers.Conv2D(filters=256,kernel_size =(3, 3), strides=1),\nkeras.layers.Activation(activation='relu'),\n   \nkeras.layers.GlobalMaxPool2D(),   \n\nkeras.layers.Dense(256,activation='relu') ,\n\nkeras.layers.BatchNormalization(),\nkeras.layers.Dense(128,activation='relu') ,\n    \n\nkeras.layers.Dense(64,activation='relu') ,\n    \nkeras.layers.Dense(32,activation='relu') ,\nkeras.layers.Dense(16,activation='relu') ,\n    \nkeras.layers.Dense(units=2,activation='softmax') ])\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.859391Z","iopub.status.idle":"2022-09-10T01:45:53.860565Z","shell.execute_reply.started":"2022-09-10T01:45:53.860300Z","shell.execute_reply":"2022-09-10T01:45:53.860328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Train and lets see the accuracy rate our Model**","metadata":{}},{"cell_type":"code","source":"epochs = 75\n\nThisModel = model.fit(X_Train, Y_Train, epochs=epochs,batch_size=32,verbose=1,callbacks=callbacks, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.861892Z","iopub.status.idle":"2022-09-10T01:45:53.862877Z","shell.execute_reply.started":"2022-09-10T01:45:53.862577Z","shell.execute_reply":"2022-09-10T01:45:53.862605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\naccuracy = ThisModel.history['accuracy']\nval_accuracy = ThisModel.history['val_accuracy']\nloss = ThisModel.history['loss']\nval_loss = ThisModel.history['val_loss']\nepochs = range(len(accuracy))\nplt.plot(epochs, accuracy, 'bo', label='Training accuracy')\nplt.plot(epochs, val_accuracy, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:53.864453Z","iopub.status.idle":"2022-09-10T01:45:53.865675Z","shell.execute_reply.started":"2022-09-10T01:45:53.865397Z","shell.execute_reply":"2022-09-10T01:45:53.865426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Prediction**","metadata":{}},{"cell_type":"code","source":"y_result = model.predict(X_test)\n\nprint('Prediction Shape is {}'.format(y_result.shape))","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:54.975600Z","iopub.execute_input":"2022-09-10T01:45:54.976060Z","iopub.status.idle":"2022-09-10T01:45:57.035989Z","shell.execute_reply.started":"2022-09-10T01:45:54.976025Z","shell.execute_reply":"2022-09-10T01:45:57.034907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_code(n):\n    if n == 1:\n        return 'Dog'\n    else:\n        return 'Cat'","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:57.038547Z","iopub.execute_input":"2022-09-10T01:45:57.039238Z","iopub.status.idle":"2022-09-10T01:45:57.045552Z","shell.execute_reply.started":"2022-09-10T01:45:57.039195Z","shell.execute_reply":"2022-09-10T01:45:57.044410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor n , i in enumerate(list(np.random.randint(0,len(X_test),36))) : \n    plt.subplot(6,6,n+1)\n    plt.imshow(array_to_img(X_test[i]))     \n    plt.axis('off')\n    plt.title(get_code(np.argmax(y_result[i])))","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:57.047186Z","iopub.execute_input":"2022-09-10T01:45:57.047802Z","iopub.status.idle":"2022-09-10T01:45:59.534576Z","shell.execute_reply.started":"2022-09-10T01:45:57.047762Z","shell.execute_reply":"2022-09-10T01:45:59.533588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y=np.argmax(y_result, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:59.535874Z","iopub.execute_input":"2022-09-10T01:45:59.537058Z","iopub.status.idle":"2022-09-10T01:45:59.542665Z","shell.execute_reply.started":"2022-09-10T01:45:59.537018Z","shell.execute_reply":"2022-09-10T01:45:59.541451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('../input/dogs-vs-cats/sampleSubmission.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:59.545526Z","iopub.execute_input":"2022-09-10T01:45:59.545919Z","iopub.status.idle":"2022-09-10T01:45:59.565743Z","shell.execute_reply.started":"2022-09-10T01:45:59.545879Z","shell.execute_reply":"2022-09-10T01:45:59.564944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_result = pd.DataFrame(\n    {\n        'id':np.arange(1,len(Y)+1),\n        'label':Y\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:59.566913Z","iopub.execute_input":"2022-09-10T01:45:59.567536Z","iopub.status.idle":"2022-09-10T01:45:59.573346Z","shell.execute_reply.started":"2022-09-10T01:45:59.567500Z","shell.execute_reply":"2022-09-10T01:45:59.572084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_result\n","metadata":{"execution":{"iopub.status.busy":"2022-09-10T01:45:59.574787Z","iopub.execute_input":"2022-09-10T01:45:59.575917Z","iopub.status.idle":"2022-09-10T01:45:59.590815Z","shell.execute_reply.started":"2022-09-10T01:45:59.575821Z","shell.execute_reply":"2022-09-10T01:45:59.589832Z"},"trusted":true},"execution_count":null,"outputs":[]}]}