{"metadata":{"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true},"kernelspec":{"display_name":"Python 3","language":"python","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"},"papermill":{"default_parameters":{},"duration":685.729059,"end_time":"2024-01-30T06:55:19.558838","environment_variables":{},"exception":true,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-01-30T06:43:53.829779","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.676787,"end_time":"2024-01-30T06:43:57.776613","exception":false,"start_time":"2024-01-30T06:43:57.099826","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:17.083597Z","iopub.execute_input":"2024-02-03T14:34:17.084089Z","iopub.status.idle":"2024-02-03T14:34:17.095035Z","shell.execute_reply.started":"2024-02-03T14:34:17.084060Z","shell.execute_reply":"2024-02-03T14:34:17.094123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"papermill":{"duration":14.051136,"end_time":"2024-01-30T06:44:11.835631","exception":false,"start_time":"2024-01-30T06:43:57.784495","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:17.096951Z","iopub.execute_input":"2024-02-03T14:34:17.097224Z","iopub.status.idle":"2024-02-03T14:34:27.743231Z","shell.execute_reply.started":"2024-02-03T14:34:17.097201Z","shell.execute_reply":"2024-02-03T14:34:27.742176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"papermill":{"duration":1.208913,"end_time":"2024-01-30T06:44:13.067949","exception":false,"start_time":"2024-01-30T06:44:11.859036","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:27.744528Z","iopub.execute_input":"2024-02-03T14:34:27.744905Z","iopub.status.idle":"2024-02-03T14:34:28.747603Z","shell.execute_reply.started":"2024-02-03T14:34:27.744871Z","shell.execute_reply":"2024-02-03T14:34:28.746648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"papermill":{"duration":1.150551,"end_time":"2024-01-30T06:44:14.230014","exception":false,"start_time":"2024-01-30T06:44:13.079463","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:28.748861Z","iopub.execute_input":"2024-02-03T14:34:28.749151Z","iopub.status.idle":"2024-02-03T14:34:29.749629Z","shell.execute_reply.started":"2024-02-03T14:34:28.749126Z","shell.execute_reply":"2024-02-03T14:34:29.748694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"papermill":{"duration":10.851838,"end_time":"2024-01-30T06:44:25.096814","exception":false,"start_time":"2024-01-30T06:44:14.244976","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:29.752960Z","iopub.execute_input":"2024-02-03T14:34:29.753352Z","iopub.status.idle":"2024-02-03T14:34:29.758486Z","shell.execute_reply.started":"2024-02-03T14:34:29.753314Z","shell.execute_reply":"2024-02-03T14:34:29.757517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random","metadata":{"papermill":{"duration":0.023736,"end_time":"2024-01-30T06:44:25.195409","exception":false,"start_time":"2024-01-30T06:44:25.171673","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:29.759703Z","iopub.execute_input":"2024-02-03T14:34:29.760051Z","iopub.status.idle":"2024-02-03T14:34:29.769384Z","shell.execute_reply.started":"2024-02-03T14:34:29.760021Z","shell.execute_reply":"2024-02-03T14:34:29.768531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create directories\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\nfor subdir in subdirs:\n    labeldirs = ['dogs/', 'cats/']\n    for labldir in labeldirs:\n        newdir = dataset_home + subdir + labldir\n        makedirs(newdir, exist_ok=True)","metadata":{"papermill":{"duration":0.022271,"end_time":"2024-01-30T06:44:25.232052","exception":false,"start_time":"2024-01-30T06:44:25.209781","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:29.770544Z","iopub.execute_input":"2024-02-03T14:34:29.770997Z","iopub.status.idle":"2024-02-03T14:34:29.780610Z","shell.execute_reply.started":"2024-02-03T14:34:29.770973Z","shell.execute_reply":"2024-02-03T14:34:29.779761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    src = src_directory + '/' + file\n    dst_dir = 'train/'\n    if random() < val_ratio:\n        dst_dir = 'test/'\n    if file.startswith('cat'):\n        dst = dataset_home + dst_dir + 'cats/' + file\n        copyfile(src, dst)\n    elif file.startswith('dog'):\n        dst = dataset_home + dst_dir + 'dogs/' + file\n        copyfile(src, dst)","metadata":{"papermill":{"duration":2.545263,"end_time":"2024-01-30T06:44:27.792152","exception":false,"start_time":"2024-01-30T06:44:25.246889","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:29.781756Z","iopub.execute_input":"2024-02-03T14:34:29.782101Z","iopub.status.idle":"2024-02-03T14:34:33.009215Z","shell.execute_reply.started":"2024-02-03T14:34:29.782069Z","shell.execute_reply":"2024-02-03T14:34:33.008148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"papermill":{"duration":1.049779,"end_time":"2024-01-30T06:44:28.858019","exception":false,"start_time":"2024-01-30T06:44:27.808240","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:33.010505Z","iopub.execute_input":"2024-02-03T14:34:33.010895Z","iopub.status.idle":"2024-02-03T14:34:35.147452Z","shell.execute_reply.started":"2024-02-03T14:34:33.010857Z","shell.execute_reply":"2024-02-03T14:34:35.146287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip -q install livelossplot","metadata":{"papermill":{"duration":13.314046,"end_time":"2024-01-30T06:44:43.241562","exception":false,"start_time":"2024-01-30T06:44:29.927516","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:35.149263Z","iopub.execute_input":"2024-02-03T14:34:35.150061Z","iopub.status.idle":"2024-02-03T14:34:47.267100Z","shell.execute_reply.started":"2024-02-03T14:34:35.150020Z","shell.execute_reply":"2024-02-03T14:34:47.265729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prepare Final Dataset","metadata":{"papermill":{"duration":0.014599,"end_time":"2024-01-30T06:44:43.317891","exception":false,"start_time":"2024-01-30T06:44:43.303292","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# save the final model to file\nfrom os import listdir\nfrom numpy import asarray, save\nfrom keras.preprocessing.image import load_img, img_to_array\nfrom os import makedirs\nfrom shutil import copyfile\nfrom random import seed, random\nfrom livelossplot import PlotLossesKeras\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense, Flatten\nfrom keras.optimizers import SGD\nfrom keras.models import load_model","metadata":{"papermill":{"duration":0.022658,"end_time":"2024-01-30T06:44:43.355231","exception":false,"start_time":"2024-01-30T06:44:43.332573","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:47.269993Z","iopub.execute_input":"2024-02-03T14:34:47.270417Z","iopub.status.idle":"2024-02-03T14:34:47.278481Z","shell.execute_reply.started":"2024-02-03T14:34:47.270373Z","shell.execute_reply":"2024-02-03T14:34:47.277562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"xcep_model = Xception(include_top=False, input_shape=(299, 299, 3))\nxcep_model.summary()","metadata":{"execution":{"iopub.execute_input":"2024-01-27T04:11:23.495217Z","iopub.status.busy":"2024-01-27T04:11:23.494572Z","iopub.status.idle":"2024-01-27T04:11:26.579883Z","shell.execute_reply":"2024-01-27T04:11:26.578335Z","shell.execute_reply.started":"2024-01-27T04:11:23.495182Z"},"papermill":{"duration":0.014351,"end_time":"2024-01-30T06:44:43.384164","exception":false,"start_time":"2024-01-30T06:44:43.369813","status":"completed"},"tags":[]}},{"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)","metadata":{"papermill":{"duration":0.96785,"end_time":"2024-01-30T06:44:44.395531","exception":false,"start_time":"2024-01-30T06:44:43.427681","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:34:47.279536Z","iopub.execute_input":"2024-02-03T14:34:47.279878Z","iopub.status.idle":"2024-02-03T14:34:47.291775Z","shell.execute_reply.started":"2024-02-03T14:34:47.279844Z","shell.execute_reply":"2024-02-03T14:34:47.291023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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":{"execution":{"iopub.status.busy":"2024-02-03T14:34:47.293056Z","iopub.execute_input":"2024-02-03T14:34:47.293391Z","iopub.status.idle":"2024-02-03T14:34:48.388932Z","shell.execute_reply.started":"2024-02-03T14:34:47.293360Z","shell.execute_reply":"2024-02-03T14:34:48.387915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG16","metadata":{}},{"cell_type":"code","source":"# Load VGG16 model\nmodelVGG16 = VGG16(\n    include_top=False,\n    input_shape=(224, 224, 3)\n)\nmodelVGG16.summary()","metadata":{"papermill":{"duration":2.214664,"end_time":"2024-01-30T06:44:46.626031","exception":false,"start_time":"2024-01-30T06:44:44.411367","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:36:34.348354Z","iopub.execute_input":"2024-02-03T14:36:34.348794Z","iopub.status.idle":"2024-02-03T14:36:34.707938Z","shell.execute_reply.started":"2024-02-03T14:36:34.348759Z","shell.execute_reply":"2024-02-03T14:36:34.705477Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Mark loaded layers as not trainable\nfor index, layer in enumerate(modelVGG16.layers[:15]):\n    print(index, layer.name, layer.output_shape)\n    layer.trainable = False\n    \nfor index, layer in enumerate(modelVGG16.layers[15:]):\n    print(index, layer.name, layer.output_shape)\n    layer.trainable = True","metadata":{"papermill":{"duration":0.02984,"end_time":"2024-01-30T06:44:46.675760","exception":false,"start_time":"2024-01-30T06:44:46.645920","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:36:38.753246Z","iopub.execute_input":"2024-02-03T14:36:38.753983Z","iopub.status.idle":"2024-02-03T14:36:38.761721Z","shell.execute_reply.started":"2024-02-03T14:36:38.753948Z","shell.execute_reply":"2024-02-03T14:36:38.760839Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelVGG16.layers[-1].output","metadata":{"papermill":{"duration":0.027915,"end_time":"2024-01-30T06:44:46.723023","exception":false,"start_time":"2024-01-30T06:44:46.695108","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:36:42.682529Z","iopub.execute_input":"2024-02-03T14:36:42.683340Z","iopub.status.idle":"2024-02-03T14:36:42.689704Z","shell.execute_reply.started":"2024-02-03T14:36:42.683304Z","shell.execute_reply":"2024-02-03T14:36:42.688672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add new classifier layers\nflat1 = Flatten()(modelVGG16.layers[-1].output)\nclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\noutput = Dense(1, activation='sigmoid')(class1)\n# Define new model\nvgg16_model = Model(inputs=modelVGG16.inputs, outputs=output)","metadata":{"papermill":{"duration":0.13734,"end_time":"2024-01-30T06:44:46.879732","exception":false,"start_time":"2024-01-30T06:44:46.742392","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-03T14:38:40.528221Z","iopub.execute_input":"2024-02-03T14:38:40.528639Z","iopub.status.idle":"2024-02-03T14:38:40.566355Z","shell.execute_reply.started":"2024-02-03T14:38:40.528609Z","shell.execute_reply":"2024-02-03T14:38:40.565452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile model\nopt = SGD(lr=0.0001, momentum=0.9)\nvgg16_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\nvgg16_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T14:38:42.220490Z","iopub.execute_input":"2024-02-03T14:38:42.221189Z","iopub.status.idle":"2024-02-03T14:38:42.281749Z","shell.execute_reply.started":"2024-02-03T14:38:42.221155Z","shell.execute_reply":"2024-02-03T14:38:42.280875Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit model\nvgg16_model.reset_states()\nhistory = vgg16_model.fit(train_it, \n                          validation_data=test_it, \n                          epochs=3, verbose=1, \n                          callbacks=[PlotLossesKeras()])\n\n\n# Evaluate model\n_, acc = vgg16_model.evaluate_generator(test_it, steps=len(test_it), verbose=1)\nprint('Accuracy > %.3f' % (acc * 100.0))","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:12:07.417362Z","iopub.execute_input":"2024-02-03T15:12:07.417998Z","iopub.status.idle":"2024-02-03T15:27:39.043907Z","shell.execute_reply.started":"2024-02-03T15:12:07.417964Z","shell.execute_reply":"2024-02-03T15:27:39.042886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model to a file\nvgg16_model.save('vgg16_model.h5')\n\n# Load the model from the file\nloaded_model = load_model('vgg16_model.h5')\n\n# Evaluate the loaded model\nloaded_model.evaluate(test_it, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:27:39.045545Z","iopub.execute_input":"2024-02-03T15:27:39.045859Z","iopub.status.idle":"2024-02-03T15:28:51.566762Z","shell.execute_reply.started":"2024-02-03T15:27:39.045832Z","shell.execute_reply":"2024-02-03T15:28:51.565773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"import random","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:28:51.568247Z","iopub.execute_input":"2024-02-03T15:28:51.568916Z","iopub.status.idle":"2024-02-03T15:28:51.573217Z","shell.execute_reply.started":"2024-02-03T15:28:51.568879Z","shell.execute_reply":"2024-02-03T15:28:51.572209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to load and prepare the image\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 / 255.0\n    return img\n\n# Function to predict the class\ndef predict_image(model, filename):\n    img = load_image(filename)\n    result = model.predict(img)\n    return result[0]\n\n# Load the VGG16 model\nvgg16_model = load_model('vgg16_model.h5')\n\n# Directory containing the images\nimage_directory = '/kaggle/working/test1/'\n\n# List all files in the directory\nimage_files = os.listdir(image_directory)\n\n# Randomly select 8 images\nselected_images = random.sample(image_files, 8)\n\n# Perform predictions for each selected image\nfor image_file in selected_images:\n    image_path = os.path.join(image_directory, image_file)\n    prediction = predict_image(vgg16_model, image_path)\n\n    # Print the prediction\n    print(f'Image: {image_file}, Prediction: {prediction[0]}')\n","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:28:51.574881Z","iopub.execute_input":"2024-02-03T15:28:51.575190Z","iopub.status.idle":"2024-02-03T15:28:53.770311Z","shell.execute_reply.started":"2024-02-03T15:28:51.575167Z","shell.execute_reply":"2024-02-03T15:28:53.769483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Xception","metadata":{}},{"cell_type":"code","source":"from keras.applications.xception import Xception\n# Load the Xception model\nbase_xception = Xception(include_top=False, \n                         input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:30:41.173723Z","iopub.execute_input":"2024-02-03T15:30:41.174097Z","iopub.status.idle":"2024-02-03T15:30:43.178656Z","shell.execute_reply.started":"2024-02-03T15:30:41.174069Z","shell.execute_reply":"2024-02-03T15:30:43.177844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_xception.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:30:44.697004Z","iopub.execute_input":"2024-02-03T15:30:44.697394Z","iopub.status.idle":"2024-02-03T15:30:44.707515Z","shell.execute_reply.started":"2024-02-03T15:30:44.697363Z","shell.execute_reply":"2024-02-03T15:30:44.706409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a new model on top of Xception\nflat1_xception = Flatten()(base_xception.layers[-1].output)\nclass1_xception = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1_xception)\noutput_xception = Dense(1, activation='sigmoid')(class1_xception)\n\nxception_model = Model(inputs=base_xception.inputs, outputs=output_xception)","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:30:46.000250Z","iopub.execute_input":"2024-02-03T15:30:46.001139Z","iopub.status.idle":"2024-02-03T15:30:46.044968Z","shell.execute_reply.started":"2024-02-03T15:30:46.001105Z","shell.execute_reply":"2024-02-03T15:30:46.044200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nopt_xception = SGD(lr=0.0001, momentum=0.9)\nxception_model.compile(optimizer=opt_xception, loss='binary_crossentropy', metrics=['accuracy'])\nxception_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:30:48.314750Z","iopub.execute_input":"2024-02-03T15:30:48.315144Z","iopub.status.idle":"2024-02-03T15:30:48.665432Z","shell.execute_reply.started":"2024-02-03T15:30:48.315113Z","shell.execute_reply":"2024-02-03T15:30:48.664519Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory_xception = xception_model.fit(train_it,\n                                      validation_data=test_it,\n                                      epochs=3, verbose=1,\n                                      callbacks=[PlotLossesKeras()])\n\n# Evaluate model\n_, acc = xception_model.evaluate_generator(test_it, steps=len(test_it), verbose=1)\nprint('Accuracy > %.3f' % (acc * 100.0))","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:30:52.378357Z","iopub.execute_input":"2024-02-03T15:30:52.378748Z","iopub.status.idle":"2024-02-03T15:46:28.050261Z","shell.execute_reply.started":"2024-02-03T15:30:52.378716Z","shell.execute_reply":"2024-02-03T15:46:28.049377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model to a file\nxception_model.save('xception_model.h5')\n# Load the model from the file\nloaded_xception_model = load_model('xception_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:46:28.051960Z","iopub.execute_input":"2024-02-03T15:46:28.052243Z","iopub.status.idle":"2024-02-03T15:46:30.496616Z","shell.execute_reply.started":"2024-02-03T15:46:28.052219Z","shell.execute_reply":"2024-02-03T15:46:30.495781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"# Function to load and prepare the image\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 / 255.0\n    return img\n\n# Function to predict the class\ndef predict_image(model, filename):\n    img = load_image(filename)\n    result = model.predict(img)\n    return result[0]\n\n# Load the VGG16 model\nxception_model = load_model('xception_model.h5')\n\n# Directory containing the images\nimage_directory = '/kaggle/working/test1/'\n\n# List all files in the directory\nimage_files = os.listdir(image_directory)\n\n# Randomly select 8 images\nselected_images = random.sample(image_files, 8)\n\n# Perform predictions for each selected image\nfor image_file in selected_images:\n    image_path = os.path.join(image_directory, image_file)\n    prediction = predict_image(xception_model, image_path)\n\n    # Print the prediction\n    print(f'Image: {image_file}, Prediction: {prediction[0]}')\n","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:46:30.497854Z","iopub.execute_input":"2024-02-03T15:46:30.498138Z","iopub.status.idle":"2024-02-03T15:46:34.465497Z","shell.execute_reply.started":"2024-02-03T15:46:30.498114Z","shell.execute_reply":"2024-02-03T15:46:34.464509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet50V2","metadata":{}},{"cell_type":"code","source":"from keras.applications.resnet_v2 import ResNet50V2\n\n# Load the ResNet50V2 model\nbase_resnet50v2 = ResNet50V2(include_top=False, \n                             input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:46:34.467613Z","iopub.execute_input":"2024-02-03T15:46:34.467973Z","iopub.status.idle":"2024-02-03T15:46:36.854818Z","shell.execute_reply.started":"2024-02-03T15:46:34.467945Z","shell.execute_reply":"2024-02-03T15:46:36.854023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_resnet50v2.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:46:36.855901Z","iopub.execute_input":"2024-02-03T15:46:36.856174Z","iopub.status.idle":"2024-02-03T15:46:36.867956Z","shell.execute_reply.started":"2024-02-03T15:46:36.856150Z","shell.execute_reply":"2024-02-03T15:46:36.867038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flat1_resnet50v2 = Flatten()(base_resnet50v2.output)\nclass1_resnet50v2 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1_resnet50v2)\noutput_resnet50v2 = Dense(1, activation='sigmoid')(class1_resnet50v2)\n\nresnet50v2_model = Model(inputs=base_resnet50v2.input, outputs=output_resnet50v2)","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:46:36.869085Z","iopub.execute_input":"2024-02-03T15:46:36.869352Z","iopub.status.idle":"2024-02-03T15:46:36.931137Z","shell.execute_reply.started":"2024-02-03T15:46:36.869329Z","shell.execute_reply":"2024-02-03T15:46:36.930415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt_resnet50v2 = SGD(lr=0.0001, momentum=0.9)\nresnet50v2_model.compile(optimizer=opt_resnet50v2, loss='binary_crossentropy', metrics=['accuracy'])\nresnet50v2_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:46:36.932183Z","iopub.execute_input":"2024-02-03T15:46:36.932461Z","iopub.status.idle":"2024-02-03T15:46:37.436424Z","shell.execute_reply.started":"2024-02-03T15:46:36.932437Z","shell.execute_reply":"2024-02-03T15:46:37.435437Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory_resnet50v2 = resnet50v2_model.fit(train_it,\n                                          validation_data=test_it,\n                                          epochs=3, verbose=1,\n                                          callbacks=[PlotLossesKeras()])\n# Evaluate model\n_, acc = resnet50v2_model.evaluate_generator(test_it, steps=len(test_it), verbose=1)\nprint('Accuracy > %.3f' % (acc * 100.0))","metadata":{"execution":{"iopub.status.busy":"2024-02-03T15:46:37.437615Z","iopub.execute_input":"2024-02-03T15:46:37.437887Z","iopub.status.idle":"2024-02-03T16:01:51.675629Z","shell.execute_reply.started":"2024-02-03T15:46:37.437864Z","shell.execute_reply":"2024-02-03T16:01:51.674481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model to a file\nresnet50v2_model.save('resnet50v2_model.h5')\n# Load the model from the file\nloaded_resnet50v2_model = load_model('resnet50v2_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-02-03T16:01:51.676950Z","iopub.execute_input":"2024-02-03T16:01:51.677241Z","iopub.status.idle":"2024-02-03T16:01:54.610372Z","shell.execute_reply.started":"2024-02-03T16:01:51.677215Z","shell.execute_reply":"2024-02-03T16:01:54.609471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"# Function to load and prepare the image\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 / 255.0\n    return img\n\n# Function to predict the class\ndef predict_image(model, filename):\n    img = load_image(filename)\n    result = model.predict(img)\n    return result[0]\n\n# Load the VGG16 model\nresnet50v2_model = load_model('resnet50v2_model.h5')\n\n# Directory containing the images\nimage_directory = '/kaggle/working/test1/'\n\n# List all files in the directory\nimage_files = os.listdir(image_directory)\n\n# Randomly select 8 images\nselected_images = random.sample(image_files, 8)\n\n# Perform predictions for each selected image\nfor image_file in selected_images:\n    image_path = os.path.join(image_directory, image_file)\n    prediction = predict_image(resnet50v2_model, image_path)\n\n    # Print the prediction\n    print(f'Image: {image_file}, Prediction: {prediction[0]}')\n","metadata":{"execution":{"iopub.status.busy":"2024-02-03T16:01:54.615569Z","iopub.execute_input":"2024-02-03T16:01:54.616348Z","iopub.status.idle":"2024-02-03T16:01:59.872278Z","shell.execute_reply.started":"2024-02-03T16:01:54.616316Z","shell.execute_reply":"2024-02-03T16:01:59.871299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EfficientNetB1","metadata":{}},{"cell_type":"code","source":"from keras.applications import EfficientNetB1\n\n# Load the EfficientNetB1 model\nbase_efficientnetb1 = EfficientNetB1(include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2024-02-03T16:01:59.873504Z","iopub.execute_input":"2024-02-03T16:01:59.873774Z","iopub.status.idle":"2024-02-03T16:02:03.307762Z","shell.execute_reply.started":"2024-02-03T16:01:59.873750Z","shell.execute_reply":"2024-02-03T16:02:03.306958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_efficientnetb1.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-03T16:02:03.309242Z","iopub.execute_input":"2024-02-03T16:02:03.309692Z","iopub.status.idle":"2024-02-03T16:02:03.326876Z","shell.execute_reply.started":"2024-02-03T16:02:03.309657Z","shell.execute_reply":"2024-02-03T16:02:03.325888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flat1_efficientnetb1 = Flatten()(base_efficientnetb1.output)\nclass1_efficientnetb1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1_efficientnetb1)\noutput_efficientnetb1 = Dense(1, activation='sigmoid')(class1_efficientnetb1)\n\nefficientnetb1_model = Model(inputs=base_efficientnetb1.input, outputs=output_efficientnetb1)","metadata":{"execution":{"iopub.status.busy":"2024-02-03T16:02:03.328071Z","iopub.execute_input":"2024-02-03T16:02:03.328345Z","iopub.status.idle":"2024-02-03T16:02:03.387726Z","shell.execute_reply.started":"2024-02-03T16:02:03.328322Z","shell.execute_reply":"2024-02-03T16:02:03.387058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nopt_efficientnetb1 = SGD(lr=0.0001, momentum=0.9)\nefficientnetb1_model.compile(optimizer=opt_efficientnetb1, loss='binary_crossentropy', metrics=['accuracy'])\n\n# Print the model summary\nefficientnetb1_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T16:02:03.388644Z","iopub.execute_input":"2024-02-03T16:02:03.388891Z","iopub.status.idle":"2024-02-03T16:02:04.229300Z","shell.execute_reply.started":"2024-02-03T16:02:03.388870Z","shell.execute_reply":"2024-02-03T16:02:04.228354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory_efficientnetb1 = efficientnetb1_model.fit(train_it, \n                                                  validation_data=test_it, \n                                                  epochs=10, verbose=1,\n                                                  callbacks=[PlotLossesKeras()])\n# Evaluate model\n_, acc = efficientnetb1_model.evaluate_generator(test_it, steps=len(test_it), verbose=1)\nprint('Accuracy > %.3f' % (acc * 100.0))","metadata":{"execution":{"iopub.status.busy":"2024-02-03T16:02:04.230725Z","iopub.execute_input":"2024-02-03T16:02:04.231053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model to a file\nefficientnetb1_model.save('efficientnetb1_model.h5')\n# Load the model from the file\nloaded_efficientnetb1_model = load_model('efficientnetb1_model.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"# Function to load and prepare the image\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 / 255.0\n    return img\n\n# Function to predict the class\ndef predict_image(model, filename):\n    img = load_image(filename)\n    result = model.predict(img)\n    return result[0]\n\n# Load the VGG16 model\nefficientnetb1_model = load_model('efficientnetb1_model.h5')\n\n# Directory containing the images\nimage_directory = '/kaggle/working/test1/'\n\n# List all files in the directory\nimage_files = os.listdir(image_directory)\n\n# Randomly select 8 images\nselected_images = random.sample(image_files, 8)\n\n# Perform predictions for each selected image\nfor image_file in selected_images:\n    image_path = os.path.join(image_directory, image_file)\n    prediction = predict_image(efficientnetb1_model, image_path)\n\n    # Print the prediction\n    print(f'Image: {image_file}, Prediction: {prediction[0]}')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DenseNet169","metadata":{}},{"cell_type":"code","source":"from keras.applications import DenseNet169\n\n# Load the DenseNet169 model\nbase_densenet169 = DenseNet169(include_top=False, input_shape=(224, 224, 3))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_densenet169.layers:\n    layer.trainable = False","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flat1_densenet169 = Flatten()(base_densenet169.output)\nclass1_densenet169 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1_densenet169)\noutput_densenet169 = Dense(1, activation='sigmoid')(class1_densenet169)\n\ndensenet169_model = Model(inputs=base_densenet169.input, outputs=output_densenet169)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt_densenet169 = SGD(lr=0.0001, momentum=0.9)\ndensenet169_model.compile(optimizer=opt_densenet169, loss='binary_crossentropy', metrics=['accuracy'])\ndensenet169_model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit the model\nhistory_densenet169 = densenet169_model.fit(train_it,\n                                            validation_data=test_it,\n                                            epochs=10, verbose=1,\n                                            callbacks=[PlotLossesKeras()])\n# Evaluate model\n_, acc = densenet169_model.evaluate_generator(test_it, steps=len(test_it), verbose=1)\nprint('Accuracy > %.3f' % (acc * 100.0))","metadata":{"execution":{"iopub.status.idle":"2024-02-03T17:41:39.704835Z","shell.execute_reply.started":"2024-02-03T16:51:20.026469Z","shell.execute_reply":"2024-02-03T17:41:39.703839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model to a file\ndensenet169_model.save('densenet169_model.h5')\n\n# Load the model from the file\nloaded_densenet169_model = load_model('densenet169_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-02-03T17:41:39.706245Z","iopub.execute_input":"2024-02-03T17:41:39.706571Z","iopub.status.idle":"2024-02-03T17:41:46.450269Z","shell.execute_reply.started":"2024-02-03T17:41:39.706544Z","shell.execute_reply":"2024-02-03T17:41:46.449176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"cell_type":"code","source":"# Function to load and prepare the image\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 / 255.0\n    return img\n\n# Function to predict the class\ndef predict_image(model, filename):\n    img = load_image(filename)\n    result = model.predict(img)\n    return result[0]\n\n# Load the VGG16 model\ndensenet169_model = load_model('densenet169_model.h5')\n\n# Directory containing the images\nimage_directory = '/kaggle/working/test1/'\n\n# List all files in the directory\nimage_files = os.listdir(image_directory)\n\n# Randomly select 8 images\nselected_images = random.sample(image_files, 8)\n\n# Perform predictions for each selected image\nfor image_file in selected_images:\n    image_path = os.path.join(image_directory, image_file)\n    prediction = predict_image(densenet169_model, image_path)\n\n    # Print the prediction\n    print(f'Image: {image_file}, Prediction: {prediction[0]}')","metadata":{"execution":{"iopub.status.busy":"2024-02-03T17:41:46.451962Z","iopub.execute_input":"2024-02-03T17:41:46.452261Z","iopub.status.idle":"2024-02-03T17:41:58.766089Z","shell.execute_reply.started":"2024-02-03T17:41:46.452236Z","shell.execute_reply":"2024-02-03T17:41:58.765171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}