{"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"},{"sourceId":8175682,"sourceType":"datasetVersion","datasetId":4839441}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-06T04:45:34.396578Z","iopub.execute_input":"2024-05-06T04:45:34.397327Z","iopub.status.idle":"2024-05-06T04:45:34.404644Z","shell.execute_reply.started":"2024-05-06T04:45:34.397295Z","shell.execute_reply":"2024-05-06T04:45:34.403560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint(os.listdir(\"../input\"))\nimport 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(\".\")","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:45:35.082563Z","iopub.execute_input":"2024-05-06T04:45:35.083323Z","iopub.status.idle":"2024-05-06T04:45:46.126412Z","shell.execute_reply.started":"2024-05-06T04:45:35.083292Z","shell.execute_reply":"2024-05-06T04:45:46.125470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot\nfrom matplotlib.image import imread\ntrain_folder = 'train/'\nfolder = train_folder\nfor i in range(9):\n\tpyplot.subplot(330 + 1 + i)\n\tfilename = folder + 'dog.' + str(i) + '.jpg'\n\timage = imread(filename)\n\tpyplot.imshow(image)\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:45:46.127957Z","iopub.execute_input":"2024-05-06T04:45:46.128311Z","iopub.status.idle":"2024-05-06T04:45:47.304975Z","shell.execute_reply.started":"2024-05-06T04:45:46.128280Z","shell.execute_reply":"2024-05-06T04:45:47.304067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(9):\n\tpyplot.subplot(330 + 1 + i)\n\tfilename = folder + 'cat.' + str(i) + '.jpg'\n\timage = imread(filename)\n\tpyplot.imshow(image)\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:45:47.306145Z","iopub.execute_input":"2024-05-06T04:45:47.306440Z","iopub.status.idle":"2024-05-06T04:45:48.653964Z","shell.execute_reply.started":"2024-05-06T04:45:47.306414Z","shell.execute_reply":"2024-05-06T04:45:48.653073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python --version","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:45:48.656431Z","iopub.execute_input":"2024-05-06T04:45:48.656837Z","iopub.status.idle":"2024-05-06T04:45:49.621117Z","shell.execute_reply.started":"2024-05-06T04:45:48.656791Z","shell.execute_reply":"2024-05-06T04:45:49.619994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n!pip show keras \n# Name: keras\n# Version: 2.13.1\n# Summary: Deep learning for humans.\n# Home-page: https://keras.io/\n# Author: Keras team\n# Author-email: keras-users@googlegroups.com\n# License: Apache 2.0\n# Location: /opt/conda/lib/python3.10/site-packages\n# Requires: \n# Required-by: tensorflow","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:45:49.622602Z","iopub.execute_input":"2024-05-06T04:45:49.622918Z","iopub.status.idle":"2024-05-06T04:46:00.640356Z","shell.execute_reply.started":"2024-05-06T04:45:49.622885Z","shell.execute_reply":"2024-05-06T04:46:00.639232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture\n\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-05-06T04:46:00.641797Z","iopub.execute_input":"2024-05-06T04:46:00.642136Z","iopub.status.idle":"2024-05-06T04:46:00.648816Z","shell.execute_reply.started":"2024-05-06T04:46:00.642102Z","shell.execute_reply":"2024-05-06T04:46:00.647845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\nfor subdir in subdirs:\n\tlabeldirs = ['dogs/', 'cats/']\n\tfor labldir in labeldirs:\n\t\tnewdir = dataset_home + subdir + labldir\n\t\tos.makedirs(newdir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:46:00.650080Z","iopub.execute_input":"2024-05-06T04:46:00.650409Z","iopub.status.idle":"2024-05-06T04:46:00.661929Z","shell.execute_reply.started":"2024-05-06T04:46:00.650378Z","shell.execute_reply":"2024-05-06T04:46:00.661061Z"},"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\ndataset_home = 'dataset_dogs_vs_cats/'\nsubdirs = ['train/', 'test/']\nfor subdir in subdirs:\n\tlabeldirs = ['dogs/', 'cats/']\n\tfor labldir in labeldirs:\n\t\tnewdir = dataset_home + subdir + labldir\n\t\tmakedirs(newdir, exist_ok=True)\n# 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-05-06T04:46:00.663135Z","iopub.execute_input":"2024-05-06T04:46:00.663444Z","iopub.status.idle":"2024-05-06T04:46:04.268076Z","shell.execute_reply.started":"2024-05-06T04:46:00.663420Z","shell.execute_reply":"2024-05-06T04:46:04.267262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Develop a Baseline CNN Model","metadata":{}},{"cell_type":"code","source":"# baseline model for the dogs vs cats dataset\nimport sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\nimport keras\nimport os, shutil, pathlib\nimport random\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.utils import image_dataset_from_directory\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import layers\n\ndef define_model():\n    data_augmentation = keras.Sequential(\n                [keras.layers.RandomFlip(\"horizontal\"), # Applies horizontal flipping to a random 50% of the images that go through it\n                 keras.layers.RandomRotation(0.1), # Rotates the input images by a value\n                 keras.layers.RandomZoom(0.2)] # Rotates the input images by a value\n     )\n    inputs = keras.Input(shape=(180, 180, 3))\n    x = data_augmentation(inputs)\n    x = layers.Rescaling(1./255)(x)\n    x = layers.Conv2D(filters=32, kernel_size=5, use_bias=False)(x)\n\n    for size in [32, 64, 128, 256, 512]:\n      residual = x\n      x = layers.BatchNormalization()(x)\n      x = layers.Activation(\"relu\")(x)\n      x = layers.SeparableConv2D(size, 3, padding=\"same\", kernel_initializer='he_uniform', use_bias=False)(x)\n\n      x = layers.BatchNormalization()(x)\n      x = layers.Activation(\"relu\")(x)\n      x = layers.SeparableConv2D(size, 3, padding=\"same\", kernel_initializer='he_uniform', use_bias=False)(x)\n      x = layers.MaxPooling2D(3, strides=2, padding=\"same\")(x)\n\n      residual = layers.Conv2D(size, 1, strides=2, padding=\"same\", kernel_initializer='he_uniform',use_bias=False)(residual)\n      x = layers.add([x, residual])\n\n\n    x = layers.GlobalAveragePooling2D()(x)\n    x = layers.Dense(64, kernel_initializer='he_uniform')(x)\n    x = layers.Dropout(0.5)(x)\n    outputs = layers.Dense(1, activation=\"sigmoid\")(x)\n\n    model = keras.Model(inputs=inputs, outputs=outputs)\n    model.compile(\n        loss=\"binary_crossentropy\",\n        optimizer=keras.optimizers.Adam(),\n        metrics=[\"accuracy\"]\n    )\n    return model\n# plot diagnostic learning curves\ndef summarize_diagnostics(history):\n\t# plot loss\n\tpyplot.subplot(211)\n\tpyplot.title('Cross Entropy Loss')\n\tpyplot.plot(history.history['loss'], color='blue', label='train')\n\tpyplot.plot(history.history['val_loss'], color='orange', label='test')\n\t# plot accuracy\n\tpyplot.subplot(212)\n\tpyplot.title('Classification Accuracy')\n\tpyplot.plot(history.history['accuracy'], color='blue', label='train')\n\tpyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n\t# save plot to file\n\tfilename = sys.argv[0].split('/')[-1]\n\tpyplot.savefig(filename + '_plot.png')\n\tpyplot.close()\n\n# run the test harness for evaluating a model\ndef run_test_harness():\n\t# define model\n\tmodel = dm()\n\t# create data generator\n\tdatagen = ImageDataGenerator(rescale=1.0/255.0)\n\t# prepare iterators\n\ttrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n\t\tclass_mode='binary', batch_size=64, target_size=(180, 180))\n\ttest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n\t\tclass_mode='binary', batch_size=64, target_size=(180, 180))\n\t# fit model\n\thistory = model.fit(train_it, steps_per_epoch=len(train_it),\n\t\tvalidation_data=test_it, validation_steps=len(test_it), epochs=100, verbose=1)\n\t# evaluate model\n\t_, acc = model.evaluate(test_it, steps=len(test_it), verbose=0)\n\tmodel.save(\"/kaggle/working/model.keras\")       \n\tprint('> %.3f' % (acc * 100.0))\n\t# learning curves\n\tsummarize_diagnostics(history)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:22:03.224066Z","iopub.execute_input":"2024-05-06T05:22:03.224995Z","iopub.status.idle":"2024-05-06T05:22:03.249644Z","shell.execute_reply.started":"2024-05-06T05:22:03.224957Z","shell.execute_reply":"2024-05-06T05:22:03.248632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T05:22:05.393004Z","iopub.execute_input":"2024-05-06T05:22:05.393408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:46:04.286941Z","iopub.execute_input":"2024-05-06T04:46:04.287258Z","iopub.status.idle":"2024-05-06T05:18:01.089383Z","shell.execute_reply.started":"2024-05-06T04:46:04.287233Z","shell.execute_reply":"2024-05-06T05:18:01.087530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot -q","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:38:29.678106Z","iopub.execute_input":"2024-05-06T04:38:29.678342Z","iopub.status.idle":"2024-05-06T04:38:42.802106Z","shell.execute_reply.started":"2024-05-06T04:38:29.678319Z","shell.execute_reply":"2024-05-06T04:38:42.801007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:38:42.803870Z","iopub.execute_input":"2024-05-06T04:38:42.804170Z","iopub.status.idle":"2024-05-06T04:38:42.818479Z","shell.execute_reply.started":"2024-05-06T04:38:42.804143Z","shell.execute_reply":"2024-05-06T04:38:42.817717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG16","metadata":{}},{"cell_type":"code","source":"# vgg16 model used for transfer learning on the dogs and cats dataset\nimport sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\nfrom keras.applications.vgg16 import VGG16\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\ndef define_model():\n\t# load model\n\tmodel = VGG16(include_top=False, input_shape=(224, 224, 3))\n\t# mark loaded layers as not trainable\n\tfor layer in model.layers:\n\t\tlayer.trainable = False\n\t# add new classifier layers\n\tflat1 = Flatten()(model.layers[-1].output)\n\tclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\n\toutput = Dense(1, activation='sigmoid')(class1)\n\t# define new model\n\tmodel = Model(inputs=model.inputs, outputs=output)\n\t# compile model\n\topt = SGD(lr=0.001, momentum=0.9)\n\tmodel.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n\treturn model\n# define cnn model\ndef summarize_diagnostics(history):\n\tpyplot.subplot(211)\n\tpyplot.title('Cross Entropy Loss')\n\tmodel = VGG16(include_top=False, input_shape=(180, 180, 3))\n\tfor layer in model.layers:\n\t\tlayer.trainable = False\n\tflat1 = Flatten()(model.layers[-1].output)\n\tclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\n\toutput = Dense(1, activation='sigmoid')(class1)\n\tmodel = Model(inputs=model.inputs, outputs=output)\n\topt = SGD(lr=0.001, momentum=0.9)\n    \n\tmodel.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n\tpyplot.plot(history.history['loss'], color='blue', label='train')\n\tpyplot.plot(history.history['val_loss'], color='orange', label='test')\n\tpyplot.subplot(212)\n\tpyplot.title('Classification Accuracy')\n\tpyplot.plot(history.history['accuracy'], color='blue', label='train')\n\tpyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n\tfilename = sys.argv[0].split('/')[-1]\n\tpyplot.savefig(filename + '_plot.png')\n\tpyplot.close()\n\ndef run_test_harness():\n\tmodel = define_model()\n\tdatagen = ImageDataGenerator(featurewise_center=True)\n\tdatagen.mean = [123.68, 116.779, 103.939]\n\ttrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n\t\tclass_mode='binary', batch_size=64, target_size=(180, 180))\n\ttest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n\t\tclass_mode='binary', batch_size=64, target_size=(180, 180))\n\thistory = model.fit(train_it, \n                        steps_per_epoch=len(train_it),\n                        validation_data=test_it, \n                        validation_steps=len(test_it), \n                        epochs=50, verbose=1,\n                        callbacks=[PlotLossesKeras()]\n                       )\n\t# evaluate model\n\t_, acc = model.evaluate(test_it, steps=len(test_it), verbose=1)\n\tprint('> %.3f' % (acc * 100.0))\n\tmodel.save(\"/kaggle/working/BTLAI.model\")    \n\t# learning curves\n\tsummarize_diagnostics(history)\n\n# entry point, run the test harness\n#run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:43:01.143864Z","iopub.execute_input":"2024-05-06T04:43:01.144729Z","iopub.status.idle":"2024-05-06T04:43:01.165176Z","shell.execute_reply.started":"2024-05-06T04:43:01.144689Z","shell.execute_reply":"2024-05-06T04:43:01.164185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = define_model()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run_test_harness()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T04:43:04.108501Z","iopub.execute_input":"2024-05-06T04:43:04.109219Z","iopub.status.idle":"2024-05-06T04:43:06.274642Z","shell.execute_reply.started":"2024-05-06T04:43:04.109183Z","shell.execute_reply":"2024-05-06T04:43:06.273064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VGG19","metadata":{}},{"cell_type":"code","source":"# vgg19 model used for transfer learning on the dogs and cats dataset\nimport sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg19 import VGG19\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\n\n# define cnn model\ndef define_model():\n\t# load model\n\tmodel = VGG19(include_top=False, input_shape=(180, 180, 3))\n\t# mark loaded layers as not trainable\n\tfor layer in model.layers:\n\t\tlayer.trainable = False\n\t# add new classifier layers\n\tflat1 = Flatten()(model.layers[-1].output)\n\tclass1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)\n\toutput = Dense(1, activation='sigmoid')(class1)\n\t# define new model\n\tmodel = Model(inputs=model.inputs, outputs=output)\n\t# compile model\n\topt = SGD(lr=0.001, momentum=0.9)\n\tmodel.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n\treturn model\n\n# plot diagnostic learning curves\ndef summarize_diagnostics(history):\n\t# plot loss\n\tpyplot.subplot(211)\n\tpyplot.title('Cross Entropy Loss')\n\tpyplot.plot(history.history['loss'], color='blue', label='train')\n\tpyplot.plot(history.history['val_loss'], color='orange', label='test')\n\t# plot accuracy\n\tpyplot.subplot(212)\n\tpyplot.title('Classification Accuracy')\n\tpyplot.plot(history.history['accuracy'], color='blue', label='train')\n\tpyplot.plot(history.history['val_accuracy'], color='orange', label='test')\n\t# save plot to file\n\tfilename = sys.argv[0].split('/')[-1]\n\tpyplot.savefig(filename + '_plot.png')\n\tpyplot.close()\n\n# run the test harness for evaluating a model\ndef run_test_harness():\n\t# define model\n\tmodel = define_model()\n\t# create data generator\n\tdatagen = ImageDataGenerator(featurewise_center=True)\n\t# specify imagenet mean values for centering\n\tdatagen.mean = [123.68, 116.779, 103.939]\n\t# prepare iterator\n\ttrain_it = datagen.flow_from_directory('dataset_dogs_vs_cats/train/',\n\t\tclass_mode='binary', batch_size=64, target_size=(180, 180))\n\ttest_it = datagen.flow_from_directory('dataset_dogs_vs_cats/test/',\n\t\tclass_mode='binary', batch_size=64, target_size=(180, 180))\n\t# fit model\n\thistory = model.fit(train_it, \n                        steps_per_epoch=len(train_it),\n                        validation_data=test_it, \n                        validation_steps=len(test_it), \n                        epochs=30, verbose=1,\n                        callbacks=[PlotLossesKeras()]\n                       )\n\t# evaluate model\n\t_, acc = model.evaluate(test_it, steps=len(test_it), verbose=1)\n\tprint('> %.3f' % (acc * 100.0))\n\t# learning curves\n\tsummarize_diagnostics(history)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}