{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2026-09-06T02:17:19.452378Z","iopub.execute_input":"2026-09-06T02:17:19.452732Z","iopub.status.idle":"2026-09-06T02:17:19.460821Z","shell.execute_reply.started":"2026-09-06T02:17:19.452702Z","shell.execute_reply":"2026-09-06T02:17:19.459761Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:17:20.85618Z","iopub.execute_input":"2026-09-06T02:17:20.857774Z","iopub.status.idle":"2026-09-06T02:17:21.949301Z","shell.execute_reply.started":"2026-09-06T02:17:20.857664Z","shell.execute_reply":"2026-09-06T02:17:21.948231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))\n\nimport zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:17:22.935523Z","iopub.execute_input":"2026-09-06T02:17:22.936368Z","iopub.status.idle":"2026-09-06T02:17:36.172875Z","shell.execute_reply.started":"2026-09-06T02:17:22.936332Z","shell.execute_reply":"2026-09-06T02:17:36.172131Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:17:36.1747Z","iopub.execute_input":"2026-09-06T02:17:36.175041Z","iopub.status.idle":"2026-09-06T02:17:37.236517Z","shell.execute_reply.started":"2026-09-06T02:17:36.175009Z","shell.execute_reply":"2026-09-06T02:17:37.235084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:17:37.238648Z","iopub.execute_input":"2026-09-06T02:17:37.238993Z","iopub.status.idle":"2026-09-06T02:17:38.293892Z","shell.execute_reply.started":"2026-09-06T02:17:37.238966Z","shell.execute_reply":"2026-09-06T02:17:38.292514Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls test1/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:17:41.543317Z","iopub.execute_input":"2026-09-06T02:17:41.543904Z","iopub.status.idle":"2026-09-06T02:17:42.669842Z","shell.execute_reply.started":"2026-09-06T02:17:41.543869Z","shell.execute_reply":"2026-09-06T02:17:42.668648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:17:42.671808Z","iopub.execute_input":"2026-09-06T02:17:42.672194Z","iopub.status.idle":"2026-09-06T02:17:43.840065Z","shell.execute_reply.started":"2026-09-06T02:17:42.67216Z","shell.execute_reply":"2026-09-06T02:17:43.839019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#ls train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:17:43.841515Z","iopub.execute_input":"2026-09-06T02:17:43.841885Z","iopub.status.idle":"2026-09-06T02:17:43.846608Z","shell.execute_reply.started":"2026-09-06T02:17:43.841857Z","shell.execute_reply":"2026-09-06T02:17:43.84554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot dog photos from the dogs vs cats dataset\nfrom matplotlib import pyplot\nfrom matplotlib.image import imread\n# define location of dataset\nfolder = 'train/'\n# plot first few images\nfor i in range(9):\n\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'dog.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tpyplot.imshow(image)\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:17:45.447362Z","iopub.execute_input":"2026-09-06T02:17:45.448301Z","iopub.status.idle":"2026-09-06T02:17:46.572453Z","shell.execute_reply.started":"2026-09-06T02:17:45.448267Z","shell.execute_reply":"2026-09-06T02:17:46.571341Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot cat photos from the dogs vs cats dataset\nfrom matplotlib import pyplot\nfrom matplotlib.image import imread\n# define location of dataset\nfolder = 'train/'\n# plot first few images\nfor i in range(9):\n\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'cat.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tpyplot.imshow(image)\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:17:46.574039Z","iopub.execute_input":"2026-09-06T02:17:46.574299Z","iopub.status.idle":"2026-09-06T02:17:47.566965Z","shell.execute_reply.started":"2026-09-06T02:17:46.574276Z","shell.execute_reply":"2026-09-06T02:17:47.565751Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nprint(tensorflow.__version__)\n\nimport keras\nprint(keras.__version__)\n\nimport tensorflow.keras\nprint(tensorflow.__version__)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:17:47.568317Z","iopub.execute_input":"2026-09-06T02:17:47.568642Z","iopub.status.idle":"2026-09-06T02:17:59.235248Z","shell.execute_reply.started":"2026-09-06T02:17:47.568607Z","shell.execute_reply":"2026-09-06T02:17:59.233947Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load dogs vs cats dataset, reshape and save to a new file\nfrom os import listdir\nfrom numpy import asarray\nfrom numpy import save\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:17:59.237475Z","iopub.execute_input":"2026-09-06T02:17:59.238651Z","iopub.status.idle":"2026-09-06T02:17:59.243245Z","shell.execute_reply.started":"2026-09-06T02:17:59.238613Z","shell.execute_reply":"2026-09-06T02:17:59.242266Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# organize dataset into a useful structure\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:17:59.244576Z","iopub.execute_input":"2026-09-06T02:17:59.244922Z","iopub.status.idle":"2026-09-06T02:17:59.268638Z","shell.execute_reply.started":"2026-09-06T02:17:59.244897Z","shell.execute_reply":"2026-09-06T02:17:59.267459Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/'] #test==>validation\nfor subdir in subdirs:\n    # create label subdirectories\n    labeldirs = ['dogs/', 'cats/']\n    for labldir in labeldirs:\n        newdir = dataset_home + subdir + labldir       \n        makedirs(newdir, exist_ok=True)\n        print(newdir)","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:17:59.270027Z","iopub.execute_input":"2026-09-06T02:17:59.270486Z","iopub.status.idle":"2026-09-06T02:17:59.279725Z","shell.execute_reply.started":"2026-09-06T02:17:59.270461Z","shell.execute_reply":"2026-09-06T02:17:59.278869Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train/dogs/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:17:59.281152Z","iopub.execute_input":"2026-09-06T02:17:59.28183Z","iopub.status.idle":"2026-09-06T02:18:00.344321Z","shell.execute_reply.started":"2026-09-06T02:17:59.281807Z","shell.execute_reply":"2026-09-06T02:18:00.343009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:18:00.346112Z","iopub.execute_input":"2026-09-06T02:18:00.346452Z","iopub.status.idle":"2026-09-06T02:18:01.448414Z","shell.execute_reply.started":"2026-09-06T02:18:00.346423Z","shell.execute_reply":"2026-09-06T02:18:01.447299Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:18:01.449926Z","iopub.execute_input":"2026-09-06T02:18:01.450269Z","iopub.status.idle":"2026-09-06T02:18:02.542271Z","shell.execute_reply.started":"2026-09-06T02:18:01.450239Z","shell.execute_reply":"2026-09-06T02:18:02.540884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:18:02.548572Z","iopub.execute_input":"2026-09-06T02:18:02.54912Z","iopub.status.idle":"2026-09-06T02:18:03.607452Z","shell.execute_reply.started":"2026-09-06T02:18:02.549067Z","shell.execute_reply":"2026-09-06T02:18:03.606428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# seed random number generator\nseed(1337)\n# define ratio of pictures to use for validation\nval_ratio = 0.25 #changable\n# copy training dataset images into subdirectories\nsrc_directory = 'train/'\n\nfor file in listdir(src_directory):\n\tsrc = src_directory + '/' + file\n\tdst_dir = 'train/'\n\tif random() < val_ratio:\n\t\tdst_dir = 'test/'\n\tif file.startswith('cat'):\n\t\tdst = dataset_home + dst_dir + 'cats/'  + file\n\t\tcopyfile(src, dst)\n\telif file.startswith('dog'):\n\t\tdst = dataset_home + dst_dir + 'dogs/'  + file\n\t\tcopyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:18:03.608998Z","iopub.execute_input":"2026-09-06T02:18:03.609362Z","iopub.status.idle":"2026-09-06T02:18:05.732151Z","shell.execute_reply.started":"2026-09-06T02:18:03.609331Z","shell.execute_reply":"2026-09-06T02:18:05.731004Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(src)\nprint(dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:18:05.733719Z","iopub.execute_input":"2026-09-06T02:18:05.734102Z","iopub.status.idle":"2026-09-06T02:18:05.739577Z","shell.execute_reply.started":"2026-09-06T02:18:05.734068Z","shell.execute_reply":"2026-09-06T02:18:05.738403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l dataset_dogs_vs_cats/train/dogs | wc -l\n!ls -l dataset_dogs_vs_cats/train/cats | wc -l","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:18:05.741263Z","iopub.execute_input":"2026-09-06T02:18:05.741685Z","iopub.status.idle":"2026-09-06T02:18:08.093213Z","shell.execute_reply.started":"2026-09-06T02:18:05.74164Z","shell.execute_reply":"2026-09-06T02:18:08.090714Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#validation set\n!ls -l dataset_dogs_vs_cats/test/dogs | wc -l\n!ls -l dataset_dogs_vs_cats/test/cats | wc -l","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:18:08.095804Z","iopub.execute_input":"2026-09-06T02:18:08.096327Z","iopub.status.idle":"2026-09-06T02:18:10.354224Z","shell.execute_reply.started":"2026-09-06T02:18:08.096278Z","shell.execute_reply":"2026-09-06T02:18:10.353131Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:18:10.355795Z","iopub.execute_input":"2026-09-06T02:18:10.356036Z","iopub.status.idle":"2026-09-06T02:18:20.944327Z","shell.execute_reply.started":"2026-09-06T02:18:10.356014Z","shell.execute_reply":"2026-09-06T02:18:20.943196Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2026-09-06T02:18:20.945826Z","iopub.execute_input":"2026-09-06T02:18:20.946298Z","iopub.status.idle":"2026-09-06T02:18:20.964741Z","shell.execute_reply.started":"2026-09-06T02:18:20.946268Z","shell.execute_reply":"2026-09-06T02:18:20.963587Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# https://keras.io/api/applications/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:18:20.965938Z","iopub.execute_input":"2026-09-06T02:18:20.966568Z","iopub.status.idle":"2026-09-06T02:18:20.970363Z","shell.execute_reply.started":"2026-09-06T02:18:20.966544Z","shell.execute_reply":"2026-09-06T02:18:20.969221Z"}},"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-06T02:18:20.971531Z","iopub.execute_input":"2026-09-06T02:18:20.97256Z","iopub.status.idle":"2026-09-06T02:18:20.980325Z","shell.execute_reply.started":"2026-09-06T02:18:20.972534Z","shell.execute_reply":"2026-09-06T02:18:20.979492Z"}},"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-06T02:18:20.98151Z","iopub.execute_input":"2026-09-06T02:18:20.981867Z","iopub.status.idle":"2026-09-06T02:18:20.989981Z","shell.execute_reply.started":"2026-09-06T02:18:20.981837Z","shell.execute_reply":"2026-09-06T02:18:20.988882Z"}},"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-06T02:18:20.991163Z","iopub.execute_input":"2026-09-06T02:18:20.991538Z","iopub.status.idle":"2026-09-06T02:18:21.002404Z","shell.execute_reply.started":"2026-09-06T02:18:20.991502Z","shell.execute_reply":"2026-09-06T02:18:21.00161Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# run the test harness for evaluating a model, not recommend to call this function\ndef run_test_harness():\n\t# define model\n\tmodel = define_model()\n\t# create data generator\n\tdatagen = ImageDataGenerator(rescale=1.0/255.0)\n\t# prepare iterators\n\ttrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n\t\tclass_mode='binary', batch_size=64, target_size=(200, 200))\n\ttest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n\t\tclass_mode='binary', batch_size=64, target_size=(200, 200))\n\t# fit model\n\thistory = model.fit_generator(train_it, steps_per_epoch=len(train_it),\n\t\tvalidation_data=test_it, validation_steps=len(test_it), epochs=20, verbose=0)\n\t# evaluate model\n\t_, acc = model.evaluate_generator(test_it, steps=len(test_it), verbose=0)\n\tprint('> %.3f' % (acc * 100.0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:18:35.693321Z","iopub.execute_input":"2026-09-06T02:18:35.693724Z","iopub.status.idle":"2026-09-06T02:18:35.700546Z","shell.execute_reply.started":"2026-09-06T02:18:35.693698Z","shell.execute_reply":"2026-09-06T02:18:35.69933Z"}},"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-06T02:18:35.819575Z","iopub.execute_input":"2026-09-06T02:18:35.819872Z","iopub.status.idle":"2026-09-06T02:18:37.409451Z","shell.execute_reply.started":"2026-09-06T02:18:35.819848Z","shell.execute_reply":"2026-09-06T02:18:37.40837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1.0/255.0)\n\n# prepare iterators\ntrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n    class_mode='binary', batch_size=64, target_size=(200, 200))\n\nval_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n    class_mode='binary', batch_size=64, target_size=(200, 200))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:18:37.410885Z","iopub.execute_input":"2026-09-06T02:18:37.41121Z","iopub.status.idle":"2026-09-06T02:18:38.228413Z","shell.execute_reply.started":"2026-09-06T02:18:37.411182Z","shell.execute_reply":"2026-09-06T02:18:38.227726Z"}},"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-06T02:18:40.860183Z","iopub.execute_input":"2026-09-06T02:18:40.861042Z","iopub.status.idle":"2026-09-06T02:18:40.869578Z","shell.execute_reply.started":"2026-09-06T02:18:40.861007Z","shell.execute_reply":"2026-09-06T02:18:40.868831Z"}},"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-06T02:18:41.05086Z","iopub.execute_input":"2026-09-06T02:18:41.05151Z","iopub.status.idle":"2026-09-06T02:18:41.058143Z","shell.execute_reply.started":"2026-09-06T02:18:41.051483Z","shell.execute_reply":"2026-09-06T02:18:41.056922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit model\nmodel_1.reset_states()\nhistory_1 = model_1.fit_generator(\n    train_it, steps_per_epoch=len(train_it),\n    validation_data=val_it,\n    validation_steps=len(val_it),\n    epochs=1, verbose=1,\n    callbacks = [PlotLossesKeras()]\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:18:41.179296Z","iopub.execute_input":"2026-09-06T02:18:41.179753Z","iopub.status.idle":"2026-09-06T02:19:31.828803Z","shell.execute_reply.started":"2026-09-06T02:18:41.179711Z","shell.execute_reply":"2026-09-06T02:19:31.827942Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\nsummarize_diagnostics(history_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:19:31.8303Z","iopub.execute_input":"2026-09-06T02:19:31.830745Z","iopub.status.idle":"2026-09-06T02:19:32.182839Z","shell.execute_reply.started":"2026-09-06T02:19:31.830713Z","shell.execute_reply":"2026-09-06T02:19:32.182003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# evaluate model\n_, acc = model_1.evaluate_generator(val_it, steps=len(val_it), verbose=1)\nprint('> %.3f' % (acc * 100.0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:19:32.183986Z","iopub.execute_input":"2026-09-06T02:19:32.184307Z","iopub.status.idle":"2026-09-06T02:19:43.978247Z","shell.execute_reply.started":"2026-09-06T02:19:32.18428Z","shell.execute_reply":"2026-09-06T02:19:43.977243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:14.231529Z","iopub.execute_input":"2026-09-06T02:20:14.232407Z","iopub.status.idle":"2026-09-06T02:20:15.331698Z","shell.execute_reply.started":"2026-09-06T02:20:14.232376Z","shell.execute_reply":"2026-09-06T02:20:15.330419Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pwd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:15.334632Z","iopub.execute_input":"2026-09-06T02:20:15.334975Z","iopub.status.idle":"2026-09-06T02:20:15.342056Z","shell.execute_reply.started":"2026-09-06T02:20:15.334949Z","shell.execute_reply":"2026-09-06T02:20:15.341028Z"}},"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\nimage_folder = '/kaggle/working/test1'\n\n# List all .jpg files in the directory (adjust the pattern if needed)\nimage_files = glob(os.path.join(image_folder, '*.jpg'))\n\n# Check if images were found\nif not image_files:\n    print(\"No images found. Please verify the directory and file extensions.\")\n    \n# Create a DataFrame with the file paths\ndf = 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-06T02:20:15.343426Z","iopub.execute_input":"2026-09-06T02:20:15.343733Z","iopub.status.idle":"2026-09-06T02:20:15.384639Z","shell.execute_reply.started":"2026-09-06T02:20:15.343671Z","shell.execute_reply":"2026-09-06T02:20:15.383966Z"}},"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-06T02:20:16.726107Z","iopub.execute_input":"2026-09-06T02:20:16.72649Z","iopub.status.idle":"2026-09-06T02:20:16.733347Z","shell.execute_reply.started":"2026-09-06T02:20:16.72646Z","shell.execute_reply":"2026-09-06T02:20:16.732355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:17.225314Z","iopub.execute_input":"2026-09-06T02:20:17.226136Z","iopub.status.idle":"2026-09-06T02:20:17.260252Z","shell.execute_reply.started":"2026-09-06T02:20:17.226103Z","shell.execute_reply":"2026-09-06T02:20:17.259386Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.loc[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:17.261836Z","iopub.execute_input":"2026-09-06T02:20:17.262471Z","iopub.status.idle":"2026-09-06T02:20:17.2686Z","shell.execute_reply.started":"2026-09-06T02:20:17.262447Z","shell.execute_reply":"2026-09-06T02:20:17.267556Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:17.300121Z","iopub.execute_input":"2026-09-06T02:20:17.300436Z","iopub.status.idle":"2026-09-06T02:20:17.3124Z","shell.execute_reply.started":"2026-09-06T02:20:17.300411Z","shell.execute_reply":"2026-09-06T02:20:17.311343Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['filename']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:17.329394Z","iopub.execute_input":"2026-09-06T02:20:17.330325Z","iopub.status.idle":"2026-09-06T02:20:17.337219Z","shell.execute_reply.started":"2026-09-06T02:20:17.330291Z","shell.execute_reply":"2026-09-06T02:20:17.336368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE = (224, 224)\n\ndatagen = ImageDataGenerator(rescale=1.0/255.0,\n                            horizontal_flip=True,)\nprint(IMAGE_SIZE)\n# prepare iterators\ntrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n    class_mode='binary', batch_size=64, target_size=IMAGE_SIZE )\n\nval_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n    class_mode='binary', batch_size=64, target_size=IMAGE_SIZE )\n\n# Prediction\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Initialize your ImageDataGenerator (modify parameters as needed)\ndatagen_test = ImageDataGenerator(rescale=1./255,\n                                 horizontal_flip=True)\n\n# Create the data generator using the DataFrame\ntest_it = datagen_test.flow_from_dataframe(\n    dataframe=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=64,\n    class_mode=None,       # Since there are no labels\n    # shuffle=True,         # Usually testing data should not be shuffled\n    shuffle=False,\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:17.363577Z","iopub.execute_input":"2026-09-06T02:20:17.363902Z","iopub.status.idle":"2026-09-06T02:20:18.183636Z","shell.execute_reply.started":"2026-09-06T02:20:17.363878Z","shell.execute_reply":"2026-09-06T02:20:18.182779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef plot_exmaple(images):\n    # Assuming test_images is a numpy array with shape (64, height, width, channels)\n    fig, axes = plt.subplots(8, 8, figsize=(16, 16))\n    axes = axes.flatten()  # Flatten the 8x8 grid into a list for easy iteration\n    \n    for i, ax in enumerate(axes):\n        # Ensure there are enough images in the batch before plotting\n        if i < images.shape[0]:\n            ax.imshow(images[i])\n        #ax.axis('off')  # Turn off axis ticks/labels for clarity\n        ax.axis('on')  # Turn off axis ticks/labels for clarity\n    \n    plt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:18.18657Z","iopub.execute_input":"2026-09-06T02:20:18.186955Z","iopub.status.idle":"2026-09-06T02:20:18.192734Z","shell.execute_reply.started":"2026-09-06T02:20:18.18693Z","shell.execute_reply":"2026-09-06T02:20:18.191751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_images, train_labels = train_it.__next__()\ntrain_images.shape\nplot_exmaple(train_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:18.193873Z","iopub.execute_input":"2026-09-06T02:20:18.194189Z","iopub.status.idle":"2026-09-06T02:20:28.315844Z","shell.execute_reply.started":"2026-09-06T02:20:18.194165Z","shell.execute_reply":"2026-09-06T02:20:28.314739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_images, val_labels = val_it.__next__()\nval_images.shape\nplot_exmaple(val_images)\nval_labels[:8]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:28.319359Z","iopub.execute_input":"2026-09-06T02:20:28.320194Z","iopub.status.idle":"2026-09-06T02:20:38.345103Z","shell.execute_reply.started":"2026-09-06T02:20:28.320166Z","shell.execute_reply":"2026-09-06T02:20:38.343562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_images = test_it.__next__()\ntest_images.shape\nplot_exmaple(test_images)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:38.34637Z","iopub.execute_input":"2026-09-06T02:20:38.346733Z","iopub.status.idle":"2026-09-06T02:20:48.682751Z","shell.execute_reply.started":"2026-09-06T02:20:38.346664Z","shell.execute_reply":"2026-09-06T02:20:48.681389Z"}},"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\nfrom keras.layers import Flatten\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# define cnn model\ndef define_model_vgg16():\n\t# load model\n\tmodel = VGG16(include_top=False, input_shape=(224, 224, 3))\n\t# mark loaded layers as not trainable\n\tfor layer in model.layers:\n\t\tlayer.trainable = False\n\t# add new classifier layers\n\tflat1 = Flatten()(model.layers[-1].output)\n\tclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\n\toutput = Dense(1, activation='sigmoid')(class1)\n\t# define new model\n\tmodel = Model(inputs=model.inputs, outputs=output)\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-06T02:20:48.684465Z","iopub.execute_input":"2026-09-06T02:20:48.684829Z","iopub.status.idle":"2026-09-06T02:20:48.69474Z","shell.execute_reply.started":"2026-09-06T02:20:48.6848Z","shell.execute_reply":"2026-09-06T02:20:48.693756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load model\n# model = VGG16(include_top=False, input_shape=(224, 224, 3))\n# mark loaded layers as not trainable\n# for layer in model.layers:\n#     layer.trainable = False\n\n# print(model.layers[-1].output)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:48.695991Z","iopub.execute_input":"2026-09-06T02:20:48.696235Z","iopub.status.idle":"2026-09-06T02:20:48.710282Z","shell.execute_reply.started":"2026-09-06T02:20:48.696212Z","shell.execute_reply":"2026-09-06T02:20:48.709164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load model\n# model_16_feature_extractor = VGG16(include_top=False, input_shape=(224, 224, 3))\n# model_16_feature_extractor.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:48.711514Z","iopub.execute_input":"2026-09-06T02:20:48.711848Z","iopub.status.idle":"2026-09-06T02:20:48.721307Z","shell.execute_reply.started":"2026-09-06T02:20:48.711823Z","shell.execute_reply":"2026-09-06T02:20:48.720158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for index, layer in enumerate(model_16_feature_extractor.layers):\n# \t\tprint(index, layer.trainable)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:48.722609Z","iopub.execute_input":"2026-09-06T02:20:48.722978Z","iopub.status.idle":"2026-09-06T02:20:48.732795Z","shell.execute_reply.started":"2026-09-06T02:20:48.722954Z","shell.execute_reply":"2026-09-06T02:20:48.731579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load model\n# model_16_top = VGG16(include_top=True, input_shape=(224, 224, 3))\n# model_16_top.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:48.736921Z","iopub.execute_input":"2026-09-06T02:20:48.737887Z","iopub.status.idle":"2026-09-06T02:20:48.742475Z","shell.execute_reply.started":"2026-09-06T02:20:48.737862Z","shell.execute_reply":"2026-09-06T02:20:48.741612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vgg16_model = define_model_vgg16()\nvgg16_model.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:48.743942Z","iopub.execute_input":"2026-09-06T02:20:48.744203Z","iopub.status.idle":"2026-09-06T02:20:49.64182Z","shell.execute_reply.started":"2026-09-06T02:20:48.744181Z","shell.execute_reply":"2026-09-06T02:20:49.64058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for index, layer in enumerate(vgg16_model.layers):\n#     print(index, layer.name, layer.trainable)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:49.64323Z","iopub.execute_input":"2026-09-06T02:20:49.643623Z","iopub.status.idle":"2026-09-06T02:20:49.648773Z","shell.execute_reply.started":"2026-09-06T02:20:49.643578Z","shell.execute_reply":"2026-09-06T02:20:49.64751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for index, layer in enumerate(vgg16_model.layers[-7:]):\n#     print(index, layer.name, layer.trainable)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:49.650174Z","iopub.execute_input":"2026-09-06T02:20:49.650467Z","iopub.status.idle":"2026-09-06T02:20:49.666923Z","shell.execute_reply.started":"2026-09-06T02:20:49.650443Z","shell.execute_reply":"2026-09-06T02:20:49.665928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for index, layer in enumerate(vgg16_model.layers[-7:]):\n#     layer.trainable = True\n#     print(index, layer.name, layer.trainable)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:49.66799Z","iopub.execute_input":"2026-09-06T02:20:49.668307Z","iopub.status.idle":"2026-09-06T02:20:49.678599Z","shell.execute_reply.started":"2026-09-06T02:20:49.668282Z","shell.execute_reply":"2026-09-06T02:20:49.677889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for index, layer in enumerate(vgg16_model.layers):\n#     if index >= 15:\n#         layer.trainable = True\n\n# for index, layer in enumerate(vgg16_model.layers):\n#     print(index, layer.name, layer.trainable)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:49.679581Z","iopub.execute_input":"2026-09-06T02:20:49.679943Z","iopub.status.idle":"2026-09-06T02:20:49.688846Z","shell.execute_reply.started":"2026-09-06T02:20:49.679913Z","shell.execute_reply":"2026-09-06T02:20:49.687739Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"id(vgg16_model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:49.690017Z","iopub.execute_input":"2026-09-06T02:20:49.690378Z","iopub.status.idle":"2026-09-06T02:20:49.704785Z","shell.execute_reply.started":"2026-09-06T02:20:49.690339Z","shell.execute_reply":"2026-09-06T02:20:49.703714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit model\nprint(id(vgg16_model))\nvgg16_model.reset_states()\nvgg16_model_history = vgg16_model.fit(\n    train_it, \n    steps_per_epoch=len(train_it),\n    validation_data=val_it,\n    validation_steps=len(val_it),\n    epochs=3, verbose=1,\n    callbacks = [PlotLossesKeras()]\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:20:49.705954Z","iopub.execute_input":"2026-09-06T02:20:49.706264Z","iopub.status.idle":"2026-09-06T02:27:46.588364Z","shell.execute_reply.started":"2026-09-06T02:20:49.706235Z","shell.execute_reply":"2026-09-06T02:27:46.587272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\nsummarize_diagnostics(vgg16_model_history)\n\n# evaluate model\n_, acc = vgg16_model.evaluate_generator(val_it, steps=len(val_it), verbose=1)\nprint('Val: > %.3f' % (acc * 100.0))\n\n# evaluate model\n_, acc = vgg16_model.evaluate_generator(test_it, steps=len(test_it), verbose=1)\nprint('Test: > %.3f' % (acc * 100.0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:27:46.589893Z","iopub.execute_input":"2026-09-06T02:27:46.590192Z","iopub.status.idle":"2026-09-06T02:28:56.426077Z","shell.execute_reply.started":"2026-09-06T02:27:46.590166Z","shell.execute_reply":"2026-09-06T02:28:56.424883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications.inception_v3 import InceptionV3\n\ndef define_model_inceptionv3():\n    # load model\n    model = InceptionV3(include_top=False, input_shape=(299, 299, 3))\n    # mark loaded layers as not trainable\n    for layer in model.layers:\n        layer.trainable = False\n        # layer.trainable = True #try to test\n    \n    # add new classifier layers\n    flat1 = Flatten()(model.layers[-1].output)\n    class1 = Dense(1024, activation='relu', kernel_initializer='he_uniform')(flat1)\n    class2 = Dense(512, activation='relu', kernel_initializer='he_uniform')(class1)\n    class3 = Dense(256, activation='relu', kernel_initializer='he_uniform')(class2)\n    class4 = Dense(128, activation='relu', kernel_initializer='he_uniform')(class3)\n    # output = Dense(1, activation='sigmoid')(flat1)\n    output = Dense(2, activation='softmax')(class4)\n    # define new model\n    model = Model(inputs=model.inputs, outputs=output)\n    # compile model\n    opt = SGD(lr=0.001, momentum=0.9)\n    model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:28:59.444803Z","iopub.execute_input":"2026-09-06T02:28:59.445147Z","iopub.status.idle":"2026-09-06T02:28:59.45406Z","shell.execute_reply.started":"2026-09-06T02:28:59.445122Z","shell.execute_reply":"2026-09-06T02:28:59.453082Z"}},"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-09-06T02:28:56.436284Z","iopub.execute_input":"2026-09-06T02:28:56.436592Z","iopub.status.idle":"2026-09-06T02:28:59.421796Z","shell.execute_reply.started":"2026-09-06T02:28:56.436557Z","shell.execute_reply":"2026-09-06T02:28:59.42066Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nfrom tensorflow.keras import backend as K\n\ntrainable_params = int(np.sum([K.count_params(w) for w in model_inceptionv3.trainable_weights]))\nprint(trainable_params)\n","metadata":{"trusted":true,"scrolled":true,"execution":{"iopub.status.busy":"2026-09-06T02:28:59.423098Z","iopub.execute_input":"2026-09-06T02:28:59.423397Z","iopub.status.idle":"2026-09-06T02:28:59.430127Z","shell.execute_reply.started":"2026-09-06T02:28:59.423371Z","shell.execute_reply":"2026-09-06T02:28:59.42908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras import backend as K\n\nfor w in model_inceptionv3.trainable_weights:\n    print(w.name, K.count_params(w))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:28:59.431336Z","iopub.execute_input":"2026-09-06T02:28:59.431613Z","iopub.status.idle":"2026-09-06T02:28:59.443656Z","shell.execute_reply.started":"2026-09-06T02:28:59.431591Z","shell.execute_reply":"2026-09-06T02:28:59.442756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# fit model\nmodel_inceptionv3.reset_states()\nhistory_inceptionv3 = model_inceptionv3.fit(\n    train_it, \n    steps_per_epoch=len(train_it),\n    validation_data=val_it,\n    validation_steps=len(val_it),\n    epochs=3, verbose=1,\n    callbacks = [PlotLossesKeras()]\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:28:59.455425Z","iopub.execute_input":"2026-09-06T02:28:59.455853Z","iopub.status.idle":"2026-09-06T02:29:01.133427Z","shell.execute_reply.started":"2026-09-06T02:28:59.455822Z","shell.execute_reply":"2026-09-06T02:29:01.131458Z"}},"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-06T02:32:58.7646Z","iopub.execute_input":"2026-09-06T02:32:58.765124Z","iopub.status.idle":"2026-09-06T02:32:58.773514Z","shell.execute_reply.started":"2026-09-06T02:32:58.765044Z","shell.execute_reply":"2026-09-06T02:32:58.772325Z"}},"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-06T02:33:02.391107Z","iopub.execute_input":"2026-09-06T02:33:02.391462Z","iopub.status.idle":"2026-09-06T02:33:02.395837Z","shell.execute_reply.started":"2026-09-06T02:33:02.391436Z","shell.execute_reply":"2026-09-06T02:33:02.394911Z"}},"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-06T02:33:02.489637Z","iopub.execute_input":"2026-09-06T02:33:02.489981Z","iopub.status.idle":"2026-09-06T02:33:02.494883Z","shell.execute_reply.started":"2026-09-06T02:33:02.489958Z","shell.execute_reply":"2026-09-06T02:33:02.493725Z"}},"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-06T02:33:02.633613Z","iopub.execute_input":"2026-09-06T02:33:02.634354Z","iopub.status.idle":"2026-09-06T02:33:02.63963Z","shell.execute_reply.started":"2026-09-06T02:33:02.634328Z","shell.execute_reply":"2026-09-06T02:33:02.638724Z"}},"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-06T02:33:04.810104Z","iopub.execute_input":"2026-09-06T02:33:04.810492Z","iopub.status.idle":"2026-09-06T02:33:05.950719Z","shell.execute_reply.started":"2026-09-06T02:33:04.81046Z","shell.execute_reply":"2026-09-06T02:33:05.949148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = test_it.__next__()\nimages.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:33:09.690397Z","iopub.execute_input":"2026-09-06T02:33:09.690768Z","iopub.status.idle":"2026-09-06T02:33:09.836136Z","shell.execute_reply.started":"2026-09-06T02:33:09.690739Z","shell.execute_reply":"2026-09-06T02:33:09.835371Z"}},"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-09-06T02:33:09.850615Z","iopub.execute_input":"2026-09-06T02:33:09.851119Z","iopub.status.idle":"2026-09-06T02:33:09.856909Z","shell.execute_reply.started":"2026-09-06T02:33:09.851093Z","shell.execute_reply":"2026-09-06T02:33:09.856011Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"probs = model_inceptionv3.predict(images_to_predict, verbose=1)\nprobs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-06T02:33:10.394733Z","iopub.execute_input":"2026-09-06T02:33:10.39546Z","iopub.status.idle":"2026-09-06T02:33:10.560527Z","shell.execute_reply.started":"2026-09-06T02:33:10.395426Z","shell.execute_reply":"2026-09-06T02:33:10.559181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}