{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":7497077,"sourceType":"datasetVersion","datasetId":4365405}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"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":"2024-01-29T08:22:08.162982Z","iopub.execute_input":"2024-01-29T08:22:08.163453Z","iopub.status.idle":"2024-01-29T08:22:08.528608Z","shell.execute_reply.started":"2024-01-29T08:22:08.163423Z","shell.execute_reply":"2024-01-29T08:22:08.527663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))\n\nimport zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:22:08.530269Z","iopub.execute_input":"2024-01-29T08:22:08.530659Z","iopub.status.idle":"2024-01-29T08:22:23.054104Z","shell.execute_reply.started":"2024-01-29T08:22:08.530633Z","shell.execute_reply":"2024-01-29T08:22:23.053329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Dataset Preparation**","metadata":{}},{"cell_type":"code","source":"# plot dog photos from the dogs vs cats dataset\nfrom matplotlib import pyplot\nfrom matplotlib.image import imread\ntrain_folder = '/kaggle/working/train/'\n\n# define location of dataset\nfolder = train_folder\n# plot first few images\nfor i in range(9):\n\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'dog.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tpyplot.imshow(image)\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T03:59:56.967341Z","iopub.execute_input":"2024-01-29T03:59:56.967636Z","iopub.status.idle":"2024-01-29T03:59:58.128527Z","shell.execute_reply.started":"2024-01-29T03:59:56.967610Z","shell.execute_reply":"2024-01-29T03:59:58.127731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot cat photos from the dogs vs cats dataset\nfrom matplotlib import pyplot\nfrom matplotlib.image import imread\n# define location of dataset\nfolder = 'train/'\n# plot first few images\nfor i in range(9):\n # define subplot\n pyplot.subplot(330 + 1 + i)\n # define filename\n filename = folder + 'cat.' + str(i) + '.jpg'\n # load image pixels\n image = imread(filename)\n # plot raw pixel data\n pyplot.imshow(image)\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T03:59:58.131260Z","iopub.execute_input":"2024-01-29T03:59:58.131538Z","iopub.status.idle":"2024-01-29T03:59:59.401086Z","shell.execute_reply.started":"2024-01-29T03:59:58.131512Z","shell.execute_reply":"2024-01-29T03:59:59.400228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Pre-Process Photo Sizes**","metadata":{}},{"cell_type":"code","source":"# load dogs vs cats dataset, reshape and save to a new file\nfrom os import listdir\nfrom numpy import asarray\nfrom numpy import save\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\n# define location of dataset\nfolder = 'train/'\nphotos, labels = list(), list()\n# enumerate files in the directory\nfor file in listdir(folder):\n    \n     # determine class\n     output = 0.0\n     if file.startswith('dog'):\n         output = 1.0\n         # load image\n         photo = load_img(folder + file, target_size=(200, 200))\n         # convert to numpy array\n         photo = img_to_array(photo)\n         # store\n         photos.append(photo)\n         labels.append(output)\n# convert to a numpy arrays\nphotos = asarray(photos)\nlabels = asarray(labels)\nprint(photos.shape, labels.shape)\n# save the reshaped photos\nsave('dogs_vs_cats_photos.npy', photos)\nsave('dogs_vs_cats_labels.npy', labels)","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:22:23.055214Z","iopub.execute_input":"2024-01-29T08:22:23.055491Z","iopub.status.idle":"2024-01-29T08:23:09.813657Z","shell.execute_reply.started":"2024-01-29T08:22:23.055466Z","shell.execute_reply":"2024-01-29T08:23:09.812789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load and confirm the shape\nfrom numpy import load\nphotos = load('dogs_vs_cats_photos.npy')\nlabels = load('dogs_vs_cats_labels.npy')\nprint(photos.shape, labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:23:09.816144Z","iopub.execute_input":"2024-01-29T08:23:09.816773Z","iopub.status.idle":"2024-01-29T08:23:19.708952Z","shell.execute_reply.started":"2024-01-29T08:23:09.816744Z","shell.execute_reply":"2024-01-29T08:23:19.708025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# # create directories\n# dataset_home = 'dataset_dogs_vs_cats/'\n# subdirs = ['train/', 'test/']\n# for subdir in subdirs:\n#  # create label subdirectories\n#  labeldirs = ['dogs/', 'cats/']\n#  for labldir in labeldirs:\n#      newdir = dataset_home + subdir + labldir\n#      os.makedirs(newdir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:23:19.710222Z","iopub.execute_input":"2024-01-29T08:23:19.710598Z","iopub.status.idle":"2024-01-29T08:23:19.715161Z","shell.execute_reply.started":"2024-01-29T08:23:19.710561Z","shell.execute_reply":"2024-01-29T08:23:19.714263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:23:19.716473Z","iopub.execute_input":"2024-01-29T08:23:19.717197Z","iopub.status.idle":"2024-01-29T08:23:20.502469Z","shell.execute_reply.started":"2024-01-29T08:23:19.717162Z","shell.execute_reply":"2024-01-29T08:23:20.501445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import shutil\n# # seed random number generator\n# random.seed(1)\n# # define ratio of pictures to use for validation\n# val_ratio = 0.25\n# # copy training dataset images into subdirectories\n# src_directory = 'train/'\n# for file in listdir(src_directory):\n#  src = src_directory + '/' + file\n#  dst_dir = 'train/'\n#  if random.random() < val_ratio:\n#      dst_dir = 'test/'\n#  if file.startswith('cat'):\n#      dst = dataset_home + dst_dir + 'cats/'  + file\n#      shutil.copy(src, dst)\n#  elif file.startswith('dog'):\n#      dst = dataset_home + dst_dir + 'dogs/'  + file\n#      shutil.copy(src, dst)","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:51.873878Z","iopub.execute_input":"2024-01-29T04:00:51.874126Z","iopub.status.idle":"2024-01-29T04:00:53.043663Z","shell.execute_reply.started":"2024-01-29T04:00:51.874104Z","shell.execute_reply":"2024-01-29T04:00:53.042830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# organize dataset into a useful structure\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random\n# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\nfor subdir in subdirs:\n # create label subdirectories\n labeldirs = ['dogs/', 'cats/']\n for labldir in labeldirs:\n     newdir = dataset_home + subdir + labldir\n     os.makedirs(newdir, exist_ok=True)\n# seed random number generator\n#random.seed(1)\n# define ratio of pictures to use for validation\nval_ratio = 0.25\n# copy training dataset images into subdirectories\nsrc_directory = 'train/'\nfor file in listdir(src_directory):\n src = src_directory + '/' + file\n dst_dir = 'train/'\n if random() < val_ratio:\n     dst_dir = 'test/'\n if file.startswith('cat'):\n     dst = dataset_home + dst_dir + 'cats/'  + file\n     copyfile(src, dst)\n elif file.startswith('dog'):\n     dst = dataset_home + dst_dir + 'dogs/'  + file\n     copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:23:20.503846Z","iopub.execute_input":"2024-01-29T08:23:20.504132Z","iopub.status.idle":"2024-01-29T08:23:36.344361Z","shell.execute_reply.started":"2024-01-29T08:23:20.504100Z","shell.execute_reply":"2024-01-29T08:23:36.343541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Develop a Baseline CNN Model**","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras import datasets, layers, models","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:57.380050Z","iopub.execute_input":"2024-01-29T04:00:57.380317Z","iopub.status.idle":"2024-01-29T04:00:57.593874Z","shell.execute_reply.started":"2024-01-29T04:00:57.380294Z","shell.execute_reply":"2024-01-29T04:00:57.593193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# define cnn model\ndef define_model():\n model = models.Sequential()\n model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\n model.add(MaxPooling2D((2, 2)))\n model.add(Flatten())\n model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n model.add(Dense(1, activation='sigmoid'))\n # compile model\n opt = SGD(lr=0.001, momentum=0.9)\n model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n return model","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:57.594908Z","iopub.execute_input":"2024-01-29T04:00:57.595261Z","iopub.status.idle":"2024-01-29T04:00:57.602445Z","shell.execute_reply.started":"2024-01-29T04:00:57.595228Z","shell.execute_reply":"2024-01-29T04:00:57.601437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = models.Sequential()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:57.603697Z","iopub.execute_input":"2024-01-29T04:00:57.604274Z","iopub.status.idle":"2024-01-29T04:00:57.623206Z","shell.execute_reply.started":"2024-01-29T04:00:57.604241Z","shell.execute_reply":"2024-01-29T04:00:57.622498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# baseline model for the dogs vs cats dataset\nimport sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\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.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\n \n# define cnn model\ndef define_model():\n model = Sequential()\n model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\n model.add(MaxPooling2D((2, 2)))\n model.add(Flatten())\n model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n model.add(Dense(1, activation='sigmoid'))\n # compile model\n opt = SGD(lr=0.001, momentum=0.9)\n model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n return model\n \n# plot diagnostic learning curves\ndef summarize_diagnostics(history):\n # plot loss\n pyplot.subplot(211)\n pyplot.title('Cross Entropy Loss')\n pyplot.plot(history.history['loss'], color='blue', label='train')\n pyplot.plot(history.history['val_loss'], color='orange', label='test')\n # plot accuracy\n pyplot.subplot(212)\n pyplot.title('Classification Accuracy')\n pyplot.plot(history.history['accuracy'], color='blue', label='train')\n pyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n # save plot to file\n filename = sys.argv[0].split('/')[-1]\n pyplot.savefig(filename + '_plot.png')\n pyplot.close()\n \n# run the test harness for evaluating a model\ndef run_test_harness():\n # define model\n model = define_model()\n # create data generator\n datagen = ImageDataGenerator(rescale=1.0/255.0)\n # prepare iterators\n train_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n class_mode='binary', batch_size=64, target_size=(200, 200))\n test_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n class_mode='binary', batch_size=64, target_size=(200, 200))\n # fit model\n history = model.fit_generator(train_it, steps_per_epoch=len(train_it),\n validation_data=test_it, validation_steps=len(test_it), epochs=20, verbose=0)\n # evaluate model\n _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)\n print('> %.3f' % (acc * 100.0))\n # learning curves\n summarize_diagnostics(history)\n \n# entry point, run the test harness\n#run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:57.624492Z","iopub.execute_input":"2024-01-29T04:00:57.624748Z","iopub.status.idle":"2024-01-29T04:00:57.638279Z","shell.execute_reply.started":"2024-01-29T04:00:57.624725Z","shell.execute_reply":"2024-01-29T04:00:57.637587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Three Block VGG Model**","metadata":{}},{"cell_type":"code","source":"#Three Block VGG Model\n# define cnn model\ndef define_model():\n model = Sequential()\n model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\n model.add(MaxPooling2D((2, 2)))\n model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n model.add(MaxPooling2D((2, 2)))\n model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n model.add(MaxPooling2D((2, 2)))\n model.add(Flatten())\n model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n model.add(Dense(1, activation='sigmoid'))\n # compile model\n opt = SGD(lr=0.001, momentum=0.9)\n model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n return model","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:57.639343Z","iopub.execute_input":"2024-01-29T04:00:57.639600Z","iopub.status.idle":"2024-01-29T04:00:57.657954Z","shell.execute_reply.started":"2024-01-29T04:00:57.639578Z","shell.execute_reply":"2024-01-29T04:00:57.657077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Develop Model Improvements**\\n\n\n*Dropout Regularization*","metadata":{}},{"cell_type":"code","source":"import sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\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\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\n \n# define cnn model\ndef define_model():\n model = Sequential()\n model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\n model.add(MaxPooling2D((2, 2)))\n model.add(Dropout(0.2))\n model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n model.add(MaxPooling2D((2, 2)))\n model.add(Dropout(0.2))\n model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n model.add(MaxPooling2D((2, 2)))\n model.add(Dropout(0.2))\n model.add(Flatten())\n model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n model.add(Dropout(0.5))\n model.add(Dense(1, activation='sigmoid'))\n # compile model\n opt = SGD(lr=0.001, momentum=0.9)\n model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n return model\n \n# plot diagnostic learning curves\ndef summarize_diagnostics(history):\n # plot loss\n pyplot.subplot(211)\n pyplot.title('Cross Entropy Loss')\n pyplot.plot(history.history['loss'], color='blue', label='train')\n pyplot.plot(history.history['val_loss'], color='orange', label='test')\n # plot accuracy\n pyplot.subplot(212)\n pyplot.title('Classification Accuracy')\n pyplot.plot(history.history['accuracy'], color='blue', label='train')\n pyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n # save plot to file\n filename = sys.argv[0].split('/')[-1]\n pyplot.savefig(filename + '_plot.png')\n pyplot.close()\n \n# run the test harness for evaluating a model\ndef run_test_harness():\n # define model\n model = define_model()\n # create data generator\n datagen = ImageDataGenerator(rescale=1.0/255.0)\n # prepare iterator\n train_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n class_mode='binary', batch_size=64, target_size=(200, 200))\n test_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n class_mode='binary', batch_size=64, target_size=(200, 200))\n # fit model\n history = model.fit_generator(train_it, steps_per_epoch=len(train_it),\n validation_data=test_it, validation_steps=len(test_it), epochs=50, verbose=0)\n # evaluate model\n _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)\n print('> %.3f' % (acc * 100.0))\n # learning curves\n summarize_diagnostics(history)\n \n# entry point, run the test harness\n#run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:57.659144Z","iopub.execute_input":"2024-01-29T04:00:57.659424Z","iopub.status.idle":"2024-01-29T04:00:57.676356Z","shell.execute_reply.started":"2024-01-29T04:00:57.659401Z","shell.execute_reply":"2024-01-29T04:00:57.675496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.2))\nmodel.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))\n# compile model\nopt = SGD(lr=0.001, momentum=0.9)\nmodel.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:57.677575Z","iopub.execute_input":"2024-01-29T04:00:57.678153Z","iopub.status.idle":"2024-01-29T04:00:59.420247Z","shell.execute_reply.started":"2024-01-29T04:00:57.678120Z","shell.execute_reply":"2024-01-29T04:00:59.419498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1.0/255.0)\n# prepare iterator\ntrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\nclass_mode='binary', batch_size=64, target_size=(200, 200))\ntest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\nclass_mode='binary', batch_size=64, target_size=(200, 200))","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:00:59.421254Z","iopub.execute_input":"2024-01-29T04:00:59.421506Z","iopub.status.idle":"2024-01-29T04:01:00.446821Z","shell.execute_reply.started":"2024-01-29T04:00:59.421483Z","shell.execute_reply":"2024-01-29T04:01:00.446070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot -q","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:38:01.362795Z","iopub.execute_input":"2024-01-29T08:38:01.363194Z","iopub.status.idle":"2024-01-29T08:38:16.200019Z","shell.execute_reply.started":"2024-01-29T08:38:01.363161Z","shell.execute_reply":"2024-01-29T08:38:16.198845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:38:16.201992Z","iopub.execute_input":"2024-01-29T08:38:16.202309Z","iopub.status.idle":"2024-01-29T08:38:16.218424Z","shell.execute_reply.started":"2024-01-29T08:38:16.202281Z","shell.execute_reply":"2024-01-29T08:38:16.217540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(train_it, steps_per_epoch=len(train_it),\nvalidation_data=test_it, validation_steps=len(test_it), epochs=10, verbose=1,callbacks=[PlotLossesKeras()])\n# evaluate model\n_, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:01:17.514071Z","iopub.execute_input":"2024-01-29T04:01:17.514425Z","iopub.status.idle":"2024-01-29T04:09:40.098144Z","shell.execute_reply.started":"2024-01-29T04:01:17.514392Z","shell.execute_reply":"2024-01-29T04:09:40.097239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pyplot.subplot(211)\npyplot.title('Cross Entropy Loss')\npyplot.plot(history.history['loss'], color='blue', label='train')\npyplot.plot(history.history['val_loss'], color='orange', label='test')\n# plot accuracy\npyplot.subplot(212)\npyplot.title('Classification Accuracy')\npyplot.plot(history.history['accuracy'], color='blue', label='train')\npyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n# save plot to file\nfilename = sys.argv[0].split('/')[-1]\n#pyplot.savefig(filename + '_plot.png')\npyplot.show()\npyplot.close()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:09:40.099556Z","iopub.execute_input":"2024-01-29T04:09:40.099850Z","iopub.status.idle":"2024-01-29T04:09:40.463346Z","shell.execute_reply.started":"2024-01-29T04:09:40.099823Z","shell.execute_reply":"2024-01-29T04:09:40.462470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # prepare iterator\n# train_it = datagen.flow_from_directory('finalize_dogs_vs_cats/',\n#  class_mode='binary', batch_size=64, target_size=(224, 224))","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:09:40.464544Z","iopub.execute_input":"2024-01-29T04:09:40.464873Z","iopub.status.idle":"2024-01-29T04:09:40.468991Z","shell.execute_reply.started":"2024-01-29T04:09:40.464839Z","shell.execute_reply":"2024-01-29T04:09:40.468200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Data Augmentation**","metadata":{}},{"cell_type":"code","source":"\n# create data generators\ntrain_datagen = ImageDataGenerator(rescale=1.0/255.0,\n width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True)\ntest_datagen = ImageDataGenerator(rescale=1.0/255.0)\n# prepare iterators\ntrain_it = train_datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n class_mode='binary', batch_size=64, target_size=(200, 200))\ntest_it = test_datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n class_mode='binary', batch_size=64, target_size=(200, 200))","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:09:40.470181Z","iopub.execute_input":"2024-01-29T04:09:40.470520Z","iopub.status.idle":"2024-01-29T04:09:41.545214Z","shell.execute_reply.started":"2024-01-29T04:09:40.470488Z","shell.execute_reply":"2024-01-29T04:09:41.544310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# baseline model with data augmentation for the dogs vs cats dataset\nimport sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\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.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# define cnn model\ndef define_model():\n    model = Sequential()\n    model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\n    model.add(MaxPooling2D((2, 2)))\n    model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n    model.add(MaxPooling2D((2, 2)))\n    model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n    model.add(MaxPooling2D((2, 2)))\n    model.add(Flatten())\n    model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n    model.add(Dense(1, activation='sigmoid'))\n    # compile model\n    opt = SGD(lr=0.001, momentum=0.9)\n    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n    return model\n\n# plot diagnostic learning curves\ndef summarize_diagnostics(history):\n    # plot loss\n    pyplot.subplot(211)\n    pyplot.title('Cross Entropy Loss')\n    pyplot.plot(history.history['loss'], color='blue', label='train')\n    pyplot.plot(history.history['val_loss'], color='orange', label='test')\n    # plot accuracy\n    pyplot.subplot(212)\n    pyplot.title('Classification Accuracy')\n    pyplot.plot(history.history['accuracy'], color='blue', label='train')\n    pyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n    # save plot to file\n    filename = sys.argv[0].split('/')[-1]\n    pyplot.savefig(filename + '_plot.png')\n    pyplot.close()\n\n# run the test harness for evaluating a model\ndef run_test_harness():\n    # define model\n    model = define_model()\n    # create data generators\n    train_datagen = ImageDataGenerator(rescale=1.0/255.0,\n    width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True)\n    test_datagen = ImageDataGenerator(rescale=1.0/255.0)\n    # prepare iterators\n    train_it = train_datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n    class_mode='binary', batch_size=64, target_size=(200, 200))\n    test_it = test_datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n    class_mode='binary', batch_size=64, target_size=(200, 200))\n    # fit model\n    history = model.fit_generator(train_it, steps_per_epoch=len(train_it),\n    validation_data=test_it, validation_steps=len(test_it), epochs=50, verbose=0)\n    # evaluate model\n    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=1 ,callbacks=[PlotLossesKeras()])\n    print('> %.3f' % (acc * 100.0))\n    # learning curves\n    summarize_diagnostics(history)\n\n# entry point, run the test harness\n#run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:09:41.546493Z","iopub.execute_input":"2024-01-29T04:09:41.547101Z","iopub.status.idle":"2024-01-29T04:09:41.565506Z","shell.execute_reply.started":"2024-01-29T04:09:41.547063Z","shell.execute_reply":"2024-01-29T04:09:41.564615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:09:41.566615Z","iopub.execute_input":"2024-01-29T04:09:41.566872Z","iopub.status.idle":"2024-01-29T04:09:41.583490Z","shell.execute_reply.started":"2024-01-29T04:09:41.566849Z","shell.execute_reply":"2024-01-29T04:09:41.582795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Explore Transfer Learning**","metadata":{}},{"cell_type":"code","source":"\n# vgg16 model used for transfer learning on the dogs and cats dataset\nimport sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# define cnn model\ndef define_model():\n    # load model\n    model = VGG16(include_top=False, input_shape=(224, 224, 3))\n    # mark loaded layers as not trainable\n    for layer in model.layers:\n        layer.trainable = False\n        # add new classifier layers\n        flat1 = Flatten()(model.layers[-1].output)\n        class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\n        output = Dense(1, activation='sigmoid')(class1)\n        # define new model\n        model = Model(inputs=model.inputs, outputs=output)\n        # compile model\n        opt = SGD(lr=0.001, momentum=0.9)\n        model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n    return model\n\n# plot diagnostic learning curves\ndef summarize_diagnostics(history):\n    # plot loss\n    pyplot.subplot(211)\n    pyplot.title('Cross Entropy Loss')\n    pyplot.plot(history.history['loss'], color='blue', label='train')\n    pyplot.plot(history.history['val_loss'], color='orange', label='test')\n    # plot accuracy\n    pyplot.subplot(212)\n    pyplot.title('Classification Accuracy')\n    pyplot.plot(history.history['accuracy'], color='blue', label='train')\n    pyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n    # save plot to file\n    filename = sys.argv[0].split('/')[-1]\n    #pyplot.savefig(filename + '_plot.png')\n    pyplot.show()\n    pyplot.close()\n\n# run the test harness for evaluating a model\ndef run_test_harness():\n    # define model\n    model = define_model()\n    # create data generator\n    datagen = ImageDataGenerator(featurewise_center=True)\n    # specify imagenet mean values for centering\n    datagen.mean = [123.68, 116.779, 103.939]\n    # prepare iterator\n    train_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n    class_mode='binary', batch_size=64, target_size=(224, 224))\n    test_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n    class_mode='binary', batch_size=64, target_size=(224, 224))\n    # fit model\n    history = model.fit_generator(train_it, steps_per_epoch=len(train_it),\n    validation_data=test_it, validation_steps=len(test_it), epochs=10, verbose=1)\n    # evaluate model\n    _, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=1,callbacks=[PlotLossesKeras()])\n    print('> %.3f' % (acc * 100.0))\n    # learning curves\n    summarize_diagnostics(history)\n    \nrun_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:09:41.584780Z","iopub.execute_input":"2024-01-29T04:09:41.585038Z","iopub.status.idle":"2024-01-29T04:28:36.144120Z","shell.execute_reply.started":"2024-01-29T04:09:41.585015Z","shell.execute_reply":"2024-01-29T04:28:36.143191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T04:28:36.151034Z","iopub.execute_input":"2024-01-29T04:28:36.151318Z","iopub.status.idle":"2024-01-29T04:28:36.155210Z","shell.execute_reply.started":"2024-01-29T04:28:36.151292Z","shell.execute_reply":"2024-01-29T04:28:36.154345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**How to Finalize the Model and Make Predictions**\n\nSave Final Model","metadata":{}},{"cell_type":"code","source":"from os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\n# create directories\ndataset_home = '/kaggle/working/finalize_dogs_vs_cats/'\n# create label subdirectories\nlabeldirs = ['dogs/', 'cats/']\nfor labldir in labeldirs:\n    newdir = dataset_home + labldir\n    makedirs(newdir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:23:36.345404Z","iopub.execute_input":"2024-01-29T08:23:36.345689Z","iopub.status.idle":"2024-01-29T08:23:36.351480Z","shell.execute_reply.started":"2024-01-29T08:23:36.345665Z","shell.execute_reply":"2024-01-29T08:23:36.350523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# organize dataset into a useful structure\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\n# create directories\ndataset_home = '/kaggle/working/finalize_dogs_vs_cats/'\n# create label subdirectories\nlabeldirs = ['dogs/', 'cats/']\nfor labldir in labeldirs:\n    newdir = dataset_home + labldir\n    makedirs(newdir, exist_ok=True)\n# copy training dataset images into subdirectories\nsrc_directory = '/kaggle/working/train'\nfor file in listdir(src_directory):\n    src = src_directory + '/' + file\n    if file.startswith('cat'):\n        dst = dataset_home + 'cats/'  + file\n        copyfile(src, dst)\n    elif file.startswith('dog'):\n        dst = dataset_home + 'dogs/'  + file\n        copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2024-01-29T09:38:12.707381Z","iopub.execute_input":"2024-01-29T09:38:12.708105Z","iopub.status.idle":"2024-01-29T09:38:16.430017Z","shell.execute_reply.started":"2024-01-29T09:38:12.708069Z","shell.execute_reply":"2024-01-29T09:38:16.429201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# save the final model to file\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\n \n# define cnn model\ndef define_model():\n # load model\n model = VGG16(include_top=False, input_shape=(224, 224, 3))\n # mark loaded layers as not trainable\n for layer in model.layers:\n    layer.trainable = False\n    # add new classifier layers\n    flat1 = Flatten()(model.layers[-1].output)\n    class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\n    output = Dense(1, activation='sigmoid')(class1)\n    # define new model\n    model = Model(inputs=model.inputs, outputs=output)\n    # compile model\n    opt = SGD(lr=0.001, momentum=0.9)\n    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n    return model\n \n# run the test harness for evaluating a model\ndef run_test_harness():\n    # define model\n    model = define_model()\n    # create data generator\n    datagen = ImageDataGenerator(featurewise_center=True)\n    # specify imagenet mean values for centering\n    datagen.mean = [123.68, 116.779, 103.939]\n    # prepare iterator\n    train_it = datagen.flow_from_directory('finalize_dogs_vs_cats/',\n    class_mode='binary', batch_size=64, target_size=(224, 224))\n    # fit model\n    model.fit_generator(train_it, steps_per_epoch=len(train_it), epochs=10, verbose=1)\n    # save model\n    model.save('final_model.h5')\n\n# entry point, run the test harness\nrun_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T08:38:23.962049Z","iopub.execute_input":"2024-01-29T08:38:23.962838Z","iopub.status.idle":"2024-01-29T09:24:16.083709Z","shell.execute_reply.started":"2024-01-29T08:38:23.962805Z","shell.execute_reply":"2024-01-29T09:24:16.082797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Make Prediction**","metadata":{}},{"cell_type":"code","source":"#os.chdir('/kaggle/input/sample-image-jpg')","metadata":{"execution":{"iopub.status.busy":"2024-01-29T09:30:41.286917Z","iopub.execute_input":"2024-01-29T09:30:41.287676Z","iopub.status.idle":"2024-01-29T09:30:41.292024Z","shell.execute_reply.started":"2024-01-29T09:30:41.287642Z","shell.execute_reply":"2024-01-29T09:30:41.291046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#os.chdir('/kaggle/working/')","metadata":{"execution":{"iopub.status.busy":"2024-01-29T09:31:07.844263Z","iopub.execute_input":"2024-01-29T09:31:07.844979Z","iopub.status.idle":"2024-01-29T09:31:07.848989Z","shell.execute_reply.started":"2024-01-29T09:31:07.844946Z","shell.execute_reply":"2024-01-29T09:31:07.848082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#os.listdir()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T09:31:30.080197Z","iopub.execute_input":"2024-01-29T09:31:30.080883Z","iopub.status.idle":"2024-01-29T09:31:30.087630Z","shell.execute_reply.started":"2024-01-29T09:31:30.080848Z","shell.execute_reply":"2024-01-29T09:31:30.086625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# make a prediction for a new image.\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import load_model\n\n# load and prepare the image\ndef load_image(filename): \n    # load the image\n    img = load_img(filename, target_size=(224, 224))\n    # convert to array\n    img = img_to_array(img)\n    # reshape into a single sample with 3 channels\n    img = img.reshape(1, 224, 224, 3)\n    # center pixel data\n    img = img.astype('float32')\n    img = img - [123.68, 116.779, 103.939]\n    return img\n\n# load an image and predict the class\ndef run_example():\n    # load the image\n    img = load_image('/kaggle/working/test1/166.jpg')\n    #print(img)\n    # load model\n    model = load_model('final_model.h5')\n    # predict the class\n    result = model.predict(img)\n    print(result)\n\n# entry point, run the example\nrun_example()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T09:41:37.558722Z","iopub.execute_input":"2024-01-29T09:41:37.559628Z","iopub.status.idle":"2024-01-29T09:41:38.234219Z","shell.execute_reply.started":"2024-01-29T09:41:37.559592Z","shell.execute_reply":"2024-01-29T09:41:38.233085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#run_example()","metadata":{"execution":{"iopub.status.busy":"2024-01-29T09:33:55.659547Z","iopub.execute_input":"2024-01-29T09:33:55.659882Z","iopub.status.idle":"2024-01-29T09:33:56.340179Z","shell.execute_reply.started":"2024-01-29T09:33:55.659857Z","shell.execute_reply":"2024-01-29T09:33:56.339133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}