{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:21.838579Z","iopub.execute_input":"2026-09-17T06:02:21.839389Z","iopub.status.idle":"2026-09-17T06:02:21.844272Z","shell.execute_reply.started":"2026-09-17T06:02:21.839352Z","shell.execute_reply":"2026-09-17T06:02:21.843063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:21.845679Z","iopub.execute_input":"2026-09-17T06:02:21.845938Z","iopub.status.idle":"2026-09-17T06:02:22.925084Z","shell.execute_reply.started":"2026-09-17T06:02:21.845915Z","shell.execute_reply":"2026-09-17T06:02:22.924054Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!uname --all","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:22.927326Z","iopub.execute_input":"2026-09-17T06:02:22.928342Z","iopub.status.idle":"2026-09-17T06:02:23.977715Z","shell.execute_reply.started":"2026-09-17T06:02:22.928292Z","shell.execute_reply":"2026-09-17T06:02:23.976714Z"}},"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-17T06:02:23.981015Z","iopub.execute_input":"2026-09-17T06:02:23.981476Z","iopub.status.idle":"2026-09-17T06:02:33.862011Z","shell.execute_reply.started":"2026-09-17T06:02:23.981442Z","shell.execute_reply":"2026-09-17T06:02:33.861229Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:33.863129Z","iopub.execute_input":"2026-09-17T06:02:33.863442Z","iopub.status.idle":"2026-09-17T06:02:35.010055Z","shell.execute_reply.started":"2026-09-17T06:02:33.863417Z","shell.execute_reply":"2026-09-17T06:02:35.008683Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:35.011777Z","iopub.execute_input":"2026-09-17T06:02:35.012545Z","iopub.status.idle":"2026-09-17T06:02:36.069624Z","shell.execute_reply.started":"2026-09-17T06:02:35.012510Z","shell.execute_reply":"2026-09-17T06:02:36.068451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls test1/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:36.071088Z","iopub.execute_input":"2026-09-17T06:02:36.071476Z","iopub.status.idle":"2026-09-17T06:02:37.164324Z","shell.execute_reply.started":"2026-09-17T06:02:36.071445Z","shell.execute_reply":"2026-09-17T06:02:37.163304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:37.165774Z","iopub.execute_input":"2026-09-17T06:02:37.166091Z","iopub.status.idle":"2026-09-17T06:02:38.296505Z","shell.execute_reply.started":"2026-09-17T06:02:37.166061Z","shell.execute_reply":"2026-09-17T06:02:38.295577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#ls train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:38.297943Z","iopub.execute_input":"2026-09-17T06:02:38.298248Z","iopub.status.idle":"2026-09-17T06:02:38.303250Z","shell.execute_reply.started":"2026-09-17T06:02:38.298220Z","shell.execute_reply":"2026-09-17T06:02:38.302199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -U train | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:38.306592Z","iopub.execute_input":"2026-09-17T06:02:38.307369Z","iopub.status.idle":"2026-09-17T06:02:39.450892Z","shell.execute_reply.started":"2026-09-17T06:02:38.307343Z","shell.execute_reply":"2026-09-17T06:02:39.449266Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -U test1 | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:39.453343Z","iopub.execute_input":"2026-09-17T06:02:39.453832Z","iopub.status.idle":"2026-09-17T06:02:40.569911Z","shell.execute_reply.started":"2026-09-17T06:02:39.453782Z","shell.execute_reply":"2026-09-17T06:02:40.568905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot dog photos from the dogs vs cats dataset\nfrom matplotlib import pyplot\n# from matplotlib import pyplot as plt\nfrom matplotlib.image import imread\n# define location of dataset\nfolder = 'train/'\n# plot first few images\nfor i in range(9):\n\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'dog.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tpyplot.imshow(image)\n# show the figure\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2026-09-17T06:02:40.571691Z","iopub.execute_input":"2026-09-17T06:02:40.572039Z","iopub.status.idle":"2026-09-17T06:02:41.631093Z","shell.execute_reply.started":"2026-09-17T06:02:40.572006Z","shell.execute_reply":"2026-09-17T06:02:41.630190Z"},"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-17T06:02:41.632516Z","iopub.execute_input":"2026-09-17T06:02:41.632817Z","iopub.status.idle":"2026-09-17T06:02:42.599985Z","shell.execute_reply.started":"2026-09-17T06:02:41.632792Z","shell.execute_reply":"2026-09-17T06:02:42.598828Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:42.601312Z","iopub.execute_input":"2026-09-17T06:02:42.601678Z","iopub.status.idle":"2026-09-17T06:02:43.669155Z","shell.execute_reply.started":"2026-09-17T06:02:42.601642Z","shell.execute_reply":"2026-09-17T06:02:43.668061Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow\nprint(f\"({tensorflow.__version__}\")\n\nimport keras\nprint(f\"{keras.__version__}\")\n\nimport tensorflow.keras\nprint(f\"{tensorflow.__version__}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:43.670746Z","iopub.execute_input":"2026-09-17T06:02:43.671085Z","iopub.status.idle":"2026-09-17T06:02:43.677586Z","shell.execute_reply.started":"2026-09-17T06:02:43.671054Z","shell.execute_reply":"2026-09-17T06:02:43.676597Z"}},"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-17T06:02:43.679232Z","iopub.execute_input":"2026-09-17T06:02:43.679549Z","iopub.status.idle":"2026-09-17T06:02:43.687051Z","shell.execute_reply.started":"2026-09-17T06:02:43.679523Z","shell.execute_reply":"2026-09-17T06:02:43.686326Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# define location of dataset\nfolder = 'train/'\nphotos, labels = list(), list()\n# enumerate files in the directory\nfor file in listdir(folder):\n\t# determine class\n\toutput = 0.0\n\tif file.startswith('dog'):\n\t\toutput = 1.0\n\t# load image\n\tphoto = load_img(folder + file, target_size=(200, 200))\n\t# convert to numpy array\n\tphoto = img_to_array(photo)\n\t# store\n\tphotos.append(photo)\n\tlabels.append(output)\n# convert to a numpy arrays\nphotos = asarray(photos)\nlabels = asarray(labels)\nprint(photos.shape, labels.shape)\n# save the reshaped photos\nsave('dogs_vs_cats_photos.npy', photos)\nsave('dogs_vs_cats_labels.npy', labels)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T03:36:15.416611Z","iopub.execute_input":"2024-01-27T03:36:15.417128Z","iopub.status.idle":"2024-01-27T03:36:38.886396Z","shell.execute_reply.started":"2024-01-27T03:36:15.417101Z","shell.execute_reply":"2024-01-27T03:36:38.883149Z"}}},{"cell_type":"markdown","source":"# load and confirm the shape\nfrom numpy import load\nphotos = load('dogs_vs_cats_photos.npy')\nlabels = load('dogs_vs_cats_labels.npy')","metadata":{"execution":{"iopub.status.busy":"2024-01-27T03:36:38.88711Z","iopub.status.idle":"2024-01-27T03:36:38.887458Z","shell.execute_reply.started":"2024-01-27T03:36:38.887281Z","shell.execute_reply":"2024-01-27T03:36:38.887296Z"}}},{"cell_type":"code","source":"# organize dataset into a useful structure\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random","metadata":{"execution":{"iopub.status.busy":"2026-09-17T06:02:43.688068Z","iopub.execute_input":"2026-09-17T06:02:43.688381Z","iopub.status.idle":"2026-09-17T06:02:43.697073Z","shell.execute_reply.started":"2026-09-17T06:02:43.688357Z","shell.execute_reply":"2026-09-17T06:02:43.696286Z"},"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-17T06:02:43.698099Z","iopub.execute_input":"2026-09-17T06:02:43.698480Z","iopub.status.idle":"2026-09-17T06:02:43.707519Z","shell.execute_reply.started":"2026-09-17T06:02:43.698456Z","shell.execute_reply":"2026-09-17T06:02:43.706461Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:43.708883Z","iopub.execute_input":"2026-09-17T06:02:43.709254Z","iopub.status.idle":"2026-09-17T06:02:44.771739Z","shell.execute_reply.started":"2026-09-17T06:02:43.709220Z","shell.execute_reply":"2026-09-17T06:02:44.770329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:44.773472Z","iopub.execute_input":"2026-09-17T06:02:44.773930Z","iopub.status.idle":"2026-09-17T06:02:45.825456Z","shell.execute_reply.started":"2026-09-17T06:02:44.773897Z","shell.execute_reply":"2026-09-17T06:02:45.824368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/test #validation","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:45.826966Z","iopub.execute_input":"2026-09-17T06:02:45.827784Z","iopub.status.idle":"2026-09-17T06:02:46.870842Z","shell.execute_reply.started":"2026-09-17T06:02:45.827751Z","shell.execute_reply":"2026-09-17T06:02:46.869860Z"}},"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-17T06:02:46.872332Z","iopub.execute_input":"2026-09-17T06:02:46.872931Z","iopub.status.idle":"2026-09-17T06:02:49.954502Z","shell.execute_reply.started":"2026-09-17T06:02:46.872901Z","shell.execute_reply":"2026-09-17T06:02:49.953327Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(src)\nprint(dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:02:49.955931Z","iopub.execute_input":"2026-09-17T06:02:49.956796Z","iopub.status.idle":"2026-09-17T06:02:49.961757Z","shell.execute_reply.started":"2026-09-17T06:02:49.956756Z","shell.execute_reply":"2026-09-17T06:02:49.960695Z"}},"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-17T06:02:49.963084Z","iopub.execute_input":"2026-09-17T06:02:49.963470Z","iopub.status.idle":"2026-09-17T06:02:52.265206Z","shell.execute_reply.started":"2026-09-17T06:02:49.963433Z","shell.execute_reply":"2026-09-17T06:02:52.264191Z"},"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-17T06:02:52.266760Z","iopub.execute_input":"2026-09-17T06:02:52.267092Z","iopub.status.idle":"2026-09-17T06:02:54.533636Z","shell.execute_reply.started":"2026-09-17T06:02:52.267064Z","shell.execute_reply":"2026-09-17T06:02:54.532259Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"execution":{"iopub.status.busy":"2026-09-17T06:02:54.535539Z","iopub.execute_input":"2026-09-17T06:02:54.535940Z","iopub.status.idle":"2026-09-17T06:03:03.298964Z","shell.execute_reply.started":"2026-09-17T06:02:54.535900Z","shell.execute_reply":"2026-09-17T06:03:03.297917Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2026-09-17T06:03:03.300493Z","iopub.execute_input":"2026-09-17T06:03:03.300902Z","iopub.status.idle":"2026-09-17T06:03:03.305827Z","shell.execute_reply.started":"2026-09-17T06:03:03.300871Z","shell.execute_reply":"2026-09-17T06:03:03.304958Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Prepare Final Dataset","metadata":{}},{"cell_type":"code","source":"# https://keras.io/api/applications/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:03.311734Z","iopub.execute_input":"2026-09-17T06:03:03.312106Z","iopub.status.idle":"2026-09-17T06:03:03.316424Z","shell.execute_reply.started":"2026-09-17T06:03:03.312080Z","shell.execute_reply":"2026-09-17T06:03:03.315365Z"}},"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-17T06:03:03.317589Z","iopub.execute_input":"2026-09-17T06:03:03.317866Z","iopub.status.idle":"2026-09-17T06:03:03.327213Z","shell.execute_reply.started":"2026-09-17T06:03:03.317844Z","shell.execute_reply":"2026-09-17T06:03:03.326087Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout\nfrom tensorflow.keras.optimizers import Adam\n\ndef define_model_vgg16():\n    base_model = VGG16(include_top=False, input_shape=(150, 150, 3))\n    \n    for index, layer in enumerate(base_model.layers):\n        if index <= 15:\n            layer.trainable = False\n        else:\n            layer.trainable = True\n        print(index, layer.name, layer.trainable) \n\n    x = Flatten()(base_model.output)\n    x = Dense(256, activation='relu', kernel_initializer='he_uniform')(x)\n    x = Dropout(0.5)(x)\n    output = Dense(1, activation='sigmoid')(x)\n    \n    model = Model(inputs=base_model.inputs, outputs=output)\n    \n    model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])\n    return model\n\nmodel_1 = define_model_vgg16()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:03.328668Z","iopub.execute_input":"2026-09-17T06:03:03.328978Z","iopub.status.idle":"2026-09-17T06:03:03.734582Z","shell.execute_reply.started":"2026-09-17T06:03:03.328947Z","shell.execute_reply":"2026-09-17T06:03:03.733551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(model_1.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:03.735831Z","iopub.execute_input":"2026-09-17T06:03:03.736513Z","iopub.status.idle":"2026-09-17T06:03:03.782916Z","shell.execute_reply.started":"2026-09-17T06:03:03.736482Z","shell.execute_reply":"2026-09-17T06:03:03.781987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:03.784808Z","iopub.execute_input":"2026-09-17T06:03:03.785208Z","iopub.status.idle":"2026-09-17T06:03:03.790089Z","shell.execute_reply.started":"2026-09-17T06:03:03.785168Z","shell.execute_reply":"2026-09-17T06:03:03.789046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filenames = os.listdir(\"./train\")\ncategories = ['dog' if name.startswith('dog') else 'cat' for name in filenames]\n\ntrain_df = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:03.791426Z","iopub.execute_input":"2026-09-17T06:03:03.792048Z","iopub.status.idle":"2026-09-17T06:03:03.821075Z","shell.execute_reply.started":"2026-09-17T06:03:03.792020Z","shell.execute_reply":"2026-09-17T06:03:03.820148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE = (128, 128)\nBATCH_SIZE = 32","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:03.822329Z","iopub.execute_input":"2026-09-17T06:03:03.822673Z","iopub.status.idle":"2026-09-17T06:03:03.827124Z","shell.execute_reply.started":"2026-09-17T06:03:03.822639Z","shell.execute_reply":"2026-09-17T06:03:03.826222Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1./255)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:03.828286Z","iopub.execute_input":"2026-09-17T06:03:03.828548Z","iopub.status.idle":"2026-09-17T06:03:03.837308Z","shell.execute_reply.started":"2026-09-17T06:03:03.828525Z","shell.execute_reply":"2026-09-17T06:03:03.836491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_it = datagen.flow_from_dataframe(\n    train_df,\n    directory=\"./train\",\n    x_col=\"filename\",\n    y_col=\"category\",\n    target_size=IMAGE_SIZE,\n    class_mode=\"binary\",\n    batch_size=BATCH_SIZE\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:03.838496Z","iopub.execute_input":"2026-09-17T06:03:03.838789Z","iopub.status.idle":"2026-09-17T06:03:04.038238Z","shell.execute_reply.started":"2026-09-17T06:03:03.838763Z","shell.execute_reply":"2026-09-17T06:03:04.037327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_it = datagen.flow_from_dataframe(\n    train_df,\n    directory=\"./train\",\n    x_col=\"filename\",\n    y_col=\"category\",\n    target_size=IMAGE_SIZE,\n    class_mode=\"binary\",\n    batch_size=BATCH_SIZE,\n    subset='validation'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:04.039397Z","iopub.execute_input":"2026-09-17T06:03:04.039695Z","iopub.status.idle":"2026-09-17T06:03:04.197341Z","shell.execute_reply.started":"2026-09-17T06:03:04.039671Z","shell.execute_reply":"2026-09-17T06:03:04.196525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_1.reset_states()\nhistory_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=10,\n    verbose=2,\n    callbacks = [PlotLossesKeras()]\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:03:04.198749Z","iopub.execute_input":"2026-09-17T06:03:04.199565Z","iopub.status.idle":"2026-09-17T06:10:33.844885Z","shell.execute_reply.started":"2026-09-17T06:03:04.199514Z","shell.execute_reply":"2026-09-17T06:10:33.843944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"history:\", list(history_1.history.keys()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:33.846268Z","iopub.execute_input":"2026-09-17T06:10:33.847372Z","iopub.status.idle":"2026-09-17T06:10:33.852800Z","shell.execute_reply.started":"2026-09-17T06:10:33.847338Z","shell.execute_reply":"2026-09-17T06:10:33.851785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_loss = history_1.history['loss'][-1]\nfinal_acc = history_1.history['accuracy'][-1]\n\nprint(f\"Final Loss: {final_loss:.4f}\")\nprint(f\"Final Accuracy: {final_acc * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:33.854199Z","iopub.execute_input":"2026-09-17T06:10:33.854574Z","iopub.status.idle":"2026-09-17T06:10:33.872581Z","shell.execute_reply.started":"2026-09-17T06:10:33.854540Z","shell.execute_reply":"2026-09-17T06:10:33.871394Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetV2M \nfrom tensorflow.keras.models import Model \nfrom tensorflow.keras.layers import Dense, Flatten, Dropout \nfrom tensorflow.keras.optimizers import Adam","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:33.874005Z","iopub.execute_input":"2026-09-17T06:10:33.875353Z","iopub.status.idle":"2026-09-17T06:10:33.883835Z","shell.execute_reply.started":"2026-09-17T06:10:33.875323Z","shell.execute_reply":"2026-09-17T06:10:33.882960Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def define_model_efficientnetv2m():\n    base_model = EfficientNetV2M(\n        include_top=False,\n        weights='imagenet',\n        input_shape=(150, 150, 3),\n        include_preprocessing=True\n    )\n    \n    for index, layer in enumerate(base_model.layers):\n        if index <= 700:\n            layer.trainable = False\n        else:\n            layer.trainable = True\n        print(index, layer.name, layer.trainable) \n\n    x = Flatten()(base_model.output)\n    x = Dense(256, activation='relu', kernel_initializer='he_uniform')(x)\n    x = Dropout(0.5)(x)\n    output = Dense(1, activation='sigmoid')(x)\n    \n    model = Model(inputs=base_model.inputs, outputs=output)\n    \n    model.compile(\n        optimizer=Adam(learning_rate=0.0001),\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n    \n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:33.885087Z","iopub.execute_input":"2026-09-17T06:10:33.885646Z","iopub.status.idle":"2026-09-17T06:10:33.896060Z","shell.execute_reply.started":"2026-09-17T06:10:33.885620Z","shell.execute_reply":"2026-09-17T06:10:33.894850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_2 = define_model_efficientnetv2m()\n\nprint(model_2.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:33.897393Z","iopub.execute_input":"2026-09-17T06:10:33.897757Z","iopub.status.idle":"2026-09-17T06:10:42.769064Z","shell.execute_reply.started":"2026-09-17T06:10:33.897732Z","shell.execute_reply":"2026-09-17T06:10:42.768055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n\nfilenames = os.listdir(\"./train\")\ncategories = ['dog' if name.startswith('dog') else 'cat' for name in filenames]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:42.770632Z","iopub.execute_input":"2026-09-17T06:10:42.771015Z","iopub.status.idle":"2026-09-17T06:10:42.797814Z","shell.execute_reply.started":"2026-09-17T06:10:42.770981Z","shell.execute_reply":"2026-09-17T06:10:42.796737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:42.799131Z","iopub.execute_input":"2026-09-17T06:10:42.799861Z","iopub.status.idle":"2026-09-17T06:10:42.807712Z","shell.execute_reply.started":"2026-09-17T06:10:42.799833Z","shell.execute_reply":"2026-09-17T06:10:42.806702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE = (150, 150)\nBATCH_SIZE = 32\n\ndatagen = ImageDataGenerator(\n    validation_split=0.2\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:42.809021Z","iopub.execute_input":"2026-09-17T06:10:42.809397Z","iopub.status.idle":"2026-09-17T06:10:42.816065Z","shell.execute_reply.started":"2026-09-17T06:10:42.809372Z","shell.execute_reply":"2026-09-17T06:10:42.814959Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_it = datagen.flow_from_dataframe(\n    train_df,\n    directory=\"./train\",\n    x_col=\"filename\",\n    y_col=\"category\",\n    target_size=IMAGE_SIZE,\n    class_mode=\"binary\",\n    batch_size=BATCH_SIZE,\n    subset='training',\n    shuffle=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:42.817311Z","iopub.execute_input":"2026-09-17T06:10:42.818010Z","iopub.status.idle":"2026-09-17T06:10:43.035974Z","shell.execute_reply.started":"2026-09-17T06:10:42.817968Z","shell.execute_reply":"2026-09-17T06:10:43.034753Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_it = datagen.flow_from_dataframe(\n    train_df,\n    directory=\"./train\",\n    x_col=\"filename\",\n    y_col=\"category\",\n    target_size=IMAGE_SIZE,\n    class_mode=\"binary\",\n    batch_size=BATCH_SIZE,\n    subset='validation',\n    shuffle=False\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:43.037349Z","iopub.execute_input":"2026-09-17T06:10:43.037663Z","iopub.status.idle":"2026-09-17T06:10:43.226415Z","shell.execute_reply.started":"2026-09-17T06:10:43.037636Z","shell.execute_reply":"2026-09-17T06:10:43.225106Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_2 = model_2.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=2,\n    callbacks=[PlotLossesKeras()]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:10:43.228016Z","iopub.execute_input":"2026-09-17T06:10:43.228817Z","iopub.status.idle":"2026-09-17T06:28:12.708358Z","shell.execute_reply.started":"2026-09-17T06:10:43.228763Z","shell.execute_reply":"2026-09-17T06:28:12.707482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"history:\", list(history_2.history.keys()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:12.709691Z","iopub.execute_input":"2026-09-17T06:28:12.710300Z","iopub.status.idle":"2026-09-17T06:28:12.717081Z","shell.execute_reply.started":"2026-09-17T06:28:12.710273Z","shell.execute_reply":"2026-09-17T06:28:12.716337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_loss = history_2.history['loss'][-1]\nfinal_acc = history_2.history['accuracy'][-1]\n\nprint(f\"Final Loss: {final_loss:.4f}\")\nprint(f\"Final Accuracy: {final_acc * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:12.718302Z","iopub.execute_input":"2026-09-17T06:28:12.718703Z","iopub.status.idle":"2026-09-17T06:28:12.758082Z","shell.execute_reply.started":"2026-09-17T06:28:12.718655Z","shell.execute_reply":"2026-09-17T06:28:12.756973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport tensorflow as tf\n\nfrom tensorflow.keras.applications import Xception\nfrom tensorflow.keras.applications.xception import preprocess_input\n\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import (\n    Dense,\n    Dropout,\n    GlobalAveragePooling2D\n)\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:12.759473Z","iopub.execute_input":"2026-09-17T06:28:12.760417Z","iopub.status.idle":"2026-09-17T06:28:12.776356Z","shell.execute_reply.started":"2026-09-17T06:28:12.760387Z","shell.execute_reply":"2026-09-17T06:28:12.775416Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def define_model_xception():\n\n    base_model = Xception(\n        include_top=False,\n        weights='imagenet',\n        input_shape=(150, 150, 3)\n    )\n\n    base_model.trainable = False\n\n    x = GlobalAveragePooling2D()(base_model.output)\n\n    x = Dense(\n        256,\n        activation='relu',\n        kernel_initializer='he_uniform'\n    )(x)\n\n    x = Dropout(0.5)(x)\n\n    output = Dense(\n        1,\n        activation='sigmoid'\n    )(x)\n\n    model = Model(\n        inputs=base_model.inputs,\n        outputs=output\n    )\n\n    model.compile(\n        optimizer=Adam(learning_rate=0.001),\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model\n\n\nmodel_3 = define_model_xception()\n\nmodel_3.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:12.777591Z","iopub.execute_input":"2026-09-17T06:28:12.777934Z","iopub.status.idle":"2026-09-17T06:28:14.611433Z","shell.execute_reply.started":"2026-09-17T06:28:12.777901Z","shell.execute_reply":"2026-09-17T06:28:14.610493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def define_model_xception():\n\n    base_model = Xception(\n        include_top=False,\n        weights='imagenet',\n        input_shape=(150, 150, 3)\n    )\n\n    base_model.trainable = False\n\n    x = GlobalAveragePooling2D()(base_model.output)\n\n    x = Dense(\n        256,\n        activation='relu',\n        kernel_initializer='he_uniform'\n    )(x)\n\n    x = Dropout(0.5)(x)\n\n    output = Dense(\n        1,\n        activation='sigmoid'\n    )(x)\n\n    model = Model(\n        inputs=base_model.inputs,\n        outputs=output\n    )\n\n    model.compile(\n        optimizer=Adam(learning_rate=0.001),\n        loss='binary_crossentropy',\n        metrics=['accuracy']\n    )\n\n    return model\n\n\nmodel_3 = define_model_xception()\n\nmodel_3.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:14.612709Z","iopub.execute_input":"2026-09-17T06:28:14.613060Z","iopub.status.idle":"2026-09-17T06:28:16.484075Z","shell.execute_reply.started":"2026-09-17T06:28:14.613015Z","shell.execute_reply":"2026-09-17T06:28:16.481631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filenames = os.listdir(\"./train\")\n\ncategories = [\n    'dog' if name.startswith('dog') else 'cat'\n    for name in filenames\n]\n\ntrain_df = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})\n\nIMAGE_SIZE = (150, 150)\nBATCH_SIZE = 32\n\nprint(train_df['category'].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:16.498746Z","iopub.execute_input":"2026-09-17T06:28:16.499109Z","iopub.status.idle":"2026-09-17T06:28:16.528735Z","shell.execute_reply.started":"2026-09-17T06:28:16.499079Z","shell.execute_reply":"2026-09-17T06:28:16.527589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    preprocessing_function=preprocess_input,\n    validation_split=0.2\n)\n\ntrain_it = datagen.flow_from_dataframe(\n    train_df,\n    directory=\"./train\",\n    x_col=\"filename\",\n    y_col=\"category\",\n    target_size=IMAGE_SIZE,\n    class_mode=\"binary\",\n    batch_size=BATCH_SIZE,\n    subset=\"training\",\n    shuffle=True,\n    seed=1337\n)\n\nval_it = datagen.flow_from_dataframe(\n    train_df,\n    directory=\"./train\",\n    x_col=\"filename\",\n    y_col=\"category\",\n    target_size=IMAGE_SIZE,\n    class_mode=\"binary\",\n    batch_size=BATCH_SIZE,\n    subset=\"validation\",\n    shuffle=False,\n    seed=1337\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:16.530176Z","iopub.execute_input":"2026-09-17T06:28:16.530504Z","iopub.status.idle":"2026-09-17T06:28:16.907215Z","shell.execute_reply.started":"2026-09-17T06:28:16.530478Z","shell.execute_reply":"2026-09-17T06:28:16.906347Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"callbacks_3 = [\n    EarlyStopping(\n        monitor='val_loss',\n        patience=5,\n        restore_best_weights=True,\n        verbose=1\n    ),\n\n    ReduceLROnPlateau(\n        monitor='val_loss',\n        factor=0.2,\n        patience=2,\n        min_lr=1e-7,\n        verbose=1\n    )\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:16.908442Z","iopub.execute_input":"2026-09-17T06:28:16.908757Z","iopub.status.idle":"2026-09-17T06:28:16.916317Z","shell.execute_reply.started":"2026-09-17T06:28:16.908729Z","shell.execute_reply":"2026-09-17T06:28:16.915227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_3 = model_3.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=2,\n    callbacks=callbacks_3\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:28:16.917541Z","iopub.execute_input":"2026-09-17T06:28:16.917849Z","iopub.status.idle":"2026-09-17T06:36:43.389106Z","shell.execute_reply.started":"2026-09-17T06:28:16.917821Z","shell.execute_reply":"2026-09-17T06:36:43.387987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"history:\", list(history_3.history.keys()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:40:49.329129Z","iopub.execute_input":"2026-09-17T06:40:49.330056Z","iopub.status.idle":"2026-09-17T06:40:49.335637Z","shell.execute_reply.started":"2026-09-17T06:40:49.330019Z","shell.execute_reply":"2026-09-17T06:40:49.334552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_loss = history_3.history['loss'][-1]\nfinal_acc = history_3.history['accuracy'][-1]\n\nprint(f\"Final Loss: {final_loss:.4f}\")\nprint(f\"Final Accuracy: {final_acc * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:41:08.368637Z","iopub.execute_input":"2026-09-17T06:41:08.368990Z","iopub.status.idle":"2026-09-17T06:41:08.374989Z","shell.execute_reply.started":"2026-09-17T06:41:08.368963Z","shell.execute_reply":"2026-09-17T06:41:08.373717Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_loss = history_2.history['loss'][-1]\nfinal_acc = history_2.history['accuracy'][-1]\n\nprint(f\"Final Loss: {final_loss:.4f}\")\nprint(f\"Final Accuracy: {final_acc * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:41:25.590910Z","iopub.execute_input":"2026-09-17T06:41:25.591761Z","iopub.status.idle":"2026-09-17T06:41:25.596950Z","shell.execute_reply.started":"2026-09-17T06:41:25.591725Z","shell.execute_reply":"2026-09-17T06:41:25.595948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"final_loss = history_1.history['loss'][-1]\nfinal_acc = history_1.history['accuracy'][-1]\n\nprint(f\"Final Loss: {final_loss:.4f}\")\nprint(f\"Final Accuracy: {final_acc * 100:.2f}%\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-17T06:41:30.791586Z","iopub.execute_input":"2026-09-17T06:41:30.792623Z","iopub.status.idle":"2026-09-17T06:41:30.797831Z","shell.execute_reply.started":"2026-09-17T06:41:30.792585Z","shell.execute_reply":"2026-09-17T06:41:30.796843Z"}},"outputs":[],"execution_count":null}]}