{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:07.277316Z","iopub.execute_input":"2025-02-21T16:04:07.278208Z","iopub.status.idle":"2025-02-21T16:04:07.284276Z","shell.execute_reply.started":"2025-02-21T16:04:07.278176Z","shell.execute_reply":"2025-02-21T16:04:07.28337Z"}},"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-02-21T16:04:07.285738Z","iopub.execute_input":"2025-02-21T16:04:07.286045Z","iopub.status.idle":"2025-02-21T16:04:20.578329Z","shell.execute_reply.started":"2025-02-21T16:04:07.286017Z","shell.execute_reply":"2025-02-21T16:04:20.577636Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"!ls train","metadata":{"execution":{"iopub.status.busy":"2024-01-08T06:57:18.293035Z","iopub.execute_input":"2024-01-08T06:57:18.293457Z","iopub.status.idle":"2024-01-08T06:57:19.311078Z","shell.execute_reply.started":"2024-01-08T06:57:18.293422Z","shell.execute_reply":"2024-01-08T06:57:19.309969Z"}}},{"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-02-21T16:04:20.579244Z","iopub.execute_input":"2025-02-21T16:04:20.579482Z","iopub.status.idle":"2025-02-21T16:04:21.561038Z","shell.execute_reply.started":"2025-02-21T16:04:20.579459Z","shell.execute_reply":"2025-02-21T16:04:21.560213Z"}},"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-02-21T16:04:21.562599Z","iopub.execute_input":"2025-02-21T16:04:21.563361Z","iopub.status.idle":"2025-02-21T16:04:22.415484Z","shell.execute_reply.started":"2025-02-21T16:04:21.563335Z","shell.execute_reply":"2025-02-21T16:04:22.41473Z"}},"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-02-21T16:04:22.416703Z","iopub.execute_input":"2025-02-21T16:04:22.417061Z","iopub.status.idle":"2025-02-21T16:04:36.004194Z","shell.execute_reply.started":"2025-02-21T16:04:22.417029Z","shell.execute_reply":"2025-02-21T16:04:36.003259Z"}},"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":"\n# 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-02-21T16:04:36.005497Z","iopub.execute_input":"2025-02-21T16:04:36.006431Z","iopub.status.idle":"2025-02-21T16:04:36.010447Z","shell.execute_reply.started":"2025-02-21T16:04:36.006397Z","shell.execute_reply":"2025-02-21T16:04:36.009547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\nfor subdir in subdirs:\n\t# create label subdirectories\n\tlabeldirs = ['dogs/', 'cats/']\n\tfor labldir in labeldirs:\n\t\tnewdir = dataset_home + subdir + labldir\n\t\tmakedirs(newdir, exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:36.011483Z","iopub.execute_input":"2025-02-21T16:04:36.01173Z","iopub.status.idle":"2025-02-21T16:04:36.037692Z","shell.execute_reply.started":"2025-02-21T16:04:36.011707Z","shell.execute_reply":"2025-02-21T16:04:36.036881Z"}},"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-02-21T16:04:36.038613Z","iopub.execute_input":"2025-02-21T16:04:36.038939Z","iopub.status.idle":"2025-02-21T16:04:38.408028Z","shell.execute_reply.started":"2025-02-21T16:04:36.038907Z","shell.execute_reply":"2025-02-21T16:04:38.407144Z"}},"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-02-21T16:04:38.409028Z","iopub.execute_input":"2025-02-21T16:04:38.409293Z","iopub.status.idle":"2025-02-21T16:04:39.525365Z","shell.execute_reply.started":"2025-02-21T16:04:38.409271Z","shell.execute_reply":"2025-02-21T16:04:39.52448Z"}},"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-02-21T16:04:39.529261Z","iopub.execute_input":"2025-02-21T16:04:39.52956Z","iopub.status.idle":"2025-02-21T16:04:40.617452Z","shell.execute_reply.started":"2025-02-21T16:04:39.529535Z","shell.execute_reply":"2025-02-21T16:04:40.616228Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:40.619372Z","iopub.execute_input":"2025-02-21T16:04:40.619833Z","iopub.status.idle":"2025-02-21T16:04:50.908425Z","shell.execute_reply.started":"2025-02-21T16:04:40.619794Z","shell.execute_reply":"2025-02-21T16:04:50.906866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:50.910156Z","iopub.execute_input":"2025-02-21T16:04:50.910602Z","iopub.status.idle":"2025-02-21T16:04:50.933435Z","shell.execute_reply.started":"2025-02-21T16:04:50.910556Z","shell.execute_reply":"2025-02-21T16:04:50.932136Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Prepare Final Dataset","metadata":{}},{"cell_type":"code","source":"# save the final model to file\nfrom keras.applications.vgg16 import VGG16\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-02-21T16:04:50.934686Z","iopub.execute_input":"2025-02-21T16:04:50.934996Z","iopub.status.idle":"2025-02-21T16:04:50.94339Z","shell.execute_reply.started":"2025-02-21T16:04:50.934974Z","shell.execute_reply":"2025-02-21T16:04:50.94255Z"}},"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":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"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=(224, 224))\ntest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n    class_mode='binary', batch_size=64, target_size=(224, 224))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:50.944507Z","iopub.execute_input":"2025-02-21T16:04:50.944753Z","iopub.status.idle":"2025-02-21T16:04:53.225856Z","shell.execute_reply.started":"2025-02-21T16:04:50.944732Z","shell.execute_reply":"2025-02-21T16:04:53.225103Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load model\nmodel = VGG16(\n    include_top=False,\n    input_shape=(224, 224, 3)\n    )\nprint(model.summary())\n#model = Xception(include_top=False, input_shape=(299, 299, 3))\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:53.227019Z","iopub.execute_input":"2025-02-21T16:04:53.227249Z","iopub.status.idle":"2025-02-21T16:04:55.482217Z","shell.execute_reply.started":"2025-02-21T16:04:53.227229Z","shell.execute_reply":"2025-02-21T16:04:55.481335Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# mark loaded layers as not trainable\nfor index, layer in enumerate(model.layers[:15]):\n    print(index, layer.name, layer.output_shape)\n    layer.trainable = False\n    \nfor index, layer in enumerate(model.layers[15:]):\n    print(index, layer.name, layer.output_shape)\n    layer.trainable = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:55.483321Z","iopub.execute_input":"2025-02-21T16:04:55.483575Z","iopub.status.idle":"2025-02-21T16:04:55.490055Z","shell.execute_reply.started":"2025-02-21T16:04:55.483553Z","shell.execute_reply":"2025-02-21T16:04:55.489203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.layers[-1].output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:55.49108Z","iopub.execute_input":"2025-02-21T16:04:55.491323Z","iopub.status.idle":"2025-02-21T16:04:55.508375Z","shell.execute_reply.started":"2025-02-21T16:04:55.491301Z","shell.execute_reply":"2025-02-21T16:04:55.507731Z"}},"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.0001, momentum=0.9)\nmy_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\nmy_model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:04:55.509278Z","iopub.execute_input":"2025-02-21T16:04:55.509487Z","iopub.status.idle":"2025-02-21T16:04:55.601634Z","shell.execute_reply.started":"2025-02-21T16:04:55.509469Z","shell.execute_reply":"2025-02-21T16:04:55.600641Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\", \"r\") as z:\n    z.extractall(\".\")\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\", \"r\") as z:\n    z.extractall(\".\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:15:58.804676Z","iopub.execute_input":"2025-02-21T16:15:58.805579Z","iopub.status.idle":"2025-02-21T16:16:08.221502Z","shell.execute_reply.started":"2025-02-21T16:15:58.805544Z","shell.execute_reply":"2025-02-21T16:16:08.220524Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = my_model.fit(train_it, \n                       validation_data=test_it, \n                       epochs=10, # เพิ่ม epochs\n                       verbose=1, \n                       callbacks=[PlotLossesKeras()])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T16:36:29.388564Z","iopub.execute_input":"2025-02-21T16:36:29.388964Z","iopub.status.idle":"2025-02-21T17:26:06.171754Z","shell.execute_reply.started":"2025-02-21T16:36:29.388934Z","shell.execute_reply":"2025-02-21T17:26:06.170821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\nimport matplotlib.pyplot as plt\nfrom keras.preprocessing.image import load_img, img_to_array\nfrom os.path import exists\n\n\ndef load_image(filename):\n    img = load_img(filename, target_size=(224, 224))\n    img = img_to_array(img)\n    img = img.reshape(1, 224, 224, 3)\n    img = img.astype('float32')\n    img = img - [123.68, 116.779, 103.939]  \n    return img\n\n\ndef predict_images(model, image_paths):\n    predictions = []\n    for image_path in image_paths:\n        img = load_image(image_path)\n        prediction = model.predict(img)\n        predictions.append(prediction)\n    return predictions\n\n\ndog_images = random.sample(os.listdir('dataset_dogs_vs_cats/test/dogs/'), 5)\ncat_images = random.sample(os.listdir('dataset_dogs_vs_cats/test/cats/'), 5)\n\n\ndog_image_paths = [f'dataset_dogs_vs_cats/test/dogs/{img}' for img in dog_images]\ncat_image_paths = [f'dataset_dogs_vs_cats/test/cats/{img}' for img in cat_images]\n\n\nimage_paths = dog_image_paths + cat_image_paths\n\n\npredictions = predict_images(my_model, image_paths)\n\n\nplt.figure(figsize=(15, 6)) \nfor i, prediction in enumerate(predictions):\n    plt.subplot(2, 5, i+1)\n    img_path = image_paths[i]\n    img = load_img(img_path)\n    plt.imshow(img)\n    label = \"Dog\" if prediction[0] > 0.5 else \"Cat\"\n    plt.title(f'Prediction: {label}')\n    plt.axis('off') \n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T17:32:17.3364Z","iopub.execute_input":"2025-02-21T17:32:17.337186Z","iopub.status.idle":"2025-02-21T17:32:19.087214Z","shell.execute_reply.started":"2025-02-21T17:32:17.337157Z","shell.execute_reply":"2025-02-21T17:32:19.0864Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model = VGG16(include_top=False, input_shape=(224, 224, 3))\nfor layer in base_model.layers:\n    layer.trainable = False\n\nflat = Flatten()(base_model.output)\ndense = Dense(128, activation='relu')(flat)\noutput = Dense(1, activation='sigmoid')(dense)\n\nmodel_vgg16 = Model(inputs=base_model.inputs, outputs=output)\nmodel_vgg16.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T17:33:21.760612Z","iopub.execute_input":"2025-02-21T17:33:21.761371Z","iopub.status.idle":"2025-02-21T17:33:22.117978Z","shell.execute_reply.started":"2025-02-21T17:33:21.761338Z","shell.execute_reply":"2025-02-21T17:33:22.117289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications import VGG19\nbase_model_vgg19 = VGG19(include_top=False, input_shape=(224, 224, 3))\nfor layer in base_model_vgg19.layers:\n    layer.trainable = False\n\nflat_vgg19 = Flatten()(base_model_vgg19.output)\ndense_vgg19 = Dense(128, activation='relu')(flat_vgg19)\noutput_vgg19 = Dense(1, activation='sigmoid')(dense_vgg19)\n\nmodel_vgg19 = Model(inputs=base_model_vgg19.inputs, outputs=output_vgg19)\nmodel_vgg19.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T17:34:05.897453Z","iopub.execute_input":"2025-02-21T17:34:05.897802Z","iopub.status.idle":"2025-02-21T17:34:06.82382Z","shell.execute_reply.started":"2025-02-21T17:34:05.897747Z","shell.execute_reply":"2025-02-21T17:34:06.823131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications import ResNet50\n\nbase_model_resnet = ResNet50(include_top=False, input_shape=(224, 224, 3))\nfor layer in base_model_resnet.layers:\n    layer.trainable = False\n\nflat_resnet = Flatten()(base_model_resnet.output)\ndense_resnet = Dense(128, activation='relu')(flat_resnet)\noutput_resnet = Dense(1, activation='sigmoid')(dense_resnet)\n\nmodel_resnet = Model(inputs=base_model_resnet.inputs, outputs=output_resnet)\nmodel_resnet.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T17:36:47.505846Z","iopub.execute_input":"2025-02-21T17:36:47.506143Z","iopub.status.idle":"2025-02-21T17:36:49.60773Z","shell.execute_reply.started":"2025-02-21T17:36:47.506121Z","shell.execute_reply":"2025-02-21T17:36:49.606815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.applications import InceptionV3\nbase_model_inception = InceptionV3(include_top=False, input_shape=(224, 224, 3))\nfor layer in base_model_inception.layers:\n    layer.trainable = False\n\nflat_inception = Flatten()(base_model_inception.output)\ndense_inception = Dense(128, activation='relu')(flat_inception)\noutput_inception = Dense(1, activation='sigmoid')(dense_inception)\n\nmodel_inception = Model(inputs=base_model_inception.inputs, outputs=output_inception)\nmodel_inception.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T17:36:05.187971Z","iopub.execute_input":"2025-02-21T17:36:05.188736Z","iopub.status.idle":"2025-02-21T17:36:07.986735Z","shell.execute_reply.started":"2025-02-21T17:36:05.188708Z","shell.execute_reply":"2025-02-21T17:36:07.986077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"base_model_xception = Xception(include_top=False, input_shape=(224, 224, 3))\nfor layer in base_model_xception.layers:\n    layer.trainable = False\n\nflat_xception = Flatten()(base_model_xception.output)\ndense_xception = Dense(128, activation='relu')(flat_xception)\noutput_xception = Dense(1, activation='sigmoid')(dense_xception)\n\nmodel_xception = Model(inputs=base_model_xception.inputs, outputs=output_xception)\nmodel_xception.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='binary_crossentropy', metrics=['accuracy'])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T17:38:35.189354Z","iopub.execute_input":"2025-02-21T17:38:35.190072Z","iopub.status.idle":"2025-02-21T17:38:36.482735Z","shell.execute_reply.started":"2025-02-21T17:38:35.190039Z","shell.execute_reply":"2025-02-21T17:38:36.481817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_vgg16 = model_vgg16.fit(train_it, validation_data=test_it, epochs=10, verbose=1, callbacks=[PlotLossesKeras()])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_vgg19 = model_vgg19.fit(train_it, validation_data=test_it, epochs=10, verbose=1, callbacks=[PlotLossesKeras()])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T18:10:32.259818Z","iopub.status.idle":"2025-02-21T18:10:32.260297Z","shell.execute_reply.started":"2025-02-21T18:10:32.260065Z","shell.execute_reply":"2025-02-21T18:10:32.260088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_resnet = model_resnet.fit(train_it, validation_data=test_it, epochs=10, verbose=1, callbacks=[PlotLossesKeras()])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T18:10:32.261451Z","iopub.status.idle":"2025-02-21T18:10:32.261832Z","shell.execute_reply.started":"2025-02-21T18:10:32.261622Z","shell.execute_reply":"2025-02-21T18:10:32.261638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_inception = model_inception.fit(train_it, validation_data=test_it, epochs=10, verbose=1, callbacks=[PlotLossesKeras()])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T18:10:32.26324Z","iopub.status.idle":"2025-02-21T18:10:32.263941Z","shell.execute_reply.started":"2025-02-21T18:10:32.263669Z","shell.execute_reply":"2025-02-21T18:10:32.263696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history_xception = model_xception.fit(train_it, validation_data=test_it, epochs=10, verbose=1, callbacks=[PlotLossesKeras()])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-21T18:10:32.265112Z","iopub.status.idle":"2025-02-21T18:10:32.265493Z","shell.execute_reply.started":"2025-02-21T18:10:32.265306Z","shell.execute_reply":"2025-02-21T18:10:32.265324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}