{"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-06-18T18:29:41.169188Z","iopub.execute_input":"2021-06-18T18:29:41.169508Z","iopub.status.idle":"2021-06-18T18:29:41.179304Z","shell.execute_reply.started":"2021-06-18T18:29:41.169477Z","shell.execute_reply":"2021-06-18T18:29:41.178009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.getcwd())","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:41.181248Z","iopub.execute_input":"2021-06-18T18:29:41.181824Z","iopub.status.idle":"2021-06-18T18:29:41.190412Z","shell.execute_reply.started":"2021-06-18T18:29:41.181742Z","shell.execute_reply":"2021-06-18T18:29:41.189478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Used to clean the working space before be\n!rm -rf *","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:41.192609Z","iopub.execute_input":"2021-06-18T18:29:41.192988Z","iopub.status.idle":"2021-06-18T18:29:42.842669Z","shell.execute_reply.started":"2021-06-18T18:29:41.192936Z","shell.execute_reply":"2021-06-18T18:29:42.841545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracting the datasets\nimport  zipfile\n\ntrain_zip = zipfile.ZipFile('../input/dogs-vs-cats/train.zip','r')\ntrain_zip.extractall('./')\ntest_zip = zipfile.ZipFile('../input/dogs-vs-cats/test1.zip')\ntest_zip.extractall('./')\ntrain_zip.close()\ntest_zip.close()","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:42.844923Z","iopub.execute_input":"2021-06-18T18:29:42.845273Z","iopub.status.idle":"2021-06-18T18:29:51.848372Z","shell.execute_reply.started":"2021-06-18T18:29:42.845235Z","shell.execute_reply":"2021-06-18T18:29:51.847423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting the size of train and test sets\nprint(len(os.listdir('./test1')))\nprint(len(os.listdir('./train')))","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:51.850639Z","iopub.execute_input":"2021-06-18T18:29:51.851207Z","iopub.status.idle":"2021-06-18T18:29:51.875878Z","shell.execute_reply.started":"2021-06-18T18:29:51.851167Z","shell.execute_reply":"2021-06-18T18:29:51.875001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizing a portion of our datasets\nprint(os.listdir('./test1')[:10])\nprint(os.listdir('./train')[:10])","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:51.877302Z","iopub.execute_input":"2021-06-18T18:29:51.877848Z","iopub.status.idle":"2021-06-18T18:29:52.064640Z","shell.execute_reply.started":"2021-06-18T18:29:51.877808Z","shell.execute_reply":"2021-06-18T18:29:52.061923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading our datasets into dataframes\ntrain = pd.DataFrame(os.listdir('./train'),columns = ['filename'])\ntest = pd.DataFrame(os.listdir('./test1'),columns = ['filename'])\ntrain,test","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:52.066395Z","iopub.execute_input":"2021-06-18T18:29:52.067006Z","iopub.status.idle":"2021-06-18T18:29:52.105837Z","shell.execute_reply.started":"2021-06-18T18:29:52.066961Z","shell.execute_reply":"2021-06-18T18:29:52.104866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting labels for train dataset based on the filename\ntrain['label'] = train['filename'].str.split('.',1,expand = True)[0]\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:52.108346Z","iopub.execute_input":"2021-06-18T18:29:52.108698Z","iopub.status.idle":"2021-06-18T18:29:52.165487Z","shell.execute_reply.started":"2021-06-18T18:29:52.108662Z","shell.execute_reply":"2021-06-18T18:29:52.164525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking whether data is balanced or not\ntrain['label'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:52.167119Z","iopub.execute_input":"2021-06-18T18:29:52.167479Z","iopub.status.idle":"2021-06-18T18:29:52.287059Z","shell.execute_reply.started":"2021-06-18T18:29:52.167444Z","shell.execute_reply":"2021-06-18T18:29:52.286129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_df , validation_df = train_test_split(train,test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:52.288359Z","iopub.execute_input":"2021-06-18T18:29:52.288695Z","iopub.status.idle":"2021-06-18T18:29:52.299128Z","shell.execute_reply.started":"2021-06-18T18:29:52.288658Z","shell.execute_reply":"2021-06-18T18:29:52.298379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validation_df","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:52.301973Z","iopub.execute_input":"2021-06-18T18:29:52.302490Z","iopub.status.idle":"2021-06-18T18:29:52.318748Z","shell.execute_reply.started":"2021-06-18T18:29:52.302463Z","shell.execute_reply":"2021-06-18T18:29:52.317696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating simple model\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\nmodel = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(16,(3,3),activation = 'relu', input_shape = (150,150,3)),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(32,(3,3),activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(64,(3,3),activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512,activation = 'relu'),\n    tf.keras.layers.Dense(1, activation='sigmoid') \n])\n\nmodel.compile(optimizer=RMSprop(lr=0.001), loss='binary_crossentropy', metrics=['acc'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:52.320525Z","iopub.execute_input":"2021-06-18T18:29:52.321032Z","iopub.status.idle":"2021-06-18T18:29:52.400377Z","shell.execute_reply.started":"2021-06-18T18:29:52.320993Z","shell.execute_reply":"2021-06-18T18:29:52.399373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating the images generator and augmenting them\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nTRAIN_DIR = './train'\ntrain_datagen =ImageDataGenerator( rescale = 1.0/255.,\n                                rotation_range=40,\n                                width_shift_range=0.2,\n                                height_shift_range=0.2,\n                                shear_range=0.2,\n                                zoom_range=0.2,\n                                horizontal_flip=True,\n                                fill_mode='nearest')\n\ntrain_generator = train_datagen.flow_from_dataframe(train_df,\n                                                    directory = TRAIN_DIR,\n                                                   batch_size = 10,\n                                                    x_col = 'filename',\n                                                    y_col = 'label',\n                                                   class_mode = 'binary',\n                                                   target_size = (150,150))\n\nvalidation_datagen =ImageDataGenerator(rescale = 1.0/255.)\nval_generator = validation_datagen.flow_from_dataframe(validation_df,\n                                                    directory = TRAIN_DIR,\n                                                   batch_size = 10,\n                                                    x_col = 'filename',\n                                                    y_col = 'label',\n                                                   class_mode = 'binary',\n                                                   target_size = (150,150))\n","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:52.401964Z","iopub.execute_input":"2021-06-18T18:29:52.402318Z","iopub.status.idle":"2021-06-18T18:29:52.650100Z","shell.execute_reply.started":"2021-06-18T18:29:52.402280Z","shell.execute_reply":"2021-06-18T18:29:52.648913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,epochs = 4, validation_data = val_generator)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:29:52.651371Z","iopub.execute_input":"2021-06-18T18:29:52.651716Z","iopub.status.idle":"2021-06-18T18:39:43.032357Z","shell.execute_reply.started":"2021-06-18T18:29:52.651679Z","shell.execute_reply":"2021-06-18T18:39:43.031518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nacc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs_range = range(len(acc))\n\nplt.figure(figsize=(15, 15))\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:39:43.033892Z","iopub.execute_input":"2021-06-18T18:39:43.034268Z","iopub.status.idle":"2021-06-18T18:39:43.309561Z","shell.execute_reply.started":"2021-06-18T18:39:43.034232Z","shell.execute_reply":"2021-06-18T18:39:43.308630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = validation_datagen.flow_from_dataframe(test,\n                                                    directory = './test1',\n                                                   batch_size = 10,\n                                                    x_col = 'filename',\n                                                    y_col = None,\n                                                   class_mode = None,\n                                                   target_size = (150,150))","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:39:43.310834Z","iopub.execute_input":"2021-06-18T18:39:43.311165Z","iopub.status.idle":"2021-06-18T18:39:43.412373Z","shell.execute_reply.started":"2021-06-18T18:39:43.311126Z","shell.execute_reply":"2021-06-18T18:39:43.411442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:41:57.382421Z","iopub.execute_input":"2021-06-18T18:41:57.382738Z","iopub.status.idle":"2021-06-18T18:42:26.652313Z","shell.execute_reply.started":"2021-06-18T18:41:57.382694Z","shell.execute_reply":"2021-06-18T18:42:26.651399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2021-06-18T18:42:26.654714Z","iopub.execute_input":"2021-06-18T18:42:26.655017Z","iopub.status.idle":"2021-06-18T18:42:26.665645Z","shell.execute_reply.started":"2021-06-18T18:42:26.654991Z","shell.execute_reply":"2021-06-18T18:42:26.664810Z"},"trusted":true},"execution_count":null,"outputs":[]}]}