{"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"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:08:59.938523Z","iopub.execute_input":"2025-02-14T10:08:59.938714Z","iopub.status.idle":"2025-02-14T10:09:00.260038Z","shell.execute_reply.started":"2025-02-14T10:08:59.938694Z","shell.execute_reply":"2025-02-14T10:09:00.259137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:00.261967Z","iopub.execute_input":"2025-02-14T10:09:00.262405Z","iopub.status.idle":"2025-02-14T10:09:01.315116Z","shell.execute_reply.started":"2025-02-14T10:09:00.262373Z","shell.execute_reply":"2025-02-14T10:09:01.314149Z"}},"outputs":[],"execution_count":null},{"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":"2025-02-14T10:09:01.316529Z","iopub.execute_input":"2025-02-14T10:09:01.316893Z","iopub.status.idle":"2025-02-14T10:09:14.175852Z","shell.execute_reply.started":"2025-02-14T10:09:01.316862Z","shell.execute_reply":"2025-02-14T10:09:14.175149Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:14.176825Z","iopub.execute_input":"2025-02-14T10:09:14.177074Z","iopub.status.idle":"2025-02-14T10:09:15.180802Z","shell.execute_reply.started":"2025-02-14T10:09:14.177053Z","shell.execute_reply":"2025-02-14T10:09:15.179723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:15.182231Z","iopub.execute_input":"2025-02-14T10:09:15.1825Z","iopub.status.idle":"2025-02-14T10:09:16.174665Z","shell.execute_reply.started":"2025-02-14T10:09:15.182476Z","shell.execute_reply":"2025-02-14T10:09:16.173574Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls test1/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:16.176083Z","iopub.execute_input":"2025-02-14T10:09:16.176357Z","iopub.status.idle":"2025-02-14T10:09:17.226652Z","shell.execute_reply.started":"2025-02-14T10:09:16.176333Z","shell.execute_reply":"2025-02-14T10:09:17.225804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:17.229551Z","iopub.execute_input":"2025-02-14T10:09:17.229848Z","iopub.status.idle":"2025-02-14T10:09:18.319004Z","shell.execute_reply.started":"2025-02-14T10:09:17.22982Z","shell.execute_reply":"2025-02-14T10:09:18.318178Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:18.320187Z","iopub.execute_input":"2025-02-14T10:09:18.320451Z","iopub.status.idle":"2025-02-14T10:09:19.438511Z","shell.execute_reply.started":"2025-02-14T10:09:18.320427Z","shell.execute_reply":"2025-02-14T10:09:19.437344Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot dog 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 + '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":"2025-02-14T10:09:19.439993Z","iopub.execute_input":"2025-02-14T10:09:19.440905Z","iopub.status.idle":"2025-02-14T10:09:20.407188Z","shell.execute_reply.started":"2025-02-14T10:09:19.440876Z","shell.execute_reply":"2025-02-14T10:09:20.406284Z"},"trusted":true},"outputs":[],"execution_count":null},{"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":"2025-02-14T10:09:20.408243Z","iopub.execute_input":"2025-02-14T10:09:20.408508Z","iopub.status.idle":"2025-02-14T10:09:21.348708Z","shell.execute_reply.started":"2025-02-14T10:09:20.408486Z","shell.execute_reply":"2025-02-14T10:09:21.347833Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nprint(tensorflow.__version__)\n\nimport keras\nprint(keras.__version__)\n\nimport tensorflow.keras\nprint(tensorflow.__version__)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:21.349843Z","iopub.execute_input":"2025-02-14T10:09:21.350094Z","iopub.status.idle":"2025-02-14T10:09:32.00532Z","shell.execute_reply.started":"2025-02-14T10:09:21.350072Z","shell.execute_reply":"2025-02-14T10:09:32.004509Z"}},"outputs":[],"execution_count":null},{"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","metadata":{"execution":{"iopub.status.busy":"2025-02-14T10:09:32.006273Z","iopub.execute_input":"2025-02-14T10:09:32.006719Z","iopub.status.idle":"2025-02-14T10:09:32.011168Z","shell.execute_reply.started":"2025-02-14T10:09:32.006695Z","shell.execute_reply":"2025-02-14T10:09:32.01019Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 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-27T03:36:15.416611Z","iopub.execute_input":"2024-01-27T03:36:15.417128Z","iopub.status.idle":"2024-01-27T03:36:38.886396Z","shell.execute_reply.started":"2024-01-27T03:36:15.417101Z","shell.execute_reply":"2024-01-27T03:36:38.883149Z"}}},{"cell_type":"markdown","source":"# load and confirm the shape\nfrom numpy import load\nphotos = load('dogs_vs_cats_photos.npy')\nlabels = load('dogs_vs_cats_labels.npy')","metadata":{"execution":{"iopub.status.busy":"2024-01-27T03:36:38.88711Z","iopub.status.idle":"2024-01-27T03:36:38.887458Z","shell.execute_reply.started":"2024-01-27T03:36:38.887281Z","shell.execute_reply":"2024-01-27T03:36:38.887296Z"}}},{"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":"2025-02-14T10:09:32.012207Z","iopub.execute_input":"2025-02-14T10:09:32.012436Z","iopub.status.idle":"2025-02-14T10:09:32.046383Z","shell.execute_reply.started":"2025-02-14T10:09:32.012415Z","shell.execute_reply":"2025-02-14T10:09:32.045605Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/'] #test==>validation\nfor subdir in subdirs:\n    # create label subdirectories\n    labeldirs = ['dogs/', 'cats/']\n    for labldir in labeldirs:\n        newdir = dataset_home + subdir + labldir       \n        makedirs(newdir, exist_ok=True)\n        print(newdir)","metadata":{"execution":{"iopub.status.busy":"2025-02-14T10:09:32.047365Z","iopub.execute_input":"2025-02-14T10:09:32.047581Z","iopub.status.idle":"2025-02-14T10:09:32.057961Z","shell.execute_reply.started":"2025-02-14T10:09:32.047562Z","shell.execute_reply":"2025-02-14T10:09:32.057255Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train/dogs/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:32.058815Z","iopub.execute_input":"2025-02-14T10:09:32.059007Z","iopub.status.idle":"2025-02-14T10:09:33.065219Z","shell.execute_reply.started":"2025-02-14T10:09:32.058989Z","shell.execute_reply":"2025-02-14T10:09:33.064315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:33.066505Z","iopub.execute_input":"2025-02-14T10:09:33.066751Z","iopub.status.idle":"2025-02-14T10:09:34.067374Z","shell.execute_reply.started":"2025-02-14T10:09:33.066728Z","shell.execute_reply":"2025-02-14T10:09:34.066494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:34.06857Z","iopub.execute_input":"2025-02-14T10:09:34.06884Z","iopub.status.idle":"2025-02-14T10:09:35.063258Z","shell.execute_reply.started":"2025-02-14T10:09:34.068818Z","shell.execute_reply":"2025-02-14T10:09:35.062131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:35.064724Z","iopub.execute_input":"2025-02-14T10:09:35.065059Z","iopub.status.idle":"2025-02-14T10:09:36.066227Z","shell.execute_reply.started":"2025-02-14T10:09:35.065032Z","shell.execute_reply":"2025-02-14T10:09:36.06507Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# seed random number generator\nseed(1337)\n# define ratio of pictures to use for validation\nval_ratio = 0.25 #changable\n# copy training dataset images into subdirectories\nsrc_directory = 'train/'\n\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":"2025-02-14T10:09:36.067954Z","iopub.execute_input":"2025-02-14T10:09:36.068671Z","iopub.status.idle":"2025-02-14T10:09:38.563933Z","shell.execute_reply.started":"2025-02-14T10:09:36.068641Z","shell.execute_reply":"2025-02-14T10:09:38.562691Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(src)\nprint(dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:38.565268Z","iopub.execute_input":"2025-02-14T10:09:38.565553Z","iopub.status.idle":"2025-02-14T10:09:38.570443Z","shell.execute_reply.started":"2025-02-14T10:09:38.56553Z","shell.execute_reply":"2025-02-14T10:09:38.569534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l dataset_dogs_vs_cats/train/dogs | wc -l\n!ls -l dataset_dogs_vs_cats/train/cats | wc -l","metadata":{"execution":{"iopub.status.busy":"2025-02-14T10:09:38.57159Z","iopub.execute_input":"2025-02-14T10:09:38.571869Z","iopub.status.idle":"2025-02-14T10:09:41.009106Z","shell.execute_reply.started":"2025-02-14T10:09:38.571848Z","shell.execute_reply":"2025-02-14T10:09:41.007971Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#validation set\n!ls -l dataset_dogs_vs_cats/test/dogs | wc -l\n!ls -l dataset_dogs_vs_cats/test/cats | wc -l","metadata":{"execution":{"iopub.status.busy":"2025-02-14T10:09:41.015323Z","iopub.execute_input":"2025-02-14T10:09:41.015596Z","iopub.status.idle":"2025-02-14T10:09:43.064518Z","shell.execute_reply.started":"2025-02-14T10:09:41.015573Z","shell.execute_reply":"2025-02-14T10:09:43.06361Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"execution":{"iopub.status.busy":"2025-02-14T10:09:43.065946Z","iopub.execute_input":"2025-02-14T10:09:43.066202Z","iopub.status.idle":"2025-02-14T10:09:52.560981Z","shell.execute_reply.started":"2025-02-14T10:09:43.066178Z","shell.execute_reply":"2025-02-14T10:09:52.559983Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2025-02-14T10:09:52.562276Z","iopub.execute_input":"2025-02-14T10:09:52.562563Z","iopub.status.idle":"2025-02-14T10:09:52.57676Z","shell.execute_reply.started":"2025-02-14T10:09:52.562536Z","shell.execute_reply":"2025-02-14T10:09:52.576058Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Prepare Final Dataset","metadata":{}},{"cell_type":"markdown","source":"# Start tutorial\n# Develop a Baseline CNN Model","metadata":{}},{"cell_type":"code","source":"# https://keras.io/api/applications/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:52.577868Z","iopub.execute_input":"2025-02-14T10:09:52.578175Z","iopub.status.idle":"2025-02-14T10:09:52.586347Z","shell.execute_reply.started":"2025-02-14T10:09:52.578147Z","shell.execute_reply":"2025-02-14T10:09:52.585482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:52.587441Z","iopub.execute_input":"2025-02-14T10:09:52.587745Z","iopub.status.idle":"2025-02-14T10:09:52.596501Z","shell.execute_reply.started":"2025-02-14T10:09:52.587717Z","shell.execute_reply":"2025-02-14T10:09:52.595816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define cnn model\ndef define_model_1():\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":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:52.597854Z","iopub.execute_input":"2025-02-14T10:09:52.598153Z","iopub.status.idle":"2025-02-14T10:09:52.607043Z","shell.execute_reply.started":"2025-02-14T10:09:52.598127Z","shell.execute_reply":"2025-02-14T10:09:52.606324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport sys\n\ndef summarize_diagnostics(history):\n    # plot loss\n    plt.figure(figsize=(8, 6))\n    plt.subplot(211)\n    plt.title('Cross Entropy Loss')\n    plt.plot(history.history['loss'], color='blue', label='train')\n    plt.plot(history.history['val_loss'], color='orange', label='test')\n    plt.legend()\n    # plot accuracy\n    plt.subplot(212)\n    plt.title('Classification Accuracy')\n    plt.plot(history.history['accuracy'], color='blue', label='train')\n    plt.plot(history.history['val_accuracy'], color='orange', label='test')\n    plt.legend()\n    # save plot to file\n    filename = sys.argv[0].split('/')[-1]\n    plt.savefig(filename + '_plot.png')\n    plt.show()\n    plt.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:52.608037Z","iopub.execute_input":"2025-02-14T10:09:52.608325Z","iopub.status.idle":"2025-02-14T10:09:52.618416Z","shell.execute_reply.started":"2025-02-14T10:09:52.608298Z","shell.execute_reply":"2025-02-14T10:09:52.617735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# run the test harness for evaluating a model\ndef run_test_harness():\n\t# define model\n\tmodel = define_model()\n\t# create data generator\n\tdatagen = ImageDataGenerator(rescale=1.0/255.0)\n\t# prepare iterators\n\ttrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n\t\tclass_mode='binary', batch_size=64, target_size=(200, 200))\n\ttest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n\t\tclass_mode='binary', batch_size=64, target_size=(200, 200))\n\t# fit model\n\thistory = model.fit_generator(train_it, steps_per_epoch=len(train_it),\n\t\tvalidation_data=test_it, validation_steps=len(test_it), epochs=20, verbose=0)\n\t# evaluate model\n\t_, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)\n\tprint('> %.3f' % (acc * 100.0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T10:09:52.6194Z","iopub.execute_input":"2025-02-14T10:09:52.61997Z","iopub.status.idle":"2025-02-14T10:09:52.631575Z","shell.execute_reply.started":"2025-02-14T10:09:52.619948Z","shell.execute_reply":"2025-02-14T10:09:52.63081Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **VGG16**","metadata":{}},{"cell_type":"code","source":"model = VGG16(\n    include_top=False,\n    input_shape=(224, 224, 3)\n    )\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:28:31.247545Z","iopub.execute_input":"2025-02-14T11:28:31.248266Z","iopub.status.idle":"2025-02-14T11:28:31.623189Z","shell.execute_reply.started":"2025-02-14T11:28:31.248235Z","shell.execute_reply":"2025-02-14T11:28:31.62236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for index, layer in enumerate(model.layers[:15]):\n    if hasattr(layer, 'output shape'):\n        print(index, layer.name, layer.output_shape)\n    layer.trainable = False\n\nfor index, layer in enumerate(model.layers[15:]):\n    if hasattr(layer, 'output shape'):\n        print(index, layer.name, layer.output_shape)\n\n    layer.trainable = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:29:27.144445Z","iopub.execute_input":"2025-02-14T11:29:27.144984Z","iopub.status.idle":"2025-02-14T11:29:27.152647Z","shell.execute_reply.started":"2025-02-14T11:29:27.144945Z","shell.execute_reply":"2025-02-14T11:29:27.151286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.layers[-1].output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:29:37.145035Z","iopub.execute_input":"2025-02-14T11:29:37.145625Z","iopub.status.idle":"2025-02-14T11:29:37.152052Z","shell.execute_reply.started":"2025-02-14T11:29:37.145592Z","shell.execute_reply":"2025-02-14T11:29:37.151137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.layers import Flatten, Dense\nfrom tensorflow.keras.optimizers import SGD\nfrom tensorflow.keras.models import Model\n\nflat1 = Flatten()(model.layers[-1].output)\nclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\noutput = Dense(1, activation='sigmoid')(class1)\n\nmy_model = Model(inputs=model.inputs, outputs=output)\n\nopt = SGD(learning_rate=0.0001, momentum=0.9)\nmy_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n\nmy_model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:30:04.329605Z","iopub.execute_input":"2025-02-14T11:30:04.329958Z","iopub.status.idle":"2025-02-14T11:30:04.406468Z","shell.execute_reply.started":"2025-02-14T11:30:04.329929Z","shell.execute_reply":"2025-02-14T11:30:04.405704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = my_model.fit(\n    train_it, \n    validation_data=test_it, \n    epochs=2,\n    verbose=1,\n    callbacks=[PlotLossesKeras()]\n)\n\n_, acc = my_model.evaluate(test_it, verbose=1) \nprint('> %.3f' % (acc * 100.0))\n\nsummarize_diagnostics(history)\n\nbest_model = 'vgg16_model.hdf5'\nmy_model.save(best_model)\nprint(f'Model saved to {best_model}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:36:04.36154Z","iopub.execute_input":"2025-02-14T11:36:04.361922Z","iopub.status.idle":"2025-02-14T11:47:02.656656Z","shell.execute_reply.started":"2025-02-14T11:36:04.361892Z","shell.execute_reply":"2025-02-14T11:47:02.655746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import load_model\n\nmodel = load_model('vgg16_model.hdf5') \n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_it = test_datagen.flow_from_directory(\n    'dataset_dogs_vs_cats/test/', \n    class_mode='binary',\n    batch_size=1,\n    target_size=(224, 224),\n    shuffle=True \n)\n\nnum_samples = 10\nselected_images = []\nselected_labels = []\n\nnum_dogs = 0\nnum_cats = 0\n\nwhile len(selected_images) < num_samples:\n    img, label = next(test_it) \n    if label[0] == 1 and num_dogs < 5: \n        selected_images.append(img[0])\n        selected_labels.append(label[0])  \n        num_dogs += 1\n    elif label[0] == 0 and num_cats < 5:\n        selected_images.append(img[0])\n        selected_labels.append(label[0]) \n        num_cats += 1\n\nselected_images = np.array(selected_images)\n\npredictions = model.predict(selected_images)\n\npred_labels = (predictions > 0.5).astype(int)\n\nfig, axes = plt.subplots(2, 5, figsize=(12, 6))\naxes = axes.ravel()\n\nfor i in range(num_samples):\n    axes[i].imshow(selected_images[i])\n    true_label = \"Dog\" if selected_labels[i] == 1 else \"Cat\" \n    pred_label = \"Dog\" if pred_labels[i] == 1 else \"Cat\" \n    pred_confidence = predictions[i][0] * 100 \n    axes[i].set_title(f\"True: {true_label}\\nPred: {pred_label} ({pred_confidence:.2f}%)\") \n    axes[i].axis('off')\n\nplt.tight_layout() \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:50:48.596072Z","iopub.execute_input":"2025-02-14T11:50:48.596899Z","iopub.status.idle":"2025-02-14T11:50:52.492154Z","shell.execute_reply.started":"2025-02-14T11:50:48.596867Z","shell.execute_reply":"2025-02-14T11:50:52.491295Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# **Model Xception**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import Xception\nfrom tensorflow.keras.layers import Flatten, Dense\nfrom tensorflow.keras.optimizers import SGD\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom livelossplot import PlotLossesKeras\n\nmodel = Xception(\n    include_top=False, \n    input_shape=(299, 299, 3) \n)\n\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:51:45.710665Z","iopub.execute_input":"2025-02-14T11:51:45.711447Z","iopub.status.idle":"2025-02-14T11:51:47.805733Z","shell.execute_reply.started":"2025-02-14T11:51:45.711416Z","shell.execute_reply":"2025-02-14T11:51:47.792413Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for index, layer in enumerate(model.layers[:15]):\n    if hasattr(layer, 'output_shape'):\n        print(index, layer.name, layer.output_shape)\n    layer.trainable = False\n\nfor index, layer in enumerate(model.layers[15:]):\n    if hasattr(layer, 'output_shape'):\n        print(index, layer.name, layer.output_shape)\n    layer.trainable = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:52:17.110626Z","iopub.execute_input":"2025-02-14T11:52:17.111125Z","iopub.status.idle":"2025-02-14T11:52:17.131832Z","shell.execute_reply.started":"2025-02-14T11:52:17.11108Z","shell.execute_reply":"2025-02-14T11:52:17.130824Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"flat1 = Flatten()(model.layers[-1].output)\nclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1) \noutput = Dense(1, activation='sigmoid')(class1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:52:43.637533Z","iopub.execute_input":"2025-02-14T11:52:43.637899Z","iopub.status.idle":"2025-02-14T11:52:43.668178Z","shell.execute_reply.started":"2025-02-14T11:52:43.63787Z","shell.execute_reply":"2025-02-14T11:52:43.667145Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"my_model = Model(inputs=model.inputs, outputs=output)\n\nopt = SGD(learning_rate=0.0001, momentum=0.9)\nmy_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n\nmy_model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:53:02.332705Z","iopub.execute_input":"2025-02-14T11:53:02.333367Z","iopub.status.idle":"2025-02-14T11:53:02.652919Z","shell.execute_reply.started":"2025-02-14T11:53:02.333337Z","shell.execute_reply":"2025-02-14T11:53:02.652057Z"},"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_it = train_datagen.flow_from_directory(\n    'dataset_dogs_vs_cats/train/',\n    class_mode='binary',\n    batch_size=32,  \n    target_size=(299, 299), \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:53:25.217917Z","iopub.execute_input":"2025-02-14T11:53:25.218253Z","iopub.status.idle":"2025-02-14T11:53:26.031065Z","shell.execute_reply.started":"2025-02-14T11:53:25.218226Z","shell.execute_reply":"2025-02-14T11:53:26.030271Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_it = test_datagen.flow_from_directory(\n    'dataset_dogs_vs_cats/test/', \n    class_mode='binary',\n    batch_size=32, \n    target_size=(299, 299), \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:53:38.655234Z","iopub.execute_input":"2025-02-14T11:53:38.655561Z","iopub.status.idle":"2025-02-14T11:53:38.834106Z","shell.execute_reply.started":"2025-02-14T11:53:38.655534Z","shell.execute_reply":"2025-02-14T11:53:38.833407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = my_model.fit(\n    train_it, \n    validation_data=test_it, \n    epochs=3, \n    verbose=1,\n)\n\n_, acc = my_model.evaluate(test_it, verbose=1) \nprint('> %.3f' % (acc * 100.0))\n\nsummarize_diagnostics(history)\n\nbest_model = 'xception_model.hdf5'\nmy_model.save(best_model)\nprint(f'Model saved to {best_model}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T11:54:06.310891Z","iopub.execute_input":"2025-02-14T11:54:06.311619Z","iopub.status.idle":"2025-02-14T12:22:35.02168Z","shell.execute_reply.started":"2025-02-14T11:54:06.311589Z","shell.execute_reply":"2025-02-14T12:22:35.020733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import load_model\n\nmodel = load_model('xception_model.hdf5') \n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_it = test_datagen.flow_from_directory(\n    'dataset_dogs_vs_cats/test/',\n    class_mode='binary',\n    batch_size=1,\n    target_size=(299, 299),\n    shuffle=True \n)\n\nnum_samples = 10\nselected_images = []\nselected_labels = []\n\nnum_dogs = 0\nnum_cats = 0\n\nwhile len(selected_images) < num_samples:\n    img, label = next(test_it)  \n    if label[0] == 1 and num_dogs < 5: \n        selected_images.append(img[0]) \n        selected_labels.append(label[0])  \n        num_dogs += 1\n    elif label[0] == 0 and num_cats < 5:  \n        selected_images.append(img[0])  \n        selected_labels.append(label[0])\n        num_cats += 1\n\nselected_images = np.array(selected_images)\n\npredictions = model.predict(selected_images)\n\npred_labels = (predictions > 0.5).astype(int)\n\nfig, axes = plt.subplots(2, 5, figsize=(12, 6))\naxes = axes.ravel()\n\nfor i in range(num_samples):\n    axes[i].imshow(selected_images[i]) \n    true_label = \"Dog\" if selected_labels[i] == 1 else \"Cat\"  \n    pred_label = \"Dog\" if pred_labels[i] == 1 else \"Cat\" \n    pred_confidence = predictions[i][0] * 100  \n    axes[i].set_title(f\"True: {true_label}\\nPred: {pred_label} ({pred_confidence:.2f}%)\") \n    axes[i].axis('off')\n\nplt.tight_layout() \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2025-02-14T12:25:36.445735Z","iopub.execute_input":"2025-02-14T12:25:36.446587Z","iopub.status.idle":"2025-02-14T12:25:42.204874Z","shell.execute_reply.started":"2025-02-14T12:25:36.446555Z","shell.execute_reply":"2025-02-14T12:25:42.203841Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"xcep_model = Xception(include_top=False, input_shape=(299, 299, 3))\nxcep_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T04:11:23.494572Z","iopub.execute_input":"2024-01-27T04:11:23.495217Z","iopub.status.idle":"2024-01-27T04:11:26.579883Z","shell.execute_reply.started":"2024-01-27T04:11:23.495182Z","shell.execute_reply":"2024-01-27T04:11:26.578335Z"}}}]}