{"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)\nimport matplotlib.pyplot as plt\n\nfrom keras import layers\nfrom keras import models\nfrom keras import optimizers\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing import image\nfrom keras.applications import VGG16","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-23T09:24:42.823348Z","iopub.execute_input":"2021-07-23T09:24:42.823683Z","iopub.status.idle":"2021-07-23T09:24:42.828456Z","shell.execute_reply.started":"2021-07-23T09:24:42.823652Z","shell.execute_reply":"2021-07-23T09:24:42.827517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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 zipfile\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n\nimport os, 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":{"execution":{"iopub.status.busy":"2021-07-23T08:41:21.172018Z","iopub.execute_input":"2021-07-23T08:41:21.172522Z","iopub.status.idle":"2021-07-23T08:41:37.632522Z","shell.execute_reply.started":"2021-07-23T08:41:21.172482Z","shell.execute_reply":"2021-07-23T08:41:37.631598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_dataset_dir = '../working/train'\nbase_dir = '../working/cats_and_dogs_small'","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:41:37.636939Z","iopub.execute_input":"2021-07-23T08:41:37.637489Z","iopub.status.idle":"2021-07-23T08:41:37.641539Z","shell.execute_reply.started":"2021-07-23T08:41:37.637445Z","shell.execute_reply":"2021-07-23T08:41:37.640710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(base_dir)\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)\ntrain_cats_dir = os.path.join(train_dir, 'cats')\nos.mkdir(train_cats_dir)\ntrain_dogs_dir = os.path.join(train_dir, 'dogs')\nos.mkdir(train_dogs_dir)\nvalidation_cats_dir = os.path.join(validation_dir, 'cats')\nos.mkdir(validation_cats_dir)\nvalidation_dogs_dir = os.path.join(validation_dir, 'dogs')\nos.mkdir(validation_dogs_dir)\ntest_cats_dir = os.path.join(test_dir, 'cats')\nos.mkdir(test_cats_dir)\ntest_dogs_dir = os.path.join(test_dir, 'dogs')\nos.mkdir(test_dogs_dir)\n\nfnames = ['cat.{}.jpg'.format(i) for i in range(1000)]\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)\nfnames = ['cat.{}.jpg'.format(i) for i in range(1000, 1500)]\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)\nfnames = ['cat.{}.jpg'.format(i) for i in range(1500, 2000)]\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)\nfnames = ['dog.{}.jpg'.format(i) for i in range(1000)]\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)\nfnames = ['dog.{}.jpg'.format(i) for i in range(1000, 1500)]\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)\nfnames = ['dog.{}.jpg'.format(i) for i in range(1500, 2000)]\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-07-23T08:41:37.642775Z","iopub.execute_input":"2021-07-23T08:41:37.643272Z","iopub.status.idle":"2021-07-23T08:41:38.172146Z","shell.execute_reply.started":"2021-07-23T08:41:37.643232Z","shell.execute_reply":"2021-07-23T08:41:38.171293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('total training cat images:', len(os.listdir(train_cats_dir)))\nprint('total training dog images:', len(os.listdir(train_dogs_dir)))\nprint('total validation cat images:', len(os.listdir(validation_cats_dir)))\nprint('total validation dog images:', len(os.listdir(validation_dogs_dir)))\nprint('total test cat images:', len(os.listdir(test_cats_dir)))\nprint('total test dog images:', len(os.listdir(test_dogs_dir)))","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:41:38.173433Z","iopub.execute_input":"2021-07-23T08:41:38.173862Z","iopub.status.idle":"2021-07-23T08:41:38.186255Z","shell.execute_reply.started":"2021-07-23T08:41:38.173818Z","shell.execute_reply":"2021-07-23T08:41:38.185146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Convnet","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu',\ninput_shape=(150, 150, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:41:38.187897Z","iopub.execute_input":"2021-07-23T08:41:38.188338Z","iopub.status.idle":"2021-07-23T08:41:40.510897Z","shell.execute_reply.started":"2021-07-23T08:41:38.188300Z","shell.execute_reply":"2021-07-23T08:41:40.510049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:41:40.521526Z","iopub.execute_input":"2021-07-23T08:41:40.521871Z","iopub.status.idle":"2021-07-23T08:41:40.535393Z","shell.execute_reply.started":"2021-07-23T08:41:40.521835Z","shell.execute_reply":"2021-07-23T08:41:40.534263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\noptimizer=optimizers.RMSprop(lr=1e-4),\nmetrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:41:40.536871Z","iopub.execute_input":"2021-07-23T08:41:40.537255Z","iopub.status.idle":"2021-07-23T08:41:40.959595Z","shell.execute_reply.started":"2021-07-23T08:41:40.537215Z","shell.execute_reply":"2021-07-23T08:41:40.958742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150, 150),\n    batch_size=20,\n    class_mode='binary')\nvalidation_generator = test_datagen.flow_from_directory(\n    validation_dir,\n    target_size=(150, 150),\n    batch_size=20,\n    class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:41:40.961143Z","iopub.execute_input":"2021-07-23T08:41:40.961527Z","iopub.status.idle":"2021-07-23T08:41:41.442342Z","shell.execute_reply.started":"2021-07-23T08:41:40.961491Z","shell.execute_reply":"2021-07-23T08:41:41.441416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train_generator:\n    print('data batch shape:', data_batch.shape)\n    print('labels batch shape:', labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:41:41.443668Z","iopub.execute_input":"2021-07-23T08:41:41.444264Z","iopub.status.idle":"2021-07-23T08:41:41.538368Z","shell.execute_reply.started":"2021-07-23T08:41:41.444224Z","shell.execute_reply":"2021-07-23T08:41:41.535580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model fitting and validation","metadata":{}},{"cell_type":"code","source":"history = model.fit_generator(\n    train_generator,\n    steps_per_epoch=100,\n    epochs=30,\n    validation_data=validation_generator,\n    validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:41:41.539862Z","iopub.execute_input":"2021-07-23T08:41:41.540211Z","iopub.status.idle":"2021-07-23T08:46:22.433391Z","shell.execute_reply.started":"2021-07-23T08:41:41.540174Z","shell.execute_reply":"2021-07-23T08:46:22.432354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cats_and_dogs_small_1.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:46:22.435383Z","iopub.execute_input":"2021-07-23T08:46:22.435831Z","iopub.status.idle":"2021-07-23T08:46:22.509835Z","shell.execute_reply.started":"2021-07-23T08:46:22.435764Z","shell.execute_reply":"2021-07-23T08:46:22.508871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\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()\nplt.figure()\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-07-23T08:46:22.511244Z","iopub.execute_input":"2021-07-23T08:46:22.511598Z","iopub.status.idle":"2021-07-23T08:46:22.849985Z","shell.execute_reply.started":"2021-07-23T08:46:22.511560Z","shell.execute_reply":"2021-07-23T08:46:22.848907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data augmentation","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\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    fill_mode='nearest')","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:46:22.851508Z","iopub.execute_input":"2021-07-23T08:46:22.851928Z","iopub.status.idle":"2021-07-23T08:46:22.857648Z","shell.execute_reply.started":"2021-07-23T08:46:22.851880Z","shell.execute_reply":"2021-07-23T08:46:22.856737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = [os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)]\nimg_path = fnames[3]\nimg = image.load_img(img_path, target_size=(150, 150))\nx = image.img_to_array(img)\nx = x.reshape((1,) + x.shape)\ni = 0\nfor batch in datagen.flow(x, batch_size=1):\n    plt.figure(i)\n    imgplot = plt.imshow(image.array_to_img(batch[0]))\n    i += 1\n    if i % 4 == 0:\n        break\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:46:22.859302Z","iopub.execute_input":"2021-07-23T08:46:22.859889Z","iopub.status.idle":"2021-07-23T08:46:23.577961Z","shell.execute_reply.started":"2021-07-23T08:46:22.859849Z","shell.execute_reply":"2021-07-23T08:46:23.576941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dropout model with data augmentation","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu',\n    input_shape=(150, 150, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy',\n    optimizer=optimizers.RMSprop(lr=1e-4),\n    metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-07-23T08:46:23.579285Z","iopub.execute_input":"2021-07-23T08:46:23.579768Z","iopub.status.idle":"2021-07-23T08:46:23.673175Z","shell.execute_reply.started":"2021-07-23T08:46:23.579729Z","shell.execute_reply":"2021-07-23T08:46:23.672239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    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,)\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150, 150),\n    batch_size=20,\n    class_mode='binary')\nvalidation_generator = test_datagen.flow_from_directory(\n    validation_dir,\n    target_size=(150, 150),\n    batch_size=20,\n    class_mode='binary')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:14:35.554774Z","iopub.execute_input":"2021-07-23T09:14:35.555142Z","iopub.status.idle":"2021-07-23T09:14:35.775119Z","shell.execute_reply.started":"2021-07-23T09:14:35.555114Z","shell.execute_reply":"2021-07-23T09:14:35.772199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch, labels_batch in train_generator:\n    print('data batch shape:', data_batch.shape)\n    print('labels batch shape:', labels_batch.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:14:35.905149Z","iopub.execute_input":"2021-07-23T09:14:35.905462Z","iopub.status.idle":"2021-07-23T09:14:36.051951Z","shell.execute_reply.started":"2021-07-23T09:14:35.905435Z","shell.execute_reply":"2021-07-23T09:14:36.051035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n    train_generator,\n    steps_per_epoch=100,\n    epochs=100,\n    validation_data=validation_generator,\n    validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:21:57.551023Z","iopub.execute_input":"2021-07-23T09:21:57.551371Z","iopub.status.idle":"2021-07-23T09:23:31.806173Z","shell.execute_reply.started":"2021-07-23T09:21:57.551341Z","shell.execute_reply":"2021-07-23T09:23:31.805372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cats_and_dogs_small_2.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:23:31.808008Z","iopub.execute_input":"2021-07-23T09:23:31.808368Z","iopub.status.idle":"2021-07-23T09:23:31.902839Z","shell.execute_reply.started":"2021-07-23T09:23:31.808322Z","shell.execute_reply":"2021-07-23T09:23:31.901944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\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()\nplt.figure()\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-07-23T09:23:31.904567Z","iopub.execute_input":"2021-07-23T09:23:31.904969Z","iopub.status.idle":"2021-07-23T09:23:32.253588Z","shell.execute_reply.started":"2021-07-23T09:23:31.904916Z","shell.execute_reply":"2021-07-23T09:23:32.252639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## VGG16","metadata":{}},{"cell_type":"code","source":"conv_base = VGG16(weights='imagenet',\ninclude_top=False,\ninput_shape=(150, 150, 3))","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:25:17.272376Z","iopub.execute_input":"2021-07-23T09:25:17.272723Z","iopub.status.idle":"2021-07-23T09:25:18.283527Z","shell.execute_reply.started":"2021-07-23T09:25:17.272695Z","shell.execute_reply":"2021-07-23T09:25:18.282559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_base.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:25:18.285394Z","iopub.execute_input":"2021-07-23T09:25:18.285761Z","iopub.status.idle":"2021-07-23T09:25:18.303661Z","shell.execute_reply.started":"2021-07-23T09:25:18.285720Z","shell.execute_reply":"2021-07-23T09:25:18.302594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '../working/cats_and_dogs_small'\ntrain_dir = os.path.join(base_dir, 'train')\nvalidation_dir = os.path.join(base_dir, 'validation')\ntest_dir = os.path.join(base_dir, 'test')\n\ndatagen = ImageDataGenerator(rescale=1./255)\nbatch_size = 20\n\ndef extract_features(directory, sample_count):\n    features = np.zeros(shape=(sample_count, 4, 4, 512))\n    labels = np.zeros(shape=(sample_count))\n    generator = datagen.flow_from_directory(\n        directory,\n        target_size=(150, 150),\n        batch_size=batch_size,\n        class_mode='binary')\n    i = 0\n    for inputs_batch, labels_batch in generator:\n        features_batch = conv_base.predict(inputs_batch)\n        features[i * batch_size : (i + 1) * batch_size] = features_batch\n        labels[i * batch_size : (i + 1) * batch_size] = labels_batch\n        i += 1\n        if i * batch_size >= sample_count:\n            break\n    return features, labels\n\ntrain_features, train_labels = extract_features(train_dir, 2000)\nvalidation_features, validation_labels = extract_features(validation_dir, 1000)\ntest_features, test_labels = extract_features(test_dir, 1000)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:30:05.001171Z","iopub.execute_input":"2021-07-23T09:30:05.001499Z","iopub.status.idle":"2021-07-23T09:30:31.597291Z","shell.execute_reply.started":"2021-07-23T09:30:05.001471Z","shell.execute_reply":"2021-07-23T09:30:31.596385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features = np.reshape(train_features, (2000, 4 * 4 * 512))\nvalidation_features = np.reshape(validation_features, (1000, 4 * 4 * 512))\ntest_features = np.reshape(test_features, (1000, 4 * 4 * 512))","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:32:43.831217Z","iopub.execute_input":"2021-07-23T09:32:43.831559Z","iopub.status.idle":"2021-07-23T09:32:43.837651Z","shell.execute_reply.started":"2021-07-23T09:32:43.831526Z","shell.execute_reply":"2021-07-23T09:32:43.835818Z"},"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))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(1, activation='sigmoid'))\nmodel.compile(optimizer=optimizers.RMSprop(lr=2e-5),\n    loss='binary_crossentropy',\n    metrics=['acc'])\nhistory = model.fit(train_features, train_labels,\n    epochs=30,\n    batch_size=20,\n    validation_data=(validation_features, validation_labels))","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:34:29.531717Z","iopub.execute_input":"2021-07-23T09:34:29.532087Z","iopub.status.idle":"2021-07-23T09:34:41.811181Z","shell.execute_reply.started":"2021-07-23T09:34:29.532058Z","shell.execute_reply":"2021-07-23T09:34:41.810237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\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()\nplt.figure()\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-07-23T09:35:03.345306Z","iopub.execute_input":"2021-07-23T09:35:03.345633Z","iopub.status.idle":"2021-07-23T09:35:03.677632Z","shell.execute_reply.started":"2021-07-23T09:35:03.345603Z","shell.execute_reply":"2021-07-23T09:35:03.676455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## VGG16 with data augmentation","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(conv_base)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:38:08.403433Z","iopub.execute_input":"2021-07-23T09:38:08.403761Z","iopub.status.idle":"2021-07-23T09:38:08.475031Z","shell.execute_reply.started":"2021-07-23T09:38:08.403730Z","shell.execute_reply":"2021-07-23T09:38:08.474150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:38:14.826843Z","iopub.execute_input":"2021-07-23T09:38:14.827163Z","iopub.status.idle":"2021-07-23T09:38:14.836838Z","shell.execute_reply.started":"2021-07-23T09:38:14.827136Z","shell.execute_reply":"2021-07-23T09:38:14.835926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('This is the number of trainable weights '\n'before freezing the conv base:', len(model.trainable_weights))\nconv_base.trainable = False\nprint('This is the number of trainable weights '\n'after freezing the conv base:', len(model.trainable_weights))","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:39:11.276598Z","iopub.execute_input":"2021-07-23T09:39:11.276987Z","iopub.status.idle":"2021-07-23T09:39:11.290829Z","shell.execute_reply.started":"2021-07-23T09:39:11.276953Z","shell.execute_reply":"2021-07-23T09:39:11.289874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    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    fill_mode='nearest')\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150, 150),\n    batch_size=20,\n    class_mode='binary')\nvalidation_generator = test_datagen.flow_from_directory(\n    validation_dir,\n    target_size=(150, 150),\n    batch_size=20,\n    class_mode='binary')\nmodel.compile(loss='binary_crossentropy',\n    optimizer=optimizers.RMSprop(lr=2e-5),\n    metrics=['acc'])\nhistory = model.fit_generator(\n    train_generator,\n    steps_per_epoch=100,\n    epochs=30,\n    validation_data=validation_generator,\n    validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:40:41.632171Z","iopub.execute_input":"2021-07-23T09:40:41.632529Z","iopub.status.idle":"2021-07-23T09:50:28.391451Z","shell.execute_reply.started":"2021-07-23T09:40:41.632498Z","shell.execute_reply":"2021-07-23T09:50:28.390499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\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()\nplt.figure()\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-07-23T09:50:28.393266Z","iopub.execute_input":"2021-07-23T09:50:28.393643Z","iopub.status.idle":"2021-07-23T09:50:28.706872Z","shell.execute_reply.started":"2021-07-23T09:50:28.393603Z","shell.execute_reply":"2021-07-23T09:50:28.705875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fine-tuning","metadata":{}},{"cell_type":"code","source":"conv_base.trainable = True\nset_trainable = False\nfor layer in conv_base.layers:\n    if layer.name == 'block5_conv1':\n        set_trainable = True\n    if set_trainable:\n        layer.trainable = True\n    else:\n        layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:51:44.196930Z","iopub.execute_input":"2021-07-23T09:51:44.197254Z","iopub.status.idle":"2021-07-23T09:51:44.203085Z","shell.execute_reply.started":"2021-07-23T09:51:44.197225Z","shell.execute_reply":"2021-07-23T09:51:44.202199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n    optimizer=optimizers.RMSprop(lr=1e-5),\n    metrics=['acc'])\nhistory = model.fit_generator(\n    train_generator,\n    steps_per_epoch=100,\n    epochs=100,\n    validation_data=validation_generator,\n    validation_steps=50)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:52:00.215732Z","iopub.execute_input":"2021-07-23T09:52:00.216118Z","iopub.status.idle":"2021-07-23T09:55:19.372710Z","shell.execute_reply.started":"2021-07-23T09:52:00.216087Z","shell.execute_reply":"2021-07-23T09:55:19.371877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\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()\nplt.figure()\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-07-23T09:55:19.376916Z","iopub.execute_input":"2021-07-23T09:55:19.377182Z","iopub.status.idle":"2021-07-23T09:55:19.662583Z","shell.execute_reply.started":"2021-07-23T09:55:19.377156Z","shell.execute_reply":"2021-07-23T09:55:19.661551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def smooth_curve(points, factor=0.8):\n    smoothed_points = []\n    for point in points:\n        if smoothed_points:\n            previous = smoothed_points[-1]\n            smoothed_points.append(previous * factor + point * (1 - factor))\n        else:\n            smoothed_points.append(point)\n    return smoothed_points\n\nplt.plot(epochs,\nsmooth_curve(acc), 'bo', label='Smoothed training acc')\nplt.plot(epochs,\nsmooth_curve(val_acc), 'b', label='Smoothed validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\nplt.plot(epochs,\nsmooth_curve(loss), 'bo', label='Smoothed training loss')\nplt.plot(epochs,\nsmooth_curve(val_loss), 'b', label='Smoothed validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:55:38.347661Z","iopub.execute_input":"2021-07-23T09:55:38.348065Z","iopub.status.idle":"2021-07-23T09:55:38.989561Z","shell.execute_reply.started":"2021-07-23T09:55:38.348035Z","shell.execute_reply":"2021-07-23T09:55:38.988485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = test_datagen.flow_from_directory(\n    test_dir,\n    target_size=(150, 150),\n    batch_size=20,\n    class_mode='binary')\ntest_loss, test_acc = model.evaluate_generator(test_generator, steps=50)\nprint('test acc:', test_acc)","metadata":{"execution":{"iopub.status.busy":"2021-07-23T09:55:19.966822Z","iopub.execute_input":"2021-07-23T09:55:19.967187Z","iopub.status.idle":"2021-07-23T09:55:23.186038Z","shell.execute_reply.started":"2021-07-23T09:55:19.967151Z","shell.execute_reply":"2021-07-23T09:55:23.185076Z"},"trusted":true},"execution_count":null,"outputs":[]}]}