{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls train","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom os import listdir","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.getcwd()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_list = os.listdir('/kaggle/working/train')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_filenames = [fn for fn in train_file_list if fn.startswith('dog')]\ncat_filenames = [fn for fn in train_file_list if fn.startswith('cat')]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_filenames","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir train_data","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir train_data/cat","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir train_data/dog","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from shutil import copyfile\n\n\n\ncopyfile(src, dst)\n\n# 2nd option\ncopy(src, dst)  # dst can be a folder; use copy2() to preserve timestamp","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from shutil import copyfile\nfor i in dog_filenames:\n    copyfile(\"train/\"+i, \"train_data/dog/\"+i)\n    \n#cat_filenames","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from shutil import copyfile\nfor i in cat_filenames:\n    copyfile(\"train/\"+i, \"train_data/cat/\"+i)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls train","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls train_data/dog","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pwd","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set= train_datagen.flow_from_directory('/kaggle/working/train_data/', target_size=(128,128), batch_size=64, class_mode='binary')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.layers import Dropout","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=16, kernel_size=(5, 5), activation=\"relu\", input_shape=(128,128,3)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(filters=32, kernel_size=(5, 5), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(filters=64, kernel_size=(5, 5), activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(filters=64, kernel_size=(5, 5), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"model.fit_generator(train_set, epochs=5 )","metadata":{"execution":{"iopub.status.idle":"2021-11-21T05:56:10.308868Z","shell.execute_reply.started":"2021-11-21T05:51:01.510744Z","shell.execute_reply":"2021-11-21T05:56:10.308164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate_generator(train_set)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T05:56:27.790227Z","iopub.execute_input":"2021-11-21T05:56:27.790918Z","iopub.status.idle":"2021-11-21T05:57:26.764477Z","shell.execute_reply.started":"2021-11-21T05:56:27.790874Z","shell.execute_reply":"2021-11-21T05:57:26.763805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Transfer Learning**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import ResNet50\npre_train = ResNet50(weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2021-11-21T05:59:24.812281Z","iopub.execute_input":"2021-11-21T05:59:24.813064Z","iopub.status.idle":"2021-11-21T05:59:26.946656Z","shell.execute_reply.started":"2021-11-21T05:59:24.813027Z","shell.execute_reply":"2021-11-21T05:59:26.94589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(pre_train)\nmodel.add(Dense(1, activation='sigmoid'))\nmodel.layers[0].trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:00:48.429002Z","iopub.execute_input":"2021-11-21T06:00:48.429261Z","iopub.status.idle":"2021-11-21T06:00:48.82187Z","shell.execute_reply.started":"2021-11-21T06:00:48.429232Z","shell.execute_reply":"2021-11-21T06:00:48.821114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:00:58.872293Z","iopub.execute_input":"2021-11-21T06:00:58.873011Z","iopub.status.idle":"2021-11-21T06:00:58.885833Z","shell.execute_reply.started":"2021-11-21T06:00:58.872972Z","shell.execute_reply":"2021-11-21T06:00:58.885075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:01:13.408682Z","iopub.execute_input":"2021-11-21T06:01:13.408945Z","iopub.status.idle":"2021-11-21T06:01:13.425858Z","shell.execute_reply.started":"2021-11-21T06:01:13.408917Z","shell.execute_reply":"2021-11-21T06:01:13.425053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(train_set, epochs=5 )","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:01:53.338209Z","iopub.execute_input":"2021-11-21T06:01:53.338901Z","iopub.status.idle":"2021-11-21T06:07:37.254006Z","shell.execute_reply.started":"2021-11-21T06:01:53.338863Z","shell.execute_reply":"2021-11-21T06:07:37.253204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate_generator(train_set)","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:07:45.021485Z","iopub.execute_input":"2021-11-21T06:07:45.021983Z","iopub.status.idle":"2021-11-21T06:08:49.038866Z","shell.execute_reply.started":"2021-11-21T06:07:45.021946Z","shell.execute_reply":"2021-11-21T06:08:49.03812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Augumentation with Trnsfer Learning**","metadata":{}},{"cell_type":"code","source":"train_datagen_aug = ImageDataGenerator(featurewise_center=True,\n    horizontal_flip=True,\n    vertical_flip=True,\n   )","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:46:45.668476Z","iopub.execute_input":"2021-11-21T06:46:45.668755Z","iopub.status.idle":"2021-11-21T06:46:45.672844Z","shell.execute_reply.started":"2021-11-21T06:46:45.668725Z","shell.execute_reply":"2021-11-21T06:46:45.672117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set_aug = train_datagen_aug.flow_from_directory('/kaggle/working/train_data/', target_size=(128,128), batch_size=64, class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:46:47.411066Z","iopub.execute_input":"2021-11-21T06:46:47.411342Z","iopub.status.idle":"2021-11-21T06:46:48.084925Z","shell.execute_reply.started":"2021-11-21T06:46:47.411297Z","shell.execute_reply":"2021-11-21T06:46:48.084104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(pre_train)\nmodel.add(Dense(1, activation='sigmoid'))\nmodel.layers[0].trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:17:10.265157Z","iopub.execute_input":"2021-11-21T06:17:10.265428Z","iopub.status.idle":"2021-11-21T06:17:10.650095Z","shell.execute_reply.started":"2021-11-21T06:17:10.265398Z","shell.execute_reply":"2021-11-21T06:17:10.649376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:17:17.411256Z","iopub.execute_input":"2021-11-21T06:17:17.412005Z","iopub.status.idle":"2021-11-21T06:17:17.426041Z","shell.execute_reply.started":"2021-11-21T06:17:17.411967Z","shell.execute_reply":"2021-11-21T06:17:17.425124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(train_set_aug, epochs=100, steps_per_epoch=10 )","metadata":{"execution":{"iopub.status.busy":"2021-11-21T06:33:20.541664Z","iopub.execute_input":"2021-11-21T06:33:20.541962Z","iopub.status.idle":"2021-11-21T06:41:36.040495Z","shell.execute_reply.started":"2021-11-21T06:33:20.541929Z","shell.execute_reply":"2021-11-21T06:41:36.039266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}