{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:54:16.078493Z","iopub.execute_input":"2025-03-05T07:54:16.079368Z","iopub.status.idle":"2025-03-05T07:54:16.473366Z","shell.execute_reply.started":"2025-03-05T07:54:16.079318Z","shell.execute_reply":"2025-03-05T07:54:16.472506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:54:18.16975Z","iopub.execute_input":"2025-03-05T07:54:18.170541Z","iopub.status.idle":"2025-03-05T07:54:19.251284Z","shell.execute_reply.started":"2025-03-05T07:54:18.170512Z","shell.execute_reply":"2025-03-05T07:54:19.250025Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))\n\nimport zipfile\n\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:54:25.69905Z","iopub.execute_input":"2025-03-05T07:54:25.69942Z","iopub.status.idle":"2025-03-05T07:54:35.188369Z","shell.execute_reply.started":"2025-03-05T07:54:25.699388Z","shell.execute_reply":"2025-03-05T07:54:35.187597Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:59:14.474935Z","iopub.execute_input":"2025-03-05T07:59:14.47579Z","iopub.status.idle":"2025-03-05T07:59:15.506149Z","shell.execute_reply.started":"2025-03-05T07:59:14.475727Z","shell.execute_reply":"2025-03-05T07:59:15.504944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:59:16.712451Z","iopub.execute_input":"2025-03-05T07:59:16.712819Z","iopub.status.idle":"2025-03-05T07:59:17.76646Z","shell.execute_reply.started":"2025-03-05T07:59:16.712788Z","shell.execute_reply":"2025-03-05T07:59:17.764925Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls test1/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:59:19.15268Z","iopub.execute_input":"2025-03-05T07:59:19.153047Z","iopub.status.idle":"2025-03-05T07:59:20.221457Z","shell.execute_reply.started":"2025-03-05T07:59:19.153018Z","shell.execute_reply":"2025-03-05T07:59:20.220192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/ | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:59:21.601666Z","iopub.execute_input":"2025-03-05T07:59:21.602397Z","iopub.status.idle":"2025-03-05T07:59:22.724715Z","shell.execute_reply.started":"2025-03-05T07:59:21.602362Z","shell.execute_reply":"2025-03-05T07:59:22.723787Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:59:24.296341Z","iopub.execute_input":"2025-03-05T07:59:24.297004Z","iopub.status.idle":"2025-03-05T07:59:25.470294Z","shell.execute_reply.started":"2025-03-05T07:59:24.29697Z","shell.execute_reply":"2025-03-05T07:59:25.469178Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T07:59:56.863872Z","iopub.execute_input":"2025-03-05T07:59:56.864659Z","iopub.status.idle":"2025-03-05T07:59:57.925528Z","shell.execute_reply.started":"2025-03-05T07:59:56.864625Z","shell.execute_reply":"2025-03-05T07:59:57.924685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# plot cat photos from the dogs vs cats dataset\nfrom matplotlib import pyplot\nfrom matplotlib.image import imread\n# define location of dataset\nfolder = 'train/'\n# plot first few images\nfor i in range(9):\n\t# define subplot\n\tpyplot.subplot(330 + 1 + i)\n\t# define filename\n\tfilename = folder + 'cat.' + str(i) + '.jpg'\n\t# load image pixels\n\timage = imread(filename)\n\t# plot raw pixel data\n\tpyplot.imshow(image)\n# show the figure\npyplot.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:00:07.129445Z","iopub.execute_input":"2025-03-05T08:00:07.130145Z","iopub.status.idle":"2025-03-05T08:00:08.032811Z","shell.execute_reply.started":"2025-03-05T08:00:07.13011Z","shell.execute_reply":"2025-03-05T08:00:08.031809Z"}},"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":"2025-03-05T08:00:21.601547Z","iopub.execute_input":"2025-03-05T08:00:21.602446Z","iopub.status.idle":"2025-03-05T08:00:24.509546Z","shell.execute_reply.started":"2025-03-05T08:00:21.602413Z","shell.execute_reply":"2025-03-05T08:00:24.508331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load dogs vs cats dataset, reshape and save to a new file\nfrom os import listdir\nfrom numpy import asarray\nfrom numpy import save\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:00:34.698473Z","iopub.execute_input":"2025-03-05T08:00:34.699449Z","iopub.status.idle":"2025-03-05T08:00:34.703723Z","shell.execute_reply.started":"2025-03-05T08:00:34.699413Z","shell.execute_reply":"2025-03-05T08:00:34.702812Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\nimport os\nfrom keras.preprocessing.image import load_img, img_to_array\n\n# Path to the zip file\nzip_path = '/kaggle/input/dogs-vs-cats/train.zip'\n# Directory to extract the zip file to\nextract_folder = '/kaggle/working/train/'\n\n# Extract the zip file\nwith zipfile.ZipFile(zip_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_folder)\n\n# Initialize the lists to store images and labels\nphotos = []\nlabels = []\n\n# List all files in the extracted folder\nfiles_in_folder = os.listdir(extract_folder)\n\n# Process each file in the folder\nfor file in files_in_folder:\n    file_path = os.path.join(extract_folder, file)\n    # Check if the file is an image (optional step)\n    if file.endswith(('.jpg', '.jpeg', '.png')):  # Adjust extensions as necessary\n        # Determine the class based on the file name\n        output = 0.0\n        if file.startswith('dog'):\n            output = 1.0\n\n        # Load the image and resize it\n        photo = load_img(file_path, target_size=(200, 200))\n\n        # Convert the image to a numpy array\n        photo = img_to_array(photo)\n\n        # Store the image and its label\n        photos.append(photo)\n        labels.append(output)\n\n# Convert lists to numpy arrays (optional, for model compatibility)\nimport numpy as np\nphotos = np.array(photos)\nlabels = np.array(labels)\n\n# Now `photos` and `labels` are ready to be used in a model\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:04:22.656417Z","iopub.execute_input":"2025-03-05T08:04:22.65752Z","iopub.status.idle":"2025-03-05T08:05:18.275536Z","shell.execute_reply.started":"2025-03-05T08:04:22.657488Z","shell.execute_reply":"2025-03-05T08:05:18.27473Z"}},"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:07:20.988678Z","iopub.execute_input":"2025-03-05T08:07:20.989627Z","iopub.status.idle":"2025-03-05T08:07:20.994766Z","shell.execute_reply.started":"2025-03-05T08:07:20.989587Z","shell.execute_reply":"2025-03-05T08:07:20.993529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/'] #test==>validation\nfor subdir in subdirs:\n    # create label subdirectories\n    labeldirs = ['dogs/', 'cats/']\n    for labldir in labeldirs:\n        newdir = dataset_home + subdir + labldir       \n        makedirs(newdir, exist_ok=True)\n        print(newdir)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:07:27.396639Z","iopub.execute_input":"2025-03-05T08:07:27.3975Z","iopub.status.idle":"2025-03-05T08:07:27.403035Z","shell.execute_reply.started":"2025-03-05T08:07:27.397468Z","shell.execute_reply":"2025-03-05T08:07:27.402087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train/dogs/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:07:39.875646Z","iopub.execute_input":"2025-03-05T08:07:39.876539Z","iopub.status.idle":"2025-03-05T08:07:41.561494Z","shell.execute_reply.started":"2025-03-05T08:07:39.876508Z","shell.execute_reply":"2025-03-05T08:07:41.560502Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:07:58.197283Z","iopub.execute_input":"2025-03-05T08:07:58.197653Z","iopub.status.idle":"2025-03-05T08:07:59.688934Z","shell.execute_reply.started":"2025-03-05T08:07:58.197623Z","shell.execute_reply":"2025-03-05T08:07:59.687662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:08:08.564434Z","iopub.execute_input":"2025-03-05T08:08:08.565408Z","iopub.status.idle":"2025-03-05T08:08:10.01953Z","shell.execute_reply.started":"2025-03-05T08:08:08.565369Z","shell.execute_reply":"2025-03-05T08:08:10.018327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:08:17.980745Z","iopub.execute_input":"2025-03-05T08:08:17.981141Z","iopub.status.idle":"2025-03-05T08:08:19.488667Z","shell.execute_reply.started":"2025-03-05T08:08:17.98111Z","shell.execute_reply":"2025-03-05T08:08:19.487651Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# seed random number generator\nseed(1337)\n# define ratio of pictures to use for validation\nval_ratio = 0.25 #changable\n# copy training dataset images into subdirectories\nsrc_directory = 'train/'\n\nfor file in listdir(src_directory):\n\tsrc = src_directory + '/' + file\n\tdst_dir = 'train/'\n\tif random() < val_ratio:\n\t\tdst_dir = 'test/'\n\tif file.startswith('cat'):\n\t\tdst = dataset_home + dst_dir + 'cats/'  + file\n\t\tcopyfile(src, dst)\n\telif file.startswith('dog'):\n\t\tdst = dataset_home + dst_dir + 'dogs/'  + file\n\t\tcopyfile(src, dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:08:35.284962Z","iopub.execute_input":"2025-03-05T08:08:35.285353Z","iopub.status.idle":"2025-03-05T08:08:49.650562Z","shell.execute_reply.started":"2025-03-05T08:08:35.285324Z","shell.execute_reply":"2025-03-05T08:08:49.649809Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(src)\nprint(dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:08:56.46244Z","iopub.execute_input":"2025-03-05T08:08:56.463407Z","iopub.status.idle":"2025-03-05T08:08:56.468862Z","shell.execute_reply.started":"2025-03-05T08:08:56.463363Z","shell.execute_reply":"2025-03-05T08:08:56.467735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l dataset_dogs_vs_cats/train/dogs | wc -l\n!ls -l dataset_dogs_vs_cats/train/cats | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:09:02.387839Z","iopub.execute_input":"2025-03-05T08:09:02.388212Z","iopub.status.idle":"2025-03-05T08:09:05.510965Z","shell.execute_reply.started":"2025-03-05T08:09:02.38818Z","shell.execute_reply":"2025-03-05T08:09:05.509991Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#validation set\n!ls -l dataset_dogs_vs_cats/test/dogs | wc -l\n!ls -l dataset_dogs_vs_cats/test/cats | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:10:05.804657Z","iopub.execute_input":"2025-03-05T08:10:05.80533Z","iopub.status.idle":"2025-03-05T08:10:08.852273Z","shell.execute_reply.started":"2025-03-05T08:10:05.805294Z","shell.execute_reply":"2025-03-05T08:10:08.851131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:10:14.291536Z","iopub.execute_input":"2025-03-05T08:10:14.292274Z","iopub.status.idle":"2025-03-05T08:10:24.067088Z","shell.execute_reply.started":"2025-03-05T08:10:14.292238Z","shell.execute_reply":"2025-03-05T08:10:24.065807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:10:26.395206Z","iopub.execute_input":"2025-03-05T08:10:26.395983Z","iopub.status.idle":"2025-03-05T08:10:26.409749Z","shell.execute_reply.started":"2025-03-05T08:10:26.395949Z","shell.execute_reply":"2025-03-05T08:10:26.408657Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Develop a Baseline CNN Model","metadata":{"trusted":true},"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":"2025-03-05T08:11:15.169969Z","iopub.execute_input":"2025-03-05T08:11:15.17038Z","iopub.status.idle":"2025-03-05T08:11:15.175866Z","shell.execute_reply.started":"2025-03-05T08:11:15.17035Z","shell.execute_reply":"2025-03-05T08:11:15.174787Z"}},"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":"2025-03-05T08:11:27.506104Z","iopub.execute_input":"2025-03-05T08:11:27.506841Z","iopub.status.idle":"2025-03-05T08:11:27.51252Z","shell.execute_reply.started":"2025-03-05T08:11:27.50681Z","shell.execute_reply":"2025-03-05T08:11:27.511587Z"}},"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":"2025-03-05T08:11:33.596575Z","iopub.execute_input":"2025-03-05T08:11:33.597538Z","iopub.status.idle":"2025-03-05T08:11:33.605539Z","shell.execute_reply.started":"2025-03-05T08:11:33.597496Z","shell.execute_reply":"2025-03-05T08:11:33.604577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# run the test harness for evaluating a model\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":"2025-03-05T08:11:39.509667Z","iopub.execute_input":"2025-03-05T08:11:39.510501Z","iopub.status.idle":"2025-03-05T08:11:39.516333Z","shell.execute_reply.started":"2025-03-05T08:11:39.510469Z","shell.execute_reply":"2025-03-05T08:11:39.515277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_1 = define_model_1()\nprint(model_1.summary())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:11:44.898431Z","iopub.execute_input":"2025-03-05T08:11:44.899228Z","iopub.status.idle":"2025-03-05T08:11:45.994962Z","shell.execute_reply.started":"2025-03-05T08:11:44.899192Z","shell.execute_reply":"2025-03-05T08:11:45.99411Z"}},"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":"2025-03-05T08:11:53.658474Z","iopub.execute_input":"2025-03-05T08:11:53.659222Z","iopub.status.idle":"2025-03-05T08:11:54.585082Z","shell.execute_reply.started":"2025-03-05T08:11:53.659187Z","shell.execute_reply":"2025-03-05T08:11:54.584157Z"}},"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":"2025-03-05T08:12:01.764401Z","iopub.execute_input":"2025-03-05T08:12:01.765278Z","iopub.status.idle":"2025-03-05T08:12:01.772664Z","shell.execute_reply.started":"2025-03-05T08:12:01.765242Z","shell.execute_reply":"2025-03-05T08:12:01.771804Z"}},"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":"2025-03-05T08:12:07.441616Z","iopub.execute_input":"2025-03-05T08:12:07.442352Z","iopub.status.idle":"2025-03-05T08:12:07.44839Z","shell.execute_reply.started":"2025-03-05T08:12:07.442321Z","shell.execute_reply":"2025-03-05T08:12:07.447429Z"}},"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=20, verbose=1,\n    callbacks = [PlotLossesKeras()]\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:27:30.99145Z","iopub.execute_input":"2025-03-05T08:27:30.992026Z","iopub.status.idle":"2025-03-05T08:45:22.155418Z","shell.execute_reply.started":"2025-03-05T08:27:30.991992Z","shell.execute_reply":"2025-03-05T08:45:22.154714Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\nsummarize_diagnostics(history_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:16.635505Z","iopub.execute_input":"2025-03-05T08:47:16.635892Z","iopub.status.idle":"2025-03-05T08:47:17.021994Z","shell.execute_reply.started":"2025-03-05T08:47:16.635861Z","shell.execute_reply":"2025-03-05T08:47:17.02125Z"}},"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":"2025-03-05T08:47:20.298992Z","iopub.execute_input":"2025-03-05T08:47:20.299347Z","iopub.status.idle":"2025-03-05T08:47:37.399196Z","shell.execute_reply.started":"2025-03-05T08:47:20.299318Z","shell.execute_reply":"2025-03-05T08:47:37.398391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:39.525432Z","iopub.execute_input":"2025-03-05T08:47:39.526229Z","iopub.status.idle":"2025-03-05T08:47:41.062901Z","shell.execute_reply.started":"2025-03-05T08:47:39.526199Z","shell.execute_reply":"2025-03-05T08:47:41.061593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pwd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:42.971033Z","iopub.execute_input":"2025-03-05T08:47:42.971968Z","iopub.status.idle":"2025-03-05T08:47:42.97875Z","shell.execute_reply.started":"2025-03-05T08:47:42.97193Z","shell.execute_reply":"2025-03-05T08:47:42.977818Z"}},"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'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:46.170585Z","iopub.execute_input":"2025-03-05T08:47:46.171531Z","iopub.status.idle":"2025-03-05T08:47:46.221282Z","shell.execute_reply.started":"2025-03-05T08:47:46.171497Z","shell.execute_reply":"2025-03-05T08:47:46.220669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_files[:5], type(image_files), len(image_files)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:48.524387Z","iopub.execute_input":"2025-03-05T08:47:48.52489Z","iopub.status.idle":"2025-03-05T08:47:48.532384Z","shell.execute_reply.started":"2025-03-05T08:47:48.524856Z","shell.execute_reply":"2025-03-05T08:47:48.531447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:50.92358Z","iopub.execute_input":"2025-03-05T08:47:50.923933Z","iopub.status.idle":"2025-03-05T08:47:50.95703Z","shell.execute_reply.started":"2025-03-05T08:47:50.923905Z","shell.execute_reply":"2025-03-05T08:47:50.956172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.loc[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:52.874957Z","iopub.execute_input":"2025-03-05T08:47:52.875293Z","iopub.status.idle":"2025-03-05T08:47:52.882262Z","shell.execute_reply.started":"2025-03-05T08:47:52.875266Z","shell.execute_reply":"2025-03-05T08:47:52.881444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:55.194619Z","iopub.execute_input":"2025-03-05T08:47:55.195304Z","iopub.status.idle":"2025-03-05T08:47:55.208033Z","shell.execute_reply.started":"2025-03-05T08:47:55.195274Z","shell.execute_reply":"2025-03-05T08:47:55.207007Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['filename']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:47:57.30774Z","iopub.execute_input":"2025-03-05T08:47:57.308157Z","iopub.status.idle":"2025-03-05T08:47:57.315534Z","shell.execute_reply.started":"2025-03-05T08:47:57.308129Z","shell.execute_reply":"2025-03-05T08:47:57.314629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE = (299, 299)\n\ndatagen = ImageDataGenerator(rescale=1.0/255.0,\n                            horizontal_flip=True,)\n\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)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:48:00.819169Z","iopub.execute_input":"2025-03-05T08:48:00.819502Z","iopub.status.idle":"2025-03-05T08:48:01.876382Z","shell.execute_reply.started":"2025-03-05T08:48:00.819476Z","shell.execute_reply":"2025-03-05T08:48:01.875724Z"}},"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 < test_images.shape[0]:\n            ax.imshow(test_images[i])\n        ax.axis('off')  # Turn off axis ticks/labels for clarity\n    \n    plt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:48:05.899333Z","iopub.execute_input":"2025-03-05T08:48:05.899679Z","iopub.status.idle":"2025-03-05T08:48:05.904964Z","shell.execute_reply.started":"2025-03-05T08:48:05.899652Z","shell.execute_reply":"2025-03-05T08:48:05.904061Z"}},"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":"2025-03-05T08:48:09.250595Z","iopub.execute_input":"2025-03-05T08:48:09.251337Z","iopub.status.idle":"2025-03-05T08:48:16.005703Z","shell.execute_reply.started":"2025-03-05T08:48:09.251304Z","shell.execute_reply":"2025-03-05T08:48:16.004461Z"}},"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":"2025-03-05T08:48:21.114688Z","iopub.execute_input":"2025-03-05T08:48:21.115057Z","iopub.status.idle":"2025-03-05T08:48:25.717078Z","shell.execute_reply.started":"2025-03-05T08:48:21.115029Z","shell.execute_reply":"2025-03-05T08:48:25.715529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\n# ใช้ ImageDataGenerator เพื่อเพิ่มข้อมูล\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=40,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nval_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_it = train_datagen.flow_from_directory(\n    'dataset_dogs_vs_cats/train/',\n    target_size=(200, 200),\n    batch_size=32,\n    class_mode='binary'\n)\n\nval_it = val_datagen.flow_from_directory(\n    'dataset_dogs_vs_cats/test/',\n    target_size=(200, 200),\n    batch_size=32,\n    class_mode='binary'\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:53:56.610621Z","iopub.execute_input":"2025-03-05T08:53:56.611217Z","iopub.status.idle":"2025-03-05T08:53:57.440796Z","shell.execute_reply.started":"2025-03-05T08:53:56.611185Z","shell.execute_reply":"2025-03-05T08:53:57.439852Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.layers import Dropout\n\nmodel_1.add(Dropout(0.5))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:54:09.433497Z","iopub.execute_input":"2025-03-05T08:54:09.434191Z","iopub.status.idle":"2025-03-05T08:54:09.444147Z","shell.execute_reply.started":"2025-03-05T08:54:09.434161Z","shell.execute_reply":"2025-03-05T08:54:09.443119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.callbacks import LearningRateScheduler\n\ndef scheduler(epoch, lr):\n    if epoch > 10:\n        return lr * 0.1\n    return lr\n\nlr_scheduler = LearningRateScheduler(scheduler)\n\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=20, verbose=1,\n    callbacks=[PlotLossesKeras(), lr_scheduler]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T08:54:20.753549Z","iopub.execute_input":"2025-03-05T08:54:20.753956Z","iopub.status.idle":"2025-03-05T10:02:57.473092Z","shell.execute_reply.started":"2025-03-05T08:54:20.753927Z","shell.execute_reply":"2025-03-05T10:02:57.472293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications import VGG16\nfrom keras.models import Model\nfrom keras.layers import Flatten, Dense\n\n# โหลด VGG16 ที่ฝึกมาแล้ว\nbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(200, 200, 3))\n\n# เพิ่มชั้นใหม่สำหรับการฝึก\nx = Flatten()(base_model.output)\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.5)(x)\nx = Dense(1, activation='sigmoid')(x)\n\n# สร้างโมเดลใหม่\nmodel_1 = Model(inputs=base_model.input, outputs=x)\n\n# Freeze layers ของ VGG16 เพื่อไม่ให้ถูกฝึกใหม่\nfor layer in base_model.layers:\n    layer.trainable = False\n\nmodel_1.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\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=20, verbose=1,\n    callbacks=[PlotLossesKeras()]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T10:08:56.518327Z","iopub.execute_input":"2025-03-05T10:08:56.518687Z","iopub.status.idle":"2025-03-05T11:27:48.135547Z","shell.execute_reply.started":"2025-03-05T10:08:56.518657Z","shell.execute_reply":"2025-03-05T11:27:48.13465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\nsummarize_diagnostics(history_1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-05T11:29:08.404256Z","iopub.execute_input":"2025-03-05T11:29:08.404623Z","iopub.status.idle":"2025-03-05T11:29:08.802959Z","shell.execute_reply.started":"2025-03-05T11:29:08.404593Z","shell.execute_reply":"2025-03-05T11:29:08.801984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}