{"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-25T14:57:47.296194Z","iopub.execute_input":"2021-06-25T14:57:47.296854Z","iopub.status.idle":"2021-06-25T14:57:47.303507Z","shell.execute_reply.started":"2021-06-25T14:57:47.296817Z","shell.execute_reply":"2021-06-25T14:57:47.302804Z"},"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-25T14:57:47.305245Z","iopub.execute_input":"2021-06-25T14:57:47.305999Z","iopub.status.idle":"2021-06-25T14:57:54.478429Z","shell.execute_reply.started":"2021-06-25T14:57:47.305732Z","shell.execute_reply":"2021-06-25T14:57:54.477516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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-25T14:57:54.480030Z","iopub.execute_input":"2021-06-25T14:57:54.480498Z","iopub.status.idle":"2021-06-25T14:58:15.357758Z","shell.execute_reply.started":"2021-06-25T14:57:54.480464Z","shell.execute_reply":"2021-06-25T14:58:15.356639Z"},"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-25T14:58:15.359495Z","iopub.execute_input":"2021-06-25T14:58:15.359872Z","iopub.status.idle":"2021-06-25T14:58:15.366054Z","shell.execute_reply.started":"2021-06-25T14:58:15.359838Z","shell.execute_reply":"2021-06-25T14:58:15.364812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:58:15.369668Z","iopub.execute_input":"2021-06-25T14:58:15.370086Z","iopub.status.idle":"2021-06-25T14:58:15.386609Z","shell.execute_reply.started":"2021-06-25T14:58:15.370048Z","shell.execute_reply":"2021-06-25T14:58:15.385329Z"},"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-25T14:58:15.388338Z","iopub.execute_input":"2021-06-25T14:58:15.388782Z","iopub.status.idle":"2021-06-25T14:58:15.445888Z","shell.execute_reply.started":"2021-06-25T14:58:15.388710Z","shell.execute_reply":"2021-06-25T14:58:15.444791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:58:15.447261Z","iopub.execute_input":"2021-06-25T14:58:15.447567Z","iopub.status.idle":"2021-06-25T14:58:15.473251Z","shell.execute_reply.started":"2021-06-25T14:58:15.447537Z","shell.execute_reply":"2021-06-25T14:58:15.471985Z"},"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-25T14:58:15.476054Z","iopub.execute_input":"2021-06-25T14:58:15.476379Z","iopub.status.idle":"2021-06-25T14:58:15.671402Z","shell.execute_reply.started":"2021-06-25T14:58:15.476347Z","shell.execute_reply":"2021-06-25T14:58:15.670310Z"},"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-25T14:58:15.673188Z","iopub.execute_input":"2021-06-25T14:58:15.673487Z","iopub.status.idle":"2021-06-25T14:58:15.701397Z","shell.execute_reply.started":"2021-06-25T14:58:15.673458Z","shell.execute_reply":"2021-06-25T14:58:15.700379Z"},"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-25T14:58:15.702884Z","iopub.execute_input":"2021-06-25T14:58:15.703201Z","iopub.status.idle":"2021-06-25T14:58:15.716704Z","shell.execute_reply.started":"2021-06-25T14:58:15.703171Z","shell.execute_reply":"2021-06-25T14:58:15.715118Z"},"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-25T14:58:15.718626Z","iopub.execute_input":"2021-06-25T14:58:15.719183Z","iopub.status.idle":"2021-06-25T14:58:15.727044Z","shell.execute_reply.started":"2021-06-25T14:58:15.719137Z","shell.execute_reply":"2021-06-25T14:58:15.726120Z"},"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-25T14:58:15.728427Z","iopub.execute_input":"2021-06-25T14:58:15.728812Z","iopub.status.idle":"2021-06-25T14:58:19.063486Z","shell.execute_reply.started":"2021-06-25T14:58:15.728763Z","shell.execute_reply":"2021-06-25T14:58:19.062381Z"},"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-25T14:58:19.064951Z","iopub.execute_input":"2021-06-25T14:58:19.065289Z","iopub.status.idle":"2021-06-25T14:58:19.071414Z","shell.execute_reply.started":"2021-06-25T14:58:19.065257Z","shell.execute_reply":"2021-06-25T14:58:19.070037Z"},"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-25T14:58:19.072911Z","iopub.execute_input":"2021-06-25T14:58:19.073219Z","iopub.status.idle":"2021-06-25T14:58:20.393491Z","shell.execute_reply.started":"2021-06-25T14:58:19.073190Z","shell.execute_reply":"2021-06-25T14:58:20.392031Z"},"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-25T14:58:20.395022Z","iopub.execute_input":"2021-06-25T14:58:20.395358Z","iopub.status.idle":"2021-06-25T14:58:20.701205Z","shell.execute_reply.started":"2021-06-25T14:58:20.395328Z","shell.execute_reply":"2021-06-25T14:58:20.700024Z"},"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-25T14:58:20.703032Z","iopub.execute_input":"2021-06-25T14:58:20.703479Z","iopub.status.idle":"2021-06-25T15:36:03.862585Z","shell.execute_reply.started":"2021-06-25T14:58:20.703435Z","shell.execute_reply":"2021-06-25T15:36:03.861011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:36:03.865042Z","iopub.execute_input":"2021-06-25T15:36:03.865419Z","iopub.status.idle":"2021-06-25T15:36:03.989784Z","shell.execute_reply.started":"2021-06-25T15:36:03.865379Z","shell.execute_reply":"2021-06-25T15:36:03.988628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#os.listdir('./')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:36:03.991443Z","iopub.execute_input":"2021-06-25T15:36:03.991868Z","iopub.status.idle":"2021-06-25T15:36:03.996937Z","shell.execute_reply.started":"2021-06-25T15:36:03.991825Z","shell.execute_reply":"2021-06-25T15:36:03.995738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!rm -rf ./dataset","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:36:03.998643Z","iopub.execute_input":"2021-06-25T15:36:03.999140Z","iopub.status.idle":"2021-06-25T15:36:04.010463Z","shell.execute_reply.started":"2021-06-25T15:36:03.999090Z","shell.execute_reply":"2021-06-25T15:36:04.009227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}