{"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","trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:37.454056Z","iopub.execute_input":"2026-09-19T08:39:37.454856Z","iopub.status.idle":"2026-09-19T08:39:37.459562Z","shell.execute_reply.started":"2026-09-19T08:39:37.454824Z","shell.execute_reply":"2026-09-19T08:39:37.458548Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:37.470182Z","iopub.execute_input":"2026-09-19T08:39:37.470453Z","iopub.status.idle":"2026-09-19T08:39:38.560732Z","shell.execute_reply.started":"2026-09-19T08:39:37.470432Z","shell.execute_reply":"2026-09-19T08:39:38.559746Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!uname --all","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:38.562872Z","iopub.execute_input":"2026-09-19T08:39:38.563297Z","iopub.status.idle":"2026-09-19T08:39:39.597039Z","shell.execute_reply.started":"2026-09-19T08:39:38.563268Z","shell.execute_reply":"2026-09-19T08:39:39.596106Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:39.598671Z","iopub.execute_input":"2026-09-19T08:39:39.598983Z","iopub.status.idle":"2026-09-19T08:39:49.422888Z","shell.execute_reply.started":"2026-09-19T08:39:39.598958Z","shell.execute_reply":"2026-09-19T08:39:49.421990Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:49.424815Z","iopub.execute_input":"2026-09-19T08:39:49.425461Z","iopub.status.idle":"2026-09-19T08:39:50.467742Z","shell.execute_reply.started":"2026-09-19T08:39:49.425435Z","shell.execute_reply":"2026-09-19T08:39:50.466720Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:50.469399Z","iopub.execute_input":"2026-09-19T08:39:50.469658Z","iopub.status.idle":"2026-09-19T08:39:51.511657Z","shell.execute_reply.started":"2026-09-19T08:39:50.469634Z","shell.execute_reply":"2026-09-19T08:39:51.510410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls test1/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:51.513703Z","iopub.execute_input":"2026-09-19T08:39:51.514134Z","iopub.status.idle":"2026-09-19T08:39:52.609978Z","shell.execute_reply.started":"2026-09-19T08:39:51.514082Z","shell.execute_reply":"2026-09-19T08:39:52.608917Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:52.611646Z","iopub.execute_input":"2026-09-19T08:39:52.612168Z","iopub.status.idle":"2026-09-19T08:39:53.763987Z","shell.execute_reply.started":"2026-09-19T08:39:52.612132Z","shell.execute_reply":"2026-09-19T08:39:53.762711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#ls train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:53.766012Z","iopub.execute_input":"2026-09-19T08:39:53.766426Z","iopub.status.idle":"2026-09-19T08:39:53.771292Z","shell.execute_reply.started":"2026-09-19T08:39:53.766390Z","shell.execute_reply":"2026-09-19T08:39:53.770375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -U train | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:53.772518Z","iopub.execute_input":"2026-09-19T08:39:53.772772Z","iopub.status.idle":"2026-09-19T08:39:54.877037Z","shell.execute_reply.started":"2026-09-19T08:39:53.772750Z","shell.execute_reply":"2026-09-19T08:39:54.876116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -U test1 | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:54.881046Z","iopub.execute_input":"2026-09-19T08:39:54.881397Z","iopub.status.idle":"2026-09-19T08:39:55.947176Z","shell.execute_reply.started":"2026-09-19T08:39:54.881372Z","shell.execute_reply":"2026-09-19T08:39:55.946167Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:55.948872Z","iopub.execute_input":"2026-09-19T08:39:55.949223Z","iopub.status.idle":"2026-09-19T08:39:56.894727Z","shell.execute_reply.started":"2026-09-19T08:39:55.949196Z","shell.execute_reply":"2026-09-19T08:39:56.893759Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:56.896070Z","iopub.execute_input":"2026-09-19T08:39:56.896441Z","iopub.status.idle":"2026-09-19T08:39:57.836626Z","shell.execute_reply.started":"2026-09-19T08:39:56.896409Z","shell.execute_reply":"2026-09-19T08:39:57.835882Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:57.838770Z","iopub.execute_input":"2026-09-19T08:39:57.839009Z","iopub.status.idle":"2026-09-19T08:39:58.912216Z","shell.execute_reply.started":"2026-09-19T08:39:57.838988Z","shell.execute_reply":"2026-09-19T08:39:58.911191Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nprint(f\"{tensorflow.__version__=}\")\n\nimport keras\nprint(f\"{keras.__version__=}\")\n\nimport tensorflow.keras\nprint(f\"{tensorflow.__version__=}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:58.914091Z","iopub.execute_input":"2026-09-19T08:39:58.914786Z","iopub.status.idle":"2026-09-19T08:39:58.920684Z","shell.execute_reply.started":"2026-09-19T08:39:58.914757Z","shell.execute_reply":"2026-09-19T08:39:58.919761Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:58.921994Z","iopub.execute_input":"2026-09-19T08:39:58.922369Z","iopub.status.idle":"2026-09-19T08:39:58.931733Z","shell.execute_reply.started":"2026-09-19T08:39:58.922346Z","shell.execute_reply":"2026-09-19T08:39:58.931010Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:58.933991Z","iopub.execute_input":"2026-09-19T08:39:58.934730Z","iopub.status.idle":"2026-09-19T08:39:58.941758Z","shell.execute_reply.started":"2026-09-19T08:39:58.934698Z","shell.execute_reply":"2026-09-19T08:39:58.941072Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:58.943183Z","iopub.execute_input":"2026-09-19T08:39:58.943391Z","iopub.status.idle":"2026-09-19T08:39:58.955072Z","shell.execute_reply.started":"2026-09-19T08:39:58.943372Z","shell.execute_reply":"2026-09-19T08:39:58.954352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:58.956172Z","iopub.execute_input":"2026-09-19T08:39:58.956551Z","iopub.status.idle":"2026-09-19T08:39:59.990079Z","shell.execute_reply.started":"2026-09-19T08:39:58.956530Z","shell.execute_reply":"2026-09-19T08:39:59.989040Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:39:59.991741Z","iopub.execute_input":"2026-09-19T08:39:59.992179Z","iopub.status.idle":"2026-09-19T08:40:01.024257Z","shell.execute_reply.started":"2026-09-19T08:39:59.992150Z","shell.execute_reply":"2026-09-19T08:40:01.023306Z"}},"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-19T08:40:01.025928Z","iopub.execute_input":"2026-09-19T08:40:01.026658Z","iopub.status.idle":"2026-09-19T08:40:02.065438Z","shell.execute_reply.started":"2026-09-19T08:40:01.026620Z","shell.execute_reply":"2026-09-19T08:40:02.064376Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:02.067043Z","iopub.execute_input":"2026-09-19T08:40:02.067486Z","iopub.status.idle":"2026-09-19T08:40:04.995413Z","shell.execute_reply.started":"2026-09-19T08:40:02.067458Z","shell.execute_reply":"2026-09-19T08:40:04.994369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(src)\nprint(dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:04.996724Z","iopub.execute_input":"2026-09-19T08:40:04.997363Z","iopub.status.idle":"2026-09-19T08:40:05.001920Z","shell.execute_reply.started":"2026-09-19T08:40:04.997339Z","shell.execute_reply":"2026-09-19T08:40:05.000937Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:05.002982Z","iopub.execute_input":"2026-09-19T08:40:05.003265Z","iopub.status.idle":"2026-09-19T08:40:07.237775Z","shell.execute_reply.started":"2026-09-19T08:40:05.003233Z","shell.execute_reply":"2026-09-19T08:40:07.236692Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:07.239363Z","iopub.execute_input":"2026-09-19T08:40:07.239683Z","iopub.status.idle":"2026-09-19T08:40:09.500370Z","shell.execute_reply.started":"2026-09-19T08:40:07.239656Z","shell.execute_reply":"2026-09-19T08:40:09.499333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:09.501921Z","iopub.execute_input":"2026-09-19T08:40:09.502513Z","iopub.status.idle":"2026-09-19T08:40:17.666014Z","shell.execute_reply.started":"2026-09-19T08:40:09.502485Z","shell.execute_reply":"2026-09-19T08:40:17.664871Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:17.667606Z","iopub.execute_input":"2026-09-19T08:40:17.667911Z","iopub.status.idle":"2026-09-19T08:40:17.672846Z","shell.execute_reply.started":"2026-09-19T08:40:17.667880Z","shell.execute_reply":"2026-09-19T08:40:17.671813Z"}},"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-19T08:40:17.674290Z","iopub.execute_input":"2026-09-19T08:40:17.674642Z","iopub.status.idle":"2026-09-19T08:40:17.684431Z","shell.execute_reply.started":"2026-09-19T08:40:17.674609Z","shell.execute_reply":"2026-09-19T08:40:17.683783Z"}},"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-19T08:40:17.694585Z","iopub.execute_input":"2026-09-19T08:40:17.695284Z","iopub.status.idle":"2026-09-19T08:40:17.699687Z","shell.execute_reply.started":"2026-09-19T08:40:17.695262Z","shell.execute_reply":"2026-09-19T08:40:17.698811Z"}},"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-19T08:40:17.700627Z","iopub.execute_input":"2026-09-19T08:40:17.700828Z","iopub.status.idle":"2026-09-19T08:40:17.711930Z","shell.execute_reply.started":"2026-09-19T08:40:17.700809Z","shell.execute_reply":"2026-09-19T08:40:17.711304Z"}},"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-19T08:40:17.713005Z","iopub.execute_input":"2026-09-19T08:40:17.713304Z","iopub.status.idle":"2026-09-19T08:40:17.789290Z","shell.execute_reply.started":"2026-09-19T08:40:17.713282Z","shell.execute_reply":"2026-09-19T08:40:17.788409Z"}},"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-19T08:40:17.790278Z","iopub.execute_input":"2026-09-19T08:40:17.790524Z","iopub.status.idle":"2026-09-19T08:40:17.795942Z","shell.execute_reply.started":"2026-09-19T08:40:17.790504Z","shell.execute_reply":"2026-09-19T08:40:17.795157Z"}},"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-19T08:40:17.796927Z","iopub.execute_input":"2026-09-19T08:40:17.797214Z","iopub.status.idle":"2026-09-19T08:40:17.865509Z","shell.execute_reply.started":"2026-09-19T08:40:17.797193Z","shell.execute_reply":"2026-09-19T08:40:17.864156Z"}},"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\nmyinputs = Input(shape=(200, 200, 3))\n\n# Construct the forward pass of the computational graph\nx1 = Conv2D(32, (3, 3), activation='relu')(myinputs)\nx2 = MaxPooling2D((2, 2))(x1)\nx3 = Flatten()(x2)\nx5 = Dense(256, activation='relu')(x3)\nx4 = Dense(128, activation='relu')(x5)\n\n# Define the output tensor with the target objective function representation\nmyoutputs = Dense(2, activation='softmax')(x4)\n\n# Instantiate the formal model architecture\nmodel = Model(inputs=myinputs, outputs=myoutputs)\n\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:17.866470Z","iopub.execute_input":"2026-09-19T08:40:17.866715Z","iopub.status.idle":"2026-09-19T08:40:17.938308Z","shell.execute_reply.started":"2026-09-19T08:40:17.866694Z","shell.execute_reply":"2026-09-19T08:40:17.937514Z"}},"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\nmyinputs = Input(shape=(200, 200, 3))\n\n# Construct the forward pass of the computational graph\nx1 = Conv2D(32, (3, 3), activation='relu')(myinputs)\nx1 = Conv2D(64, (3, 3), activation='relu')(x1)\nx2 = MaxPooling2D((2, 2))(x1)\nx3 = Flatten()(x2)\nx5 = Dense(256, activation='relu')(x3)\nx4 = Dense(128, activation='relu')(x5)\n\n# Define the output tensor with the target objective function representation\nmyoutputs = Dense(2, activation='softmax')(x4)\n\n# Instantiate the formal model architecture\nmodel = Model(inputs=myinputs, outputs=myoutputs)\n\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:17.939580Z","iopub.execute_input":"2026-09-19T08:40:17.939923Z","iopub.status.idle":"2026-09-19T08:40:18.025667Z","shell.execute_reply.started":"2026-09-19T08:40:17.939892Z","shell.execute_reply":"2026-09-19T08:40:18.024821Z"}},"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-19T08:40:18.026718Z","iopub.execute_input":"2026-09-19T08:40:18.027228Z","iopub.status.idle":"2026-09-19T08:40:18.032827Z","shell.execute_reply.started":"2026-09-19T08:40:18.027206Z","shell.execute_reply":"2026-09-19T08:40:18.031862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls dataset_dogs_vs_cats/train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:18.033865Z","iopub.execute_input":"2026-09-19T08:40:18.034181Z","iopub.status.idle":"2026-09-19T08:40:19.086690Z","shell.execute_reply.started":"2026-09-19T08:40:18.034151Z","shell.execute_reply":"2026-09-19T08:40:19.085301Z"}},"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-19T08:40:19.088592Z","iopub.execute_input":"2026-09-19T08:40:19.089019Z","iopub.status.idle":"2026-09-19T08:40:19.093666Z","shell.execute_reply.started":"2026-09-19T08:40:19.088990Z","shell.execute_reply":"2026-09-19T08:40:19.092729Z"}},"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-19T08:40:19.094868Z","iopub.execute_input":"2026-09-19T08:40:19.095217Z","iopub.status.idle":"2026-09-19T08:40:44.053639Z","shell.execute_reply.started":"2026-09-19T08:40:19.095186Z","shell.execute_reply":"2026-09-19T08:40:44.052622Z"}},"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-19T08:40:44.054820Z","iopub.execute_input":"2026-09-19T08:40:44.055234Z","iopub.status.idle":"2026-09-19T08:40:44.062032Z","shell.execute_reply.started":"2026-09-19T08:40:44.055209Z","shell.execute_reply":"2026-09-19T08:40:44.061030Z"}},"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-19T08:40:44.063283Z","iopub.execute_input":"2026-09-19T08:40:44.063534Z","iopub.status.idle":"2026-09-19T08:40:44.078304Z","shell.execute_reply.started":"2026-09-19T08:40:44.063513Z","shell.execute_reply":"2026-09-19T08:40:44.077392Z"}},"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-19T08:40:44.079415Z","iopub.execute_input":"2026-09-19T08:40:44.079823Z","iopub.status.idle":"2026-09-19T08:40:44.086424Z","shell.execute_reply.started":"2026-09-19T08:40:44.079798Z","shell.execute_reply":"2026-09-19T08:40:44.085659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\n# summarize_diagnostics(history_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:44.087417Z","iopub.execute_input":"2026-09-19T08:40:44.087769Z","iopub.status.idle":"2026-09-19T08:40:44.095802Z","shell.execute_reply.started":"2026-09-19T08:40:44.087748Z","shell.execute_reply":"2026-09-19T08:40:44.095151Z"}},"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-19T08:40:44.096943Z","iopub.execute_input":"2026-09-19T08:40:44.097753Z","iopub.status.idle":"2026-09-19T08:40:44.104187Z","shell.execute_reply.started":"2026-09-19T08:40:44.097729Z","shell.execute_reply":"2026-09-19T08:40:44.103306Z"}},"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-19T08:40:44.105464Z","iopub.execute_input":"2026-09-19T08:40:44.106080Z","iopub.status.idle":"2026-09-19T08:40:45.151831Z","shell.execute_reply.started":"2026-09-19T08:40:44.106037Z","shell.execute_reply":"2026-09-19T08:40:45.150541Z"}},"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-19T08:40:45.153998Z","iopub.execute_input":"2026-09-19T08:40:45.154487Z","iopub.status.idle":"2026-09-19T08:40:45.160856Z","shell.execute_reply.started":"2026-09-19T08:40:45.154452Z","shell.execute_reply":"2026-09-19T08:40:45.159885Z"}},"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-19T08:40:45.161940Z","iopub.execute_input":"2026-09-19T08:40:45.162271Z","iopub.status.idle":"2026-09-19T08:40:45.171627Z","shell.execute_reply.started":"2026-09-19T08:40:45.162248Z","shell.execute_reply":"2026-09-19T08:40:45.170975Z"}},"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-19T08:40:45.172649Z","iopub.execute_input":"2026-09-19T08:40:45.172865Z","iopub.status.idle":"2026-09-19T08:40:45.184393Z","shell.execute_reply.started":"2026-09-19T08:40:45.172846Z","shell.execute_reply":"2026-09-19T08:40:45.183582Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:45.185492Z","iopub.execute_input":"2026-09-19T08:40:45.186131Z","iopub.status.idle":"2026-09-19T08:40:45.195409Z","shell.execute_reply.started":"2026-09-19T08:40:45.186068Z","shell.execute_reply":"2026-09-19T08:40:45.194464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.loc[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:45.196616Z","iopub.execute_input":"2026-09-19T08:40:45.197219Z","iopub.status.idle":"2026-09-19T08:40:45.205238Z","shell.execute_reply.started":"2026-09-19T08:40:45.197188Z","shell.execute_reply":"2026-09-19T08:40:45.204542Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:45.206459Z","iopub.execute_input":"2026-09-19T08:40:45.206820Z","iopub.status.idle":"2026-09-19T08:40:45.214830Z","shell.execute_reply.started":"2026-09-19T08:40:45.206770Z","shell.execute_reply":"2026-09-19T08:40:45.213892Z"}},"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-19T08:40:45.215965Z","iopub.execute_input":"2026-09-19T08:40:45.216486Z","iopub.status.idle":"2026-09-19T08:40:45.224139Z","shell.execute_reply.started":"2026-09-19T08:40:45.216462Z","shell.execute_reply":"2026-09-19T08:40:45.223163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:45.225014Z","iopub.execute_input":"2026-09-19T08:40:45.225375Z","iopub.status.idle":"2026-09-19T08:40:45.242433Z","shell.execute_reply.started":"2026-09-19T08:40:45.225333Z","shell.execute_reply":"2026-09-19T08:40:45.241635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(\n    rescale=1.0/255.0,\n    horizontal_flip=True,\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)\ntest_it = test_datagen.flow_from_directory(\n    test_path,\n    class_mode='binary',\n    batch_size=BATCH_SIZE,\n    target_size=(IMAGE_SIZE, IMAGE_SIZE))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:40:45.243481Z","iopub.execute_input":"2026-09-19T08:40:45.243865Z","iopub.status.idle":"2026-09-19T08:40:54.438747Z","shell.execute_reply.started":"2026-09-19T08:40:45.243842Z","shell.execute_reply":"2026-09-19T08:40:54.438051Z"}},"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-19T08:40:54.440092Z","iopub.execute_input":"2026-09-19T08:40:54.440476Z","iopub.status.idle":"2026-09-19T08:40:54.446451Z","shell.execute_reply.started":"2026-09-19T08:40:54.440426Z","shell.execute_reply":"2026-09-19T08:40:54.445678Z"}},"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-19T08:40:54.447544Z","iopub.execute_input":"2026-09-19T08:40:54.447947Z","iopub.status.idle":"2026-09-19T08:41:04.547795Z","shell.execute_reply.started":"2026-09-19T08:40:54.447925Z","shell.execute_reply":"2026-09-19T08:41:04.545914Z"}},"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-19T08:41:04.548969Z","iopub.execute_input":"2026-09-19T08:41:04.549271Z","iopub.status.idle":"2026-09-19T08:41:12.867746Z","shell.execute_reply.started":"2026-09-19T08:41:04.549246Z","shell.execute_reply":"2026-09-19T08:41:12.866146Z"}},"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,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2026-09-19T08:41:12.868995Z","iopub.execute_input":"2026-09-19T08:41:12.869327Z","iopub.status.idle":"2026-09-19T08:41:12.876831Z","shell.execute_reply.started":"2026-09-19T08:41:12.869297Z","shell.execute_reply":"2026-09-19T08:41:12.875892Z"}},"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-19T08:41:12.878039Z","iopub.execute_input":"2026-09-19T08:41:12.878345Z","iopub.status.idle":"2026-09-19T08:41:22.107655Z","shell.execute_reply.started":"2026-09-19T08:41:12.878318Z","shell.execute_reply":"2026-09-19T08:41:22.104865Z"}},"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-19T08:41:22.108834Z","iopub.execute_input":"2026-09-19T08:41:22.109446Z","iopub.status.idle":"2026-09-19T08:41:22.113270Z","shell.execute_reply.started":"2026-09-19T08:41:22.109419Z","shell.execute_reply":"2026-09-19T08:41:22.112492Z"}},"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-19T08:41:22.114368Z","iopub.execute_input":"2026-09-19T08:41:22.114709Z","iopub.status.idle":"2026-09-19T08:41:22.125928Z","shell.execute_reply.started":"2026-09-19T08:41:22.114686Z","shell.execute_reply":"2026-09-19T08:41:22.125290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"id(train_it),id(val_it), id(test_it)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:22.127012Z","iopub.execute_input":"2026-09-19T08:41:22.127316Z","iopub.status.idle":"2026-09-19T08:41:22.137428Z","shell.execute_reply.started":"2026-09-19T08:41:22.127294Z","shell.execute_reply":"2026-09-19T08:41:22.136435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Create transfer learning model for VGG16\nimport sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout\nfrom keras.layers import Flatten\nfrom keras.optimizers import SGD\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# Hyperparameter => changable\nIMAGE_SIZE = 224\nBATCH_SIE = 64\nEPOCHS = 25\n\ndef define_model_vgg16():\n    # load model\n    model = VGG16(\n        include_top=False,\n        input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3)\n    )\n\n        # freeze all VGG16 layers\n    for layer in model.layers:\n        layer.trainable = False\n\n    # fine-tuning last 4 layers\n    for layer in model.layers[-4:]:\n        layer.trainable = True\n\n    # mark loaded layers as not trainable\n    for layer in model.layers:\n        layer.trainable = False\n\n    # add new classifier layers\n    flat1 = Flatten()(model.layers[-1].output)\n    \n    class1 = Dense(\n        128,\n        activation='relu',\n        kernel_initializer='he_uniform'\n    )(flat1)\n\n    class1 = Dropout(0.1)(class1)\n    \n    class2 = Dense(\n        64,\n        activation='relu',\n        kernel_initializer='he_uniform'\n    )(class1)\n\n    class2 = Dropout(0.1)(class2)\n\n    class3 = Dense(\n        32,\n        activation='relu',\n        kernel_initializer='he_uniform'\n    )(class2)\n\n    class3 = Dropout(0.1)(class3)\n    \n    output = Dense(1, activation='sigmoid')(class1)\n\n    # define new model\n    model = Model(inputs=model.inputs, outputs=output)\n\n    # compile model\n    opt = Adam(learning_rate=0.00001)\n\n    model.compile(\n        optimizer =opt,\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model\n\nvgg16 = define_model_vgg16()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:22.138581Z","iopub.execute_input":"2026-09-19T08:41:22.138885Z","iopub.status.idle":"2026-09-19T08:41:22.515886Z","shell.execute_reply.started":"2026-09-19T08:41:22.138857Z","shell.execute_reply":"2026-09-19T08:41:22.515084Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:41:22.517404Z","iopub.execute_input":"2026-09-19T08:41:22.517721Z","iopub.status.idle":"2026-09-19T08:41:24.801819Z","shell.execute_reply.started":"2026-09-19T08:41:22.517695Z","shell.execute_reply":"2026-09-19T08:41:24.800942Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:41:24.803117Z","iopub.execute_input":"2026-09-19T08:41:24.803460Z","iopub.status.idle":"2026-09-19T08:41:25.144837Z","shell.execute_reply.started":"2026-09-19T08:41:24.803426Z","shell.execute_reply":"2026-09-19T08:41:25.143916Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.145937Z","iopub.execute_input":"2026-09-19T08:41:25.146251Z","iopub.status.idle":"2026-09-19T08:41:25.151371Z","shell.execute_reply.started":"2026-09-19T08:41:25.146225Z","shell.execute_reply":"2026-09-19T08:41:25.150409Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.152470Z","iopub.execute_input":"2026-09-19T08:41:25.152689Z","iopub.status.idle":"2026-09-19T08:41:25.164924Z","shell.execute_reply.started":"2026-09-19T08:41:25.152669Z","shell.execute_reply":"2026-09-19T08:41:25.164164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_16_feature_extractor.layers[-1].output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.165964Z","iopub.execute_input":"2026-09-19T08:41:25.167157Z","iopub.status.idle":"2026-09-19T08:41:25.175652Z","shell.execute_reply.started":"2026-09-19T08:41:25.167083Z","shell.execute_reply":"2026-09-19T08:41:25.174863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#USe functional API\nflat1 = Flatten()(model_16_feature_extractor.layers[-1].output)\nprint(flat1.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.176588Z","iopub.execute_input":"2026-09-19T08:41:25.176994Z","iopub.status.idle":"2026-09-19T08:41:25.192328Z","shell.execute_reply.started":"2026-09-19T08:41:25.176970Z","shell.execute_reply":"2026-09-19T08:41:25.191181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\nprint(class1.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.193510Z","iopub.execute_input":"2026-09-19T08:41:25.193786Z","iopub.status.idle":"2026-09-19T08:41:25.212022Z","shell.execute_reply.started":"2026-09-19T08:41:25.193765Z","shell.execute_reply":"2026-09-19T08:41:25.210967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output = Dense(1, activation='sigmoid')(class1)\nprint(output.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.213181Z","iopub.execute_input":"2026-09-19T08:41:25.213474Z","iopub.status.idle":"2026-09-19T08:41:25.227956Z","shell.execute_reply.started":"2026-09-19T08:41:25.213446Z","shell.execute_reply":"2026-09-19T08:41:25.227311Z"}},"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\n# opt = SGD(lr=0.001, momentum=0.9)\nopt = 'adam'\n\nmodel_vgg16.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.228872Z","iopub.execute_input":"2026-09-19T08:41:25.229172Z","iopub.status.idle":"2026-09-19T08:41:25.244305Z","shell.execute_reply.started":"2026-09-19T08:41:25.229142Z","shell.execute_reply":"2026-09-19T08:41:25.243297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(model_vgg16.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.245375Z","iopub.execute_input":"2026-09-19T08:41:25.245694Z","iopub.status.idle":"2026-09-19T08:41:25.294826Z","shell.execute_reply.started":"2026-09-19T08:41:25.245665Z","shell.execute_reply":"2026-09-19T08:41:25.293891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"id(train_it), id(val_it)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.295780Z","iopub.execute_input":"2026-09-19T08:41:25.296155Z","iopub.status.idle":"2026-09-19T08:41:25.302646Z","shell.execute_reply.started":"2026-09-19T08:41:25.296085Z","shell.execute_reply":"2026-09-19T08:41:25.301841Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model_vgg16 = define_model_vgg16()\n# print(model_vgg16)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.303714Z","iopub.execute_input":"2026-09-19T08:41:25.304050Z","iopub.status.idle":"2026-09-19T08:41:25.311943Z","shell.execute_reply.started":"2026-09-19T08:41:25.304028Z","shell.execute_reply":"2026-09-19T08:41:25.311293Z"}},"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":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit model\nimport tensorflow as tf\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau # 1. เพิ่ม import ตรงนี้\n\n# 2. กำหนด Callbacks เพื่อป้องกัน Overfitting และปรับ Learning Rate ให้กราฟลู่เข้า\nearly_stop = EarlyStopping(\n    monitor='val_loss', \n    patience=5,               # ถ้ารอ 5 epochs แล้ว val_loss ไม่ลดลง ให้หยุดเทรน\n    restore_best_weights=True # คืนค่าน้ำหนักใน epoch ที่ดีที่สุด\n)\n\nreduce_lr = ReduceLROnPlateau(\n    monitor='val_loss', \n    factor=0.5,               # ลด Learning rate ลงครึ่งหนึ่งเมื่อกราฟเริ่มตัน\n    patience=2,               # ถ้ารอ 2 epochs แล้ว val_loss ไม่ลด ให้ปรับ LR\n    min_lr=1e-6               # กันไม่ให้ LR ต่ำเกินไป\n)\n\n# เก็บ callbacks ไว้ในตัวแปรเดียวเพื่อให้โค้ดอ่านง่าย\nmy_callbacks = [PlotLossesKeras(), early_stop, reduce_lr]\n\n# 1. Verify that TensorFlow can communicate with the GPU hardware\nphysical_devices = tf.config.list_physical_devices('GPU')\nprint(f\"Hardware status: {len(physical_devices)} GPU(s) detected.\")\n\nif 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=50,\n            verbose=1,\n            callbacks=my_callbacks # 4. ใส่ callbacks ที่ตั้งค่าไว้\n        )\nelse:\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=1, \n        verbose=1,\n        callbacks=my_callbacks # 4. ใส่ callbacks ตรงนี้ด้วยเช่นกัน\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:41:25.313189Z","iopub.execute_input":"2026-09-19T08:41:25.313487Z","iopub.status.idle":"2026-09-19T08:54:39.365513Z","shell.execute_reply.started":"2026-09-19T08:41:25.313459Z","shell.execute_reply":"2026-09-19T08:54:39.364608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\nsummarize_diagnostics(model_vgg16_history)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:54:39.366643Z","iopub.execute_input":"2026-09-19T08:54:39.367472Z","iopub.status.idle":"2026-09-19T08:54:39.668136Z","shell.execute_reply.started":"2026-09-19T08:54:39.367443Z","shell.execute_reply":"2026-09-19T08:54:39.667428Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:54:39.669263Z","iopub.execute_input":"2026-09-19T08:54:39.669571Z","iopub.status.idle":"2026-09-19T08:54:55.993024Z","shell.execute_reply.started":"2026-09-19T08:54:39.669545Z","shell.execute_reply":"2026-09-19T08:54:55.992284Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:54:55.994396Z","iopub.execute_input":"2026-09-19T08:54:55.995394Z","iopub.status.idle":"2026-09-19T08:55:31.143372Z","shell.execute_reply.started":"2026-09-19T08:54:55.995369Z","shell.execute_reply":"2026-09-19T08:55:31.142342Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:55:31.144648Z","iopub.execute_input":"2026-09-19T08:55:31.145016Z","iopub.status.idle":"2026-09-19T08:55:58.632546Z","shell.execute_reply.started":"2026-09-19T08:55:31.144989Z","shell.execute_reply":"2026-09-19T08:55:58.631770Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:55:58.633645Z","iopub.execute_input":"2026-09-19T08:55:58.634237Z","iopub.status.idle":"2026-09-19T08:55:58.654144Z","shell.execute_reply.started":"2026-09-19T08:55:58.634210Z","shell.execute_reply":"2026-09-19T08:55:58.653330Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:55:58.655329Z","iopub.execute_input":"2026-09-19T08:55:58.656251Z","iopub.status.idle":"2026-09-19T08:55:59.896359Z","shell.execute_reply.started":"2026-09-19T08:55:58.656226Z","shell.execute_reply":"2026-09-19T08:55:59.895584Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T08:55:59.904077Z","iopub.execute_input":"2026-09-19T08:55:59.904622Z","iopub.status.idle":"2026-09-19T08:56:08.638087Z","shell.execute_reply.started":"2026-09-19T08:55:59.904593Z","shell.execute_reply":"2026-09-19T08:56:08.636453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# stop","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:56:08.639801Z","iopub.execute_input":"2026-09-19T08:56:08.640401Z","iopub.status.idle":"2026-09-19T08:56:08.644988Z","shell.execute_reply.started":"2026-09-19T08:56:08.640334Z","shell.execute_reply":"2026-09-19T08:56:08.644166Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# InceptionV3","metadata":{}},{"cell_type":"code","source":"# Use another dataset\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.inception_v3 import preprocess_input\n\n# 1. Instantiate the generator with the validation_split ratio\ntrain_datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input,\n    horizontal_flip=True,\n    validation_split=0.2\n)\n\nval_datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input,\n    validation_split=0.2\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 = train_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 = val_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-19T08:56:08.646073Z","iopub.execute_input":"2026-09-19T08:56:08.646433Z","iopub.status.idle":"2026-09-19T08:56:26.524961Z","shell.execute_reply.started":"2026-09-19T08:56:08.646409Z","shell.execute_reply":"2026-09-19T08:56:26.524166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications.inception_v3 import InceptionV3\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\n\n\ndef define_model_inceptionv3():\n\n    # load model\n    model = InceptionV3(\n        include_top=False,\n        weights='imagenet',\n        input_shape=(224, 224, 3)\n    )\n\n    # mark loaded layers as not trainable\n    for layer in model.layers:\n        layer.trainable = False\n\n    # fine-tuning last 4 layers\n    for layer in model.layers[-4:]:\n        layer.trainable = True\n\n    # add new classifier layers\n    flat1 = GlobalAveragePooling2D()(model.output)\n\n    class1 = Dense(\n        256,\n        activation='relu',\n        kernel_initializer='he_uniform'\n    )(flat1)\n\n    class1 = Dropout(0.1)(class1)\n\n    # binary classification\n    output = Dense(\n        1,\n        activation='sigmoid'\n    )(class1)\n\n    # define new model\n    model = Model(\n        inputs=model.input,\n        outputs=output\n    )\n\n    # compile model\n    opt = Adam(\n        learning_rate=0.00001\n    )\n\n    model.compile(\n        optimizer=opt,\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:56:26.526027Z","iopub.execute_input":"2026-09-19T08:56:26.526446Z","iopub.status.idle":"2026-09-19T08:56:26.533349Z","shell.execute_reply.started":"2026-09-19T08:56:26.526421Z","shell.execute_reply":"2026-09-19T08:56:26.532320Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_inceptionv3 = define_model_inceptionv3()\nprint(id(model_inceptionv3))\n#model_inceptionv3.summary()","metadata":{"trusted":true,"scrolled":true,"execution":{"iopub.status.busy":"2026-09-19T08:56:26.534656Z","iopub.execute_input":"2026-09-19T08:56:26.534960Z","iopub.status.idle":"2026-09-19T08:56:28.764062Z","shell.execute_reply.started":"2026-09-19T08:56:26.534938Z","shell.execute_reply":"2026-09-19T08:56:28.763289Z"}},"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)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:56:28.765170Z","iopub.execute_input":"2026-09-19T08:56:28.765779Z","iopub.status.idle":"2026-09-19T08:56:28.772638Z","shell.execute_reply.started":"2026-09-19T08:56:28.765753Z","shell.execute_reply":"2026-09-19T08:56:28.771842Z"}},"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))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:56:28.773843Z","iopub.execute_input":"2026-09-19T08:56:28.774441Z","iopub.status.idle":"2026-09-19T08:56:28.783922Z","shell.execute_reply.started":"2026-09-19T08:56:28.774408Z","shell.execute_reply":"2026-09-19T08:56:28.783273Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_it.image_shape)\nprint(train_it.batch_size)\nprint(model_inceptionv3.input_shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:56:28.785015Z","iopub.execute_input":"2026-09-19T08:56:28.785354Z","iopub.status.idle":"2026-09-19T08:56:28.794181Z","shell.execute_reply.started":"2026-09-19T08:56:28.785331Z","shell.execute_reply":"2026-09-19T08:56:28.793400Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit model\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=10,\n    verbose=1,\n    callbacks=[PlotLossesKeras()]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T08:56:28.795355Z","iopub.execute_input":"2026-09-19T08:56:28.795969Z","iopub.status.idle":"2026-09-19T09:09:44.270713Z","shell.execute_reply.started":"2026-09-19T08:56:28.795916Z","shell.execute_reply":"2026-09-19T09:09:44.269926Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T09:09:44.271847Z","iopub.execute_input":"2026-09-19T09:09:44.272650Z","iopub.status.idle":"2026-09-19T09:10:22.975797Z","shell.execute_reply.started":"2026-09-19T09:09:44.272621Z","shell.execute_reply":"2026-09-19T09:10:22.974932Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T09:10:22.977285Z","iopub.execute_input":"2026-09-19T09:10:22.977573Z","iopub.status.idle":"2026-09-19T09:10:22.997519Z","shell.execute_reply.started":"2026-09-19T09:10:22.977549Z","shell.execute_reply":"2026-09-19T09:10:22.996642Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T09:10:22.998696Z","iopub.execute_input":"2026-09-19T09:10:22.999049Z","iopub.status.idle":"2026-09-19T09:10:23.006715Z","shell.execute_reply.started":"2026-09-19T09:10:22.999025Z","shell.execute_reply":"2026-09-19T09:10:23.005672Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot_images_with_labels(images, labels, \"Dog-Cat\", \"Training\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:10:23.007837Z","iopub.execute_input":"2026-09-19T09:10:23.008420Z","iopub.status.idle":"2026-09-19T09:10:23.023855Z","shell.execute_reply.started":"2026-09-19T09:10:23.008398Z","shell.execute_reply":"2026-09-19T09:10:23.023122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for index, layer in enumerate(model.layers):\n#     print(index, layer.output.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:10:23.024946Z","iopub.execute_input":"2026-09-19T09:10:23.026030Z","iopub.status.idle":"2026-09-19T09:10:23.034717Z","shell.execute_reply.started":"2026-09-19T09:10:23.026008Z","shell.execute_reply":"2026-09-19T09:10:23.034002Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T09:10:23.035691Z","iopub.execute_input":"2026-09-19T09:10:23.036202Z","iopub.status.idle":"2026-09-19T09:10:23.044825Z","shell.execute_reply.started":"2026-09-19T09:10:23.036169Z","shell.execute_reply":"2026-09-19T09:10:23.043789Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T09:10:23.045822Z","iopub.execute_input":"2026-09-19T09:10:23.046464Z","iopub.status.idle":"2026-09-19T09:10:51.912881Z","shell.execute_reply.started":"2026-09-19T09:10:23.046442Z","shell.execute_reply":"2026-09-19T09:10:51.912173Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# images = test_it.__next__()\n# images.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:10:51.913966Z","iopub.execute_input":"2026-09-19T09:10:51.914349Z","iopub.status.idle":"2026-09-19T09:10:51.918568Z","shell.execute_reply.started":"2026-09-19T09:10:51.914324Z","shell.execute_reply":"2026-09-19T09:10:51.917592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # 5 first img: 1, 0, 1, 1, 0\n# images_to_predict = images[:5]\n# images_to_predict.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:10:51.920082Z","iopub.execute_input":"2026-09-19T09:10:51.920522Z","iopub.status.idle":"2026-09-19T09:10:51.928586Z","shell.execute_reply.started":"2026-09-19T09:10:51.920486Z","shell.execute_reply":"2026-09-19T09:10:51.927891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# probs = model_inceptionv3.predict(images_to_predict, verbose=1)\n# probs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:10:51.929843Z","iopub.execute_input":"2026-09-19T09:10:51.930175Z","iopub.status.idle":"2026-09-19T09:10:51.939717Z","shell.execute_reply.started":"2026-09-19T09:10:51.930144Z","shell.execute_reply":"2026-09-19T09:10:51.938907Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"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_inceptionv3.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,"execution":{"iopub.status.busy":"2026-09-19T09:10:51.940900Z","iopub.execute_input":"2026-09-19T09:10:51.941192Z","iopub.status.idle":"2026-09-19T09:10:54.098073Z","shell.execute_reply.started":"2026-09-19T09:10:51.941168Z","shell.execute_reply":"2026-09-19T09:10:54.097343Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T09:10:54.099361Z","iopub.execute_input":"2026-09-19T09:10:54.099736Z","iopub.status.idle":"2026-09-19T09:11:02.827274Z","shell.execute_reply.started":"2026-09-19T09:10:54.099710Z","shell.execute_reply":"2026-09-19T09:11:02.825958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# ***NASNetLarge***","metadata":{}},{"cell_type":"code","source":"# Use another dataset\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.nasnet import preprocess_input\n\n# 1. Instantiate the generator with the validation_split ratio\ntrain_datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input,\n    horizontal_flip=True,\n    validation_split=0.2\n)\n\nval_datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input,\n    validation_split=0.2\n)\n\ntest_datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input\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 = train_datagen.flow_from_directory(\n    train_path,\n    class_mode='binary',\n    batch_size=BATCH_SIZE,\n    target_size=(331, 331),\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 = val_datagen.flow_from_directory(\n    train_path,\n    class_mode='binary',\n    batch_size=BATCH_SIZE,\n    target_size=(331, 331),\n    subset='validation',   # Explicitly request the validation partition\n    shuffle=False          # Disable shuffle to maintain index alignment for metrics\n)\n\ntest_it = test_datagen.flow_from_directory(\n    test_path,\n    class_mode='binary',\n    batch_size=BATCH_SIZE,\n    target_size=(331, 331),\n    shuffle=False\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:11:02.829141Z","iopub.execute_input":"2026-09-19T09:11:02.829474Z","iopub.status.idle":"2026-09-19T09:11:20.010003Z","shell.execute_reply.started":"2026-09-19T09:11:02.829449Z","shell.execute_reply":"2026-09-19T09:11:20.008956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications import NASNetLarge\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\n\n\ndef define_model_nasnetlarge():\n\n    # Load NASNetLarge\n    model = NASNetLarge(\n        include_top=False,\n        weights='imagenet',\n        input_shape=(331, 331, 3)\n    )\n\n    # Freeze all layers\n    for layer in model.layers:\n        layer.trainable = False\n\n    # Fine-tuning last 4 layers\n    for layer in model.layers[-4:]:\n        layer.trainable = True\n\n    # Global Average Pooling\n    x = GlobalAveragePooling2D()(model.output)\n\n    # Classifier\n    x = Dense(\n        256,\n        activation='relu',\n        kernel_initializer='he_uniform'\n    )(x)\n\n    x = Dropout(0.1)(x)\n\n    # Binary classification\n    output = Dense(\n        1,\n        activation='sigmoid'\n    )(x)\n\n    # Define model\n    model = Model(\n        inputs=model.input,\n        outputs=output\n    )\n\n    # Compile\n    opt = Adam(\n        learning_rate=0.00001\n    )\n\n    model.compile(\n        optimizer=opt,\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:11:20.012369Z","iopub.execute_input":"2026-09-19T09:11:20.012701Z","iopub.status.idle":"2026-09-19T09:11:20.020586Z","shell.execute_reply.started":"2026-09-19T09:11:20.012676Z","shell.execute_reply":"2026-09-19T09:11:20.019876Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_nasnetlarge = define_model_nasnetlarge()\n# model_nasnetlarge.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:11:20.021536Z","iopub.execute_input":"2026-09-19T09:11:20.022443Z","iopub.status.idle":"2026-09-19T09:11:28.569197Z","shell.execute_reply.started":"2026-09-19T09:11:20.022416Z","shell.execute_reply":"2026-09-19T09:11:28.568161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_nasnetlarge = model_nasnetlarge.fit(\n    train_it,\n    steps_per_epoch=len(train_it),\n    validation_data=val_it,\n    validation_steps=len(val_it),\n    epochs=1,\n    verbose=1,\n    callbacks=[PlotLossesKeras()]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:11:28.570454Z","iopub.execute_input":"2026-09-19T09:11:28.570755Z","iopub.status.idle":"2026-09-19T09:20:39.990411Z","shell.execute_reply.started":"2026-09-19T09:11:28.570731Z","shell.execute_reply":"2026-09-19T09:20:39.989590Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# evaluate model\n_, acc =  model_nasnetlarge.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,"execution":{"iopub.status.busy":"2026-09-19T09:20:39.991689Z","iopub.execute_input":"2026-09-19T09:20:39.992041Z","iopub.status.idle":"2026-09-19T09:23:38.197449Z","shell.execute_reply.started":"2026-09-19T09:20:39.992016Z","shell.execute_reply":"2026-09-19T09:23:38.196633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_it.image_shape)\nprint(val_it.image_shape)\nprint(model_nasnetlarge.input_shape)\nprint(test_it.image_shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-19T09:23:38.198587Z","iopub.execute_input":"2026-09-19T09:23:38.198949Z","iopub.status.idle":"2026-09-19T09:23:38.204495Z","shell.execute_reply.started":"2026-09-19T09:23:38.198922Z","shell.execute_reply":"2026-09-19T09:23:38.203635Z"}},"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_nasnetlarge.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,"execution":{"iopub.status.busy":"2026-09-19T09:23:38.205480Z","iopub.execute_input":"2026-09-19T09:23:38.206085Z","iopub.status.idle":"2026-09-19T09:23:47.190360Z","shell.execute_reply.started":"2026-09-19T09:23:38.206053Z","shell.execute_reply":"2026-09-19T09:23:47.189264Z"}},"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,"execution":{"iopub.status.busy":"2026-09-19T09:23:47.191609Z","iopub.execute_input":"2026-09-19T09:23:47.192415Z","iopub.status.idle":"2026-09-19T09:23:55.494548Z","shell.execute_reply.started":"2026-09-19T09:23:47.192389Z","shell.execute_reply":"2026-09-19T09:23:55.492903Z"}},"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":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}