{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30628,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:08.466847Z","iopub.execute_input":"2025-03-03T10:58:08.467361Z","iopub.status.idle":"2025-03-03T10:58:08.851634Z","shell.execute_reply.started":"2025-03-03T10:58:08.467309Z","shell.execute_reply":"2025-03-03T10:58:08.850978Z"}},"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-03T10:58:08.852891Z","iopub.execute_input":"2025-03-03T10:58:08.853230Z","iopub.status.idle":"2025-03-03T10:58:23.394762Z","shell.execute_reply.started":"2025-03-03T10:58:08.853204Z","shell.execute_reply":"2025-03-03T10:58:23.394065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:23.395671Z","iopub.execute_input":"2025-03-03T10:58:23.395904Z","iopub.status.idle":"2025-03-03T10:58:23.521226Z","shell.execute_reply.started":"2025-03-03T10:58:23.395880Z","shell.execute_reply":"2025-03-03T10:58:23.520422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls test1/ |wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:23.523243Z","iopub.execute_input":"2025-03-03T10:58:23.523516Z","iopub.status.idle":"2025-03-03T10:58:23.688877Z","shell.execute_reply.started":"2025-03-03T10:58:23.523488Z","shell.execute_reply":"2025-03-03T10:58:23.688002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls train/ |wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:23.690005Z","iopub.execute_input":"2025-03-03T10:58:23.690253Z","iopub.status.idle":"2025-03-03T10:58:23.907460Z","shell.execute_reply.started":"2025-03-03T10:58:23.690226Z","shell.execute_reply":"2025-03-03T10:58:23.906515Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"ls train/","metadata":{"execution":{"iopub.status.busy":"2025-02-28T13:29:48.767617Z","iopub.execute_input":"2025-02-28T13:29:48.767891Z","iopub.status.idle":"2025-02-28T13:29:49.025620Z","shell.execute_reply.started":"2025-02-28T13:29:48.767866Z","shell.execute_reply":"2025-02-28T13:29:49.024871Z"}}},{"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-03T10:58:23.908834Z","iopub.execute_input":"2025-03-03T10:58:23.909110Z","iopub.status.idle":"2025-03-03T10:58:25.721195Z","shell.execute_reply.started":"2025-03-03T10:58:23.909082Z","shell.execute_reply":"2025-03-03T10:58:25.720425Z"}},"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-03T10:58:25.722430Z","iopub.execute_input":"2025-03-03T10:58:25.722800Z","iopub.status.idle":"2025-03-03T10:58:26.484565Z","shell.execute_reply.started":"2025-03-03T10:58:25.722771Z","shell.execute_reply":"2025-03-03T10:58:26.483909Z"}},"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-03T10:58:26.485485Z","iopub.execute_input":"2025-03-03T10:58:26.485721Z","iopub.status.idle":"2025-03-03T10:58:39.027248Z","shell.execute_reply.started":"2025-03-03T10:58:26.485696Z","shell.execute_reply":"2025-03-03T10:58:39.026494Z"}},"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-03T10:58:39.028318Z","iopub.execute_input":"2025-03-03T10:58:39.028827Z","iopub.status.idle":"2025-03-03T10:58:39.032881Z","shell.execute_reply.started":"2025-03-03T10:58:39.028793Z","shell.execute_reply":"2025-03-03T10:58:39.032256Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# define location of dataset\nfolder = 'train/'\nphotos, labels = list(), list()\n# enumerate files in the directory\nfor file in listdir(folder):\n\t# determine class\n\toutput = 0.0\n\tif file.startswith('dog'):\n\t\toutput = 1.0\n\t# load image\n\tphoto = load_img(folder + file, target_size=(200, 200))\n\t# convert to numpy array\n\tphoto = img_to_array(photo)\n\t# store\n\tphotos.append(photo)\n\tlabels.append(output)\n# convert to a numpy arrays\nphotos = asarray(photos)\nlabels = asarray(labels)\nprint(photos.shape, labels.shape)\n# save the reshaped photos\nsave('dogs_vs_cats_photos.npy', photos)\nsave('dogs_vs_cats_labels.npy', labels)","metadata":{"execution":{"iopub.status.busy":"2024-01-27T03:36:15.416611Z","iopub.execute_input":"2024-01-27T03:36:15.417128Z","iopub.status.idle":"2024-01-27T03:36:38.886396Z","shell.execute_reply.started":"2024-01-27T03:36:15.417101Z","shell.execute_reply":"2024-01-27T03:36:38.883149Z"}}},{"cell_type":"markdown","source":"# load and confirm the shape\nfrom numpy import load\nphotos = load('dogs_vs_cats_photos.npy')\nlabels = load('dogs_vs_cats_labels.npy')","metadata":{"execution":{"iopub.status.busy":"2024-01-27T03:36:38.88711Z","iopub.status.idle":"2024-01-27T03:36:38.887458Z","shell.execute_reply.started":"2024-01-27T03:36:38.887281Z","shell.execute_reply":"2024-01-27T03:36:38.887296Z"}}},{"cell_type":"code","source":"# organize dataset into a useful structure\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:39.035799Z","iopub.execute_input":"2025-03-03T10:58:39.036260Z","iopub.status.idle":"2025-03-03T10:58:39.046165Z","shell.execute_reply.started":"2025-03-03T10:58:39.036230Z","shell.execute_reply":"2025-03-03T10:58:39.045512Z"}},"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-03T10:58:39.047117Z","iopub.execute_input":"2025-03-03T10:58:39.047389Z","iopub.status.idle":"2025-03-03T10:58:39.056914Z","shell.execute_reply.started":"2025-03-03T10:58:39.047362Z","shell.execute_reply":"2025-03-03T10:58:39.056284Z"}},"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-03T10:58:39.057787Z","iopub.execute_input":"2025-03-03T10:58:39.058314Z","iopub.status.idle":"2025-03-03T10:58:39.193624Z","shell.execute_reply.started":"2025-03-03T10:58:39.058278Z","shell.execute_reply":"2025-03-03T10:58:39.192573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:39.194827Z","iopub.execute_input":"2025-03-03T10:58:39.195106Z","iopub.status.idle":"2025-03-03T10:58:39.321977Z","shell.execute_reply.started":"2025-03-03T10:58:39.195076Z","shell.execute_reply":"2025-03-03T10:58:39.321064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:39.323241Z","iopub.execute_input":"2025-03-03T10:58:39.323510Z","iopub.status.idle":"2025-03-03T10:58:39.451150Z","shell.execute_reply.started":"2025-03-03T10:58:39.323481Z","shell.execute_reply":"2025-03-03T10:58:39.450264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls -l dataset_dogs_vs_cats/test","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:39.452447Z","iopub.execute_input":"2025-03-03T10:58:39.452709Z","iopub.status.idle":"2025-03-03T10:58:39.578999Z","shell.execute_reply.started":"2025-03-03T10:58:39.452681Z","shell.execute_reply":"2025-03-03T10:58:39.577922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# seed random number generator\nseed(1)\n# define ratio of pictures to use for validation\nval_ratio = 0.25\n# copy training dataset images into subdirectories\nsrc_directory = 'train/'\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-03T10:58:39.580535Z","iopub.execute_input":"2025-03-03T10:58:39.581161Z","iopub.status.idle":"2025-03-03T10:58:42.473633Z","shell.execute_reply.started":"2025-03-03T10:58:39.581124Z","shell.execute_reply":"2025-03-03T10:58:42.472822Z"}},"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":"2025-03-03T10:58:42.474689Z","iopub.execute_input":"2025-03-03T10:58:42.474968Z","iopub.status.idle":"2025-03-03T10:58:42.691518Z","shell.execute_reply.started":"2025-03-03T10:58:42.474925Z","shell.execute_reply":"2025-03-03T10:58:42.690626Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l dataset_dogs_vs_cats/train/cats | wc -l","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:42.692752Z","iopub.execute_input":"2025-03-03T10:58:42.693042Z","iopub.status.idle":"2025-03-03T10:58:42.912258Z","shell.execute_reply.started":"2025-03-03T10:58:42.693011Z","shell.execute_reply":"2025-03-03T10:58:42.911159Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:42.913536Z","iopub.execute_input":"2025-03-03T10:58:42.913814Z","iopub.status.idle":"2025-03-03T10:58:48.487212Z","shell.execute_reply.started":"2025-03-03T10:58:42.913783Z","shell.execute_reply":"2025-03-03T10:58:48.486107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:48.488735Z","iopub.execute_input":"2025-03-03T10:58:48.489111Z","iopub.status.idle":"2025-03-03T10:58:48.507790Z","shell.execute_reply.started":"2025-03-03T10:58:48.489075Z","shell.execute_reply":"2025-03-03T10:58:48.507053Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Prepare Final Dataset","metadata":{}},{"cell_type":"code","source":"#start tutorial\n#Develop a Baseline CNN Model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:48.508863Z","iopub.execute_input":"2025-03-03T10:58:48.509164Z","iopub.status.idle":"2025-03-03T10:58:48.525984Z","shell.execute_reply.started":"2025-03-03T10:58:48.509136Z","shell.execute_reply":"2025-03-03T10:58:48.525362Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"CNN Model","metadata":{}},{"cell_type":"code","source":"\n# 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-03T10:58:48.526950Z","iopub.execute_input":"2025-03-03T10:58:48.527250Z","iopub.status.idle":"2025-03-03T10:58:48.549905Z","shell.execute_reply.started":"2025-03-03T10:58:48.527222Z","shell.execute_reply":"2025-03-03T10:58:48.549110Z"}},"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    # save plot to file\n    filename = sys.argv[0].split('/')[-1]\n    pyplot.savefig(filename + '_plot.png')\n    pyplot.show()\n    pyplot.close()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:48.550965Z","iopub.execute_input":"2025-03-03T10:58:48.551218Z","iopub.status.idle":"2025-03-03T10:58:48.573058Z","shell.execute_reply.started":"2025-03-03T10:58:48.551192Z","shell.execute_reply":"2025-03-03T10:58:48.572332Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# InceptionResNetV2","metadata":{}},{"cell_type":"code","source":"# save the final model to file\nfrom keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom keras.applications.xception import Xception\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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:48.573929Z","iopub.execute_input":"2025-03-03T10:58:48.574226Z","iopub.status.idle":"2025-03-03T10:58:48.593947Z","shell.execute_reply.started":"2025-03-03T10:58:48.574186Z","shell.execute_reply":"2025-03-03T10:58:48.593263Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"xcep_model = Xception(include_top=False, input_shape=(299, 299, 3))\nxcep_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-27T04:11:23.494572Z","iopub.execute_input":"2024-01-27T04:11:23.495217Z","iopub.status.idle":"2024-01-27T04:11:26.579883Z","shell.execute_reply.started":"2024-01-27T04:11:23.495182Z","shell.execute_reply":"2024-01-27T04:11:26.578335Z"}}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n# create data generator\ndatagen = 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# prepare iterator\ntrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n    class_mode='binary', batch_size=64, target_size=(299, 299))\ntest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n    class_mode='binary', batch_size=64, target_size=(299, 299))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:48.595200Z","iopub.execute_input":"2025-03-03T10:58:48.595460Z","iopub.status.idle":"2025-03-03T10:58:49.351176Z","shell.execute_reply.started":"2025-03-03T10:58:48.595434Z","shell.execute_reply":"2025-03-03T10:58:49.350458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load model\nmodel = InceptionResNetV2(\n    include_top=False,\n    input_shape=(299, 299, 3)\n    )\nprint(model.summary())\n#model = Xception(include_top=False, input_shape=(299, 299, 3))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:58:49.352326Z","iopub.execute_input":"2025-03-03T10:58:49.352602Z","iopub.status.idle":"2025-03-03T10:59:06.350765Z","shell.execute_reply.started":"2025-03-03T10:58:49.352573Z","shell.execute_reply":"2025-03-03T10:59:06.349948Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# mark loaded layers as not trainable\nfor index, layer in enumerate(model.layers):\n    print(index, layer.name, layer.output_shape)\n    layer.trainable = False\n    \nfor index, layer in enumerate(model.layers[-30:]):\n    print(index, layer.name, layer.output_shape)\n    layer.trainable = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:59:06.351784Z","iopub.execute_input":"2025-03-03T10:59:06.352076Z","iopub.status.idle":"2025-03-03T10:59:06.405659Z","shell.execute_reply.started":"2025-03-03T10:59:06.352046Z","shell.execute_reply":"2025-03-03T10:59:06.404958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.layers[-1].output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:59:06.409210Z","iopub.execute_input":"2025-03-03T10:59:06.409456Z","iopub.status.idle":"2025-03-03T10:59:06.415288Z","shell.execute_reply.started":"2025-03-03T10:59:06.409431Z","shell.execute_reply":"2025-03-03T10:59:06.414573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# add new classifier layers\nflat1 = Flatten()(model.layers[-1].output)\nclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\noutput = Dense(1, activation='sigmoid')(class1)\n# define new model\nmy_model = Model(inputs=model.inputs, outputs=output)\n# compile model\nopt = SGD(lr=0.001, momentum=0.9)\nmy_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\nmy_model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:59:06.416165Z","iopub.execute_input":"2025-03-03T10:59:06.416399Z","iopub.status.idle":"2025-03-03T10:59:07.673244Z","shell.execute_reply.started":"2025-03-03T10:59:06.416374Z","shell.execute_reply":"2025-03-03T10:59:07.672486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":" # fit model\nmy_model.reset_states()\nhistory = my_model.fit(train_it, \n                    steps_per_epoch=len(train_it),\n                    validation_data=test_it, \n                    validation_steps=len(test_it), \n                    epochs=2, verbose=1,\n                    callbacks=[PlotLossesKeras()])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T10:59:07.674153Z","iopub.execute_input":"2025-03-03T10:59:07.674387Z","iopub.status.idle":"2025-03-03T11:31:44.138955Z","shell.execute_reply.started":"2025-03-03T10:59:07.674363Z","shell.execute_reply":"2025-03-03T11:31:44.138010Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# evaluate model\n_, acc = my_model.evaluate(test_it, steps=len(test_it), verbose=1)\nprint('> %.3f' % (acc * 100.0))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T11:31:44.140512Z","iopub.execute_input":"2025-03-03T11:31:44.140818Z","iopub.status.idle":"2025-03-03T11:35:38.491105Z","shell.execute_reply.started":"2025-03-03T11:31:44.140787Z","shell.execute_reply":"2025-03-03T11:35:38.490126Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learning curves\nsummarize_diagnostics(history)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T11:35:38.492385Z","iopub.execute_input":"2025-03-03T11:35:38.492702Z","iopub.status.idle":"2025-03-03T11:35:38.831047Z","shell.execute_reply.started":"2025-03-03T11:35:38.492670Z","shell.execute_reply":"2025-03-03T11:35:38.830291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"best_model = 'InceptionResNetV2_model.hdf5'\nmy_model.save(best_model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T11:35:38.832025Z","iopub.execute_input":"2025-03-03T11:35:38.832288Z","iopub.status.idle":"2025-03-03T11:35:40.101644Z","shell.execute_reply.started":"2025-03-03T11:35:38.832260Z","shell.execute_reply":"2025-03-03T11:35:40.100501Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# make a prediction for a new image.\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\nfrom keras.models import load_model\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T11:35:40.103285Z","iopub.execute_input":"2025-03-03T11:35:40.103543Z","iopub.status.idle":"2025-03-03T11:35:40.107571Z","shell.execute_reply.started":"2025-03-03T11:35:40.103518Z","shell.execute_reply":"2025-03-03T11:35:40.106890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"best_model = load_model(best_model)\nbest_model.evaluate(test_it, verbose=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T11:35:40.108454Z","iopub.execute_input":"2025-03-03T11:35:40.108700Z","iopub.status.idle":"2025-03-03T11:39:41.645293Z","shell.execute_reply.started":"2025-03-03T11:35:40.108672Z","shell.execute_reply":"2025-03-03T11:39:41.644246Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T11:39:41.646576Z","iopub.execute_input":"2025-03-03T11:39:41.646869Z","iopub.status.idle":"2025-03-03T11:39:41.650691Z","shell.execute_reply.started":"2025-03-03T11:39:41.646838Z","shell.execute_reply":"2025-03-03T11:39:41.649986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\ndef predict_random_images(test_dir, model, class_names):\n    img_size=(299, 299)\n    images = []  # เก็บ path รูปภาพ\n    labels = []  # เก็บ label จริง\n\n    # เลือก 5 รูปจากแต่ละ class\n    for label in class_names:\n        folder = os.path.join(test_dir, label)\n        files = random.sample(list(os.listdir(folder)), 5)\n        for f in files:\n            images.append(os.path.join(folder, f))\n            labels.append(label)\n\n    # Plot\n    fig, axes = plt.subplots(2, 5, figsize=(12, 6))\n    for i, (img_path, true_label) in enumerate(zip(images, labels)):\n        img = load_img(img_path, target_size=img_size)\n        img_array = img_to_array(img) / 255\n        img_array = np.expand_dims(img_array, axis=0)\n\n        pred = model.predict(img_array)[0]  # ค่าระหว่าง 0-1\n        pred_label = class_names[1] if pred > 0.5 else class_names[0]\n\n        axes[i // 5, i % 5].imshow(img)\n        axes[i // 5, i % 5].set_title(f\"Pred: {pred_label}\\nTrue: {true_label}\", fontsize=10)\n        axes[i // 5, i % 5].axis(\"off\")\n\n    plt.tight_layout()\n    plt.show()\npredict_random_images(\"dataset_dogs_vs_cats/test/\", best_model, [\"cats\", \"dogs\"])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-03T11:39:41.651666Z","iopub.execute_input":"2025-03-03T11:39:41.651917Z","iopub.status.idle":"2025-03-03T11:39:47.821184Z","shell.execute_reply.started":"2025-03-03T11:39:41.651890Z","shell.execute_reply":"2025-03-03T11:39:47.820152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}