{"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\nimport shutil\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","_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-12-29T08:27:21.934314Z","iopub.execute_input":"2021-12-29T08:27:21.935166Z","iopub.status.idle":"2021-12-29T08:27:21.969797Z","shell.execute_reply.started":"2021-12-29T08:27:21.935036Z","shell.execute_reply":"2021-12-29T08:27:21.969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r ./*","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:12.723318Z","iopub.execute_input":"2021-12-29T08:29:12.723657Z","iopub.status.idle":"2021-12-29T08:29:13.481985Z","shell.execute_reply.started":"2021-12-29T08:29:12.723621Z","shell.execute_reply":"2021-12-29T08:29:13.481011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip ../input/dogs-vs-cats/train.zip -d ./new_data","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2021-12-29T08:29:16.971722Z","iopub.execute_input":"2021-12-29T08:29:16.972379Z","iopub.status.idle":"2021-12-29T08:29:29.808212Z","shell.execute_reply.started":"2021-12-29T08:29:16.972341Z","shell.execute_reply":"2021-12-29T08:29:29.806942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The path to the directory where the original\n# dataset was uncompressed\noriginal_dataset_dir = './new_data/train'","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:29.810475Z","iopub.execute_input":"2021-12-29T08:29:29.810734Z","iopub.status.idle":"2021-12-29T08:29:29.814969Z","shell.execute_reply.started":"2021-12-29T08:29:29.810704Z","shell.execute_reply":"2021-12-29T08:29:29.814037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The directory where we will\n# store our smaller dataset\nbase_dir = './new_data_base'\nos.mkdir(base_dir)\n\n# Directories for our training,\n# validation and test splits\ntrain_dir = os.path.join(base_dir, 'train')\nos.mkdir(train_dir)\nvalidation_dir = os.path.join(base_dir, 'validation')\nos.mkdir(validation_dir)\ntest_dir = os.path.join(base_dir, 'test')\nos.mkdir(test_dir)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:29.816161Z","iopub.execute_input":"2021-12-29T08:29:29.81671Z","iopub.status.idle":"2021-12-29T08:29:29.829734Z","shell.execute_reply.started":"2021-12-29T08:29:29.816673Z","shell.execute_reply":"2021-12-29T08:29:29.828964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Directory with our training cat pictures\ntrain_cats_dir = os.path.join(train_dir, 'cats')\nos.mkdir(train_cats_dir)\n\n# Directory with our training dog pictures\ntrain_dogs_dir = os.path.join(train_dir, 'dogs')\nos.mkdir(train_dogs_dir)\n\n# Directory with our validation cat pictures\nvalidation_cats_dir = os.path.join(validation_dir, 'cats')\nos.mkdir(validation_cats_dir)\n\n# Directory with our validation dog pictures\nvalidation_dogs_dir = os.path.join(validation_dir, 'dogs')\nos.mkdir(validation_dogs_dir)\n\n# Directory with our validation cat pictures\ntest_cats_dir = os.path.join(test_dir, 'cats')\nos.mkdir(test_cats_dir)\n\n# Directory with our validation dog pictures\ntest_dogs_dir = os.path.join(test_dir, 'dogs')\nos.mkdir(test_dogs_dir)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:29.831248Z","iopub.execute_input":"2021-12-29T08:29:29.831988Z","iopub.status.idle":"2021-12-29T08:29:29.842179Z","shell.execute_reply.started":"2021-12-29T08:29:29.831956Z","shell.execute_reply":"2021-12-29T08:29:29.841109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy first 1000 cat images to train_cats_dir\nfnames = ['cat.{}.jpg'.format(i) for i in range(1000)]\n\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)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:29.948421Z","iopub.execute_input":"2021-12-29T08:29:29.948699Z","iopub.status.idle":"2021-12-29T08:29:30.105236Z","shell.execute_reply.started":"2021-12-29T08:29:29.948671Z","shell.execute_reply":"2021-12-29T08:29:30.104516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy next 500 cat images to validation_cats_dir\nfnames = ['cat.{}.jpg'.format(i) for i in range(1000, 1500)]\n\nfor fname in fnames:\n    src = os.path.join(original_dataset_dir, fname)\n    dst = os.path.join(validation_cats_dir, fname)\n    shutil.copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:30.686952Z","iopub.execute_input":"2021-12-29T08:29:30.688123Z","iopub.status.idle":"2021-12-29T08:29:30.771893Z","shell.execute_reply.started":"2021-12-29T08:29:30.688062Z","shell.execute_reply":"2021-12-29T08:29:30.770554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy next 500 cat images to test_cats_dir\nfnames = ['cat.{}.jpg'.format(i) for i in range(1500, 2000)]\n\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)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:30.980301Z","iopub.execute_input":"2021-12-29T08:29:30.980606Z","iopub.status.idle":"2021-12-29T08:29:31.062688Z","shell.execute_reply.started":"2021-12-29T08:29:30.980578Z","shell.execute_reply":"2021-12-29T08:29:31.061631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy first 1000 dog images to train_dogs_dir\nfnames = ['dog.{}.jpg'.format(i) for i in range(1000)]\n\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)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:31.622553Z","iopub.execute_input":"2021-12-29T08:29:31.622833Z","iopub.status.idle":"2021-12-29T08:29:31.795759Z","shell.execute_reply.started":"2021-12-29T08:29:31.622804Z","shell.execute_reply":"2021-12-29T08:29:31.794629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy next 500 dog images to validation_dogs_dir\nfnames = ['dog.{}.jpg'.format(i) for i in range(1000, 1500)]\n\nfor fname in fnames:\n    src = os.path.join(original_dataset_dir, fname)\n    dst = os.path.join(validation_dogs_dir, fname)\n    shutil.copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:32.945622Z","iopub.execute_input":"2021-12-29T08:29:32.945911Z","iopub.status.idle":"2021-12-29T08:29:33.033808Z","shell.execute_reply.started":"2021-12-29T08:29:32.945881Z","shell.execute_reply":"2021-12-29T08:29:33.032755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy next 500 dog images to test_dogs_dir\nfnames = ['dog.{}.jpg'.format(i) for i in range(1500, 2000)]\n\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-12-29T08:29:45.327994Z","iopub.execute_input":"2021-12-29T08:29:45.32834Z","iopub.status.idle":"2021-12-29T08:29:45.400712Z","shell.execute_reply.started":"2021-12-29T08:29:45.328306Z","shell.execute_reply":"2021-12-29T08:29:45.39976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('total training dog images:', len(os.listdir(train_dogs_dir)))","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:29:50.185339Z","iopub.execute_input":"2021-12-29T08:29:50.185679Z","iopub.status.idle":"2021-12-29T08:29:50.191948Z","shell.execute_reply.started":"2021-12-29T08:29:50.185644Z","shell.execute_reply":"2021-12-29T08:29:50.191296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers\nfrom tensorflow.keras import models","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:30:15.174294Z","iopub.execute_input":"2021-12-29T08:30:15.174619Z","iopub.status.idle":"2021-12-29T08:30:23.659679Z","shell.execute_reply.started":"2021-12-29T08:30:15.174587Z","shell.execute_reply":"2021-12-29T08:30:23.658523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\n\nconv_base = VGG16(weights='imagenet',\n                  include_top=False, \n                  input_shape=(150, 150, 3))\n\nconv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:35:51.050874Z","iopub.execute_input":"2021-12-29T08:35:51.051218Z","iopub.status.idle":"2021-12-29T08:35:51.55832Z","shell.execute_reply.started":"2021-12-29T08:35:51.051185Z","shell.execute_reply":"2021-12-29T08:35:51.556913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(rescale=1./255)\nbatch_size = 20","metadata":{"execution":{"iopub.status.busy":"2021-12-29T08:44:50.393028Z","iopub.execute_input":"2021-12-29T08:44:50.393369Z","iopub.status.idle":"2021-12-29T08:44:50.399969Z","shell.execute_reply.started":"2021-12-29T08:44:50.393333Z","shell.execute_reply":"2021-12-29T08:44:50.399409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_features(directory, sample_count):\n    features = np.zeros(shape=(sample_count, 4, 4, 512))\n    labels = np.zeros(shape=(sample_count))\n    \n    generator = datagen.flow_from_directory(\n        directory,\n        target_size=(150, 150),\n        batch_size=batch_size,\n        class_mode='binary')\n    \n    i = 0\n    \n    for inputs_batch, labels_batch in generator:\n        features_batch = conv_base.predict(inputs_batch)\n        \n        features[i * batch_size : (i + 1) * batch_size] = features_batch\n        labels[i * batch_size : (i + 1) * batch_size] = labels_batch\n        \n        i += 1\n        if i * batch_size >= sample_count:\n            # Note that since generators yield data indefinitely in a loop, \n            # we must `break` after every image has been seen once.\n            break\n    return features, labels","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:25:24.886872Z","iopub.execute_input":"2021-12-29T09:25:24.887313Z","iopub.status.idle":"2021-12-29T09:25:24.896433Z","shell.execute_reply.started":"2021-12-29T09:25:24.887249Z","shell.execute_reply":"2021-12-29T09:25:24.895602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features, train_labels = extract_features(train_dir, 2000)\nvalidation_features, validation_labels = extract_features(validation_dir, 1000)\ntest_feature, test_labels = extract_features(test_dir, 1000)","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:27:52.681564Z","iopub.execute_input":"2021-12-29T09:27:52.681857Z","iopub.status.idle":"2021-12-29T09:35:45.075874Z","shell.execute_reply.started":"2021-12-29T09:27:52.681823Z","shell.execute_reply":"2021-12-29T09:35:45.074622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# flattening our input data for dense layers\ntrain_features = np.reshape(train_features, (2000, 4 * 4 * 512))\nvalidation_features = np.reshape(validation_features, (1000, 4 * 4 * 512))\ntest_feature = np.reshape(test_feature, (1000, 4 * 4 * 512))","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:36:57.115844Z","iopub.execute_input":"2021-12-29T09:36:57.116498Z","iopub.status.idle":"2021-12-29T09:36:57.123599Z","shell.execute_reply.started":"2021-12-29T09:36:57.116459Z","shell.execute_reply":"2021-12-29T09:36:57.122223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Dense(256, activation=\"relu\", input_dim = 4 * 4 * 512))\n\n# adding Dropout layer for regularization\nmodel.add(layers.Dropout(0.5))\n\nmodel.add(layers.Dense(1, activation=\"sigmoid\"))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:37:01.964385Z","iopub.execute_input":"2021-12-29T09:37:01.964973Z","iopub.status.idle":"2021-12-29T09:37:02.011854Z","shell.execute_reply.started":"2021-12-29T09:37:01.964922Z","shell.execute_reply":"2021-12-29T09:37:02.010806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import optimizers\n\nmodel.compile(\n    loss=\"binary_crossentropy\", \n    optimizer=optimizers.RMSprop(lr=2e-5), \n    metrics=[\"acc\"])","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:37:06.321936Z","iopub.execute_input":"2021-12-29T09:37:06.322203Z","iopub.status.idle":"2021-12-29T09:37:06.339227Z","shell.execute_reply.started":"2021-12-29T09:37:06.322176Z","shell.execute_reply":"2021-12-29T09:37:06.3384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_features, train_labels, \n    epochs=30, \n    batch_size=20,\n    validation_data=(validation_features, validation_labels))","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:39:19.264654Z","iopub.execute_input":"2021-12-29T09:39:19.265221Z","iopub.status.idle":"2021-12-29T09:40:09.189567Z","shell.execute_reply.started":"2021-12-29T09:39:19.265182Z","shell.execute_reply":"2021-12-29T09:40:09.188536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"./cats_and_dogs_vgg16.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:56:40.840319Z","iopub.execute_input":"2021-12-29T09:56:40.840625Z","iopub.status.idle":"2021-12-29T09:56:40.884434Z","shell.execute_reply.started":"2021-12-29T09:56:40.840595Z","shell.execute_reply":"2021-12-29T09:56:40.883631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:40:09.192326Z","iopub.execute_input":"2021-12-29T09:40:09.192782Z","iopub.status.idle":"2021-12-29T09:40:09.199479Z","shell.execute_reply.started":"2021-12-29T09:40:09.192731Z","shell.execute_reply":"2021-12-29T09:40:09.198174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = range(1, len(acc) + 1)\n\nplt.plot(epochs, acc, 'bo', label='Training acc') \nplt.plot(epochs, val_acc, 'b', label='Validation acc')\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()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-29T09:40:09.201037Z","iopub.execute_input":"2021-12-29T09:40:09.201454Z","iopub.status.idle":"2021-12-29T09:40:09.684412Z","shell.execute_reply.started":"2021-12-29T09:40:09.201416Z","shell.execute_reply":"2021-12-29T09:40:09.683702Z"},"trusted":true},"execution_count":null,"outputs":[]}]}