{"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-11-24T03:41:55.228943Z","iopub.execute_input":"2021-11-24T03:41:55.229314Z","iopub.status.idle":"2021-11-24T03:41:55.256009Z","shell.execute_reply.started":"2021-11-24T03:41:55.229227Z","shell.execute_reply":"2021-11-24T03:41:55.255327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Unzip the train data\nfrom zipfile import ZipFile\nfile_name = '/kaggle/input/dogs-vs-cats/train.zip'\nwith ZipFile(file_name, 'r') as zip:\n    zip.extractall()\n    print('done unzipping the train data')","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:41:55.257545Z","iopub.execute_input":"2021-11-24T03:41:55.258006Z","iopub.status.idle":"2021-11-24T03:42:06.564358Z","shell.execute_reply.started":"2021-11-24T03:41:55.25797Z","shell.execute_reply":"2021-11-24T03:42:06.563588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir_list = os.listdir('./train')\npath, dir, files = next(os.walk('./train'))\nprint(\"file count in training set : \", len(files))\n\noriginal_dataset_dir = './train'\nbase_dir = '/kaggle/working/cats_and_dogs_small'\nos.mkdir(base_dir)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:06.565558Z","iopub.execute_input":"2021-11-24T03:42:06.566082Z","iopub.status.idle":"2021-11-24T03:42:06.614828Z","shell.execute_reply.started":"2021-11-24T03:42:06.566031Z","shell.execute_reply":"2021-11-24T03:42:06.614044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating train, validation and test set\ntrain_dir = os.path.join(base_dir, 'train')\nos.mkdir(train_dir)\n\nval_dir = os.path.join(base_dir, 'validation')\nos.mkdir(val_dir)\n\ntest_dir = os.path.join(base_dir, 'test')\nos.mkdir(test_dir)\n\n# seperate folderes for cats and dogs\n\ntrain_cats_dir = os.path.join(train_dir, 'cats')\nos.mkdir(train_cats_dir)\n\ntrain_dogs_dir = os.path.join(train_dir, 'dogs')\nos.mkdir(train_dogs_dir)\n\nval_cats_dir = os.path.join(val_dir, 'cats')\nos.mkdir(val_cats_dir)\n\nval_dogs_dir = os.path.join(val_dir, 'dogs')\nos.mkdir(val_dogs_dir)\n\ntest_cats_dir = os.path.join(test_dir, 'cats')\nos.mkdir(test_cats_dir)\n\ntest_dogs_dir = os.path.join(test_dir, 'dogs')\nos.mkdir(test_dogs_dir)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:06.616897Z","iopub.execute_input":"2021-11-24T03:42:06.617228Z","iopub.status.idle":"2021-11-24T03:42:06.626319Z","shell.execute_reply.started":"2021-11-24T03:42:06.61719Z","shell.execute_reply":"2021-11-24T03:42:06.625471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfnames = ['cat.{}.jpg'.format(i) for i in range(10000)]\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir, fname)\n  dst = os.path.join(train_cats_dir, fname)\n  shutil.copyfile(src, dst)\n\nfnames = ['dog.{}.jpg'.format(i) for i in range(10000)]\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir, fname)\n  dst = os.path.join(train_dogs_dir, fname)\n  shutil.copyfile(src, dst)\n\nfnames = ['cat.{}.jpg'.format(i) for i in range(10000, 11000)]\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir, fname)\n  dst = os.path.join(val_cats_dir, fname)\n  shutil.copyfile(src, dst)\n\nfnames = ['dog.{}.jpg'.format(i) for i in range(10000, 11000)]\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir, fname)\n  dst = os.path.join(val_dogs_dir, fname)\n  shutil.copyfile(src, dst)\n\nfnames = ['cat.{}.jpg'.format(i) for i in range(11000, 12000)]\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir, fname)\n  dst = os.path.join(test_cats_dir, fname)\n  shutil.copyfile(src, dst)\n\nfnames = ['dog.{}.jpg'.format(i) for i in range(11000, 12000)]\nfor fname in fnames:\n  src = os.path.join(original_dataset_dir, fname)\n  dst = os.path.join(test_dogs_dir, fname)\n  shutil.copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:06.627762Z","iopub.execute_input":"2021-11-24T03:42:06.628103Z","iopub.status.idle":"2021-11-24T03:42:09.243946Z","shell.execute_reply.started":"2021-11-24T03:42:06.628054Z","shell.execute_reply":"2021-11-24T03:42:09.243164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('number of cat images in train set: ', len(os.listdir(train_cats_dir)))\nprint('number of dog images in train set: ', len(os.listdir(train_dogs_dir)))\n\nprint('number of cat images in validation set: ', len(os.listdir(val_cats_dir)))\nprint('number of dog images in validation set: ', len(os.listdir(val_dogs_dir)))\n\nprint('number of cat images in test set: ', len(os.listdir(test_cats_dir)))\nprint('number of dog images in test set: ', len(os.listdir(test_dogs_dir)))","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:09.250455Z","iopub.execute_input":"2021-11-24T03:42:09.252712Z","iopub.status.idle":"2021-11-24T03:42:09.31638Z","shell.execute_reply.started":"2021-11-24T03:42:09.252674Z","shell.execute_reply":"2021-11-24T03:42:09.300152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n    \ndata_augmentation = tf.keras.Sequential([tf.keras.layers.RandomFlip(\"horizontal\"),\n                                         tf.keras.layers.RandomRotation(0.1),])","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:36.368046Z","iopub.execute_input":"2021-11-24T03:42:36.368315Z","iopub.status.idle":"2021-11-24T03:42:39.478535Z","shell.execute_reply.started":"2021-11-24T03:42:36.368278Z","shell.execute_reply":"2021-11-24T03:42:39.476937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.layers.experimental.preprocessing import RandomFlip, RandomRotation","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:30.726687Z","iopub.execute_input":"2021-11-24T03:42:30.726944Z","iopub.status.idle":"2021-11-24T03:42:36.366506Z","shell.execute_reply.started":"2021-11-24T03:42:30.726914Z","shell.execute_reply":"2021-11-24T03:42:36.365766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess_input = tf.keras.applications.xception.preprocess_input","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:45.164348Z","iopub.execute_input":"2021-11-24T03:42:45.165062Z","iopub.status.idle":"2021-11-24T03:42:45.169188Z","shell.execute_reply.started":"2021-11-24T03:42:45.165023Z","shell.execute_reply":"2021-11-24T03:42:45.168139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nbase_model = Xception(weights='imagenet', input_shape=(150, 150, 3), include_top=False)\nbase_model.trainable = False\ninputs = tf.keras.Input(shape=(150, 150, 3))\nx = data_augmentation(inputs)\nx = preprocess_input(x)\nx = base_model(inputs, training=False)\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx = tf.keras.layers.Dropout(0.2)(x)\noutputs = tf.keras.layers.Dense(1, activation='sigmoid')(x)\nmodel = tf.keras.Model(inputs, outputs)\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:45.691609Z","iopub.execute_input":"2021-11-24T03:42:45.691874Z","iopub.status.idle":"2021-11-24T03:42:47.843619Z","shell.execute_reply.started":"2021-11-24T03:42:45.691844Z","shell.execute_reply":"2021-11-24T03:42:47.842751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:48.782787Z","iopub.execute_input":"2021-11-24T03:42:48.783338Z","iopub.status.idle":"2021-11-24T03:42:48.800186Z","shell.execute_reply.started":"2021-11-24T03:42:48.783298Z","shell.execute_reply":"2021-11-24T03:42:48.799287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:49.924646Z","iopub.execute_input":"2021-11-24T03:42:49.925436Z","iopub.status.idle":"2021-11-24T03:42:49.932836Z","shell.execute_reply.started":"2021-11-24T03:42:49.925393Z","shell.execute_reply":"2021-11-24T03:42:49.932145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./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\nval_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:50.967956Z","iopub.execute_input":"2021-11-24T03:42:50.968532Z","iopub.status.idle":"2021-11-24T03:42:50.974733Z","shell.execute_reply.started":"2021-11-24T03:42:50.968492Z","shell.execute_reply":"2021-11-24T03:42:50.973993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(train_dir,\n                                                    target_size = (150, 150),\n                                                    batch_size = 20,\n                                                    class_mode = 'binary')\n\nvalidation_generator = val_datagen.flow_from_directory(val_dir,\n                                                       target_size = (150, 150),\n                                                       batch_size = 20,\n                                                       class_mode = 'binary')","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:51.754261Z","iopub.execute_input":"2021-11-24T03:42:51.75476Z","iopub.status.idle":"2021-11-24T03:42:52.42049Z","shell.execute_reply.started":"2021-11-24T03:42:51.754721Z","shell.execute_reply":"2021-11-24T03:42:52.419756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(train_generator,\n                              steps_per_epoch = 100,\n                              epochs = 20,\n                              validation_data = validation_generator,\n                              validation_steps = 10)\n\nmodel.save('cats_and_dogs_small_1.h5')","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:42:52.628882Z","iopub.execute_input":"2021-11-24T03:42:52.629122Z","iopub.status.idle":"2021-11-24T03:49:03.61724Z","shell.execute_reply.started":"2021-11-24T03:42:52.629089Z","shell.execute_reply":"2021-11-24T03:49:03.616453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-24T03:49:03.619057Z","iopub.execute_input":"2021-11-24T03:49:03.619303Z","iopub.status.idle":"2021-11-24T03:49:04.21326Z","shell.execute_reply.started":"2021-11-24T03:49:03.619274Z","shell.execute_reply":"2021-11-24T03:49:04.212535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}