{"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":"gpu","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","execution":{"iopub.status.busy":"2024-02-04T14:13:27.069443Z","iopub.execute_input":"2024-02-04T14:13:27.069812Z","iopub.status.idle":"2024-02-04T14:13:27.077035Z","shell.execute_reply.started":"2024-02-04T14:13:27.069779Z","shell.execute_reply":"2024-02-04T14:13:27.076026Z"},"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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:28.572170Z","iopub.execute_input":"2024-02-04T14:13:28.572597Z","iopub.status.idle":"2024-02-04T14:13:44.182170Z","shell.execute_reply.started":"2024-02-04T14:13:28.572563Z","shell.execute_reply":"2024-02-04T14:13:44.181343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:44.183644Z","iopub.execute_input":"2024-02-04T14:13:44.183929Z","iopub.status.idle":"2024-02-04T14:13:45.332130Z","shell.execute_reply.started":"2024-02-04T14:13:44.183904Z","shell.execute_reply":"2024-02-04T14:13:45.331138Z"},"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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:45.333255Z","iopub.execute_input":"2024-02-04T14:13:45.333563Z","iopub.status.idle":"2024-02-04T14:13:46.437238Z","shell.execute_reply.started":"2024-02-04T14:13:45.333536Z","shell.execute_reply":"2024-02-04T14:13:46.436267Z"},"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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:46.439478Z","iopub.execute_input":"2024-02-04T14:13:46.439771Z","iopub.status.idle":"2024-02-04T14:13:46.444795Z","shell.execute_reply.started":"2024-02-04T14:13:46.439745Z","shell.execute_reply":"2024-02-04T14:13:46.443694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:46.445869Z","iopub.execute_input":"2024-02-04T14:13:46.446155Z","iopub.status.idle":"2024-02-04T14:13:46.455931Z","shell.execute_reply.started":"2024-02-04T14:13:46.446112Z","shell.execute_reply":"2024-02-04T14:13:46.455051Z"},"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\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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:46.457052Z","iopub.execute_input":"2024-02-04T14:13:46.457353Z","iopub.status.idle":"2024-02-04T14:13:46.465703Z","shell.execute_reply.started":"2024-02-04T14:13:46.457319Z","shell.execute_reply":"2024-02-04T14:13:46.464842Z"},"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\tsrc = src_directory + '/' + file\n\tdst_dir = 'train/'\n\tif random() < val_ratio:\n\t\tdst_dir = 'test/'\n\tif file.startswith('cat'):\n\t\tdst = dataset_home + dst_dir + 'cats/'  + file\n\t\tcopyfile(src, dst)\n\telif file.startswith('dog'):\n\t\tdst = dataset_home + dst_dir + 'dogs/'  + file\n\t\tcopyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:13:46.466763Z","iopub.execute_input":"2024-02-04T14:13:46.468989Z","iopub.status.idle":"2024-02-04T14:13:48.971188Z","shell.execute_reply.started":"2024-02-04T14:13:46.468954Z","shell.execute_reply":"2024-02-04T14:13:48.970122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l dataset_dogs_vs_cats/train/dogs | wc -l","metadata":{"execution":{"iopub.status.busy":"2024-02-04T07:59:56.009079Z","iopub.execute_input":"2024-02-04T07:59:56.009359Z","iopub.status.idle":"2024-02-04T07:59:57.035685Z","shell.execute_reply.started":"2024-02-04T07:59:56.009333Z","shell.execute_reply":"2024-02-04T07:59:57.034726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls -l dataset_dogs_vs_cats/train/cats | wc -l","metadata":{"execution":{"iopub.status.busy":"2024-02-04T07:59:57.039282Z","iopub.execute_input":"2024-02-04T07:59:57.039602Z","iopub.status.idle":"2024-02-04T07:59:58.079042Z","shell.execute_reply.started":"2024-02-04T07:59:57.039572Z","shell.execute_reply":"2024-02-04T07:59:58.077905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip -q install livelossplot==0.5.5","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:14:23.671905Z","iopub.execute_input":"2024-02-04T14:14:23.672302Z","iopub.status.idle":"2024-02-04T14:14:36.429071Z","shell.execute_reply.started":"2024-02-04T14:14:23.672263Z","shell.execute_reply":"2024-02-04T14:14:36.427660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:14:36.431128Z","iopub.execute_input":"2024-02-04T14:14:36.431468Z","iopub.status.idle":"2024-02-04T14:14:36.446396Z","shell.execute_reply.started":"2024-02-04T14:14:36.431437Z","shell.execute_reply":"2024-02-04T14:14:36.445580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 tensorflow.keras.applications import VGG19\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.applications import DenseNet121\nfrom tensorflow.keras.applications import MobileNetV2\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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:48.972797Z","iopub.execute_input":"2024-02-04T14:13:48.973098Z","iopub.status.idle":"2024-02-04T14:13:49.075574Z","shell.execute_reply.started":"2024-02-04T14:13:48.973073Z","shell.execute_reply":"2024-02-04T14:13:49.074824Z"},"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.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},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:49.076623Z","iopub.execute_input":"2024-02-04T14:13:49.076909Z","iopub.status.idle":"2024-02-04T14:13:50.128594Z","shell.execute_reply.started":"2024-02-04T14:13:49.076883Z","shell.execute_reply":"2024-02-04T14:13:50.127739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\nvgg16_model = VGG16(\n    include_top=False,\n    input_shape=(224, 224, 3)\n    )\nprint(vgg16_model.summary())\n#model = Xception(include_top=False, input_shape=(299, 299, 3))\n\n","metadata":{"execution":{"iopub.status.busy":"2024-02-04T08:00:13.354488Z","iopub.execute_input":"2024-02-04T08:00:13.354798Z","iopub.status.idle":"2024-02-04T08:00:15.111177Z","shell.execute_reply.started":"2024-02-04T08:00:13.354770Z","shell.execute_reply":"2024-02-04T08:00:15.110088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mark loaded layers as not trainable\nfor index, layer in enumerate(vgg16_model.layers[:]):\n    #print(index, layer.name, layer.output_shape)\n    layer.trainable = False\n    \n#for index, layer in enumerate(model.layers[15:]):\n#    print(index, layer.name, layer.output_shape)\n#   layer.trainable = True","metadata":{"execution":{"iopub.status.busy":"2024-02-04T08:00:15.112674Z","iopub.execute_input":"2024-02-04T08:00:15.113027Z","iopub.status.idle":"2024-02-04T08:00:15.119383Z","shell.execute_reply.started":"2024-02-04T08:00:15.112991Z","shell.execute_reply":"2024-02-04T08:00:15.118488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16_model.layers[-1].output","metadata":{"execution":{"iopub.status.busy":"2024-02-04T08:00:15.120716Z","iopub.execute_input":"2024-02-04T08:00:15.121049Z","iopub.status.idle":"2024-02-04T08:00:15.135403Z","shell.execute_reply.started":"2024-02-04T08:00:15.121017Z","shell.execute_reply":"2024-02-04T08:00:15.134530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add new classifier layers\nflat1 = Flatten()(vgg16_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=vgg16_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":{"execution":{"iopub.status.busy":"2024-02-04T08:00:15.136655Z","iopub.execute_input":"2024-02-04T08:00:15.137217Z","iopub.status.idle":"2024-02-04T08:00:15.249800Z","shell.execute_reply.started":"2024-02-04T08:00:15.137183Z","shell.execute_reply":"2024-02-04T08:00:15.248924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=10, verbose=1,\n                    callbacks=[PlotLossesKeras()])\n# evaluate model\n_, acc = my_model.evaluate_generator(test_it, steps=len(test_it), verbose=1)\nprint('> %.3f' % (acc * 100.0))\n# learning curves\n\nbest_model = 'vgg16_model.hdf5'\nmy_model.save(best_model)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-03T05:32:44.130393Z","iopub.execute_input":"2024-02-03T05:32:44.130681Z","iopub.status.idle":"2024-02-03T05:33:01.628355Z","shell.execute_reply.started":"2024-02-03T05:32:44.130655Z","shell.execute_reply":"2024-02-03T05:33:01.626873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2024-02-03T05:33:01.629406Z","iopub.status.idle":"2024-02-03T05:33:01.629790Z","shell.execute_reply.started":"2024-02-03T05:33:01.629615Z","shell.execute_reply":"2024-02-03T05:33:01.629632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = load_model(best_model)\nbest_model.evaluate(test_it, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-02-02T15:16:08.699836Z","iopub.execute_input":"2024-02-02T15:16:08.700700Z","iopub.status.idle":"2024-02-02T15:17:24.950088Z","shell.execute_reply.started":"2024-02-02T15:16:08.700665Z","shell.execute_reply":"2024-02-02T15:17:24.949159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Make Prediction","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:50.130859Z","iopub.execute_input":"2024-02-04T14:13:50.131156Z","iopub.status.idle":"2024-02-04T14:13:50.135992Z","shell.execute_reply.started":"2024-02-04T14:13:50.131130Z","shell.execute_reply":"2024-02-04T14:13:50.134944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# load and prepare the image\ndef load_image(filename):\n\t# load the image\n\timg = load_img(filename, target_size=(224, 224))\n\t# convert to array\n\timg = img_to_array(img)\n\t# reshape into a single sample with 3 channels\n\timg = img.reshape(1, 224, 224, 3)\n\t# center pixel data\n\timg = img.astype('float32')\n\timg = img - [123.68, 116.779, 103.939]\n\treturn img\n\n# load an image and predict the class\ndef run_example():\n\t# load the image\n\timg = load_image('/kaggle/working/test1/166.jpg')\n\t# load model\n\tmodel = load_model('vgg16_model.hdf5')\n\t# predict the class\n\tresult = model.predict(img)\n\tprint(result[0])\n\n# entry point, run the example\nrun_example()","metadata":{"execution":{"iopub.status.busy":"2024-02-03T06:20:07.085881Z","iopub.execute_input":"2024-02-03T06:20:07.086303Z","iopub.status.idle":"2024-02-03T06:20:07.224077Z","shell.execute_reply.started":"2024-02-03T06:20:07.086270Z","shell.execute_reply":"2024-02-03T06:20:07.222899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Xception","metadata":{}},{"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=(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":{"execution":{"iopub.status.busy":"2024-02-04T14:13:52.654193Z","iopub.execute_input":"2024-02-04T14:13:52.654578Z","iopub.status.idle":"2024-02-04T14:13:53.769886Z","shell.execute_reply.started":"2024-02-04T14:13:52.654547Z","shell.execute_reply":"2024-02-04T14:13:53.768926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\nxcep_model = Xception(\n    include_top=False,\n    input_shape=(299, 299, 3)\n    )\n#print(model.summary())","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:13:55.672443Z","iopub.execute_input":"2024-02-04T14:13:55.672856Z","iopub.status.idle":"2024-02-04T14:13:58.643065Z","shell.execute_reply.started":"2024-02-04T14:13:55.672824Z","shell.execute_reply":"2024-02-04T14:13:58.642168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mark loaded layers as not trainable\nfor index, layer in enumerate(xcep_model.layers[:]):\n    #print(index, layer.name, layer.output_shape)\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:13:58.644785Z","iopub.execute_input":"2024-02-04T14:13:58.645086Z","iopub.status.idle":"2024-02-04T14:13:58.654605Z","shell.execute_reply.started":"2024-02-04T14:13:58.645061Z","shell.execute_reply":"2024-02-04T14:13:58.653704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xcep_model.layers[-1].output","metadata":{"execution":{"iopub.status.busy":"2024-02-03T05:33:11.066974Z","iopub.execute_input":"2024-02-03T05:33:11.067208Z","iopub.status.idle":"2024-02-03T05:33:11.080282Z","shell.execute_reply.started":"2024-02-03T05:33:11.067186Z","shell.execute_reply":"2024-02-03T05:33:11.079532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Dense, Flatten\n\n# Assuming the output shape of the preceding layer is (None, height, width, channels)\npreceding_layer_output = xcep_model.layers[-1].output\nflat1 = Flatten()(preceding_layer_output)  # Flatten the output\n\n# Adjust input size as needed for the Dense layer\nclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\noutput = Dense(1, activation='sigmoid')(class1)\n\n# define new model\nxcep_model = Model(inputs=xcep_model.inputs, outputs=output)\n\n# compile model\nopt = SGD(lr=0.0001, momentum=0.9)\nxcep_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\nxcep_model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:13:58.655883Z","iopub.execute_input":"2024-02-04T14:13:58.656198Z","iopub.status.idle":"2024-02-04T14:13:59.089890Z","shell.execute_reply.started":"2024-02-04T14:13:58.656174Z","shell.execute_reply":"2024-02-04T14:13:59.088964Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Assuming the output shape of the preceding layer is (None, height, width, channels)\n# preceding_layer_output = my_model.layers[-1].output\n# flat1 = Flatten()(preceding_layer_output)  # Flatten the output\n\n# # Adjust input size as needed for the Dense layer\n# class1 = Dense(64, activation='relu', kernel_initializer='he_uniform')(flat1)\n# output = Dense(1, activation='sigmoid')(class1)\n\n# # Define new model\n# updated_model = Model(inputs=my_model.inputs, outputs=output)\n\n# # Compile model\n# opt = SGD(lr=0.0001, momentum=0.9)\n# updated_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n# updated_model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-03T05:38:51.809327Z","iopub.execute_input":"2024-02-03T05:38:51.809705Z","iopub.status.idle":"2024-02-03T05:38:51.900432Z","shell.execute_reply.started":"2024-02-03T05:38:51.809672Z","shell.execute_reply":"2024-02-03T05:38:51.899409Z"},"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=(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":{"execution":{"iopub.status.busy":"2024-02-04T14:14:01.221868Z","iopub.execute_input":"2024-02-04T14:14:01.222238Z","iopub.status.idle":"2024-02-04T14:14:02.377600Z","shell.execute_reply.started":"2024-02-04T14:14:01.222205Z","shell.execute_reply":"2024-02-04T14:14:02.376688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#updated_model.reset_states()\nhistory = xcep_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()])\n","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:14:36.447535Z","iopub.execute_input":"2024-02-04T14:14:36.447834Z","iopub.status.idle":"2024-02-04T14:31:39.086205Z","shell.execute_reply.started":"2024-02-04T14:14:36.447808Z","shell.execute_reply":"2024-02-04T14:31:39.085247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate model\n_, acc = xcep_model.evaluate(test_it, steps=len(test_it), verbose=1,)\nprint('> %.3f' % (acc * 100.0))\n\nbest_model = 'Xception_model.hdf5'\nxcep_model.save(best_model)","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:36:30.057231Z","iopub.execute_input":"2024-02-04T14:36:30.057671Z","iopub.status.idle":"2024-02-04T14:38:41.330657Z","shell.execute_reply.started":"2024-02-04T14:36:30.057635Z","shell.execute_reply":"2024-02-04T14:38:41.329787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# load and prepare the image\ndef load_image(filename):\n\t# load the image\n\timg = load_img(filename, target_size=(299, 299))\n\t# convert to array\n\timg = img_to_array(img)\n\t# reshape into a single sample with 3 channels\n\timg = img.reshape(1, 299, 299, 3)\n\t# center pixel data\n\timg = img.astype('float32')\n\timg = img - [123.68, 116.779, 103.939]\n\treturn img\n\n# load an image and predict the class\ndef run_example():\n\t# load the image\n\timg = load_image('/kaggle/working/test1/166.jpg')\n\t# load model\n\tmodel = load_model('Xception_model.hdf5')\n\t# predict the class\n\tresult = model.predict(img)\n\tprint(result[0])\n\n# entry point, run the example\nrun_example()","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:40:09.810412Z","iopub.execute_input":"2024-02-04T14:40:09.810801Z","iopub.status.idle":"2024-02-04T14:40:13.331817Z","shell.execute_reply.started":"2024-02-04T14:40:09.810763Z","shell.execute_reply":"2024-02-04T14:40:13.330856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/working/test1/166.jpg'\nimg_test = imread(path)\npyplot.imshow(img_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:43:22.498600Z","iopub.execute_input":"2024-02-04T14:43:22.499319Z","iopub.status.idle":"2024-02-04T14:43:22.908692Z","shell.execute_reply.started":"2024-02-04T14:43:22.499277Z","shell.execute_reply":"2024-02-04T14:43:22.907874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG19","metadata":{}},{"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-04T14:48:08.154573Z","iopub.execute_input":"2024-02-04T14:48:08.154990Z","iopub.status.idle":"2024-02-04T14:48:09.346914Z","shell.execute_reply.started":"2024-02-04T14:48:08.154958Z","shell.execute_reply":"2024-02-04T14:48:09.345959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\nvgg19_model = VGG19(\n    include_top=False,\n    input_shape=(224, 224, 3)\n    )\n#print(model.summary())","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:48:11.472573Z","iopub.execute_input":"2024-02-04T14:48:11.472983Z","iopub.status.idle":"2024-02-04T14:48:12.546532Z","shell.execute_reply.started":"2024-02-04T14:48:11.472948Z","shell.execute_reply":"2024-02-04T14:48:12.545565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mark loaded layers as not trainable\nfor index, layer in enumerate(vgg19_model.layers[:]):\n    #print(index, layer.name, layer.output_shape)\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:48:12.548338Z","iopub.execute_input":"2024-02-04T14:48:12.549109Z","iopub.status.idle":"2024-02-04T14:48:12.554728Z","shell.execute_reply.started":"2024-02-04T14:48:12.549071Z","shell.execute_reply":"2024-02-04T14:48:12.553663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Assuming the output shape of the preceding layer is (None, height, width, channels)\npreceding_layer_output = vgg19_model.layers[-1].output\nflat1 = Flatten()(preceding_layer_output)  # Flatten the output\n\n# Adjust input size as needed for the Dense layer\nclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\noutput = Dense(1, activation='sigmoid')(class1)\n\n# define new model\nmy_model = Model(inputs=vgg19_model.inputs, outputs=output)\n\n# compile model\nopt = SGD(lr=0.0001, momentum=0.9)\nmy_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\nmy_model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:48:12.556038Z","iopub.execute_input":"2024-02-04T14:48:12.556404Z","iopub.status.idle":"2024-02-04T14:48:12.652472Z","shell.execute_reply.started":"2024-02-04T14:48:12.556371Z","shell.execute_reply":"2024-02-04T14:48:12.651620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=10, verbose=1,\n                    callbacks=[PlotLossesKeras()])\n","metadata":{"execution":{"iopub.status.busy":"2024-02-04T14:48:13.813061Z","iopub.execute_input":"2024-02-04T14:48:13.813958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate model\n_, acc = my_model.evaluate(test_it, steps=len(test_it), verbose=1)\nprint('> %.3f' % (acc * 100.0))\n\n\ntrain_model = 'vgg19_model.hdf5'\nmy_model.save(train_model)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# load and prepare the image\ndef load_image(filename):\n\t# load the image\n\timg = load_img(filename, target_size=(224, 224))\n\t# convert to array\n\timg = img_to_array(img)\n\t# reshape into a single sample with 3 channels\n\timg = img.reshape(1, 224, 224, 3)\n\t# center pixel data\n\timg = img.astype('float32')\n\timg = img - [123.68, 116.779, 103.939]\n\treturn img\n\n# load an image and predict the class\ndef run_example():\n\t# load the image\n\timg = load_image('/kaggle/working/test1/166.jpg')\n\t# load model\n\tmodel = load_model('vgg19_model.hdf5')\n\t# predict the class\n\tresult = model.predict(img)\n\tprint(result[0])\n\n# entry point, run the example\nrun_example()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ResNet50","metadata":{}},{"cell_type":"code","source":"# load model\nresnet_model = ResNet50(\n    include_top=False,\n    input_shape=(224, 224, 3)\n    )","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:07:26.531596Z","iopub.execute_input":"2024-02-04T09:07:26.531928Z","iopub.status.idle":"2024-02-04T09:07:28.184529Z","shell.execute_reply.started":"2024-02-04T09:07:26.531901Z","shell.execute_reply":"2024-02-04T09:07:28.183477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mark loaded layers as not trainable\nfor index, layer in enumerate(resnet_model.layers[:]):\n    #print(index, layer.name, layer.output_shape)\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:07:28.186535Z","iopub.execute_input":"2024-02-04T09:07:28.186861Z","iopub.status.idle":"2024-02-04T09:07:28.197619Z","shell.execute_reply.started":"2024-02-04T09:07:28.186833Z","shell.execute_reply":"2024-02-04T09:07:28.196732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add new classifier layers\nflat1 = Flatten()(resnet_model.layers[-1].output)\nclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\noutput = Dense(1, activation='sigmoid')(class1)\n\n# define new model\nresnet_model = Model(inputs=resnet_model.inputs, outputs=output)\n\n# compile model\nopt = SGD(lr=0.0001, momentum=0.9)\nresnet_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:07:28.198834Z","iopub.execute_input":"2024-02-04T09:07:28.199173Z","iopub.status.idle":"2024-02-04T09:07:28.253243Z","shell.execute_reply.started":"2024-02-04T09:07:28.199137Z","shell.execute_reply":"2024-02-04T09:07:28.252552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit model\nresnet_model.reset_states()\nhistory = resnet_model.fit(train_it, \n                    #steps_per_epoch=len(train_it),\n                    validation_data=test_it, \n                    #validation_steps=len(test_it), \n                    epochs=10, verbose=1,\n                    callbacks=[PlotLossesKeras()])\n# evaluate model\n_, acc = resnet_model.evaluate(test_it, steps=len(test_it), verbose=1)\nprint('> %.3f' % (acc * 100.0))\n\n\ntrain_model = 'resnet50_model.hdf5'\nresnet_model.save(train_model)","metadata":{"execution":{"iopub.status.busy":"2024-02-04T08:56:16.940220Z","iopub.execute_input":"2024-02-04T08:56:16.941028Z","iopub.status.idle":"2024-02-04T09:07:25.893337Z","shell.execute_reply.started":"2024-02-04T08:56:16.940994Z","shell.execute_reply":"2024-02-04T09:07:25.892467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# load and prepare the image\ndef load_image(filename):\n\t# load the image\n\timg = load_img(filename, target_size=(224, 224))\n\t# convert to array\n\timg = img_to_array(img)\n\t# reshape into a single sample with 3 channels\n\timg = img.reshape(1, 224, 224, 3)\n\t# center pixel data\n\timg = img.astype('float32')\n\timg = img - [123.68, 116.779, 103.939]\n\treturn img\n\n# load an image and predict the class\ndef run_example():\n\t# load the image\n\timg = load_image('/kaggle/working/test1/166.jpg')\n\t# load model\n\tmodel = load_model('resnet50_model.hdf5')\n\t# predict the class\n\tresult = model.predict(img)\n\tprint(result[0])\n\n# entry point, run the example\nrun_example()","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:19:20.221195Z","iopub.execute_input":"2024-02-04T09:19:20.221605Z","iopub.status.idle":"2024-02-04T09:19:24.036389Z","shell.execute_reply.started":"2024-02-04T09:19:20.221573Z","shell.execute_reply":"2024-02-04T09:19:24.035563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# *DenseNet121*","metadata":{}},{"cell_type":"code","source":"# load model\nDN_model = DenseNet121(\n    include_top=False,\n    input_shape=(224, 224, 3)\n    )\nprint(DN_model.summary())","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:24:41.810233Z","iopub.execute_input":"2024-02-04T09:24:41.811005Z","iopub.status.idle":"2024-02-04T09:24:46.434615Z","shell.execute_reply.started":"2024-02-04T09:24:41.810967Z","shell.execute_reply":"2024-02-04T09:24:46.433674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mark loaded layers as not trainable\nfor index, layer in enumerate(DN_model.layers[:]):\n    #print(index, layer.name, layer.output_shape)\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:51:20.474868Z","iopub.execute_input":"2024-02-04T09:51:20.475311Z","iopub.status.idle":"2024-02-04T09:51:20.498453Z","shell.execute_reply.started":"2024-02-04T09:51:20.475275Z","shell.execute_reply":"2024-02-04T09:51:20.497424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add new classifier layers\nflat1 = Flatten()(DN_model.layers[-1].output)\nclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\noutput = Dense(1, activation='sigmoid')(class1)\n\n# define new model\nmy_model = Model(inputs=DN_model.inputs, outputs=output)\n\n# compile model\nopt = SGD(lr=0.0001, momentum=0.9)\nmy_model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:51:21.737949Z","iopub.execute_input":"2024-02-04T09:51:21.738325Z","iopub.status.idle":"2024-02-04T09:51:21.821033Z","shell.execute_reply.started":"2024-02-04T09:51:21.738294Z","shell.execute_reply":"2024-02-04T09:51:21.820132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_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=10, verbose=1,\n                    callbacks=[PlotLossesKeras()])","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:24:46.546456Z","iopub.execute_input":"2024-02-04T09:24:46.546750Z","iopub.status.idle":"2024-02-04T09:34:42.290522Z","shell.execute_reply.started":"2024-02-04T09:24:46.546724Z","shell.execute_reply":"2024-02-04T09:34:42.289778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate model\n_, acc = my_model.evaluate(test_it, steps=len(test_it), verbose=1)\nprint('> %.3f' % (acc * 100.0))\n\n\ntrain_model = 'denseNet121.hdf5'\nmy_model.save(train_model)","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:51:31.377153Z","iopub.execute_input":"2024-02-04T09:51:31.377995Z","iopub.status.idle":"2024-02-04T09:52:52.520282Z","shell.execute_reply.started":"2024-02-04T09:51:31.377957Z","shell.execute_reply":"2024-02-04T09:52:52.519413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# load and prepare the image\ndef load_image(filename):\n\t# load the image\n\timg = load_img(filename, target_size=(224, 224))\n\t# convert to array\n\timg = img_to_array(img)\n\t# reshape into a single sample with 3 channels\n\timg = img.reshape(1, 224, 224, 3)\n\t# center pixel data\n\timg = img.astype('float32')\n\timg = img - [123.68, 116.779, 103.939]\n\treturn img\n\n# load an image and predict the class\ndef run_example():\n\t# load the image\n\timg = load_image('/kaggle/working/test1/166.jpg')\n\t# load model\n\tmodel = load_model('denseNet121.hdf5')\n\t# predict the class\n\tresult = model.predict(img)\n\tprint(result[0])\n\n# entry point, run the example\nrun_example()","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:52:52.522287Z","iopub.execute_input":"2024-02-04T09:52:52.522611Z","iopub.status.idle":"2024-02-04T09:52:59.165974Z","shell.execute_reply.started":"2024-02-04T09:52:52.522583Z","shell.execute_reply":"2024-02-04T09:52:59.165124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MobileNetV2","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNetV2","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:57:21.360784Z","iopub.execute_input":"2024-02-04T09:57:21.361589Z","iopub.status.idle":"2024-02-04T09:57:21.366022Z","shell.execute_reply.started":"2024-02-04T09:57:21.361554Z","shell.execute_reply":"2024-02-04T09:57:21.364943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\nmoblie_model = MobileNetV2(\n    include_top=False,\n    input_shape=(224, 224, 3)\n    )","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:57:22.260043Z","iopub.execute_input":"2024-02-04T09:57:22.260771Z","iopub.status.idle":"2024-02-04T09:57:23.725302Z","shell.execute_reply.started":"2024-02-04T09:57:22.260737Z","shell.execute_reply":"2024-02-04T09:57:23.724472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, layer in enumerate(DN_model.layers[:]):\n    #print(index, layer.name, layer.output_shape)\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:57:24.502112Z","iopub.execute_input":"2024-02-04T09:57:24.502988Z","iopub.status.idle":"2024-02-04T09:57:24.523213Z","shell.execute_reply.started":"2024-02-04T09:57:24.502951Z","shell.execute_reply":"2024-02-04T09:57:24.522391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add new classifier layers\nflat1 = Flatten()(moblie_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=moblie_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":{"execution":{"iopub.status.busy":"2024-02-04T09:57:25.746984Z","iopub.execute_input":"2024-02-04T09:57:25.747916Z","iopub.status.idle":"2024-02-04T09:57:26.180808Z","shell.execute_reply.started":"2024-02-04T09:57:25.747879Z","shell.execute_reply":"2024-02-04T09:57:26.179889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=10, verbose=1,\n                    callbacks=[PlotLossesKeras()])\n# evaluate model\n_, acc = my_model.evaluate(test_it, steps=len(test_it), verbose=1)\nprint('> %.3f' % (acc * 100.0))\n# learning curves\n\nbest_model = 'MobileNetV2.hdf5'\nmy_model.save(best_model)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-04T09:57:31.254794Z","iopub.execute_input":"2024-02-04T09:57:31.255210Z","iopub.status.idle":"2024-02-04T10:08:45.468391Z","shell.execute_reply.started":"2024-02-04T09:57:31.255177Z","shell.execute_reply":"2024-02-04T10:08:45.467525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# load and prepare the image\ndef load_image(filename):\n\t# load the image\n\timg = load_img(filename, target_size=(224, 224))\n\t# convert to array\n\timg = img_to_array(img)\n\t# reshape into a single sample with 3 channels\n\timg = img.reshape(1, 224, 224, 3)\n\t# center pixel data\n\timg = img.astype('float32')\n\timg = img - [123.68, 116.779, 103.939]\n\treturn img\n\n# load an image and predict the class\ndef run_example():\n\t# load the image\n\timg = load_image('/kaggle/working/test1/166.jpg')\n\t# load model\n\tmodel = load_model('MobileNetV2.hdf5')\n\t# predict the class\n\tresult = model.predict(img)\n\tprint(result[0])\n\n# entry point, run the example\nrun_example()","metadata":{"execution":{"iopub.status.busy":"2024-02-04T10:08:45.470248Z","iopub.execute_input":"2024-02-04T10:08:45.470555Z","iopub.status.idle":"2024-02-04T10:08:48.824853Z","shell.execute_reply.started":"2024-02-04T10:08:45.470527Z","shell.execute_reply":"2024-02-04T10:08:48.823854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}