{"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":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-01-26T04:27:05.148028Z","iopub.execute_input":"2024-01-26T04:27:05.148498Z","iopub.status.idle":"2024-01-26T04:27:05.514552Z","shell.execute_reply.started":"2024-01-26T04:27:05.148445Z","shell.execute_reply":"2024-01-26T04:27:05.513536Z"},"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-26T04:27:05.516488Z","iopub.execute_input":"2024-01-26T04:27:05.516973Z","iopub.status.idle":"2024-01-26T04:27:21.095017Z","shell.execute_reply.started":"2024-01-26T04:27:05.516942Z","shell.execute_reply":"2024-01-26T04:27:21.093764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:27:21.096400Z","iopub.execute_input":"2024-01-26T04:27:21.096784Z","iopub.status.idle":"2024-01-26T04:27:22.093195Z","shell.execute_reply.started":"2024-01-26T04:27:21.096749Z","shell.execute_reply":"2024-01-26T04:27:22.092178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l dataset_dogs_vs_cats/train/dogs | wc -l","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:27:22.094683Z","iopub.execute_input":"2024-01-26T04:27:22.095054Z","iopub.status.idle":"2024-01-26T04:27:23.085371Z","shell.execute_reply.started":"2024-01-26T04:27:22.095027Z","shell.execute_reply":"2024-01-26T04:27:23.084176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l dataset_dogs_vs_cats/train/cats | wc -l","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:27:23.088427Z","iopub.execute_input":"2024-01-26T04:27:23.088769Z","iopub.status.idle":"2024-01-26T04:27:24.072802Z","shell.execute_reply.started":"2024-01-26T04:27:23.088741Z","shell.execute_reply":"2024-01-26T04:27:24.071536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T04:27:24.075646Z","iopub.execute_input":"2024-01-26T04:27:24.076316Z","iopub.status.idle":"2024-01-26T04:27:25.387668Z","shell.execute_reply.started":"2024-01-26T04:27:24.076276Z","shell.execute_reply":"2024-01-26T04:27:25.386900Z"},"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\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'cat.' + 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-26T04:27:25.389012Z","iopub.execute_input":"2024-01-26T04:27:25.389719Z","iopub.status.idle":"2024-01-26T04:27:26.491853Z","shell.execute_reply.started":"2024-01-26T04:27:25.389686Z","shell.execute_reply":"2024-01-26T04:27:26.491162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install numpy==1.23.0\n","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:27:26.493042Z","iopub.execute_input":"2024-01-26T04:27:26.494190Z","iopub.status.idle":"2024-01-26T04:27:45.404819Z","shell.execute_reply.started":"2024-01-26T04:27:26.494161Z","shell.execute_reply":"2024-01-26T04:27:45.403935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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\t# determine class\n\toutput = 0.0\n\tif file.startswith('dog'):\n\t\toutput = 1.0\n\t# load image\n\tphoto = load_img(folder + file, target_size=(200, 200))\n\t# convert to numpy array\n\tphoto = img_to_array(photo)\n\t# store\n\tphotos.append(photo)\n\tlabels.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-26T04:27:45.406023Z","iopub.execute_input":"2024-01-26T04:27:45.406332Z","iopub.status.idle":"2024-01-26T04:29:17.605283Z","shell.execute_reply.started":"2024-01-26T04:27:45.406308Z","shell.execute_reply":"2024-01-26T04:29:17.604132Z"},"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-26T04:30:08.428268Z","iopub.execute_input":"2024-01-26T04:30:08.428997Z","iopub.status.idle":"2024-01-26T04:31:02.957386Z","shell.execute_reply.started":"2024-01-26T04:30:08.428951Z","shell.execute_reply":"2024-01-26T04:31:02.956416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:31:02.959642Z","iopub.execute_input":"2024-01-26T04:31:02.960028Z","iopub.status.idle":"2024-01-26T04:31:02.965525Z","shell.execute_reply.started":"2024-01-26T04:31:02.959989Z","shell.execute_reply":"2024-01-26T04:31:02.964366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\nfor subdir in subdirs:\n\t# create label subdirectories\n\tlabeldirs = ['dogs/', 'cats/']\n\tfor labldir in labeldirs:\n\t\tnewdir = dataset_home + subdir + labldir\n\t\tmakedirs(newdir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:31:02.966729Z","iopub.execute_input":"2024-01-26T04:31:02.967120Z","iopub.status.idle":"2024-01-26T04:31:02.977745Z","shell.execute_reply.started":"2024-01-26T04:31:02.967064Z","shell.execute_reply":"2024-01-26T04:31:02.977127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# seed random number generator\nseed(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\tsrc = src_directory + '/' + file\n\tdst_dir = 'train/'\n\tif random() < val_ratio:\n\t\tdst_dir = 'test/'\n\tif file.startswith('cat'):\n\t\tdst = dataset_home + dst_dir + 'cats/'  + file\n\t\tcopyfile(src, dst)\n\telif file.startswith('dog'):\n\t\tdst = dataset_home + dst_dir + 'dogs/'  + file\n\t\tcopyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:31:02.978591Z","iopub.execute_input":"2024-01-26T04:31:02.979445Z","iopub.status.idle":"2024-01-26T04:31:19.914559Z","shell.execute_reply.started":"2024-01-26T04:31:02.979418Z","shell.execute_reply":"2024-01-26T04:31:19.913407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install livelossplot\n","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:05.489057Z","iopub.execute_input":"2024-01-26T04:32:05.489485Z","iopub.status.idle":"2024-01-26T04:32:18.997790Z","shell.execute_reply.started":"2024-01-26T04:32:05.489450Z","shell.execute_reply":"2024-01-26T04:32:18.996801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:28.308956Z","iopub.execute_input":"2024-01-26T04:32:28.309902Z","iopub.status.idle":"2024-01-26T04:32:28.325455Z","shell.execute_reply.started":"2024-01-26T04:32:28.309863Z","shell.execute_reply":"2024-01-26T04:32:28.324442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot==0.5.5","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:35.340409Z","iopub.execute_input":"2024-01-26T04:32:35.340893Z","iopub.status.idle":"2024-01-26T04:32:47.700744Z","shell.execute_reply.started":"2024-01-26T04:32:35.340801Z","shell.execute_reply":"2024-01-26T04:32:47.699674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define cnn model\ndef define_model():\n\tmodel = Sequential()\n\tmodel.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\n\tmodel.add(MaxPooling2D((2, 2)))\n\tmodel.add(Flatten())\n\tmodel.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n\tmodel.add(Dense(1, activation='sigmoid'))\n\t# compile model\n\topt = SGD(lr=0.001, momentum=0.9)\n\tmodel.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n\treturn model","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:47.703067Z","iopub.execute_input":"2024-01-26T04:32:47.703390Z","iopub.status.idle":"2024-01-26T04:32:47.711248Z","shell.execute_reply.started":"2024-01-26T04:32:47.703363Z","shell.execute_reply":"2024-01-26T04:32:47.710371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = define_model()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:47.712468Z","iopub.execute_input":"2024-01-26T04:32:47.712742Z","iopub.status.idle":"2024-01-26T04:32:48.223374Z","shell.execute_reply.started":"2024-01-26T04:32:47.712717Z","shell.execute_reply":"2024-01-26T04:32:48.222224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:48.225944Z","iopub.execute_input":"2024-01-26T04:32:48.226370Z","iopub.status.idle":"2024-01-26T04:32:48.232859Z","shell.execute_reply.started":"2024-01-26T04:32:48.226328Z","shell.execute_reply":"2024-01-26T04:32:48.230626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create data generator\ndatagen = ImageDataGenerator(rescale=1.0/255.0)","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:48.234410Z","iopub.execute_input":"2024-01-26T04:32:48.234967Z","iopub.status.idle":"2024-01-26T04:32:48.245361Z","shell.execute_reply.started":"2024-01-26T04:32:48.234891Z","shell.execute_reply":"2024-01-26T04:32:48.244410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare iterators\ntrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n\tclass_mode='binary', batch_size=64, target_size=(200, 200))\ntest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n\tclass_mode='binary', batch_size=64, target_size=(200, 200))","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:48.247963Z","iopub.execute_input":"2024-01-26T04:32:48.248374Z","iopub.status.idle":"2024-01-26T04:32:49.255820Z","shell.execute_reply.started":"2024-01-26T04:32:48.248336Z","shell.execute_reply":"2024-01-26T04:32:49.255099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit model\nhistory = model.fit(train_it, steps_per_epoch=len(train_it),\n                    validation_data=test_it, validation_steps=len(test_it),\n                    epochs=30, verbose=0)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate model\nresult = model.evaluate(test_it, verbose=0)\nacc = result[1]  # Assuming accuracy is the second element in the result tuple\n\nprint('> %.3f' % (acc * 100.0))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot diagnostic learning curves\ndef summarize_diagnostics(history):\n\t# plot loss\n\tpyplot.subplot(211)\n\tpyplot.title('Cross Entropy Loss')\n\tpyplot.plot(history.history['loss'], color='blue', label='train')\n\tpyplot.plot(history.history['val_loss'], color='orange', label='test')\n\t# plot accuracy\n\tpyplot.subplot(212)\n\tpyplot.title('Classification Accuracy')\n\tpyplot.plot(history.history['accuracy'], color='blue', label='train')\n\tpyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n\t# save plot to file\n\tfilename = sys.argv[0].split('/')[-1]\n\tpyplot.savefig(filename + '_plot.png')\n\tpyplot.close()","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:59.293577Z","iopub.execute_input":"2024-01-26T04:32:59.294038Z","iopub.status.idle":"2024-01-26T04:32:59.301581Z","shell.execute_reply.started":"2024-01-26T04:32:59.293991Z","shell.execute_reply":"2024-01-26T04:32:59.300364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:32:59.303219Z","iopub.execute_input":"2024-01-26T04:32:59.303517Z","iopub.status.idle":"2024-01-26T04:32:59.314616Z","shell.execute_reply.started":"2024-01-26T04:32:59.303490Z","shell.execute_reply":"2024-01-26T04:32:59.313496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    # fit model\n    history = model.fit(train_it, steps_per_epoch=len(train_it),\n                        validation_data=test_it, validation_steps=len(test_it), epochs=20, verbose=0)\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate model\n_, acc = model.evaluate(test_it, steps=len(test_it), verbose=0)\nprint('> %.3f' % (acc * 100.0))\n# learning curves\nsummarize_diagnostics(history)\n\n# entry point, run the test harness\nrun_test_harness()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define cnn model\ndef define_model():\n\tmodel = Sequential()\n\tmodel.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\n\tmodel.add(MaxPooling2D((2, 2)))\n\tmodel.add(Flatten())\n\tmodel.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n\tmodel.add(Dense(1, activation='sigmoid'))\n\t# compile model\n\topt = SGD(lr=0.001, momentum=0.9)\n\tmodel.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n\treturn model","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:33:11.249668Z","iopub.execute_input":"2024-01-26T04:33:11.250106Z","iopub.status.idle":"2024-01-26T04:33:11.257574Z","shell.execute_reply.started":"2024-01-26T04:33:11.250054Z","shell.execute_reply":"2024-01-26T04:33:11.256565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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-26T04:33:11.259533Z","iopub.execute_input":"2024-01-26T04:33:11.259796Z","iopub.status.idle":"2024-01-26T04:33:11.272577Z","shell.execute_reply.started":"2024-01-26T04:33:11.259773Z","shell.execute_reply":"2024-01-26T04:33:11.271624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:33:11.273874Z","iopub.execute_input":"2024-01-26T04:33:11.274286Z","iopub.status.idle":"2024-01-26T04:33:11.285229Z","shell.execute_reply.started":"2024-01-26T04:33:11.274247Z","shell.execute_reply":"2024-01-26T04:33:11.284486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# baseline model with dropout 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.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","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:33:11.286644Z","iopub.execute_input":"2024-01-26T04:33:11.286998Z","iopub.status.idle":"2024-01-26T04:33:11.303434Z","shell.execute_reply.started":"2024-01-26T04:33:11.286971Z","shell.execute_reply":"2024-01-26T04:33:11.302510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit model\nhistory = model.fit(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(test_it, steps=len(test_it), verbose=0)\nprint('> %.3f' % (acc * 100.0))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # learning curves\n summarize_diagnostics(history)\n \n# entry point, run the test harness\nrun_test_harness()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:33:26.241300Z","iopub.execute_input":"2024-01-26T04:33:26.241719Z","iopub.status.idle":"2024-01-26T04:33:26.248222Z","shell.execute_reply.started":"2024-01-26T04:33:26.241688Z","shell.execute_reply":"2024-01-26T04:33:26.247489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:33:26.249446Z","iopub.execute_input":"2024-01-26T04:33:26.249702Z","iopub.status.idle":"2024-01-26T04:33:26.262049Z","shell.execute_reply.started":"2024-01-26T04:33:26.249680Z","shell.execute_reply":"2024-01-26T04:33:26.261156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \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","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:33:26.262960Z","iopub.execute_input":"2024-01-26T04:33:26.263224Z","iopub.status.idle":"2024-01-26T04:33:26.278439Z","shell.execute_reply.started":"2024-01-26T04:33:26.263201Z","shell.execute_reply":"2024-01-26T04:33:26.277643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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\n# Note: The rest of your code continues here...\n# fit model, evaluate model, and other relevant parts of the code\n","metadata":{"execution":{"iopub.status.busy":"2024-01-26T04:33:26.280246Z","iopub.execute_input":"2024-01-26T04:33:26.280541Z","iopub.status.idle":"2024-01-26T04:33:26.292158Z","shell.execute_reply.started":"2024-01-26T04:33:26.280506Z","shell.execute_reply":"2024-01-26T04:33:26.290967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit model\nhistory = model.fit(train_it, steps_per_epoch=len(train_it),\n                    validation_data=test_it, validation_steps=len(test_it), epochs=6, verbose=1)\n\n# evaluate model\n_, acc = model.evaluate(test_it, steps=len(test_it), verbose=0)\nprint('> %.3f' % (acc * 100.0))\n\n# learning curves\nsummarize_diagnostics(history)\n\n# entry point, run the test harness\nrun_test_harness()\n","metadata":{"execution":{"iopub.status.busy":"2024-01-26T06:15:16.633498Z","iopub.execute_input":"2024-01-26T06:15:16.634692Z","iopub.status.idle":"2024-01-26T06:31:56.705543Z","shell.execute_reply.started":"2024-01-26T06:15:16.634642Z","shell.execute_reply":"2024-01-26T06:31:56.704471Z"},"trusted":true},"execution_count":null,"outputs":[]}]}