{"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\n# import os\n# for 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":"2026-09-18T09:40:45.550592Z","iopub.execute_input":"2026-09-18T09:40:45.551604Z","iopub.status.idle":"2026-09-18T09:40:46.023916Z","shell.execute_reply.started":"2026-09-18T09:40:45.551573Z","shell.execute_reply":"2026-09-18T09:40:46.022974Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:40:46.026114Z","iopub.execute_input":"2026-09-18T09:40:46.026838Z","iopub.status.idle":"2026-09-18T09:40:47.178423Z","shell.execute_reply.started":"2026-09-18T09:40:46.026806Z","shell.execute_reply":"2026-09-18T09:40:47.177181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!uname --all","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:40:47.180167Z","iopub.execute_input":"2026-09-18T09:40:47.180519Z","iopub.status.idle":"2026-09-18T09:40:48.213407Z","shell.execute_reply.started":"2026-09-18T09:40:47.180491Z","shell.execute_reply":"2026-09-18T09:40:48.212404Z"}},"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":"2026-09-18T09:40:48.215011Z","iopub.execute_input":"2026-09-18T09:40:48.215314Z","iopub.status.idle":"2026-09-18T09:41:01.981355Z","shell.execute_reply.started":"2026-09-18T09:40:48.215288Z","shell.execute_reply":"2026-09-18T09:41:01.980555Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:01.983585Z","iopub.execute_input":"2026-09-18T09:41:01.984283Z","iopub.status.idle":"2026-09-18T09:41:03.016698Z","shell.execute_reply.started":"2026-09-18T09:41:01.984254Z","shell.execute_reply":"2026-09-18T09:41:03.015766Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:03.018101Z","iopub.execute_input":"2026-09-18T09:41:03.018408Z","iopub.status.idle":"2026-09-18T09:41:04.04686Z","shell.execute_reply.started":"2026-09-18T09:41:03.01838Z","shell.execute_reply":"2026-09-18T09:41:04.045506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls test1/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:04.048999Z","iopub.execute_input":"2026-09-18T09:41:04.050032Z","iopub.status.idle":"2026-09-18T09:41:05.251296Z","shell.execute_reply.started":"2026-09-18T09:41:04.049992Z","shell.execute_reply":"2026-09-18T09:41:05.249902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:05.253087Z","iopub.execute_input":"2026-09-18T09:41:05.253425Z","iopub.status.idle":"2026-09-18T09:41:06.479723Z","shell.execute_reply.started":"2026-09-18T09:41:05.253398Z","shell.execute_reply":"2026-09-18T09:41:06.478368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#ls train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:06.481334Z","iopub.execute_input":"2026-09-18T09:41:06.481661Z","iopub.status.idle":"2026-09-18T09:41:06.486587Z","shell.execute_reply.started":"2026-09-18T09:41:06.481635Z","shell.execute_reply":"2026-09-18T09:41:06.485556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -U train | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:06.488077Z","iopub.execute_input":"2026-09-18T09:41:06.489038Z","iopub.status.idle":"2026-09-18T09:41:07.599242Z","shell.execute_reply.started":"2026-09-18T09:41:06.489Z","shell.execute_reply":"2026-09-18T09:41:07.597915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -U test1 | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:07.601156Z","iopub.execute_input":"2026-09-18T09:41:07.602842Z","iopub.status.idle":"2026-09-18T09:41:08.698493Z","shell.execute_reply.started":"2026-09-18T09:41:07.602809Z","shell.execute_reply":"2026-09-18T09:41:08.697229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot dog photos from the dogs vs cats dataset\nfrom matplotlib import pyplot\n# from matplotlib import pyplot as plt\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":"2026-09-18T09:41:08.701752Z","iopub.execute_input":"2026-09-18T09:41:08.702064Z","iopub.status.idle":"2026-09-18T09:41:09.949837Z","shell.execute_reply.started":"2026-09-18T09:41:08.702036Z","shell.execute_reply":"2026-09-18T09:41:09.948774Z"},"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":"2026-09-18T09:41:09.951184Z","iopub.execute_input":"2026-09-18T09:41:09.951916Z","iopub.status.idle":"2026-09-18T09:41:11.121708Z","shell.execute_reply.started":"2026-09-18T09:41:09.951885Z","shell.execute_reply":"2026-09-18T09:41:11.120579Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:11.125744Z","iopub.execute_input":"2026-09-18T09:41:11.126344Z","iopub.status.idle":"2026-09-18T09:41:12.202045Z","shell.execute_reply.started":"2026-09-18T09:41:11.126313Z","shell.execute_reply":"2026-09-18T09:41:12.201025Z"}},"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":"2026-09-18T09:41:12.20408Z","iopub.execute_input":"2026-09-18T09:41:12.204542Z","iopub.status.idle":"2026-09-18T09:41:25.189578Z","shell.execute_reply.started":"2026-09-18T09:41:12.204512Z","shell.execute_reply":"2026-09-18T09:41:25.188413Z"}},"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":"2026-09-18T09:41:25.19091Z","iopub.execute_input":"2026-09-18T09:41:25.191554Z","iopub.status.idle":"2026-09-18T09:41:25.196458Z","shell.execute_reply.started":"2026-09-18T09:41:25.191523Z","shell.execute_reply":"2026-09-18T09:41:25.19543Z"},"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":"2026-09-18T09:41:25.197747Z","iopub.execute_input":"2026-09-18T09:41:25.198318Z","iopub.status.idle":"2026-09-18T09:41:25.223272Z","shell.execute_reply.started":"2026-09-18T09:41:25.19829Z","shell.execute_reply":"2026-09-18T09:41:25.222172Z"},"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":"2026-09-18T09:41:25.224784Z","iopub.execute_input":"2026-09-18T09:41:25.225205Z","iopub.status.idle":"2026-09-18T09:41:25.234386Z","shell.execute_reply.started":"2026-09-18T09:41:25.225168Z","shell.execute_reply":"2026-09-18T09:41:25.233534Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:25.23574Z","iopub.execute_input":"2026-09-18T09:41:25.23755Z","iopub.status.idle":"2026-09-18T09:41:26.294745Z","shell.execute_reply.started":"2026-09-18T09:41:25.237521Z","shell.execute_reply":"2026-09-18T09:41:26.293375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:26.296555Z","iopub.execute_input":"2026-09-18T09:41:26.297029Z","iopub.status.idle":"2026-09-18T09:41:27.376534Z","shell.execute_reply.started":"2026-09-18T09:41:26.296987Z","shell.execute_reply":"2026-09-18T09:41:27.375531Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/test #validation","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:27.378292Z","iopub.execute_input":"2026-09-18T09:41:27.378749Z","iopub.status.idle":"2026-09-18T09:41:28.438946Z","shell.execute_reply.started":"2026-09-18T09:41:27.378706Z","shell.execute_reply":"2026-09-18T09:41:28.437919Z"}},"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":"2026-09-18T09:41:28.440454Z","iopub.execute_input":"2026-09-18T09:41:28.440818Z","iopub.status.idle":"2026-09-18T09:41:30.717116Z","shell.execute_reply.started":"2026-09-18T09:41:28.44079Z","shell.execute_reply":"2026-09-18T09:41:30.716271Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(src)\nprint(dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:30.718292Z","iopub.execute_input":"2026-09-18T09:41:30.718715Z","iopub.status.idle":"2026-09-18T09:41:30.724077Z","shell.execute_reply.started":"2026-09-18T09:41:30.718651Z","shell.execute_reply":"2026-09-18T09:41:30.723025Z"}},"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":"2026-09-18T09:41:30.725261Z","iopub.execute_input":"2026-09-18T09:41:30.7256Z","iopub.status.idle":"2026-09-18T09:41:33.033366Z","shell.execute_reply.started":"2026-09-18T09:41:30.725576Z","shell.execute_reply":"2026-09-18T09:41:33.03178Z"},"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":"2026-09-18T09:41:33.035099Z","iopub.execute_input":"2026-09-18T09:41:33.035443Z","iopub.status.idle":"2026-09-18T09:41:35.24141Z","shell.execute_reply.started":"2026-09-18T09:41:33.035412Z","shell.execute_reply":"2026-09-18T09:41:35.239882Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"execution":{"iopub.status.busy":"2026-09-18T09:41:35.243237Z","iopub.execute_input":"2026-09-18T09:41:35.243804Z","iopub.status.idle":"2026-09-18T09:41:46.379833Z","shell.execute_reply.started":"2026-09-18T09:41:35.243751Z","shell.execute_reply":"2026-09-18T09:41:46.37868Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2026-09-18T09:41:46.381325Z","iopub.execute_input":"2026-09-18T09:41:46.381736Z","iopub.status.idle":"2026-09-18T09:41:46.39927Z","shell.execute_reply.started":"2026-09-18T09:41:46.38166Z","shell.execute_reply":"2026-09-18T09:41:46.398231Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Prepare Final Dataset","metadata":{}},{"cell_type":"code","source":"# https://keras.io/api/applications/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:46.400574Z","iopub.execute_input":"2026-09-18T09:41:46.401132Z","iopub.status.idle":"2026-09-18T09:41:46.407464Z","shell.execute_reply.started":"2026-09-18T09:41:46.401103Z","shell.execute_reply":"2026-09-18T09:41:46.406468Z"}},"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":"2026-09-18T09:41:46.408731Z","iopub.execute_input":"2026-09-18T09:41:46.409045Z","iopub.status.idle":"2026-09-18T09:41:46.419497Z","shell.execute_reply.started":"2026-09-18T09:41:46.40902Z","shell.execute_reply":"2026-09-18T09:41:46.418746Z"}},"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":"2026-09-18T09:41:46.420986Z","iopub.execute_input":"2026-09-18T09:41:46.42173Z","iopub.status.idle":"2026-09-18T09:41:46.430784Z","shell.execute_reply.started":"2026-09-18T09:41:46.421677Z","shell.execute_reply":"2026-09-18T09:41:46.429643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_1 = define_model_1()\nprint(model_1.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:46.432226Z","iopub.execute_input":"2026-09-18T09:41:46.432698Z","iopub.status.idle":"2026-09-18T09:41:48.359915Z","shell.execute_reply.started":"2026-09-18T09:41:46.432643Z","shell.execute_reply":"2026-09-18T09:41:48.358941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 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    pyplot.show()\n    \n    # save plot to file\n    filename = sys.argv[0].split('/')[-1]\n    pyplot.savefig(filename + '_plot.png') \n    pyplot.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:48.361413Z","iopub.execute_input":"2026-09-18T09:41:48.361832Z","iopub.status.idle":"2026-09-18T09:41:48.370848Z","shell.execute_reply.started":"2026-09-18T09:41:48.361796Z","shell.execute_reply":"2026-09-18T09:41:48.369761Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32,\n                 (3, 3),\n                 activation='relu',\n                 input_shape=(200, 200, 3)\n                )\n         )\n\nmodel.add(MaxPooling2D((2, 2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, activation='relu'))\n\nmodel.add(Dense(2, activation='softmax'))\n\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:48.372171Z","iopub.execute_input":"2026-09-18T09:41:48.372573Z","iopub.status.idle":"2026-09-18T09:41:48.455438Z","shell.execute_reply.started":"2026-09-18T09:41:48.372546Z","shell.execute_reply":"2026-09-18T09:41:48.454377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# The Functional API\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense\n\n# explicitly define the input tensor topology\ninputs = Input(shape=(200, 200, 3))\n\n# Construct the forward pass of the computational graph\nx = Conv2D(32, (3, 3), activation='relu')(inputs)\nx = MaxPooling2D((2, 2))(x)\nx = Flatten()(x)\nx = Dense(128, activation='relu')(x)\n\n# Define the output tensor with the target objective function representation\noutputs = Dense(2, activation='softmax')(x)\n\n# Instantiate the formal model architecture\nmodel = Model(inputs=inputs, outputs=outputs)\n\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:48.456781Z","iopub.execute_input":"2026-09-18T09:41:48.457629Z","iopub.status.idle":"2026-09-18T09:41:48.529601Z","shell.execute_reply.started":"2026-09-18T09:41:48.457599Z","shell.execute_reply":"2026-09-18T09:41:48.528455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1.0/255.0)\ntype(datagen)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:48.531041Z","iopub.execute_input":"2026-09-18T09:41:48.531381Z","iopub.status.idle":"2026-09-18T09:41:48.537923Z","shell.execute_reply.started":"2026-09-18T09:41:48.531343Z","shell.execute_reply":"2026-09-18T09:41:48.536928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls dataset_dogs_vs_cats/train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:48.539161Z","iopub.execute_input":"2026-09-18T09:41:48.539833Z","iopub.status.idle":"2026-09-18T09:41:49.613472Z","shell.execute_reply.started":"2026-09-18T09:41:48.539807Z","shell.execute_reply":"2026-09-18T09:41:49.612426Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE = 224\nBATCH_SIZE = 64\nEPOCHS = 30","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:49.614952Z","iopub.execute_input":"2026-09-18T09:41:49.615269Z","iopub.status.idle":"2026-09-18T09:41:49.619858Z","shell.execute_reply.started":"2026-09-18T09:41:49.615244Z","shell.execute_reply":"2026-09-18T09:41:49.618952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Use another dataset\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# 1. Instantiate the generator with the validation_split ratio\ndatagen = ImageDataGenerator(\n    rescale=1.0/255.0,\n    horizontal_flip=True,\n    validation_split=0.2  # Defined here at initialization\n)\n\ntrain_path = '/kaggle/input/datasets/kasikrit/308-456-dogs-vs-cats-dataset/dataset_dogs_vs_cats/train'\n\n# 2. Extract the training subset (80% of data)\ntrain_it = datagen.flow_from_directory(\n    train_path,\n    class_mode='binary',\n    batch_size=BATCH_SIZE,\n    target_size=(IMAGE_SIZE, IMAGE_SIZE),\n    subset='training',     # Explicitly request the training partition\n    shuffle=True           # Stochastic sampling is standard for training\n)\n\n# 3. Extract the validation subset (20% of data)\nval_it = datagen.flow_from_directory(\n    train_path,\n    class_mode='binary',\n    batch_size=BATCH_SIZE,\n    target_size=(IMAGE_SIZE, IMAGE_SIZE),\n    subset='validation',   # Explicitly request the validation partition\n    shuffle=False          # Disable shuffle to maintain index alignment for metrics\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:41:49.621236Z","iopub.execute_input":"2026-09-18T09:41:49.621594Z","iopub.status.idle":"2026-09-18T09:42:04.351117Z","shell.execute_reply.started":"2026-09-18T09:41:49.621562Z","shell.execute_reply":"2026-09-18T09:42:04.350258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_it), len(train_it.classes), train_it.classes[:10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:04.352255Z","iopub.execute_input":"2026-09-18T09:42:04.352637Z","iopub.status.idle":"2026-09-18T09:42:04.360512Z","shell.execute_reply.started":"2026-09-18T09:42:04.352611Z","shell.execute_reply":"2026-09-18T09:42:04.359311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(val_it), len(val_it.classes), val_it.classes[-10:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:04.361845Z","iopub.execute_input":"2026-09-18T09:42:04.362212Z","iopub.status.idle":"2026-09-18T09:42:04.376223Z","shell.execute_reply.started":"2026-09-18T09:42:04.362187Z","shell.execute_reply":"2026-09-18T09:42:04.37513Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit model\n# model_1.reset_states()\n# history_1 = model_1.fit(\n#     train_it,\n#     steps_per_epoch=len(train_it),\n#     validation_data=val_it,\n#     validation_steps=len(val_it),\n#     epochs=EPOCHS,\n#     verbose=2,\n#     callbacks = [PlotLossesKeras()]\n#     )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:04.386139Z","iopub.execute_input":"2026-09-18T09:42:04.386527Z","iopub.status.idle":"2026-09-18T09:42:04.390716Z","shell.execute_reply.started":"2026-09-18T09:42:04.3865Z","shell.execute_reply":"2026-09-18T09:42:04.389743Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\n# summarize_diagnostics(history_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:04.391932Z","iopub.execute_input":"2026-09-18T09:42:04.392459Z","iopub.status.idle":"2026-09-18T09:42:04.399548Z","shell.execute_reply.started":"2026-09-18T09:42:04.392425Z","shell.execute_reply":"2026-09-18T09:42:04.398645Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# evaluate model\n# _, acc = model_1.evaluate(val_it, steps=len(val_it), verbose=1)\n# print('> %.3f' % (acc * 100.0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:04.400571Z","iopub.execute_input":"2026-09-18T09:42:04.401111Z","iopub.status.idle":"2026-09-18T09:42:04.407712Z","shell.execute_reply.started":"2026-09-18T09:42:04.401087Z","shell.execute_reply":"2026-09-18T09:42:04.406971Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Create test datagenerator from 308-456 Dogs vs Cats ","metadata":{}},{"cell_type":"code","source":"ls /kaggle/input/datasets/kasikrit/308-456-dogs-vs-cats-dataset/dataset_dogs_vs_cats/test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:04.408801Z","iopub.execute_input":"2026-09-18T09:42:04.409119Z","iopub.status.idle":"2026-09-18T09:42:05.4892Z","shell.execute_reply.started":"2026-09-18T09:42:04.409098Z","shell.execute_reply":"2026-09-18T09:42:05.487958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom glob import glob\n\n# Define the image folder path\ntest_path = '/kaggle/input/datasets/kasikrit/308-456-dogs-vs-cats-dataset/dataset_dogs_vs_cats/test'\n\nprint(test_path)\n# # List all .jpg files in the directory (adjust the pattern if needed)\n# image_files = glob(os.path.join(image_folder, '*.jpg'))\n\n# # Check if images were found\n# if not image_files:\n#     print(\"No images found. Please verify the directory and file extensions.\")\n    \n# # Create a DataFrame with the file paths\n# test_df = pd.DataFrame({'filename': image_files})\n\n# If your use-case doesn't require labels (for example, during testing), you can leave out the label column.\n# Otherwise, if you need a dummy label, you can add one:\n# df['class'] = 'dummy_label'\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.490825Z","iopub.execute_input":"2026-09-18T09:42:05.491148Z","iopub.status.idle":"2026-09-18T09:42:05.497043Z","shell.execute_reply.started":"2026-09-18T09:42:05.491122Z","shell.execute_reply":"2026-09-18T09:42:05.496089Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.49838Z","iopub.execute_input":"2026-09-18T09:42:05.499292Z","iopub.status.idle":"2026-09-18T09:42:05.50946Z","shell.execute_reply.started":"2026-09-18T09:42:05.499258Z","shell.execute_reply":"2026-09-18T09:42:05.508657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# image_files[:5], type(image_files), len(image_files)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.510808Z","iopub.execute_input":"2026-09-18T09:42:05.511608Z","iopub.status.idle":"2026-09-18T09:42:05.518874Z","shell.execute_reply.started":"2026-09-18T09:42:05.511572Z","shell.execute_reply":"2026-09-18T09:42:05.518031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.520122Z","iopub.execute_input":"2026-09-18T09:42:05.520452Z","iopub.status.idle":"2026-09-18T09:42:05.527788Z","shell.execute_reply.started":"2026-09-18T09:42:05.520411Z","shell.execute_reply":"2026-09-18T09:42:05.526959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.loc[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.528784Z","iopub.execute_input":"2026-09-18T09:42:05.529049Z","iopub.status.idle":"2026-09-18T09:42:05.537277Z","shell.execute_reply.started":"2026-09-18T09:42:05.529028Z","shell.execute_reply":"2026-09-18T09:42:05.53657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.538457Z","iopub.execute_input":"2026-09-18T09:42:05.538847Z","iopub.status.idle":"2026-09-18T09:42:05.54584Z","shell.execute_reply.started":"2026-09-18T09:42:05.538823Z","shell.execute_reply":"2026-09-18T09:42:05.545058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Create test data generator\n# from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# # Initialize your ImageDataGenerator (modify parameters as needed)\n# datagen_test = ImageDataGenerator(rescale=1./255,\n#                                  vertical_flip=True)\n\n# # Create the data generator using the DataFrame\n# test_it = datagen_test.flow_from_dataframe(\n#     dataframe=test_df,\n#     x_col='filename',\n#     y_col=None,            # No labels for testing, so set to None\n#     target_size=IMAGE_SIZE,\n#     batch_size=BATCH_SIZE,\n#     class_mode=None,       # Since there are no labels\n#     # shuffle=True,         # Usually testing data should not be shuffled\n#     shuffle=False,\n# )\n\n# type(test_it)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.54858Z","iopub.execute_input":"2026-09-18T09:42:05.549282Z","iopub.status.idle":"2026-09-18T09:42:05.554471Z","shell.execute_reply.started":"2026-09-18T09:42:05.549246Z","shell.execute_reply":"2026-09-18T09:42:05.553718Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.555606Z","iopub.execute_input":"2026-09-18T09:42:05.556025Z","iopub.status.idle":"2026-09-18T09:42:05.564394Z","shell.execute_reply.started":"2026-09-18T09:42:05.556Z","shell.execute_reply":"2026-09-18T09:42:05.563342Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_it = datagen.flow_from_directory(\n    test_path,\n    class_mode='binary',\n    batch_size=BATCH_SIZE,\n    target_size=(IMAGE_SIZE, IMAGE_SIZE)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:05.565821Z","iopub.execute_input":"2026-09-18T09:42:05.566767Z","iopub.status.idle":"2026-09-18T09:42:09.96377Z","shell.execute_reply.started":"2026-09-18T09:42:05.566728Z","shell.execute_reply":"2026-09-18T09:42:09.962643Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plot_exmaple(images, labels):\n    # Assuming images shape is (64, height, width, channels)\n    fig, axes = plt.subplots(8, 8, figsize=(16, 16))\n    axes = axes.flatten()  # Flatten the 8x8 grid into a 1D array for easy iteration\n\n    for i, ax in enumerate(axes):\n        if i < len(images):\n            ax.imshow(images[i])\n            ax.set_title(str(labels[i]))  # Fixed: set_title instead of title\n            # ax.axis(\"off\")  # Turn off axis ticks/labels for clarity\n            ax.axis(\"on\")\n        else:\n            ax.axis(\"off\")  # Hide unused subplots if images < 64\n\n    plt.tight_layout()\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:09.96513Z","iopub.execute_input":"2026-09-18T09:42:09.965916Z","iopub.status.idle":"2026-09-18T09:42:09.973328Z","shell.execute_reply.started":"2026-09-18T09:42:09.965873Z","shell.execute_reply":"2026-09-18T09:42:09.972358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images, train_labels = train_it.__next__()\nprint(train_images.shape, len(train_labels))\nplot_exmaple(train_images, train_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:09.974676Z","iopub.execute_input":"2026-09-18T09:42:09.975116Z","iopub.status.idle":"2026-09-18T09:42:19.86283Z","shell.execute_reply.started":"2026-09-18T09:42:09.975039Z","shell.execute_reply":"2026-09-18T09:42:19.861263Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_images, val_labels = val_it.__next__()\nval_images.shape\nplot_exmaple(val_images, val_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:19.864571Z","iopub.execute_input":"2026-09-18T09:42:19.864931Z","iopub.status.idle":"2026-09-18T09:42:29.881244Z","shell.execute_reply.started":"2026-09-18T09:42:19.864905Z","shell.execute_reply":"2026-09-18T09:42:29.879986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_exmaple_test(images, indices):\n    # Assuming images shape is (64, height, width, channels)\n    fig, axes = plt.subplots(8, 8, figsize=(16, 16))\n    axes = axes.flatten()  # Flatten the 8x8 grid into a 1D array for easy iteration\n\n    for i, ax in enumerate(axes):\n        if i < len(images):\n            ax.imshow(images[i])\n            ax.set_title(str(indices[i]))  # ตั้งค่า title ด้วย index จาก list/array indices\n            ax.axis(\"off\")  # Turn off axis ticks/labels for clarity\n        else:\n            ax.axis(\"off\")  # Hide unused subplots if images < 64\n\n    plt.tight_layout()\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:29.883285Z","iopub.execute_input":"2026-09-18T09:42:29.88374Z","iopub.status.idle":"2026-09-18T09:42:29.889821Z","shell.execute_reply.started":"2026-09-18T09:42:29.883661Z","shell.execute_reply":"2026-09-18T09:42:29.888987Z"},"jupyter":{"source_hidden":true}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_images, test_labels = test_it.__next__()  # หรือ next(test_it)\nprint(f\"{test_images.shape=}, {test_labels.shape}\")\n\n# สร้าง list เลขดัชนีตามจำนวนภาพที่มี\nindices = list(range(len(test_images)))\n\nplot_exmaple(test_images, test_labels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:29.891031Z","iopub.execute_input":"2026-09-18T09:42:29.891307Z","iopub.status.idle":"2026-09-18T09:42:40.378355Z","shell.execute_reply.started":"2026-09-18T09:42:29.891272Z","shell.execute_reply":"2026-09-18T09:42:40.37506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import numpy as np\n\n\n# # 1. สุ่ม index มา 10 ตัวจากชุดข้อมูล\n# sample_indices = np.random.choice(len(test_images), size=10, replace=False)\n# sample_images = test_images[sample_indices]\n\n# # 2. Predict \n# predictions = model_1.predict(sample_images)\n# predicted_classes = np.argmax(predictions, axis=1)  # ดึง class ที่ได้คะแนนสูงสุด\n\n# # 3. แสดงผล 10 ภาพ (ปรับเป็น grid 2x5)\n# fig, axes = plt.subplots(2, 5, figsize=(15, 6))\n# axes = axes.flatten()\n\n# for i, ax in enumerate(axes):\n#     ax.imshow(sample_images[i])\n#     # แสดง Index เดิม และ Class ที่ทายได้\n#     ax.set_title(f\"Idx: {sample_indices[i]}\\nPred: {predicted_classes[i]}\")\n#     ax.axis(\"off\")\n\n# plt.tight_layout()\n# plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:40.379789Z","iopub.execute_input":"2026-09-18T09:42:40.380094Z","iopub.status.idle":"2026-09-18T09:42:40.385515Z","shell.execute_reply.started":"2026-09-18T09:42:40.380069Z","shell.execute_reply":"2026-09-18T09:42:40.384438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 2026-09-13\n# Transfer Learning","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:40.38723Z","iopub.execute_input":"2026-09-18T09:42:40.387583Z","iopub.status.idle":"2026-09-18T09:42:40.400904Z","shell.execute_reply.started":"2026-09-18T09:42:40.387553Z","shell.execute_reply":"2026-09-18T09:42:40.399764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport random\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\nfrom pathlib import Path\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix, accuracy_score\n\nprint(\"TensorFlow:\", tf.__version__)\nprint(\"GPU:\", tf.config.list_physical_devices(\"GPU\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:40.402455Z","iopub.execute_input":"2026-09-18T09:42:40.402931Z","iopub.status.idle":"2026-09-18T09:42:40.809842Z","shell.execute_reply.started":"2026-09-18T09:42:40.402861Z","shell.execute_reply":"2026-09-18T09:42:40.808659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"SEED = 42\nrandom.seed(SEED)\nnp.random.seed(SEED)\ntf.random.set_seed(SEED)\n\nIMG_SIZE = (224, 224)\nBATCH_SIZE = 32\nNUM_CLASSES = 2\nAUTOTUNE = tf.data.AUTOTUNE\n\nHEAD_EPOCHS = 8\nFINE_TUNE_EPOCHS = 12\nLEARNING_RATE_HEAD = 1e-3\nLEARNING_RATE_FINE = 1e-5\n\nCLASS_NAMES = [\"cat\", \"dog\"]\nLABEL_TO_ID = {\"cat\": 0, \"dog\": 1}\nID_TO_LABEL = {0: \"cat\", 1: \"dog\"}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:40.811119Z","iopub.execute_input":"2026-09-18T09:42:40.811528Z","iopub.status.idle":"2026-09-18T09:42:40.821962Z","shell.execute_reply.started":"2026-09-18T09:42:40.811498Z","shell.execute_reply":"2026-09-18T09:42:40.820606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Search common Kaggle locations first, then fall back to /kaggle/input.\nSEARCH_ROOTS = [\n    Path(\"/kaggle/input\"),\n    Path(\"/kaggle/working\"),\n    Path(\".\")\n]\n\nimage_files = []\nfor root in SEARCH_ROOTS:\n    if root.exists():\n        image_files.extend(root.rglob(\"*.jpg\"))\n        image_files.extend(root.rglob(\"*.jpeg\"))\n        image_files.extend(root.rglob(\"*.png\"))\n\n# Keep only labeled dog/cat images.\nrecords = []\nfor p in sorted(set(image_files)):\n    name = p.name.lower()\n    if name.startswith(\"cat.\") or name.startswith(\"dog.\"):\n        label = \"cat\" if name.startswith(\"cat.\") else \"dog\"\n        records.append((str(p), label, LABEL_TO_ID[label]))\n\ndf = pd.DataFrame(records, columns=[\"filepath\", \"label\", \"label_id\"]).drop_duplicates(\"filepath\")\n\nif len(df) == 0:\n    raise FileNotFoundError(\n        \"No cat/dog images found. Add the Dogs vs Cats dataset to the Kaggle notebook input.\"\n    )\n\nprint(\"Total labeled images:\", len(df))\nprint(df[\"label\"].value_counts())\ndf.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:40.823603Z","iopub.execute_input":"2026-09-18T09:42:40.824075Z","iopub.status.idle":"2026-09-18T09:42:54.27617Z","shell.execute_reply.started":"2026-09-18T09:42:40.824036Z","shell.execute_reply":"2026-09-18T09:42:54.27539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Stratified split: 20% test, then 20% of the remaining 80% for validation -> 16% overall.\ntrain_val_df, test_df = train_test_split(\n    df,\n    test_size=0.20,\n    stratify=df[\"label_id\"],\n    random_state=SEED\n)\n\ntrain_df, val_df = train_test_split(\n    train_val_df,\n    test_size=0.20,\n    stratify=train_val_df[\"label_id\"],\n    random_state=SEED\n)\n\ntrain_df = train_df.reset_index(drop=True)\nval_df = val_df.reset_index(drop=True)\ntest_df = test_df.reset_index(drop=True)\n\nprint(f\"Train: {len(train_df)}\")\nprint(f\"Validation: {len(val_df)}\")\nprint(f\"Test: {len(test_df)}\")\n\nassert set(train_df.filepath).isdisjoint(val_df.filepath)\nassert set(train_df.filepath).isdisjoint(test_df.filepath)\nassert set(val_df.filepath).isdisjoint(test_df.filepath)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:54.277211Z","iopub.execute_input":"2026-09-18T09:42:54.277892Z","iopub.status.idle":"2026-09-18T09:42:54.444803Z","shell.execute_reply.started":"2026-09-18T09:42:54.277864Z","shell.execute_reply":"2026-09-18T09:42:54.443816Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_image(path, label):\n    image = tf.io.read_file(path)\n    image = tf.image.decode_image(image, channels=3, expand_animations=False)\n    image = tf.image.resize(image, IMG_SIZE)\n    image = tf.cast(image, tf.float32)\n    return image, label\n\n\ndef make_dataset(frame, training=False):\n    paths = frame[\"filepath\"].values\n    labels = frame[\"label_id\"].values.astype(np.int32)\n\n    ds = tf.data.Dataset.from_tensor_slices((paths, labels))\n    if training:\n        ds = ds.shuffle(len(frame), seed=SEED, reshuffle_each_iteration=True)\n    ds = ds.map(load_image, num_parallel_calls=AUTOTUNE)\n    ds = ds.batch(BATCH_SIZE).prefetch(AUTOTUNE)\n    return ds\n\ntrain_ds = make_dataset(train_df, training=True)\nval_ds = make_dataset(val_df, training=False)\ntest_ds = make_dataset(test_df, training=False)\n\nprint(train_ds, val_ds, test_ds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:54.445995Z","iopub.execute_input":"2026-09-18T09:42:54.44624Z","iopub.status.idle":"2026-09-18T09:42:54.611652Z","shell.execute_reply.started":"2026-09-18T09:42:54.446219Z","shell.execute_reply":"2026-09-18T09:42:54.610765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_augmentation = tf.keras.Sequential([\n    tf.keras.layers.RandomFlip(\"horizontal\"),\n    tf.keras.layers.RandomRotation(0.08),\n    tf.keras.layers.RandomZoom(0.10),\n    tf.keras.layers.RandomContrast(0.10),\n], name=\"data_augmentation\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:54.612875Z","iopub.execute_input":"2026-09-18T09:42:54.613186Z","iopub.status.idle":"2026-09-18T09:42:54.630235Z","shell.execute_reply.started":"2026-09-18T09:42:54.613161Z","shell.execute_reply":"2026-09-18T09:42:54.628947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def build_transfer_model(base_model, model_name):\n    base_model.trainable = False\n\n    inputs = tf.keras.Input(shape=(*IMG_SIZE, 3), name=\"image\")\n    x = data_augmentation(inputs)\n    x = base_model(x, training=False)\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.keras.layers.Dropout(0.35)(x)\n    x = tf.keras.layers.Dense(128, activation=\"relu\")(x)\n    x = tf.keras.layers.Dropout(0.25)(x)\n    outputs = tf.keras.layers.Dense(1, activation=\"sigmoid\", name=\"prediction\")(x)\n\n    model = tf.keras.Model(inputs, outputs, name=model_name)\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE_HEAD),\n        loss=\"binary_crossentropy\",\n        metrics=[tf.keras.metrics.BinaryAccuracy(name=\"accuracy\"),\n                 tf.keras.metrics.AUC(name=\"auc\")]\n    )\n    return model\n\n\nclass LiveTrainingPlot(tf.keras.callbacks.Callback):\n    \"\"\"One persistent figure per model; update the same figure after every epoch.\"\"\"\n\n    def __init__(self, model_label):\n        super().__init__()\n        self.model_label = model_label\n        self.global_epoch = 0\n        self.epochs = []\n        self.train_acc = []\n        self.val_acc = []\n        self.train_loss = []\n        self.val_loss = []\n        self.fig = None\n        self.axes = None\n        self.display_handle = None\n\n    def on_train_begin(self, logs=None):\n        # Create the chart only once. Reuse it for both head training and fine-tuning.\n        if self.fig is None:\n            from IPython.display import display\n            self.fig, self.axes = plt.subplots(1, 2, figsize=(13, 4.5))\n            self.fig.suptitle(f\"{self.model_label} — Training Progress\")\n            self.display_handle = display(self.fig, display_id=True)\n\n    def on_epoch_end(self, epoch, logs=None):\n        logs = logs or {}\n        self.global_epoch += 1\n        self.epochs.append(self.global_epoch)\n        self.train_acc.append(logs.get(\"accuracy\"))\n        self.val_acc.append(logs.get(\"val_accuracy\"))\n        self.train_loss.append(logs.get(\"loss\"))\n        self.val_loss.append(logs.get(\"val_loss\"))\n\n        ax_acc, ax_loss = self.axes\n\n        # Update the same axes instead of creating a new figure.\n        ax_acc.clear()\n        ax_loss.clear()\n\n        ax_acc.plot(self.epochs, self.train_acc, marker=\"o\", label=\"Train Accuracy\")\n        ax_acc.plot(self.epochs, self.val_acc, marker=\"o\", label=\"Validation Accuracy\")\n        ax_acc.set_title(\"Accuracy\")\n        ax_acc.set_xlabel(\"Epoch\")\n        ax_acc.set_ylabel(\"Accuracy\")\n        ax_acc.set_ylim(0, 1.0)\n        ax_acc.grid(True, alpha=0.25)\n        ax_acc.legend()\n\n        ax_loss.plot(self.epochs, self.train_loss, marker=\"o\", label=\"Train Loss\")\n        ax_loss.plot(self.epochs, self.val_loss, marker=\"o\", label=\"Validation Loss\")\n        ax_loss.set_title(\"Loss\")\n        ax_loss.set_xlabel(\"Epoch\")\n        ax_loss.set_ylabel(\"Loss\")\n        ax_loss.grid(True, alpha=0.25)\n        ax_loss.legend()\n\n        self.fig.suptitle(f\"{self.model_label} — Training Progress (Epoch {self.global_epoch})\")\n        self.fig.tight_layout()\n\n        if self.display_handle is not None:\n            self.display_handle.update(self.fig)\n\n        print(\n            f\"Epoch {self.global_epoch}: \"\n            f\"accuracy={logs.get('accuracy', float('nan')):.4f}, \"\n            f\"val_accuracy={logs.get('val_accuracy', float('nan')):.4f}, \"\n            f\"loss={logs.get('loss', float('nan')):.4f}, \"\n            f\"val_loss={logs.get('val_loss', float('nan')):.4f}\"\n        )\n\n\ndef make_callbacks(model_name, live_plot):\n    return [\n        live_plot,\n        tf.keras.callbacks.EarlyStopping(\n            monitor=\"val_accuracy\", patience=3, mode=\"max\", restore_best_weights=True\n        ),\n        tf.keras.callbacks.ReduceLROnPlateau(\n            monitor=\"val_loss\", factor=0.25, patience=2, min_lr=1e-7\n        ),\n        tf.keras.callbacks.ModelCheckpoint(\n            f\"{model_name}_best.weights.h5\",\n            monitor=\"val_accuracy\",\n            mode=\"max\",\n            save_best_only=True,\n            save_weights_only=True\n        )\n    ]\n\n\ndef fine_tune(model, unfreeze_from_fraction=0.65):\n    base_model = None\n    for layer in model.layers:\n        if isinstance(layer, tf.keras.Model) and layer.name in [\"efficientnetb0\", \"resnet50v2\", \"mobilenetv2_1.00_224\"]:\n            base_model = layer\n            break\n\n    if base_model is None:\n        base_model = next((layer for layer in model.layers if isinstance(layer, tf.keras.Model)), None)\n\n    if base_model is None:\n        raise RuntimeError(\"Could not locate the pretrained backbone.\")\n\n    base_model.trainable = True\n    freeze_until = int(len(base_model.layers) * unfreeze_from_fraction)\n    for i, layer in enumerate(base_model.layers):\n        layer.trainable = i >= freeze_until\n        if isinstance(layer, tf.keras.layers.BatchNormalization):\n            layer.trainable = False\n\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=LEARNING_RATE_FINE),\n        loss=\"binary_crossentropy\",\n        metrics=[tf.keras.metrics.BinaryAccuracy(name=\"accuracy\"),\n                 tf.keras.metrics.AUC(name=\"auc\")]\n    )\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:54.631962Z","iopub.execute_input":"2026-09-18T09:42:54.632238Z","iopub.status.idle":"2026-09-18T09:42:54.653185Z","shell.execute_reply.started":"2026-09-18T09:42:54.632214Z","shell.execute_reply":"2026-09-18T09:42:54.652055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"efficientnet_base = tf.keras.applications.EfficientNetB0(\n    include_top=False,\n    weights=\"imagenet\",\n    input_shape=(*IMG_SIZE, 3)\n)\n\nefficientnet_model = build_transfer_model(\n    efficientnet_base,\n    \"EfficientNetB0_DogCat\"\n)\n\nefficientnet_model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:54.654523Z","iopub.execute_input":"2026-09-18T09:42:54.654969Z","iopub.status.idle":"2026-09-18T09:42:58.713687Z","shell.execute_reply.started":"2026-09-18T09:42:54.654932Z","shell.execute_reply":"2026-09-18T09:42:58.712981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"eff_live_plot = LiveTrainingPlot(\"EfficientNetB0\")\n\nhistory_eff_head = efficientnet_model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=HEAD_EPOCHS,\n    callbacks=make_callbacks(\"efficientnetb0_head\", eff_live_plot),\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T09:42:58.714952Z","iopub.execute_input":"2026-09-18T09:42:58.715328Z","iopub.status.idle":"2026-09-18T10:12:51.938212Z","shell.execute_reply.started":"2026-09-18T09:42:58.715294Z","shell.execute_reply":"2026-09-18T10:12:51.937337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"efficientnet_model = fine_tune(efficientnet_model, unfreeze_from_fraction=0.65)\n\nhistory_eff_ft = efficientnet_model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=FINE_TUNE_EPOCHS,\n    callbacks=make_callbacks(\"efficientnetb0_finetune\", eff_live_plot),\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-18T10:12:51.939347Z","iopub.execute_input":"2026-09-18T10:12:51.940005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def build_xception_model():\n    base = tf.keras.applications.Xception(\n        include_top=False,\n        weights=\"imagenet\",\n        input_shape=(*IMG_SIZE, 3)\n    )\n    base.trainable = False\n\n    inputs = tf.keras.Input(shape=(*IMG_SIZE, 3), name=\"image\")\n    x = data_augmentation(inputs)\n\n    # Xception ImageNet preprocessing: [0,255] -> [-1,1]\n    x = tf.keras.applications.xception.preprocess_input(x)\n    x = base(x, training=False)\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.keras.layers.Dropout(0.35)(x)\n    x = tf.keras.layers.Dense(256, activation=\"relu\")(x)\n    x = tf.keras.layers.Dropout(0.25)(x)\n    outputs = tf.keras.layers.Dense(1, activation=\"sigmoid\", name=\"prediction\")(x)\n\n    model = tf.keras.Model(inputs, outputs, name=\"Xception_DogCat\")\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=5e-4),\n        loss=\"binary_crossentropy\",\n        metrics=[\n            tf.keras.metrics.BinaryAccuracy(name=\"accuracy\"),\n            tf.keras.metrics.AUC(name=\"auc\")\n        ]\n    )\n    return model\n\nxception_model = build_xception_model()\nxception_model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xception_live_plot = LiveTrainingPlot(\"Xception\")\n\nhistory_xc_head = xception_model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=HEAD_EPOCHS,\n    callbacks=make_callbacks(\"xception_head\", xception_live_plot),\n    verbose=1\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xception_model = fine_tune(xception_model, unfreeze_from_fraction=0.65)\n\nhistory_xc_ft = xception_model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=FINE_TUNE_EPOCHS,\n    callbacks=make_callbacks(\"xception_finetune\", xception_live_plot),\n    verbose=1\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def build_densenet_model():\n    base = tf.keras.applications.DenseNet121(\n        include_top=False,\n        weights=\"imagenet\",\n        input_shape=(*IMG_SIZE, 3)\n    )\n    base.trainable = False\n\n    inputs = tf.keras.Input(shape=(*IMG_SIZE, 3), name=\"image\")\n    x = data_augmentation(inputs)\n\n    # DenseNet ImageNet preprocessing: RGB -> BGR and subtract ImageNet mean\n    x = tf.keras.applications.densenet.preprocess_input(x)\n    x = base(x, training=False)\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.keras.layers.Dropout(0.35)(x)\n    x = tf.keras.layers.Dense(256, activation=\"relu\")(x)\n    x = tf.keras.layers.Dropout(0.25)(x)\n    outputs = tf.keras.layers.Dense(1, activation=\"sigmoid\", name=\"prediction\")(x)\n\n    model = tf.keras.Model(inputs, outputs, name=\"DenseNet121_DogCat\")\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=5e-4),\n        loss=\"binary_crossentropy\",\n        metrics=[\n            tf.keras.metrics.BinaryAccuracy(name=\"accuracy\"),\n            tf.keras.metrics.AUC(name=\"auc\")\n        ]\n    )\n    return model\n\ndensenet_model = build_densenet_model()\ndensenet_model.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"densenet_live_plot = LiveTrainingPlot(\"DenseNet121\")\n\nhistory_dn_head = densenet_model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=HEAD_EPOCHS,\n    callbacks=make_callbacks(\"densenet121_head\", densenet_live_plot),\n    verbose=1\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"densenet_model = fine_tune(densenet_model, unfreeze_from_fraction=0.65)\n\nhistory_dn_ft = densenet_model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=FINE_TUNE_EPOCHS,\n    callbacks=make_callbacks(\"densenet121_finetune\", densenet_live_plot),\n    verbose=1\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def merge_histories(h1, h2):\n    history = {}\n    for key in h1.history.keys():\n        history[key] = h1.history[key] + h2.history.get(key, [])\n    return history\n\n\ndef plot_history(history, title):\n    plt.figure(figsize=(10, 4))\n    plt.plot(history[\"accuracy\"], label=\"train accuracy\")\n    plt.plot(history[\"val_accuracy\"], label=\"validation accuracy\")\n    plt.title(title)\n    plt.xlabel(\"Epoch\")\n    plt.ylabel(\"Accuracy\")\n    plt.legend()\n    plt.grid(True)\n    plt.show()\n\nplot_history(merge_histories(history_eff_head, history_eff_ft), \"EfficientNetB0 Accuracy\")\nplot_history(merge_histories(history_xc_head, history_xc_ft), \"Xception Accuracy\")\nplot_history(merge_histories(history_dn_head, history_dn_ft), \"DenseNet121 Accuracy\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def evaluate_model(model, model_name):\n    y_true = test_df[\"label_id\"].to_numpy()\n    prob = model.predict(test_ds, verbose=1).reshape(-1)\n    y_pred = (prob >= 0.5).astype(np.int32)\n\n    acc = accuracy_score(y_true, y_pred)\n    print(\"=\" * 80)\n    print(model_name)\n    print(f\"Test accuracy: {acc:.4f}\")\n    print(\"Meets 0.97 target:\", acc >= 0.97)\n    print(\"\\nClassification Report\")\n    print(classification_report(y_true, y_pred, target_names=CLASS_NAMES, digits=4))\n    print(\"Confusion Matrix\")\n    print(confusion_matrix(y_true, y_pred))\n    print(\"=\" * 80)\n    return acc, y_true, y_pred, prob","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"eff_acc, y_true, eff_pred, eff_prob = evaluate_model(\n    efficientnet_model,\n    \"EfficientNetB0\"\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xception_acc, y_true_xc, xception_pred, xception_prob = evaluate_model(\n    xception_model,\n    \"Xception\"\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"densenet_acc, _, densenet_pred, densenet_prob = evaluate_model(\n    densenet_model,\n    \"DenseNet121\"\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"results = pd.DataFrame({\n    \"Model\": [\"EfficientNetB0\", \"Xception\", \"DenseNet121\"],\n    \"Test Accuracy\": [eff_acc, xception_acc, densenet_acc],\n})\n\nresults[\"Target >= 0.97\"] = results[\"Test Accuracy\"] >= 0.97\n\ndisplay(results.style.format({\"Test Accuracy\": \"{:.4f}\"}))\n\nif not (results[\"Test Accuracy\"] >= 0.97).all():\n    print(\"At least one model is below 0.97. Increase fine-tuning epochs, unfreeze more layers, or use stronger augmentation.\")\nelse:\n    print(\"All 3 models achieved the target test accuracy >= 0.97 on this run.\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load model\nmodel_vgg16_top = VGG16(include_top=True, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))\nmodel_vgg16_top.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load model\nmodel_16_feature_extractor = VGG16(include_top=False, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))\nmodel_16_feature_extractor.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for index, layer in enumerate(model_16_feature_extractor.layers):\n\t\tprint(index, layer.trainable, layer.output.shape)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for index, layer in enumerate(model_16_feature_extractor.layers):\n    if index <= 15:\n        layer.trainable = False\n\nfor index, layer in enumerate(model_16_feature_extractor.layers):\n    print(index, layer.name, layer.trainable)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_16_feature_extractor.layers[-1].output","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"flat1 = Flatten()(model_16_feature_extractor.layers[-1].output)\nprint(flat1.shape)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\nprint(class1.shape)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = Dense(1, activation='sigmoid')(class1)\nprint(output.shape)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define new model to be trained\nmodel_vgg16 = Model(inputs=model_16_feature_extractor.inputs, outputs=output)\n# compile model\nopt = SGD(lr=0.001, momentum=0.9)\n\nmodel_vgg16.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(model_vgg16.summary())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"id(train_it), id(val_it)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_vgg16 = define_model_vgg16()\nprint(model_vgg16)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for name in [\"tf\", \"model_vgg16\", \"train_it\", \"val_it\", \"PlotLossesKeras\"]:\n    print(f\"{name:20s} {'OK' if name in globals() else 'MISSING'}\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # fit model\n# import tensorflow as tf\n\n# # 1. Verify that TensorFlow can communicate with the GPU hardware\n# physical_devices = tf.config.list_physical_devices('GPU')\n# print(f\"Hardware status: {len(physical_devices)} GPU(s) detected.\")\n\n# if len(physical_devices) > 0:\n#     # Optional but highly recommended: Prevent TF from pre-allocating 100% of VRAM\n#     try:\n#         tf.config.experimental.set_memory_growth(physical_devices[0], True)\n#     except Exception as e:\n#         print(f\"Memory growth configuration failed: {e}\")\n\n#     # 2. Explicitly bind the execution to the primary GPU\n#     with tf.device('/GPU:0'):\n        \n#         print(f\"Model ID: {id(model_vgg16)}\")\n        \n#         # Note: reset_states() is strictly for stateful RNNs (LSTMs/GRUs). \n#         # For a feed-forward CNN like VGG16, this method has no effect and can be removed.\n#         model_vgg16.reset_states() \n        \n#         model_vgg16_history = model_vgg16.fit(\n#             train_it, \n#             steps_per_epoch=len(train_it),\n#             validation_data=val_it,\n#             validation_steps=len(val_it),\n#             epochs=10, \n#             verbose=1,\n#             callbacks=[PlotLossesKeras()]\n#         )\n# else:\n#     print(\"WARNING: No GPU detected. Execution will proceed on the CPU, which will be significantly slower.\")\n#     # Fallback to standard execution\n#     model_vgg16_history = model_vgg16.fit(\n#         train_it, \n#         steps_per_epoch=len(train_it),\n#         validation_data=val_it,\n#         validation_steps=len(val_it),\n#         epochs=10, \n#         verbose=1,\n#         callbacks=[PlotLossesKeras()]\n#     )","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# evaluate model\n_, acc = model_vgg16.evaluate(val_it, steps=len(val_it), verbose=1)\nprint('Val: > %.3f' % (acc * 100.0))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# evaluate model for the test set\n_, test_acc = model_vgg16.evaluate(test_it, steps=len(test_it), verbose=1)\nprint('Test: > %.3f' % (test_acc * 100.0))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import classification_report\n\n# 1. Reset the generator state to ensure alignment from index 0\ntest_it.reset()\n\n# 2. Extract the ordered ground truth labels from the generator\ny_true = test_it.classes\nprint(y_true.shape)\n\n# 3. Execute the forward pass over the entire test dataset\n# Calculate exact steps to prevent infinite looping or partial evaluation\nsteps = np.ceil(test_it.samples / test_it.batch_size)\nprobabilities = model_vgg16.predict(test_it, steps=steps, verbose=1).squeeze()\nprint(probabilities.shape)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 4. Discretize continuous logistic probabilities into binary class predictions\ny_pred = (probabilities > 0.5).astype(int)\nprint(y_pred.shape)\n\n# 5. Extract dynamic class labels mapping (e.g., {'cats': 0, 'dogs': 1})\n# This ensures the report correctly maps index 0 and 1 to your string labels\ntarget_names = list(test_it.class_indices.keys())\n\n# 6. Compute and output the classification report\nreport = classification_report(y_true, y_pred, target_names=target_names)\nprint(report)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# 1. Aggregate sufficient samples from the iterator\nimages_list = []\nlabels_list = []\n\n# Fetch 2 batches (2 * 64 = 128 samples) to ensure we have pool > 100\nfor _ in range(2):\n    batch_images, batch_labels = next(test_it)\n    images_list.append(batch_images)\n    labels_list.append(batch_labels)\n\n# Concatenate into a single tensor\nall_images = np.concatenate(images_list, axis=0)\nall_labels = np.concatenate(labels_list, axis=0)\n\n# 2. Stochastically sample 100 unique indices\nnp.random.seed(42) # For reproducibility\nrandom_indices = np.random.choice(all_images.shape[0], size=100, replace=False)\n\nsample_images = all_images[random_indices]\nsample_labels = all_labels[random_indices]\n\n# 3. Execute the forward pass\n# Squeeze is used to convert shape (100, 1) to (100,)\nprobabilities = model_vgg16.predict(sample_images).squeeze()\n\n# 4. Thresholding (Assuming standard mapping: 0 = Cat, 1 = Dog)\npredictions = (probabilities > 0.5).astype(int)\nclass_mapping = {0: 'Cat', 1: 'Dog'}","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initialize a 10x10 matrix visualization\nfig, axes = plt.subplots(10, 10, figsize=(20, 22))\naxes = axes.flatten()\n\nfor i in range(100):\n    ax = axes[i]\n    \n    # Isolate image tensor. \n    # NOTE: If your VGG16 preprocessor applies mean-centering (caffe style) \n    # instead of standard [0,1] scaling, you must reverse it here for correct plt.imshow() rendering.\n    # We assume standard [0, 1] or [0, 255] spatial tensors below.\n    img = sample_images[i]\n    if img.max() > 1.0 or img.min() < 0:\n        # Simple normalization for display purposes\n        img = (img - img.min()) / (img.max() - img.min())\n        \n    ax.imshow(img)\n    ax.axis('off')\n    \n    # Extract structural labels\n    gt_class = class_mapping[int(sample_labels[i])]\n    pred_class = class_mapping[predictions[i]]\n    prob = probabilities[i]\n    \n    # Apply conditional color coding (Green for True Positives/Negatives, Red for False Positives/Negatives)\n    text_color = 'darkgreen' if gt_class == pred_class else 'darkred'\n    \n    title_str = f\"GT: {gt_class}\\nPred: {pred_class}\\nProb: {prob:.3f}\"\n    ax.set_title(title_str, color=text_color, fontsize=9, pad=4)\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stop","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# InceptionV3","metadata":{}},{"cell_type":"code","source":"from keras.applications.inception_v3 import InceptionV3\n\ndef define_model_inceptionv3():\n    # load model\n    model = InceptionV3(include_top=False, input_shape=(299, 299, 3))\n    # mark loaded layers as not trainable\n    for layer in model.layers:\n        layer.trainable = False\n        # layer.trainable = True #try to test\n    \n    # add new classifier layers\n    flat1 = Flatten()(model.layers[-1].output)\n    class1 = Dense(1024, activation='relu', kernel_initializer='he_uniform')(flat1)\n    class2 = Dense(512, activation='relu', kernel_initializer='he_uniform')(class1)\n    class3 = Dense(256, activation='relu', kernel_initializer='he_uniform')(class2)\n    class4 = Dense(128, activation='relu', kernel_initializer='he_uniform')(class3)\n    # output = Dense(1, activation='sigmoid')(flat1)\n    output = Dense(2, activation='softmax')(class4)\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","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_inceptionv3 = define_model_inceptionv3()\nprint(id(model_inceptionv3))\n# model_inceptionv3.summary()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom tensorflow.keras import backend as K\n\ntrainable_params = int(np.sum([K.count_params(w) for w in model_inceptionv3.trainable_weights]))\nprint(trainable_params)\n","metadata":{"trusted":true,"scrolled":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import backend as K\n\nfor w in model_inceptionv3.trainable_weights:\n    print(w.name, K.count_params(w))\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit model\nmodel_inceptionv3.reset_states()\nhistory_inceptionv3 = model_inceptionv3.fit(\n    train_it, \n    steps_per_epoch=len(train_it),\n    validation_data=val_it,\n    validation_steps=len(val_it),\n    epochs=3, verbose=1,\n    callbacks = [PlotLossesKeras()]\n    )","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plot_images_with_labels(images, labels, train_log, set_label, save=False):\n    \"\"\"\n    Plots a grid of images with their corresponding labels and saves the figure.\n    \n    Args:\n    - images (list or np.array): List of images to be displayed.\n    - labels (np.array): Array of one-hot encoded labels.\n    - train_log (str): The directory or log name to save the plot.\n    - set_label (str): The specific set label (e.g., \"train\", \"validation\", etc.).\n    - config (dict): Configuration dictionary containing image dimensions.\n    \n    Returns:\n    - None: Displays and saves the plot.\n    \"\"\"\n    # Convert one-hot encoded labels to class indices\n    class_indices = labels\n\n    # Create subplots\n    fig, axes = plt.subplots(3, 4, figsize=(14, 10), dpi=120)\n    axes = axes.flatten()\n\n    for img, label_idx, ax in zip(images, class_indices, axes):\n        # Undo any preprocessing (like rescaling for pre-trained models)\n        if np.max(img) == 1:\n            img = img * 255\n\n        # Display the image\n        img_shape = (224, 224, 3)\n        ax.imshow(img.astype(\"uint8\"))\n        # ax.imshow(img)\n\n        # Set the title to the corresponding class label\n        ax.set_title(f\"Label: {label_idx}\", fontsize=18)\n        ax.axis('on')\n\n    # Set the overall title for the figure\n    plot_title = f\"{train_log}-{set_label}\"\n    plt.suptitle(plot_title, fontsize=18)\n\n    # Show the plot\n    plt.show()\n\n    # Save the figure\n    if save:\n        plot_file = os.path.join(train_log, f\"{plot_title}.png\")\n        fig.savefig(plot_file, dpi=120)\n        print(f'Saved {plot_file}')\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot_images_with_labels(images, labels, \"Dog-Cat\", \"Training\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for index, layer in enumerate(model.layers):\n#     print(index, layer.output.shape)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # mark loaded layers as not trainable\n# for index, layer in enumerate(model.layers[:15]):\n#     print(index, layer.name, layer.output_shape)\n#     layer.trainable = False\n    \n# for index, layer in enumerate(model.layers[15:]):\n#     print(index, layer.name, layer.output_shape)\n#     layer.trainable = True","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# evaluate model\n_, acc = model_inceptionv3.evaluate(test_it, steps=len(test_it), verbose=1)\nprint('> %.3f' % (acc * 100.0))\n# learning curves\n# summarize_diagnostics(history)\n\n# best_model = 'vgg16_model.hdf5'\n# my_model.save(best_model)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = test_it.__next__()\nimages.shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 5 first img: 1, 0, 1, 1, 0\nimages_to_predict = images[:5]\nimages_to_predict.shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"probs = model_inceptionv3.predict(images_to_predict, verbose=1)\nprobs","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}