{"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":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input/'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-10T03:25:29.36835Z","iopub.execute_input":"2021-06-10T03:25:29.368989Z","iopub.status.idle":"2021-06-10T03:25:29.378295Z","shell.execute_reply.started":"2021-06-10T03:25:29.368887Z","shell.execute_reply":"2021-06-10T03:25:29.377542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, BatchNormalization, Conv2D, MaxPooling2D\nfrom tensorflow.keras.models import Sequential\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img\nfrom zipfile import ZipFile\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-06-10T03:25:32.470894Z","iopub.execute_input":"2021-06-10T03:25:32.471246Z","iopub.status.idle":"2021-06-10T03:25:38.49551Z","shell.execute_reply.started":"2021-06-10T03:25:32.471214Z","shell.execute_reply":"2021-06-10T03:25:38.494666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile(\"../input/dogs-vs-cats/train.zip\", 'r') as zip_ref:\n    zip_ref.extractall('.')\nwith ZipFile(\"../input/dogs-vs-cats/test1.zip\", 'r') as zip_ref:\n    zip_ref.extractall('.')","metadata":{"execution":{"iopub.status.busy":"2021-06-10T03:26:19.348846Z","iopub.execute_input":"2021-06-10T03:26:19.349174Z","iopub.status.idle":"2021-06-10T03:26:39.88926Z","shell.execute_reply.started":"2021-06-10T03:26:19.349146Z","shell.execute_reply":"2021-06-10T03:26:39.888302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/dataset')\n\nos.mkdir('/kaggle/working/dataset/training')\nos.mkdir('/kaggle/working/dataset/validation')\n\nos.mkdir('/kaggle/working/dataset/training/cats')\nos.mkdir('/kaggle/working/dataset/training/dogs')\nos.mkdir('/kaggle/working/dataset/validation/cats')\nos.mkdir('/kaggle/working/dataset/validation/dogs')","metadata":{"execution":{"iopub.status.busy":"2021-06-10T03:28:56.491582Z","iopub.execute_input":"2021-06-10T03:28:56.491916Z","iopub.status.idle":"2021-06-10T03:28:56.496992Z","shell.execute_reply.started":"2021-06-10T03:28:56.491889Z","shell.execute_reply":"2021-06-10T03:28:56.496078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2021-06-10T03:28:59.45316Z","iopub.execute_input":"2021-06-10T03:28:59.453487Z","iopub.status.idle":"2021-06-10T03:28:59.461737Z","shell.execute_reply.started":"2021-06-10T03:28:59.453461Z","shell.execute_reply":"2021-06-10T03:28:59.460495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(\"./train\")\ncategories = []\nfor name in filenames:\n    category = name.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n        \ndf = pd.DataFrame({\n    'filename' : filenames,\n    'category' : categories\n})","metadata":{"execution":{"iopub.status.busy":"2021-06-10T03:31:31.268562Z","iopub.execute_input":"2021-06-10T03:31:31.269139Z","iopub.status.idle":"2021-06-10T03:31:31.309487Z","shell.execute_reply.started":"2021-06-10T03:31:31.269082Z","shell.execute_reply":"2021-06-10T03:31:31.308806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:56:42.509721Z","iopub.execute_input":"2021-06-02T04:56:42.510096Z","iopub.status.idle":"2021-06-02T04:56:42.521364Z","shell.execute_reply.started":"2021-06-02T04:56:42.510063Z","shell.execute_reply":"2021-06-02T04:56:42.520626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.category.value_counts().plot.bar()\nplt.xticks([0,1],['Cats','Dogs'])\nplt.title('Cats vs Dogs')\nplt.ylabel('Number of Pictures')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:56:47.212027Z","iopub.execute_input":"2021-06-02T04:56:47.212457Z","iopub.status.idle":"2021-06-02T04:56:47.364384Z","shell.execute_reply.started":"2021-06-02T04:56:47.212426Z","shell.execute_reply":"2021-06-02T04:56:47.363137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cats = pd.DataFrame(df[df.category == 0])\ndogs = pd.DataFrame(df[df.category == 1])","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:56:50.622571Z","iopub.execute_input":"2021-06-02T04:56:50.622969Z","iopub.status.idle":"2021-06-02T04:56:50.633078Z","shell.execute_reply.started":"2021-06-02T04:56:50.622933Z","shell.execute_reply":"2021-06-02T04:56:50.631916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cats, val_cats = train_test_split(cats, test_size = 0.2, random_state = 42)\ntrain_dogs, val_dogs = train_test_split(dogs, test_size = 0.2, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:56:53.664687Z","iopub.execute_input":"2021-06-02T04:56:53.665067Z","iopub.status.idle":"2021-06-02T04:56:53.676869Z","shell.execute_reply.started":"2021-06-02T04:56:53.665034Z","shell.execute_reply":"2021-06-02T04:56:53.676025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Number of files in train_cats : {train_cats.shape[0]}')\nprint(f'Number of files in val_cats: {val_cats.shape[0]}')\nprint(f'Number of files in train_dogs : {train_dogs.shape[0]}')\nprint(f'Number of files in val_dogs : {val_dogs.shape[0]}')","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:56:55.409468Z","iopub.execute_input":"2021-06-02T04:56:55.409853Z","iopub.status.idle":"2021-06-02T04:56:55.416121Z","shell.execute_reply.started":"2021-06-02T04:56:55.409821Z","shell.execute_reply":"2021-06-02T04:56:55.415364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for filename in train_cats.filename:\n    shutil.copy(os.path.join('./train', filename),\n                os.path.join('./dataset/training/cats', filename))\nfor filename in train_dogs.filename:\n    shutil.copy(os.path.join('./train', filename), \n                os.path.join('./dataset/training/dogs', filename))\n    \nfor filename in val_cats.filename:\n    shutil.copy(os.path.join('./train', filename), \n                os.path.join('./dataset/validation/cats', filename))\nfor filename in val_dogs.filename:\n    shutil.copy(os.path.join('./train', filename), \n                os.path.join('./dataset/validation/dogs', filename))","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:56:58.416169Z","iopub.execute_input":"2021-06-02T04:56:58.416526Z","iopub.status.idle":"2021-06-02T04:57:01.827241Z","shell.execute_reply.started":"2021-06-02T04:56:58.416498Z","shell.execute_reply":"2021-06-02T04:57:01.826199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale = 1./255,\n    rotation_range = 20,\n    horizontal_flip = True,\n    shear_range = 0.2,\n    fill_mode = 'nearest'\n)\n\nval_datagen = ImageDataGenerator(\n    rescale = 1./255,\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:57:17.617669Z","iopub.execute_input":"2021-06-02T04:57:17.618049Z","iopub.status.idle":"2021-06-02T04:57:17.624835Z","shell.execute_reply.started":"2021-06-02T04:57:17.618019Z","shell.execute_reply":"2021-06-02T04:57:17.623322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory('./dataset/training',\n                                                   batch_size=32,\n                                                   class_mode='binary',\n                                                   target_size=(128,128))\n\nval_generator = train_datagen.flow_from_directory('./dataset/validation',\n                                                   batch_size=32,\n                                                   class_mode='binary',\n                                                   target_size=(128,128))","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:57:24.19297Z","iopub.execute_input":"2021-06-02T04:57:24.193371Z","iopub.status.idle":"2021-06-02T04:57:25.596916Z","shell.execute_reply.started":"2021-06-02T04:57:24.193333Z","shell.execute_reply":"2021-06-02T04:57:25.595778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(16,(3,3), activation = 'relu', input_shape = (128,128,3)))\n#model.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n#model.add(Dropout(0.25))\n\nmodel.add(Conv2D(64,(3,3), activation = 'relu'))\n#model.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n#model.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\n#model.add(BatchNormalization())\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(optimizer=\"adam\",\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-02T05:33:12.493364Z","iopub.execute_input":"2021-06-02T05:33:12.493742Z","iopub.status.idle":"2021-06-02T05:33:12.652679Z","shell.execute_reply.started":"2021-06-02T05:33:12.493696Z","shell.execute_reply":"2021-06-02T05:33:12.651657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n      train_generator,\n      steps_per_epoch=313,\n      epochs=15, \n      validation_data=val_generator, \n      validation_steps=79,  \n      verbose=2,\n      )","metadata":{"execution":{"iopub.status.busy":"2021-06-02T05:33:19.115603Z","iopub.execute_input":"2021-06-02T05:33:19.116009Z","iopub.status.idle":"2021-06-02T06:07:12.31536Z","shell.execute_reply.started":"2021-06-02T05:33:19.115974Z","shell.execute_reply":"2021-06-02T06:07:12.314228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-06-02T06:09:28.547755Z","iopub.execute_input":"2021-06-02T06:09:28.548156Z","iopub.status.idle":"2021-06-02T06:09:28.633967Z","shell.execute_reply.started":"2021-06-02T06:09:28.548123Z","shell.execute_reply":"2021-06-02T06:09:28.632879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#os.listdir('./')","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:56:09.420782Z","iopub.execute_input":"2021-06-02T04:56:09.421201Z","iopub.status.idle":"2021-06-02T04:56:09.427665Z","shell.execute_reply.started":"2021-06-02T04:56:09.421168Z","shell.execute_reply":"2021-06-02T04:56:09.426768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!rm -rf ./dataset","metadata":{"execution":{"iopub.status.busy":"2021-06-02T04:56:02.973965Z","iopub.execute_input":"2021-06-02T04:56:02.974484Z","iopub.status.idle":"2021-06-02T04:56:04.066517Z","shell.execute_reply.started":"2021-06-02T04:56:02.974446Z","shell.execute_reply":"2021-06-02T04:56:04.065105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2021-06-10T03:39:54.107331Z","iopub.execute_input":"2021-06-10T03:39:54.107628Z","iopub.status.idle":"2021-06-10T03:39:54.238074Z","shell.execute_reply.started":"2021-06-10T03:39:54.107603Z","shell.execute_reply":"2021-06-10T03:39:54.236759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}