{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-02T09:51:24.983844Z","iopub.execute_input":"2021-10-02T09:51:24.984722Z","iopub.status.idle":"2021-10-02T09:51:25.076581Z","shell.execute_reply.started":"2021-10-02T09:51:24.984612Z","shell.execute_reply":"2021-10-02T09:51:25.075728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Imports**","metadata":{}},{"cell_type":"code","source":"import os\nimport zipfile\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nlocal = '../input/dogs-vs-cats/train.zip'\n\nwith zipfile.ZipFile('../input/dogs-vs-cats/train.zip', 'r') as z :\n    z.extractall()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:25.078280Z","iopub.execute_input":"2021-10-02T09:51:25.078629Z","iopub.status.idle":"2021-10-02T09:51:42.070556Z","shell.execute_reply.started":"2021-10-02T09:51:25.078591Z","shell.execute_reply":"2021-10-02T09:51:42.069779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Get Images**","metadata":{}},{"cell_type":"code","source":"filename = os.listdir('./train')\ncategories = []\n\nfor imag in filename:\n    categ = imag.split('.')[0]\n\n    if(categ == 'dog'):\n        categories.append(1)\n    else:\n        categories.append(0)\n    \n\ndf = pd.DataFrame({'filename' : filename , 'categories' : categories})\ndf.head()\n    ","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:42.072008Z","iopub.execute_input":"2021-10-02T09:51:42.072289Z","iopub.status.idle":"2021-10-02T09:51:42.137231Z","shell.execute_reply.started":"2021-10-02T09:51:42.072241Z","shell.execute_reply":"2021-10-02T09:51:42.136323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['categories'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:42.139509Z","iopub.execute_input":"2021-10-02T09:51:42.139778Z","iopub.status.idle":"2021-10-02T09:51:42.150037Z","shell.execute_reply.started":"2021-10-02T09:51:42.139745Z","shell.execute_reply":"2021-10-02T09:51:42.149169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Showing Few Images ****","metadata":{}},{"cell_type":"code","source":"for i in range(10):\n    sample = filename[i+10]\n    image = tf.keras.preprocessing.image.load_img('./train/' + sample)\n    plt.imshow(image)\n    plt.title('dog' if categories[i+10] == 1 else 'cat')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:42.151562Z","iopub.execute_input":"2021-10-02T09:51:42.151975Z","iopub.status.idle":"2021-10-02T09:51:44.397906Z","shell.execute_reply.started":"2021-10-02T09:51:42.151940Z","shell.execute_reply":"2021-10-02T09:51:44.397258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Creation","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential()\n\n#Conv1\nmodel.add(tf.keras.layers.Conv2D(32 , 3 , input_shape=[128 , 128 , 3] , activation = 'relu' , padding = 'same') )\nmodel.add(tf.keras.layers.MaxPool2D(2))\nmodel.add(tf.keras.layers.Dropout(0.2))\n\n#Conv2\nmodel.add(tf.keras.layers.Conv2D(64 , 3  , activation = 'relu' , padding = 'same') )\nmodel.add(tf.keras.layers.MaxPool2D(2))\nmodel.add(tf.keras.layers.Dropout(0.2))\n\n#Conv3\nmodel.add(tf.keras.layers.Conv2D(128 , 3  , activation = 'relu' , padding = 'same') )\nmodel.add(tf.keras.layers.MaxPool2D(2))\nmodel.add(tf.keras.layers.Dropout(0.3))\n\n#Conv4\nmodel.add(tf.keras.layers.Conv2D(128 , 3  , activation = 'relu' , padding = 'same') )\nmodel.add(tf.keras.layers.MaxPool2D(2))\nmodel.add(tf.keras.layers.Dropout(0.25))\n\n\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(512 , activation = 'relu'))\nmodel.add(tf.keras.layers.Dropout(0.25))\n\nmodel.add(tf.keras.layers.Dense(2 , activation = 'sigmoid'))\n\n\n# COMPILING\nmodel.compile(loss = 'binary_crossentropy' , optimizer = RMSprop(lr=1e-4) , metrics=['accuracy'])\n\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:44.399064Z","iopub.execute_input":"2021-10-02T09:51:44.399351Z","iopub.status.idle":"2021-10-02T09:51:46.405622Z","shell.execute_reply.started":"2021-10-02T09:51:44.399315Z","shell.execute_reply":"2021-10-02T09:51:46.404919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop = tf.keras.callbacks.EarlyStopping(patience=2)\nlearning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\ncallbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:58:51.229496Z","iopub.execute_input":"2021-10-02T09:58:51.229802Z","iopub.status.idle":"2021-10-02T09:58:51.235372Z","shell.execute_reply.started":"2021-10-02T09:58:51.229771Z","shell.execute_reply":"2021-10-02T09:58:51.234453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"code","source":"df['categories'] = df['categories'].map({0 : 'cat', 1 : 'dog'}) \n# Cos flow from data requires y to be string","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:46.413416Z","iopub.execute_input":"2021-10-02T09:51:46.413650Z","iopub.status.idle":"2021-10-02T09:51:46.425884Z","shell.execute_reply.started":"2021-10-02T09:51:46.413620Z","shell.execute_reply":"2021-10-02T09:51:46.425167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:46.428854Z","iopub.execute_input":"2021-10-02T09:51:46.429050Z","iopub.status.idle":"2021-10-02T09:51:46.439565Z","shell.execute_reply.started":"2021-10-02T09:51:46.429029Z","shell.execute_reply":"2021-10-02T09:51:46.438818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df , test_df = train_test_split(df , test_size=0.2 , random_state=42)\n\ntrain_df.reset_index(drop=True , inplace=True)\ntest_df.reset_index(drop=True , inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:46.442500Z","iopub.execute_input":"2021-10-02T09:51:46.442782Z","iopub.status.idle":"2021-10-02T09:51:46.453117Z","shell.execute_reply.started":"2021-10-02T09:51:46.442719Z","shell.execute_reply":"2021-10-02T09:51:46.452397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_test = test_df.shape[0]\nbatch_size = 20","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:46.454460Z","iopub.execute_input":"2021-10-02T09:51:46.454894Z","iopub.status.idle":"2021-10-02T09:51:46.459243Z","shell.execute_reply.started":"2021-10-02T09:51:46.454862Z","shell.execute_reply":"2021-10-02T09:51:46.458155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train and Valid Generator**","metadata":{}},{"cell_type":"code","source":"# train_datagen = ImageDataGenerator(\n#       rescale=1./255,\n#       rotation_range=20,\n#       horizontal_flip=True,\n#       fill_mode='nearest')\n\n# train_generator = train_datagen.flow_from_dataframe(train_df , '/.train/' , \n#                                                     x_col='filename' , y_col='categories',\n#                                                     target_size=[128,128] , class_mode='categorical',\n#                                                     batch_size=batch_size)\n\ntrain_data = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=20,\n                                                         rescale = 1./255,\n                                                         horizontal_flip=True)\ntrain_generator = train_data.flow_from_dataframe(train_df,\n                                                './train/',\n                                                x_col = 'filename',\n                                                y_col = 'categories',\n                                                target_size=[128,128],\n                                                class_mode='categorical',\n                                                batch_size=batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:46.460572Z","iopub.execute_input":"2021-10-02T09:51:46.460927Z","iopub.status.idle":"2021-10-02T09:51:46.721107Z","shell.execute_reply.started":"2021-10-02T09:51:46.460896Z","shell.execute_reply":"2021-10-02T09:51:46.720265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_data = ImageDataGenerator(rescale = 1./255)\n\nvalid_generator = valid_data.flow_from_dataframe(test_df , './train/' , \n                                                 x_col = 'filename' , y_col = 'categories' ,\n                                                 target_size = [128 , 128] , class_mode='categorical' ,\n                                                batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:51:46.722347Z","iopub.execute_input":"2021-10-02T09:51:46.722727Z","iopub.status.idle":"2021-10-02T09:51:46.804961Z","shell.execute_reply.started":"2021-10-02T09:51:46.722691Z","shell.execute_reply":"2021-10-02T09:51:46.804278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(train_generator , epochs = 9 , validation_data=valid_generator , \n                              validation_steps = total_test // batch_size , \n                              steps_per_epoch = total_train // batch_size , \n                              callbacks = callbacks)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T09:59:00.756742Z","iopub.execute_input":"2021-10-02T09:59:00.757349Z","iopub.status.idle":"2021-10-02T10:17:41.464393Z","shell.execute_reply.started":"2021-10-02T09:59:00.757316Z","shell.execute_reply":"2021-10-02T10:17:41.461020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Saving Parameters**","metadata":{}},{"cell_type":"code","source":"model.save_weights('model.hdf5')","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:19:38.337731Z","iopub.execute_input":"2021-10-02T10:19:38.338011Z","iopub.status.idle":"2021-10-02T10:19:38.394331Z","shell.execute_reply.started":"2021-10-02T10:19:38.337983Z","shell.execute_reply":"2021-10-02T10:19:38.393587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nepochs = range(len(history.history['accuracy']))\n\nplt.plot(epochs , history.history['accuracy'] , 'b' , label='Training Acc')\nplt.plot(epochs , history.history['val_accuracy'] , 'r' , label='Validation Acc')\nplt.title('Training and Validation Accuracy')\n\nplt.figure()\n\nplt.plot(epochs, history.history['loss'], 'b', label='Training Loss')\nplt.plot(epochs, history.history['val_loss'], 'g', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:23:27.118083Z","iopub.execute_input":"2021-10-02T10:23:27.118634Z","iopub.status.idle":"2021-10-02T10:23:27.506426Z","shell.execute_reply.started":"2021-10-02T10:23:27.118597Z","shell.execute_reply":"2021-10-02T10:23:27.505717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing Test DATA","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as z :\n    z.extractall()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:25:15.843102Z","iopub.execute_input":"2021-10-02T10:25:15.843896Z","iopub.status.idle":"2021-10-02T10:25:22.705472Z","shell.execute_reply.started":"2021-10-02T10:25:15.843852Z","shell.execute_reply":"2021-10-02T10:25:22.704707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(\"./test1\")\ntest_df = pd.DataFrame({'filename' : filenames})    \nsamples = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:25:35.443672Z","iopub.execute_input":"2021-10-02T10:25:35.444342Z","iopub.status.idle":"2021-10-02T10:25:35.460110Z","shell.execute_reply.started":"2021-10-02T10:25:35.444306Z","shell.execute_reply":"2021-10-02T10:25:35.459457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)\ntest_generator = test_data.flow_from_dataframe(\n    test_df, \n    \"./test1/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=[128,128],\n    batch_size=batch_size,\n    shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:26:08.889842Z","iopub.execute_input":"2021-10-02T10:26:08.890598Z","iopub.status.idle":"2021-10-02T10:26:08.994293Z","shell.execute_reply.started":"2021-10-02T10:26:08.890560Z","shell.execute_reply":"2021-10-02T10:26:08.993328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(samples/batch_size))","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:26:28.690111Z","iopub.execute_input":"2021-10-02T10:26:28.690689Z","iopub.status.idle":"2021-10-02T10:26:57.168842Z","shell.execute_reply.started":"2021-10-02T10:26:28.690653Z","shell.execute_reply":"2021-10-02T10:26:57.168068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)\ntest_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:27:46.116838Z","iopub.execute_input":"2021-10-02T10:27:46.117102Z","iopub.status.idle":"2021-10-02T10:27:46.125931Z","shell.execute_reply.started":"2021-10-02T10:27:46.117073Z","shell.execute_reply":"2021-10-02T10:27:46.125250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:27:47.980949Z","iopub.execute_input":"2021-10-02T10:27:47.981205Z","iopub.status.idle":"2021-10-02T10:27:48.034898Z","shell.execute_reply.started":"2021-10-02T10:27:47.981177Z","shell.execute_reply":"2021-10-02T10:27:48.034227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-02T10:28:10.610135Z","iopub.execute_input":"2021-10-02T10:28:10.610813Z","iopub.status.idle":"2021-10-02T10:28:10.619955Z","shell.execute_reply.started":"2021-10-02T10:28:10.610782Z","shell.execute_reply":"2021-10-02T10:28:10.619026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}