{"cells":[{"metadata":{"_uuid":"a6e6dcca-5e29-4e83-975d-a4c9fe5e8de4","_cell_guid":"2c24b748-0c3f-45b3-9c36-85c9e334340d","trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8d2738bd-f7bc-46b2-b013-21e5c2813945","_cell_guid":"e87d3068-6106-47b6-ac1c-306f920fc575","trusted":true},"cell_type":"code","source":"# read train.zip dataset and test1.zip dataset\nimport zipfile as Zip\n\nZip.ZipFile('/kaggle/input/dogs-vs-cats/train.zip').extractall()\nZip.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip').extractall()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"37061ab6-bb95-4253-94dc-8131514f6f40","_cell_guid":"72314bf2-5193-4bdc-845d-a4144d5ee395","trusted":true},"cell_type":"code","source":"# optimize train dataset\n!cd '/kaggle/working/train'; mkdir dogs; mkdir cats\n\n!cd '/kaggle/working/train'; ls | grep -e cat | xargs mv -t cats\n!cd '/kaggle/working/train'; ls | grep -e dog | xargs mv -t dogs","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2abc5073-cb32-4776-9fdc-491f75bbc639","_cell_guid":"f8a63d28-a3b3-47de-ab6d-1c4189d0c21b","trusted":true},"cell_type":"code","source":"# check train dataset directory\n!ls /kaggle/working/train -la","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1fb6f59b-3cfa-4c54-b04d-85a4da18eea1","_cell_guid":"da24caba-0933-4b2f-b1f3-3b0a3d3b13cc","trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ngenerator = ImageDataGenerator(rescale = 1/255., validation_split = 0.2)\n\ntrain_gen = generator.flow_from_directory('/kaggle/working/train', target_size = (64, 64), batch_size = 16, class_mode = 'binary', subset = 'training')\nvalid_gen = generator.flow_from_directory('/kaggle/working/train', target_size = (64, 64), batch_size = 16, class_mode = 'binary', subset = 'validation')\ntest_gen = generator.flow_from_directory('/kaggle/working', target_size = (64, 64), batch_size = 16, classes = ['test1'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f3f78f8f-5869-4e13-96c0-b022ed389d83","_cell_guid":"cf0e72a9-752a-4118-b7da-b27b5a1922ab","trusted":true},"cell_type":"code","source":"# check the shape\nx_train, y_train = next(train_gen)\nx_test, y_test = next(test_gen)\ny_train = y_train.reshape((y_train.shape[0], 1))\n\nx_train.shape, y_train.shape, x_test.shape, y_test.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1e9cd8d8-39a3-4f3f-914e-0a165c5ecbba","_cell_guid":"eb2a4d18-e02a-4aee-9826-232133a53867","trusted":true},"cell_type":"code","source":"# build model\nfrom keras.layers import *\nfrom keras.models import *\nfrom keras.applications import *\nimport numpy as np\n\ndef build_model():\n    model = Sequential()\n    model.add(VGG16(input_shape = (x_train.shape[1], x_train.shape[2], x_train.shape[3]), include_top = False, weights = 'imagenet', pooling = 'max'))\n    model.add(Dense(1024, activation = 'relu', kernel_initializer = 'he_normal'))\n    model.add(Dense(512, activation = 'relu', kernel_initializer = 'he_normal'))\n    model.add(Dropout(0.3))\n    model.add(Dense(1, activation = 'sigmoid', kernel_initializer = 'he_normal'))\n\n    return model\n\nmodel = build_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e2530e6e-a27e-453a-8bba-83301bd68721","_cell_guid":"ac81fda9-25ac-4bb5-b7ab-ceb31e973d80","trusted":true},"cell_type":"code","source":"# if val_loss will not imporve consecutive 10 times(patience = 5), epochs will be stopped\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\n\nes = EarlyStopping(monitor = 'val_loss', mode = 'min', verbose = 1, patience = 5)\nmc = ModelCheckpoint('check.h5', monitor = 'val_binary_accuracy', mode = 'max', verbose = 1, save_best_only=True)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f6b1c31e-8a04-4337-bab0-aa670bece64c","_cell_guid":"303f54b8-9525-452b-9b9f-66388ea00bee","trusted":true},"cell_type":"code","source":"from keras.optimizers import SGD\n\nmodel.compile(loss = 'binary_crossentropy', optimizer = SGD(lr=0.00002, momentum = 0.9, nesterov = True), metrics = ['binary_accuracy'])\nhistory = model.fit(train_gen, epochs = 200, callbacks = [es, mc], batch_size = 8, steps_per_epoch = len(train_gen), validation_data = valid_gen)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ea66038d-e88b-4769-b444-acffd73d6f05","_cell_guid":"d039868a-7cef-4d9a-84e4-3e63b537a557","trusted":true},"cell_type":"code","source":"# check result on graph\nimport matplotlib.pyplot as plt\n\nloss = history.history['loss']\nacc = history.history['binary_accuracy']\nval_loss = history.history['val_loss']\nval_acc = history.history['val_binary_accuracy']\n\nplt.plot(loss, label= 'loss')\nplt.plot(acc, label= 'binary_accuracy')\nplt.plot(val_loss, label= 'val_loss')\nplt.plot(val_acc, label= 'val_binary_accuracy')\n\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}