{"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-10-03T12:59:51.272757Z","iopub.execute_input":"2026-10-03T12:59:51.273594Z","iopub.status.idle":"2026-10-03T12:59:51.278221Z","shell.execute_reply.started":"2026-10-03T12:59:51.273561Z","shell.execute_reply":"2026-10-03T12:59:51.277145Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T12:59:51.281314Z","iopub.execute_input":"2026-10-03T12:59:51.281651Z","iopub.status.idle":"2026-10-03T12:59:52.347315Z","shell.execute_reply.started":"2026-10-03T12:59:51.281629Z","shell.execute_reply":"2026-10-03T12:59:52.345966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!uname --all","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T12:59:52.349243Z","iopub.execute_input":"2026-10-03T12:59:52.349947Z","iopub.status.idle":"2026-10-03T12:59:53.368942Z","shell.execute_reply.started":"2026-10-03T12:59:52.349916Z","shell.execute_reply":"2026-10-03T12:59:53.368043Z"}},"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-10-03T12:59:53.370391Z","iopub.execute_input":"2026-10-03T12:59:53.370694Z","iopub.status.idle":"2026-10-03T13:00:06.288072Z","shell.execute_reply.started":"2026-10-03T12:59:53.370667Z","shell.execute_reply":"2026-10-03T13:00:06.287249Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:06.290183Z","iopub.execute_input":"2026-10-03T13:00:06.290534Z","iopub.status.idle":"2026-10-03T13:00:07.387111Z","shell.execute_reply.started":"2026-10-03T13:00:06.290508Z","shell.execute_reply":"2026-10-03T13:00:07.386181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:07.388461Z","iopub.execute_input":"2026-10-03T13:00:07.388767Z","iopub.status.idle":"2026-10-03T13:00:08.441841Z","shell.execute_reply.started":"2026-10-03T13:00:07.388739Z","shell.execute_reply":"2026-10-03T13:00:08.440817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls test1/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:08.443371Z","iopub.execute_input":"2026-10-03T13:00:08.443683Z","iopub.status.idle":"2026-10-03T13:00:09.55274Z","shell.execute_reply.started":"2026-10-03T13:00:08.443653Z","shell.execute_reply":"2026-10-03T13:00:09.551498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:09.554338Z","iopub.execute_input":"2026-10-03T13:00:09.554649Z","iopub.status.idle":"2026-10-03T13:00:10.738072Z","shell.execute_reply.started":"2026-10-03T13:00:09.55462Z","shell.execute_reply":"2026-10-03T13:00:10.737047Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#ls train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:10.739738Z","iopub.execute_input":"2026-10-03T13:00:10.740146Z","iopub.status.idle":"2026-10-03T13:00:10.922131Z","shell.execute_reply.started":"2026-10-03T13:00:10.740097Z","shell.execute_reply":"2026-10-03T13:00:10.921189Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -U train | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:10.923503Z","iopub.execute_input":"2026-10-03T13:00:10.923862Z","iopub.status.idle":"2026-10-03T13:00:12.019127Z","shell.execute_reply.started":"2026-10-03T13:00:10.923829Z","shell.execute_reply":"2026-10-03T13:00:12.018245Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -U test1 | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:12.022233Z","iopub.execute_input":"2026-10-03T13:00:12.022523Z","iopub.status.idle":"2026-10-03T13:00:13.07845Z","shell.execute_reply.started":"2026-10-03T13:00:12.022498Z","shell.execute_reply":"2026-10-03T13:00:13.077488Z"}},"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-10-03T13:00:13.08004Z","iopub.execute_input":"2026-10-03T13:00:13.080447Z","iopub.status.idle":"2026-10-03T13:00:14.11906Z","shell.execute_reply.started":"2026-10-03T13:00:13.080403Z","shell.execute_reply":"2026-10-03T13:00:14.118207Z"},"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-10-03T13:00:14.120309Z","iopub.execute_input":"2026-10-03T13:00:14.120593Z","iopub.status.idle":"2026-10-03T13:00:15.097799Z","shell.execute_reply.started":"2026-10-03T13:00:14.12057Z","shell.execute_reply":"2026-10-03T13:00:15.096739Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:15.098894Z","iopub.execute_input":"2026-10-03T13:00:15.099291Z","iopub.status.idle":"2026-10-03T13:00:16.135174Z","shell.execute_reply.started":"2026-10-03T13:00:15.099266Z","shell.execute_reply":"2026-10-03T13:00:16.134271Z"}},"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-10-03T13:00:16.136634Z","iopub.execute_input":"2026-10-03T13:00:16.136934Z","iopub.status.idle":"2026-10-03T13:00:16.142874Z","shell.execute_reply.started":"2026-10-03T13:00:16.136909Z","shell.execute_reply":"2026-10-03T13:00:16.141873Z"}},"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-10-03T13:00:16.143971Z","iopub.execute_input":"2026-10-03T13:00:16.1444Z","iopub.status.idle":"2026-10-03T13:00:16.155619Z","shell.execute_reply.started":"2026-10-03T13:00:16.144378Z","shell.execute_reply":"2026-10-03T13:00:16.154736Z"},"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-10-03T13:00:16.156659Z","iopub.execute_input":"2026-10-03T13:00:16.15693Z","iopub.status.idle":"2026-10-03T13:00:16.169677Z","shell.execute_reply.started":"2026-10-03T13:00:16.156909Z","shell.execute_reply":"2026-10-03T13:00:16.168865Z"},"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-10-03T13:00:16.170739Z","iopub.execute_input":"2026-10-03T13:00:16.171254Z","iopub.status.idle":"2026-10-03T13:00:16.189695Z","shell.execute_reply.started":"2026-10-03T13:00:16.17123Z","shell.execute_reply":"2026-10-03T13:00:16.188727Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:16.190824Z","iopub.execute_input":"2026-10-03T13:00:16.191307Z","iopub.status.idle":"2026-10-03T13:00:17.210683Z","shell.execute_reply.started":"2026-10-03T13:00:16.191285Z","shell.execute_reply":"2026-10-03T13:00:17.209438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:17.21243Z","iopub.execute_input":"2026-10-03T13:00:17.21283Z","iopub.status.idle":"2026-10-03T13:00:18.226171Z","shell.execute_reply.started":"2026-10-03T13:00:17.212792Z","shell.execute_reply":"2026-10-03T13:00:18.224922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/test #validation","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:18.228046Z","iopub.execute_input":"2026-10-03T13:00:18.228433Z","iopub.status.idle":"2026-10-03T13:00:19.252526Z","shell.execute_reply.started":"2026-10-03T13:00:18.228397Z","shell.execute_reply":"2026-10-03T13:00:19.251532Z"}},"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-10-03T13:00:19.253934Z","iopub.execute_input":"2026-10-03T13:00:19.254299Z","iopub.status.idle":"2026-10-03T13:00:21.142219Z","shell.execute_reply.started":"2026-10-03T13:00:19.254272Z","shell.execute_reply":"2026-10-03T13:00:21.141432Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(src)\nprint(dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:21.143368Z","iopub.execute_input":"2026-10-03T13:00:21.144203Z","iopub.status.idle":"2026-10-03T13:00:21.148472Z","shell.execute_reply.started":"2026-10-03T13:00:21.144177Z","shell.execute_reply":"2026-10-03T13:00:21.147456Z"}},"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-10-03T13:00:21.1496Z","iopub.execute_input":"2026-10-03T13:00:21.149966Z","iopub.status.idle":"2026-10-03T13:00:23.328093Z","shell.execute_reply.started":"2026-10-03T13:00:21.149933Z","shell.execute_reply":"2026-10-03T13:00:23.327126Z"},"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-10-03T13:00:23.32961Z","iopub.execute_input":"2026-10-03T13:00:23.330033Z","iopub.status.idle":"2026-10-03T13:00:25.431229Z","shell.execute_reply.started":"2026-10-03T13:00:23.32997Z","shell.execute_reply":"2026-10-03T13:00:25.430257Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"execution":{"iopub.status.busy":"2026-10-03T13:00:25.43282Z","iopub.execute_input":"2026-10-03T13:00:25.433516Z","iopub.status.idle":"2026-10-03T13:00:37.196045Z","shell.execute_reply.started":"2026-10-03T13:00:25.433477Z","shell.execute_reply":"2026-10-03T13:00:37.19458Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2026-10-03T13:00:37.19776Z","iopub.execute_input":"2026-10-03T13:00:37.198409Z","iopub.status.idle":"2026-10-03T13:00:37.214159Z","shell.execute_reply.started":"2026-10-03T13:00:37.19838Z","shell.execute_reply":"2026-10-03T13:00:37.213046Z"},"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-10-03T13:00:37.215432Z","iopub.execute_input":"2026-10-03T13:00:37.216143Z","iopub.status.idle":"2026-10-03T13:00:37.22022Z","shell.execute_reply.started":"2026-10-03T13:00:37.216117Z","shell.execute_reply":"2026-10-03T13:00:37.219244Z"}},"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-10-03T13:00:37.228338Z","iopub.execute_input":"2026-10-03T13:00:37.228625Z","iopub.status.idle":"2026-10-03T13:00:37.23429Z","shell.execute_reply.started":"2026-10-03T13:00:37.228604Z","shell.execute_reply":"2026-10-03T13:00:37.233321Z"}},"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',\n                     kernel_initializer='he_uniform',\n                     padding='same',\n                     input_shape=(200, 200, 3))\n             )\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-10-03T13:00:37.235358Z","iopub.execute_input":"2026-10-03T13:00:37.235702Z","iopub.status.idle":"2026-10-03T13:00:37.246103Z","shell.execute_reply.started":"2026-10-03T13:00:37.235658Z","shell.execute_reply":"2026-10-03T13:00:37.245262Z"}},"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-10-03T13:00:37.247058Z","iopub.execute_input":"2026-10-03T13:00:37.247353Z","iopub.status.idle":"2026-10-03T13:00:37.330105Z","shell.execute_reply.started":"2026-10-03T13:00:37.247325Z","shell.execute_reply":"2026-10-03T13:00:37.329339Z"}},"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-10-03T13:00:37.331245Z","iopub.execute_input":"2026-10-03T13:00:37.331519Z","iopub.status.idle":"2026-10-03T13:00:37.338017Z","shell.execute_reply.started":"2026-10-03T13:00:37.331496Z","shell.execute_reply":"2026-10-03T13:00:37.337032Z"}},"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-10-03T13:00:37.339177Z","iopub.execute_input":"2026-10-03T13:00:37.33954Z","iopub.status.idle":"2026-10-03T13:00:37.422851Z","shell.execute_reply.started":"2026-10-03T13:00:37.339485Z","shell.execute_reply":"2026-10-03T13:00:37.419756Z"}},"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\nmy_inputs = Input(shape=(200, 200, 3))\n\n# Construct the forward pass of the computational graph\nx1 = Conv2D(32, (3, 3), activation='relu')(my_inputs)\n\nx2 = MaxPooling2D((2, 2))(x1)\n\nx3 = Flatten()(x2)\n\nx5 = Dense(256, activation='relu')(x3)\n\nx4 = Dense(128, activation='relu')(x5)\n\n# Define the output tensor with the target objective function representation\nmy_outputs = Dense(2, activation='softmax')(x4)\n\n# Instantiate the formal model architecture\nmodel = Model(inputs=my_inputs, outputs=my_outputs)\n\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:37.425078Z","iopub.execute_input":"2026-10-03T13:00:37.425472Z","iopub.status.idle":"2026-10-03T13:00:37.505016Z","shell.execute_reply.started":"2026-10-03T13:00:37.425446Z","shell.execute_reply":"2026-10-03T13:00:37.504051Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# The Functional API - new cell\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense\n\n# explicitly define the input tensor topology\nmy_inputs = Input(shape=(200, 200, 3))\n\n# Construct the forward pass of the computational graph\nx1 = Conv2D(32, (3, 3), activation='relu')(my_inputs)\n\nx1 = Conv2D(64, (3, 3), activation='relu')(x1)\n\nx2 = MaxPooling2D((2, 2))(x1)\n\nx3 = Flatten()(x2)\n\nx5 = Dense(256, activation='relu')(x3)\n\nx4 = Dense(128, activation='relu')(x5)\n\n# Define the output tensor with the target objective function representation\nmy_outputs = Dense(2, activation='softmax')(x4)\n\n# Instantiate the formal model architecture\nmodel = Model(inputs=my_inputs, outputs=my_outputs)\n\nprint(model.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:37.506191Z","iopub.execute_input":"2026-10-03T13:00:37.506544Z","iopub.status.idle":"2026-10-03T13:00:37.594926Z","shell.execute_reply.started":"2026-10-03T13:00:37.506521Z","shell.execute_reply":"2026-10-03T13:00:37.591801Z"}},"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-10-03T13:00:37.596606Z","iopub.execute_input":"2026-10-03T13:00:37.597149Z","iopub.status.idle":"2026-10-03T13:00:37.603176Z","shell.execute_reply.started":"2026-10-03T13:00:37.597125Z","shell.execute_reply":"2026-10-03T13:00:37.602182Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls dataset_dogs_vs_cats/train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:00:37.604451Z","iopub.execute_input":"2026-10-03T13:00:37.605031Z","iopub.status.idle":"2026-10-03T13:00:38.643505Z","shell.execute_reply.started":"2026-10-03T13:00:37.604968Z","shell.execute_reply":"2026-10-03T13:00:38.64254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE = 224 #for vgg16\nBATCH_SIZE = 64\nEPOCHS = 30","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T14:16:34.338006Z","iopub.execute_input":"2026-10-03T14:16:34.338512Z","iopub.status.idle":"2026-10-03T14:16:34.342894Z","shell.execute_reply.started":"2026-10-03T14:16:34.33848Z","shell.execute_reply":"2026-10-03T14:16:34.342076Z"}},"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-10-03T13:00:38.651343Z","iopub.execute_input":"2026-10-03T13:00:38.651669Z","iopub.status.idle":"2026-10-03T13:01:14.341392Z","shell.execute_reply.started":"2026-10-03T13:00:38.65164Z","shell.execute_reply":"2026-10-03T13:01:14.340388Z"}},"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-10-03T13:01:14.342623Z","iopub.execute_input":"2026-10-03T13:01:14.342963Z","iopub.status.idle":"2026-10-03T13:01:14.349569Z","shell.execute_reply.started":"2026-10-03T13:01:14.342939Z","shell.execute_reply":"2026-10-03T13:01:14.348628Z"}},"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-10-03T13:01:14.350862Z","iopub.execute_input":"2026-10-03T13:01:14.351455Z","iopub.status.idle":"2026-10-03T13:01:14.359913Z","shell.execute_reply.started":"2026-10-03T13:01:14.351422Z","shell.execute_reply":"2026-10-03T13:01:14.358883Z"}},"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-10-03T13:01:14.361161Z","iopub.execute_input":"2026-10-03T13:01:14.361654Z","iopub.status.idle":"2026-10-03T13:01:14.371361Z","shell.execute_reply.started":"2026-10-03T13:01:14.361624Z","shell.execute_reply":"2026-10-03T13:01:14.37044Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\n# summarize_diagnostics(history_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:01:14.372774Z","iopub.execute_input":"2026-10-03T13:01:14.373125Z","iopub.status.idle":"2026-10-03T13:01:14.385441Z","shell.execute_reply.started":"2026-10-03T13:01:14.373096Z","shell.execute_reply":"2026-10-03T13:01:14.38444Z"}},"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-10-03T13:01:14.387924Z","iopub.execute_input":"2026-10-03T13:01:14.388678Z","iopub.status.idle":"2026-10-03T13:01:14.397174Z","shell.execute_reply.started":"2026-10-03T13:01:14.388645Z","shell.execute_reply":"2026-10-03T13:01:14.396343Z"}},"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-10-03T13:01:14.398511Z","iopub.execute_input":"2026-10-03T13:01:14.398833Z","iopub.status.idle":"2026-10-03T13:01:15.530209Z","shell.execute_reply.started":"2026-10-03T13:01:14.39881Z","shell.execute_reply":"2026-10-03T13:01:15.529288Z"}},"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-10-03T13:01:15.531623Z","iopub.execute_input":"2026-10-03T13:01:15.532084Z","iopub.status.idle":"2026-10-03T13:01:15.53876Z","shell.execute_reply.started":"2026-10-03T13:01:15.532057Z","shell.execute_reply":"2026-10-03T13:01:15.537823Z"}},"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-10-03T13:01:15.539797Z","iopub.execute_input":"2026-10-03T13:01:15.540096Z","iopub.status.idle":"2026-10-03T13:01:15.558588Z","shell.execute_reply.started":"2026-10-03T13:01:15.540074Z","shell.execute_reply":"2026-10-03T13:01:15.55747Z"}},"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-10-03T13:01:15.560321Z","iopub.execute_input":"2026-10-03T13:01:15.56067Z","iopub.status.idle":"2026-10-03T13:01:15.56769Z","shell.execute_reply.started":"2026-10-03T13:01:15.560617Z","shell.execute_reply":"2026-10-03T13:01:15.566669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:01:15.568857Z","iopub.execute_input":"2026-10-03T13:01:15.570191Z","iopub.status.idle":"2026-10-03T13:01:15.581665Z","shell.execute_reply.started":"2026-10-03T13:01:15.570168Z","shell.execute_reply":"2026-10-03T13:01:15.580974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.loc[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:01:15.582604Z","iopub.execute_input":"2026-10-03T13:01:15.582914Z","iopub.status.idle":"2026-10-03T13:01:15.593825Z","shell.execute_reply.started":"2026-10-03T13:01:15.582892Z","shell.execute_reply":"2026-10-03T13:01:15.592926Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# test_df.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:01:15.594976Z","iopub.execute_input":"2026-10-03T13:01:15.595344Z","iopub.status.idle":"2026-10-03T13:01:15.607486Z","shell.execute_reply.started":"2026-10-03T13:01:15.595322Z","shell.execute_reply":"2026-10-03T13:01:15.606424Z"}},"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-10-03T13:01:15.608962Z","iopub.execute_input":"2026-10-03T13:01:15.609492Z","iopub.status.idle":"2026-10-03T13:01:15.615421Z","shell.execute_reply.started":"2026-10-03T13:01:15.609467Z","shell.execute_reply":"2026-10-03T13:01:15.614405Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE, BATCH_SIZE","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:01:15.616516Z","iopub.execute_input":"2026-10-03T13:01:15.616819Z","iopub.status.idle":"2026-10-03T13:01:15.629747Z","shell.execute_reply.started":"2026-10-03T13:01:15.616791Z","shell.execute_reply":"2026-10-03T13:01:15.62858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. Instantiate the generator with the validation_split ratio\ntest_datagen = ImageDataGenerator(\n    rescale=1.0/255.0,\n    horizontal_flip=True\n)\n\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)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:01:15.63115Z","iopub.execute_input":"2026-10-03T13:01:15.631507Z","iopub.status.idle":"2026-10-03T13:01:34.66381Z","shell.execute_reply.started":"2026-10-03T13:01:15.631475Z","shell.execute_reply":"2026-10-03T13:01:34.663122Z"}},"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-10-03T13:01:34.664848Z","iopub.execute_input":"2026-10-03T13:01:34.665154Z","iopub.status.idle":"2026-10-03T13:01:34.671011Z","shell.execute_reply.started":"2026-10-03T13:01:34.665131Z","shell.execute_reply":"2026-10-03T13:01:34.670188Z"}},"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-10-03T13:01:34.672214Z","iopub.execute_input":"2026-10-03T13:01:34.672548Z","iopub.status.idle":"2026-10-03T13:01:44.356062Z","shell.execute_reply.started":"2026-10-03T13:01:34.672513Z","shell.execute_reply":"2026-10-03T13:01:44.354905Z"}},"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-10-03T13:01:44.358201Z","iopub.execute_input":"2026-10-03T13:01:44.359178Z","iopub.status.idle":"2026-10-03T13:01:54.220472Z","shell.execute_reply.started":"2026-10-03T13:01:44.35915Z","shell.execute_reply":"2026-10-03T13:01:54.219034Z"}},"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-10-03T13:01:54.221767Z","iopub.execute_input":"2026-10-03T13:01:54.222174Z","iopub.status.idle":"2026-10-03T13:01:54.228135Z","shell.execute_reply.started":"2026-10-03T13:01:54.222147Z","shell.execute_reply":"2026-10-03T13:01:54.227007Z"},"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-10-03T13:01:54.229444Z","iopub.execute_input":"2026-10-03T13:01:54.229765Z","iopub.status.idle":"2026-10-03T13:02:03.860317Z","shell.execute_reply.started":"2026-10-03T13:01:54.229738Z","shell.execute_reply":"2026-10-03T13:02:03.85885Z"}},"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-10-03T13:02:03.861769Z","iopub.execute_input":"2026-10-03T13:02:03.862132Z","iopub.status.idle":"2026-10-03T13:02:03.867376Z","shell.execute_reply.started":"2026-10-03T13:02:03.862105Z","shell.execute_reply":"2026-10-03T13:02:03.866076Z"}},"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-10-03T13:02:03.868604Z","iopub.execute_input":"2026-10-03T13:02:03.869138Z","iopub.status.idle":"2026-10-03T13:02:03.88872Z","shell.execute_reply.started":"2026-10-03T13:02:03.869113Z","shell.execute_reply":"2026-10-03T13:02:03.887794Z"}},"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-10-03T13:02:03.889964Z","iopub.execute_input":"2026-10-03T13:02:03.890757Z","iopub.status.idle":"2026-10-03T13:02:03.901523Z","shell.execute_reply.started":"2026-10-03T13:02:03.890721Z","shell.execute_reply":"2026-10-03T13:02:03.90075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import sys\nfrom matplotlib import pyplot\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense, Flatten\nfrom keras.optimizers import SGD\n\nIMAGE_SIZE = 224\nBATCH_SIZE = 64\nEPOCHS = 20\n\ndef define_model_vgg16_finetune():\n    base_model = VGG16(include_top=False, input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3))\n    \n    for layer in base_model.layers:\n        if layer.name.startswith('block5'):\n            layer.trainable = True   # ให้ Block 5 เรียนรู้เพิ่มได้\n        else:\n            layer.trainable = False  # Freeze Block 1-4 ไว้\n            \n    flat1 = Flatten()(base_model.output)\n    class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\n    output = Dense(1, activation='sigmoid')(class1)\n    \n    model = Model(inputs=base_model.input, outputs=output)\n    \n    opt = SGD(learning_rate=0.0001, momentum=0.9)\n    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n    \n    return model\n\nvgg16_finetuned = define_model_vgg16_finetune()\nvgg16_finetuned.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:03.902534Z","iopub.execute_input":"2026-10-03T13:02:03.902838Z","iopub.status.idle":"2026-10-03T13:02:04.323225Z","shell.execute_reply.started":"2026-10-03T13:02:03.902816Z","shell.execute_reply":"2026-10-03T13:02:04.321961Z"}},"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,\n                        input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3)\n                       )\nmodel_vgg16_top.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:04.324344Z","iopub.execute_input":"2026-10-03T13:02:04.324717Z","iopub.status.idle":"2026-10-03T13:02:06.676047Z","shell.execute_reply.started":"2026-10-03T13:02:04.324691Z","shell.execute_reply":"2026-10-03T13:02:06.674955Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load model\nmodel_16_feature_extractor = VGG16(include_top=False,\n                                   input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3)\n                                  )\nmodel_16_feature_extractor.summary() #feature extractor as a backbone","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:06.677308Z","iopub.execute_input":"2026-10-03T13:02:06.677603Z","iopub.status.idle":"2026-10-03T13:02:07.022667Z","shell.execute_reply.started":"2026-10-03T13:02:06.67758Z","shell.execute_reply":"2026-10-03T13:02:07.021913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for index, layer in enumerate(model_16_feature_extractor.layers):\n\t\tprint(index, \n              layer.name,\n              layer.trainable, \n              layer.output.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.024038Z","iopub.execute_input":"2026-10-03T13:02:07.024395Z","iopub.status.idle":"2026-10-03T13:02:07.030791Z","shell.execute_reply.started":"2026-10-03T13:02:07.024351Z","shell.execute_reply":"2026-10-03T13:02:07.029829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for index, layer in enumerate(model_16_feature_extractor.layers):\n    if index <= 14:\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-10-03T13:02:07.032037Z","iopub.execute_input":"2026-10-03T13:02:07.032347Z","iopub.status.idle":"2026-10-03T13:02:07.044036Z","shell.execute_reply.started":"2026-10-03T13:02:07.032318Z","shell.execute_reply":"2026-10-03T13:02:07.043064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_16_feature_extractor.layers[-1].output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.045144Z","iopub.execute_input":"2026-10-03T13:02:07.046175Z","iopub.status.idle":"2026-10-03T13:02:07.05701Z","shell.execute_reply.started":"2026-10-03T13:02:07.046152Z","shell.execute_reply":"2026-10-03T13:02:07.055884Z"}},"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-10-03T13:02:07.05816Z","iopub.execute_input":"2026-10-03T13:02:07.058555Z","iopub.status.idle":"2026-10-03T13:02:07.071824Z","shell.execute_reply.started":"2026-10-03T13:02:07.058525Z","shell.execute_reply":"2026-10-03T13:02:07.070868Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"layer1 = Dense(256, activation='relu', kernel_initializer='he_uniform')(flat1)\nprint(layer1.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.073161Z","iopub.execute_input":"2026-10-03T13:02:07.073545Z","iopub.status.idle":"2026-10-03T13:02:07.097858Z","shell.execute_reply.started":"2026-10-03T13:02:07.073523Z","shell.execute_reply":"2026-10-03T13:02:07.096889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"layer2 = Dense(128, activation='relu', kernel_initializer='he_uniform')(layer1)\nprint(layer2.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.099193Z","iopub.execute_input":"2026-10-03T13:02:07.099511Z","iopub.status.idle":"2026-10-03T13:02:07.114874Z","shell.execute_reply.started":"2026-10-03T13:02:07.099481Z","shell.execute_reply":"2026-10-03T13:02:07.113899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output1 = Dense(2, activation='softmax')(layer2)\nprint(f\"{output1.shape=}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.11596Z","iopub.execute_input":"2026-10-03T13:02:07.116285Z","iopub.status.idle":"2026-10-03T13:02:07.129884Z","shell.execute_reply.started":"2026-10-03T13:02:07.116256Z","shell.execute_reply":"2026-10-03T13:02:07.128973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output2 = Dense(1, activation='sigmoid')(layer2)\nprint(output2.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.131261Z","iopub.execute_input":"2026-10-03T13:02:07.131544Z","iopub.status.idle":"2026-10-03T13:02:07.147705Z","shell.execute_reply.started":"2026-10-03T13:02:07.131523Z","shell.execute_reply":"2026-10-03T13:02:07.146647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# define new model to be trained\nmodel_vgg16 = Model(inputs=model_16_feature_extractor.inputs, outputs=output2)\n# compile model\nopt = SGD(lr=0.005, momentum=0.9)\n\nmodel_vgg16.compile(optimizer=opt,\n                    loss='binary_crossentropy',\n                    metrics=['accuracy']\n                   )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.148784Z","iopub.execute_input":"2026-10-03T13:02:07.149418Z","iopub.status.idle":"2026-10-03T13:02:07.16423Z","shell.execute_reply.started":"2026-10-03T13:02:07.149394Z","shell.execute_reply":"2026-10-03T13:02:07.163253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for index, layer in enumerate(model_16_feature_extractor.layers):\n    print(index, layer.name, layer.trainable)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.165344Z","iopub.execute_input":"2026-10-03T13:02:07.165656Z","iopub.status.idle":"2026-10-03T13:02:07.170873Z","shell.execute_reply.started":"2026-10-03T13:02:07.165628Z","shell.execute_reply":"2026-10-03T13:02:07.170046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(model_vgg16.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.172144Z","iopub.execute_input":"2026-10-03T13:02:07.172487Z","iopub.status.idle":"2026-10-03T13:02:07.228805Z","shell.execute_reply.started":"2026-10-03T13:02:07.172457Z","shell.execute_reply":"2026-10-03T13:02:07.228137Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"id(train_it), id(val_it)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.229947Z","iopub.execute_input":"2026-10-03T13:02:07.230471Z","iopub.status.idle":"2026-10-03T13:02:07.237473Z","shell.execute_reply.started":"2026-10-03T13:02:07.230449Z","shell.execute_reply":"2026-10-03T13:02:07.236392Z"}},"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-10-03T13:02:07.238696Z","iopub.execute_input":"2026-10-03T13:02:07.239016Z","iopub.status.idle":"2026-10-03T13:02:07.248635Z","shell.execute_reply.started":"2026-10-03T13:02:07.238971Z","shell.execute_reply":"2026-10-03T13:02:07.247661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit model\nimport tensorflow as tf\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=5, \n            verbose=1,\n            callbacks=[PlotLossesKeras()]\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=5, \n        verbose=30,\n        callbacks=[PlotLossesKeras()]\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:02:07.24984Z","iopub.execute_input":"2026-10-03T13:02:07.250123Z","iopub.status.idle":"2026-10-03T13:11:46.437079Z","shell.execute_reply.started":"2026-10-03T13:02:07.250097Z","shell.execute_reply":"2026-10-03T13:11:46.436104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\nsummarize_diagnostics(model_vgg16_history)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:11:46.438498Z","iopub.execute_input":"2026-10-03T13:11:46.439329Z","iopub.status.idle":"2026-10-03T13:11:46.813187Z","shell.execute_reply.started":"2026-10-03T13:11:46.439291Z","shell.execute_reply":"2026-10-03T13:11:46.812046Z"}},"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-10-03T13:11:46.814634Z","iopub.execute_input":"2026-10-03T13:11:46.815482Z","iopub.status.idle":"2026-10-03T13:12:03.878735Z","shell.execute_reply.started":"2026-10-03T13:11:46.815442Z","shell.execute_reply":"2026-10-03T13:12:03.877939Z"}},"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-10-03T13:12:03.879824Z","iopub.execute_input":"2026-10-03T13:12:03.880213Z","iopub.status.idle":"2026-10-03T13:13:19.771431Z","shell.execute_reply.started":"2026-10-03T13:12:03.880188Z","shell.execute_reply":"2026-10-03T13:13:19.770586Z"}},"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-10-03T13:13:19.780752Z","iopub.execute_input":"2026-10-03T13:13:19.781446Z","iopub.status.idle":"2026-10-03T13:13:48.722096Z","shell.execute_reply.started":"2026-10-03T13:13:19.781422Z","shell.execute_reply":"2026-10-03T13:13:48.721048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"probabilities[:20]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:13:48.723332Z","iopub.execute_input":"2026-10-03T13:13:48.723706Z","iopub.status.idle":"2026-10-03T13:13:48.730434Z","shell.execute_reply.started":"2026-10-03T13:13:48.723673Z","shell.execute_reply":"2026-10-03T13:13:48.729509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(probabilities[0] > 0.5).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:13:48.73159Z","iopub.execute_input":"2026-10-03T13:13:48.731813Z","iopub.status.idle":"2026-10-03T13:13:48.747347Z","shell.execute_reply.started":"2026-10-03T13:13:48.731793Z","shell.execute_reply":"2026-10-03T13:13:48.746516Z"}},"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-10-03T13:13:48.748619Z","iopub.execute_input":"2026-10-03T13:13:48.749247Z","iopub.status.idle":"2026-10-03T13:13:48.780561Z","shell.execute_reply.started":"2026-10-03T13:13:48.749198Z","shell.execute_reply":"2026-10-03T13:13:48.779536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_true[:10], y_pred[:10]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:13:48.781724Z","iopub.execute_input":"2026-10-03T13:13:48.782697Z","iopub.status.idle":"2026-10-03T13:13:48.788969Z","shell.execute_reply.started":"2026-10-03T13:13:48.782674Z","shell.execute_reply":"2026-10-03T13:13:48.787732Z"}},"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'}\n\nprint(\"Done.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T13:13:48.790262Z","iopub.execute_input":"2026-10-03T13:13:48.790547Z","iopub.status.idle":"2026-10-03T13:13:56.059519Z","shell.execute_reply.started":"2026-10-03T13:13:48.790524Z","shell.execute_reply":"2026-10-03T13:13:56.058482Z"}},"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-10-03T13:13:56.060625Z","iopub.execute_input":"2026-10-03T13:13:56.061028Z","iopub.status.idle":"2026-10-03T13:14:04.709468Z","shell.execute_reply.started":"2026-10-03T13:13:56.060964Z","shell.execute_reply":"2026-10-03T13:14:04.707995Z"}},"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-10-03T13:14:04.711492Z","iopub.execute_input":"2026-10-03T13:14:04.711937Z","iopub.status.idle":"2026-10-03T13:14:04.774609Z","shell.execute_reply.started":"2026-10-03T13:14:04.711895Z","shell.execute_reply":"2026-10-03T13:14:04.773542Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# InceptionV3","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# 0. กำหนดค่า Hyperparameters\nIMAGE_SIZE = 224\nBATCH_SIZE = 64  # ตรวจสอบตัวสเปลลิ้งให้ถูกต้อง (ไม่ใช่ BATCH_SIE)\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-10-03T14:56:59.507998Z","iopub.execute_input":"2026-10-03T14:56:59.509073Z","iopub.status.idle":"2026-10-03T14:59:28.088474Z","shell.execute_reply.started":"2026-10-03T14:56:59.509034Z","shell.execute_reply":"2026-10-03T14:59:28.087238Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras.applications.inception_v3 import InceptionV3\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D, BatchNormalization\nfrom keras.optimizers import Adam\n\ndef define_model_inceptionv3(input_shape=(200, 200, 3)):\n    base_model = InceptionV3(\n        include_top=False,\n        weights='imagenet',\n        input_shape=input_shape\n    )\n\n    base_model.trainable = False\n\n    # ต่อ Classifier Head\n    x = base_model.output\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(256, activation='relu', kernel_initializer='he_uniform')(x)\n    x = Dropout(0.2)(x)\n    output = Dense(1, activation='sigmoid')(x)\n\n    model = Model(inputs=base_model.input, outputs=output)\n\n    model.compile(\n        optimizer=Adam(learning_rate=1e-3),\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model, base_model\n\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T15:12:31.088134Z","iopub.execute_input":"2026-10-03T15:12:31.088648Z","iopub.status.idle":"2026-10-03T15:12:31.09607Z","shell.execute_reply.started":"2026-10-03T15:12:31.088615Z","shell.execute_reply":"2026-10-03T15:12:31.095041Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_inceptionv3 = define_model_inceptionv3()\nprint(id(model_inceptionv3))\n# model_inceptionv3.summary()","metadata":{"execution":{"iopub.status.busy":"2026-10-03T15:12:31.108142Z","iopub.execute_input":"2026-10-03T15:12:31.108874Z","iopub.status.idle":"2026-10-03T15:12:33.827494Z","shell.execute_reply.started":"2026-10-03T15:12:31.10884Z","shell.execute_reply":"2026-10-03T15:12:33.826209Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom tensorflow.keras import backend as K\n\n# เติม [0] หลัง model_inceptionv3\ntrainable_params = int(np.sum([K.count_params(w) for w in model_inceptionv3[0].trainable_weights]))\nprint(f\"Trainable params: {trainable_params:,}\")","metadata":{"trusted":true,"scrolled":true,"execution":{"iopub.status.busy":"2026-10-03T15:12:33.831889Z","iopub.execute_input":"2026-10-03T15:12:33.832351Z","iopub.status.idle":"2026-10-03T15:12:33.840534Z","shell.execute_reply.started":"2026-10-03T15:12:33.832308Z","shell.execute_reply":"2026-10-03T15:12:33.839205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import backend as K\n\nfor w in model_inceptionv3[0].trainable_weights:\n    print(w.name, K.count_params(w))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T15:12:33.842214Z","iopub.execute_input":"2026-10-03T15:12:33.842753Z","iopub.status.idle":"2026-10-03T15:12:33.857254Z","shell.execute_reply.started":"2026-10-03T15:12:33.842705Z","shell.execute_reply":"2026-10-03T15:12:33.855809Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# แปลงตัวแปร tuple ให้เหลือเฉพาะตัว model\nmodel_inceptionv3 = model_inceptionv3[0]\n\nprint(train_it.image_shape)\nprint(train_it.batch_size)\nprint(model_inceptionv3.input_shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T15:12:33.86033Z","iopub.execute_input":"2026-10-03T15:12:33.86086Z","iopub.status.idle":"2026-10-03T15:12:33.868081Z","shell.execute_reply.started":"2026-10-03T15:12:33.860825Z","shell.execute_reply":"2026-10-03T15:12:33.866964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_inceptionv3 = define_model_inceptionv3()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T15:12:33.869368Z","iopub.execute_input":"2026-10-03T15:12:33.86973Z","iopub.status.idle":"2026-10-03T15:12:36.643043Z","shell.execute_reply.started":"2026-10-03T15:12:33.869703Z","shell.execute_reply":"2026-10-03T15:12:36.641787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install livelossplot","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T15:23:19.314211Z","iopub.execute_input":"2026-10-03T15:23:19.314767Z","iopub.status.idle":"2026-10-03T15:23:31.51968Z","shell.execute_reply.started":"2026-10-03T15:23:19.314731Z","shell.execute_reply":"2026-10-03T15:23:31.518396Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras\n# หรือหากใช้ tf.keras ให้ใช้ PlotLossesKerasTF\n# from livelossplot import PlotLossesKerasTF\n\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-10-03T15:26:17.214847Z","iopub.execute_input":"2026-10-03T15:26:17.215337Z","iopub.status.idle":"2026-10-03T15:54:55.221161Z","shell.execute_reply.started":"2026-10-03T15:26:17.215307Z","shell.execute_reply":"2026-10-03T15:54:55.219858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def enable_finetuning(model, base_model, unfreeze_from='mixed7'):\n    base_model.trainable = True\n\n    set_trainable = False\n    for layer in base_model.layers:\n        if layer.name == unfreeze_from:\n            set_trainable = True\n        \n        layer.trainable = set_trainable\n\n        if isinstance(layer, BatchNormalization):\n            layer.trainable = False\n\n    # model.compile() รับเฉพาะ optimizer, loss, และ metrics\n    model.compile(\n        optimizer=Adam(learning_rate=1e-5),\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n    \n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T15:59:59.10099Z","iopub.execute_input":"2026-10-03T15:59:59.102133Z","iopub.status.idle":"2026-10-03T15:59:59.108308Z","shell.execute_reply.started":"2026-10-03T15:59:59.102076Z","shell.execute_reply":"2026-10-03T15:59:59.10729Z"}},"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-10-03T15:59:59.112423Z","iopub.execute_input":"2026-10-03T15:59:59.112744Z","iopub.status.idle":"2026-10-03T15:59:59.128641Z","shell.execute_reply.started":"2026-10-03T15:59:59.112721Z","shell.execute_reply":"2026-10-03T15:59:59.127259Z"}},"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-10-03T15:59:59.130698Z","iopub.execute_input":"2026-10-03T15:59:59.131212Z","iopub.status.idle":"2026-10-03T15:59:59.144194Z","shell.execute_reply.started":"2026-10-03T15:59:59.131181Z","shell.execute_reply":"2026-10-03T15:59:59.143281Z"}},"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-10-03T15:59:59.145416Z","iopub.execute_input":"2026-10-03T15:59:59.145829Z","iopub.status.idle":"2026-10-03T15:59:59.160989Z","shell.execute_reply.started":"2026-10-03T15:59:59.145803Z","shell.execute_reply":"2026-10-03T15:59:59.159697Z"}},"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-10-03T15:59:59.163479Z","iopub.execute_input":"2026-10-03T15:59:59.164476Z","iopub.status.idle":"2026-10-03T15:59:59.172293Z","shell.execute_reply.started":"2026-10-03T15:59:59.164419Z","shell.execute_reply":"2026-10-03T15:59:59.171209Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# evaluate model ด้วย val_it\n_, acc = model_inceptionv3.evaluate(val_it, steps=len(val_it), verbose=1)\nprint('Validation Accuracy: %.3f%%' % (acc * 100.0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T16:01:37.171523Z","iopub.execute_input":"2026-10-03T16:01:37.172297Z","iopub.status.idle":"2026-10-03T16:02:10.183601Z","shell.execute_reply.started":"2026-10-03T16:01:37.172262Z","shell.execute_reply":"2026-10-03T16:02:10.182491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ดึงข้อมูล Batch ถัดไปจาก val_it\nimages, labels = next(val_it)  # หรือ val_it.__next__()\n\nprint(images.shape)\nprint(labels.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T16:02:52.165991Z","iopub.execute_input":"2026-10-03T16:02:52.167209Z","iopub.status.idle":"2026-10-03T16:02:52.433231Z","shell.execute_reply.started":"2026-10-03T16:02:52.167166Z","shell.execute_reply":"2026-10-03T16:02:52.432142Z"}},"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,"execution":{"iopub.status.busy":"2026-10-03T16:02:54.402662Z","iopub.execute_input":"2026-10-03T16:02:54.403496Z","iopub.status.idle":"2026-10-03T16:02:54.410622Z","shell.execute_reply.started":"2026-10-03T16:02:54.403457Z","shell.execute_reply":"2026-10-03T16:02:54.409555Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# EfficientNetB7","metadata":{}},{"cell_type":"code","source":"# 1. กำหนด Path สำหรับ Test Set (ปรับ Path ตามโครงสร้างไฟล์ใน Kaggle ของคุณ)\ntest_path = '/kaggle/input/datasets/kasikrit/308-456-dogs-vs-cats-dataset/dataset_dogs_vs_cats/test'\n\n# 2. เรียกใช้งาน test_it\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-10-03T16:11:30.481677Z","iopub.execute_input":"2026-10-03T16:11:30.482445Z","iopub.status.idle":"2026-10-03T16:11:36.372742Z","shell.execute_reply.started":"2026-10-03T16:11:30.482406Z","shell.execute_reply":"2026-10-03T16:11:36.371773Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications import EfficientNetB7\nfrom keras.applications.efficientnet import preprocess_input\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\n\n\ndef define_model_EfficientNetB7():\n\n    # Base model\n    base_model = EfficientNetB7(\n        include_top=False,\n        weights='imagenet',\n        input_shape=(331, 331, 3)\n    )\n\n    # Freeze all layers\n    for layer in base_model.layers:\n        layer.trainable = False\n\n    # Fine-tune last 40 layers\n    for layer in base_model.layers[-40:]:\n        layer.trainable = True\n\n    # Classifier\n    x = base_model.output\n\n    x = GlobalAveragePooling2D()(x)\n\n    x = Dense(\n        256,\n        activation='relu',\n        kernel_initializer='he_uniform'\n    )(x)\n\n    x = Dropout(0.3)(x)\n\n    output = Dense(\n        1,\n        activation='sigmoid'\n    )(x)\n\n    # IMPORTANT\n    model = Model(\n        inputs=base_model.input,\n        outputs=output\n    )\n\n    # Compile\n    model.compile(\n        optimizer=Adam(learning_rate=1e-5),\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T16:20:23.907711Z","iopub.execute_input":"2026-10-03T16:20:23.90869Z","iopub.status.idle":"2026-10-03T16:20:23.916269Z","shell.execute_reply.started":"2026-10-03T16:20:23.908642Z","shell.execute_reply":"2026-10-03T16:20:23.915295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_EfficientNetB7 = define_model_EfficientNetB7()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T16:20:28.73482Z","iopub.execute_input":"2026-10-03T16:20:28.735455Z","iopub.status.idle":"2026-10-03T16:20:39.250139Z","shell.execute_reply.started":"2026-10-03T16:20:28.73541Z","shell.execute_reply":"2026-10-03T16:20:39.248787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_EfficientNetB7 = model_EfficientNetB7.fit(\n    train_it,\n    steps_per_epoch=len(train_it),\n    validation_data=val_it,\n    validation_steps=len(val_it),\n    epochs=5,\n    verbose=1,\n    callbacks=[PlotLossesKeras()]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T16:20:48.182979Z","iopub.execute_input":"2026-10-03T16:20:48.183505Z","iopub.status.idle":"2026-10-03T17:08:27.34813Z","shell.execute_reply.started":"2026-10-03T16:20:48.183472Z","shell.execute_reply":"2026-10-03T17:08:27.347148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def enable_efficientnet_finetuning(model, base_model, unfreeze_from_block='block6a'):\n    \"\"\"\n    ปลดล็อก MBConv Blocks ด้านบน (เช่น ตั้งแต่ block6a หรือ block7a ขึ้นไป)\n    และ Freeze BatchNormalization ทุกตัวเพื่อป้องกัน Weights เสียหาย\n    \"\"\"\n    base_model.trainable = True\n\n    set_trainable = False\n    for layer in base_model.layers:\n        # เริ่ม Unfreeze ตั้งแต่ Block ที่กำหนด\n        if unfreeze_from_block in layer.name:\n            set_trainable = True\n\n        if set_trainable:\n            # ปลดล็อก Layer ทั่วไป แต่บังคับ Freeze BatchNormalization เสมอ!\n            if isinstance(layer, BatchNormalization):\n                layer.trainable = False\n            else:\n                layer.trainable = True\n        else:\n            layer.trainable = False\n\n    # Re-compile สำหรับ Fine-tuning Stage (ใช้ Learning Rate ต่ำมาก)\n    model.compile(\n        optimizer=Adam(learning_rate=1e-5),\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-10-03T17:15:42.943699Z","iopub.execute_input":"2026-10-03T17:15:42.944528Z","iopub.status.idle":"2026-10-03T17:15:42.951152Z","shell.execute_reply.started":"2026-10-03T17:15:42.944492Z","shell.execute_reply":"2026-10-03T17:15:42.950102Z"}},"outputs":[],"execution_count":null}]}