{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Importing Libraries\nfrom tensorflow import keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import layers\nfrom keras import models\nimport os\nfrom sklearn.model_selection import train_test_split\nimport shutil\nimport random\nimport math\nimport numpy as np\nimport zipfile\nimport pandas as pd\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-13T08:31:24.455121Z","iopub.execute_input":"2023-06-13T08:31:24.456010Z","iopub.status.idle":"2023-06-13T08:31:32.483609Z","shell.execute_reply.started":"2023-06-13T08:31:24.455974Z","shell.execute_reply":"2023-06-13T08:31:32.482661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracting Training & Testing Data\npath1 = '/kaggle/input/dogs-vs-cats/train.zip'\ndestination1 = '/kaggle/working/train_images/'\n\npath2 = '/kaggle/input/dogs-vs-cats/test1.zip'\ndestination2 = '/kaggle/working/test_images/'\n\nos.makedirs(destination1,exist_ok=True)\nos.makedirs(destination2,exist_ok=True)\n\n\nwith zipfile.ZipFile(path1,'r') as zip_ref:\n    zip_ref.extractall(destination1)\n\nwith zipfile.ZipFile(path2,'r') as zip_ref:\n    zip_ref.extractall(destination2)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:32.486628Z","iopub.execute_input":"2023-06-13T08:31:32.487236Z","iopub.status.idle":"2023-06-13T08:31:47.797106Z","shell.execute_reply.started":"2023-06-13T08:31:32.487208Z","shell.execute_reply":"2023-06-13T08:31:47.796128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr='/kaggle/working/train_images'\nts='/kaggle/working/test_images'\nprint(os.listdir(tr))\nprint(os.listdir(ts))","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:47.798344Z","iopub.execute_input":"2023-06-13T08:31:47.798678Z","iopub.status.idle":"2023-06-13T08:31:47.809454Z","shell.execute_reply.started":"2023-06-13T08:31:47.798644Z","shell.execute_reply":"2023-06-13T08:31:47.808648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path='/kaggle/working/train_images/train/'","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:47.813633Z","iopub.execute_input":"2023-06-13T08:31:47.813915Z","iopub.status.idle":"2023-06-13T08:31:47.911279Z","shell.execute_reply.started":"2023-06-13T08:31:47.813891Z","shell.execute_reply":"2023-06-13T08:31:47.910302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of Images\nprint(f\"We have total {len(os.listdir(train_path))} images in our training data.\")","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:47.912717Z","iopub.execute_input":"2023-06-13T08:31:47.913083Z","iopub.status.idle":"2023-06-13T08:31:47.939290Z","shell.execute_reply.started":"2023-06-13T08:31:47.913052Z","shell.execute_reply":"2023-06-13T08:31:47.937901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('/kaggle/working/train/')\nos.mkdir('/kaggle/working/val')\nos.mkdir('/kaggle/working/train/cats/')\nos.mkdir('/kaggle/working/train/dogs/')\nos.mkdir('/kaggle/working/val/cats/')\nos.mkdir('/kaggle/working/val/dogs/')","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:47.940637Z","iopub.execute_input":"2023-06-13T08:31:47.940997Z","iopub.status.idle":"2023-06-13T08:31:47.948747Z","shell.execute_reply.started":"2023-06-13T08:31:47.940965Z","shell.execute_reply":"2023-06-13T08:31:47.947940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=[]\nfor file_name in os.listdir(train_path):\n    train.append(train_path+file_name)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:47.951875Z","iopub.execute_input":"2023-06-13T08:31:47.952125Z","iopub.status.idle":"2023-06-13T08:31:47.978049Z","shell.execute_reply.started":"2023-06-13T08:31:47.952103Z","shell.execute_reply":"2023-06-13T08:31:47.977243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, val = train_test_split(train,test_size=0.20,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:47.980101Z","iopub.execute_input":"2023-06-13T08:31:47.980401Z","iopub.status.idle":"2023-06-13T08:31:47.992844Z","shell.execute_reply.started":"2023-06-13T08:31:47.980371Z","shell.execute_reply":"2023-06-13T08:31:47.992022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file_name in train:\n    if(file_name.find('cat') != -1 ):\n        shutil.move(file_name, '/kaggle/working/train/cats/')\n    else:\n        shutil.move(file_name, '/kaggle/working/train/dogs/')\nfor file_name in val:\n    if(file_name.find('cat') != -1 ):\n        shutil.move(file_name, '/kaggle/working/val/cats/')\n    else:\n        shutil.move(file_name, '/kaggle/working/val/dogs/')","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:47.994248Z","iopub.execute_input":"2023-06-13T08:31:47.994548Z","iopub.status.idle":"2023-06-13T08:31:49.219873Z","shell.execute_reply.started":"2023-06-13T08:31:47.994518Z","shell.execute_reply":"2023-06-13T08:31:49.218959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data augmentation\nfrom keras.applications.imagenet_utils import preprocess_input\n\ntrain_datagen=ImageDataGenerator(rescale = 1.0/255,\n                                    #width_shift_range=0.2,\n                                    #height_shift_range=0.2,\n                                    rotation_range=40,\n                                    #shear_range=0.2,\n                                    zoom_range=0.2,\n                                    horizontal_flip=True,\n                                    fill_mode='nearest')\n\ntest_datagen=ImageDataGenerator(rescale=1.0/255)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:49.221502Z","iopub.execute_input":"2023-06-13T08:31:49.221886Z","iopub.status.idle":"2023-06-13T08:31:49.228097Z","shell.execute_reply.started":"2023-06-13T08:31:49.221815Z","shell.execute_reply":"2023-06-13T08:31:49.226969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n    '/kaggle/working/train', \n    color_mode=\"rgb\",\n    target_size=(224,224),\n    batch_size=64,\n    class_mode='binary'\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:49.229596Z","iopub.execute_input":"2023-06-13T08:31:49.230331Z","iopub.status.idle":"2023-06-13T08:31:49.741080Z","shell.execute_reply.started":"2023-06-13T08:31:49.230300Z","shell.execute_reply":"2023-06-13T08:31:49.740054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = test_datagen.flow_from_directory(\n    '/kaggle/working/val', \n    color_mode=\"rgb\",\n    target_size=(224,224),\n    batch_size=100,\n    class_mode='binary'\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:49.742468Z","iopub.execute_input":"2023-06-13T08:31:49.742941Z","iopub.status.idle":"2023-06-13T08:31:49.880362Z","shell.execute_reply.started":"2023-06-13T08:31:49.742906Z","shell.execute_reply":"2023-06-13T08:31:49.879575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building the model \nfrom keras.applications import ResNet50V2\n\nmodel=models.Sequential()\n\nconv_base=ResNet50V2( weights='imagenet' , include_top=False ,input_shape=(224,224,3) )\nconv_base.trainable=False\nmodel.add(conv_base)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:49.885307Z","iopub.execute_input":"2023-06-13T08:31:49.885916Z","iopub.status.idle":"2023-06-13T08:31:55.780116Z","shell.execute_reply.started":"2023-06-13T08:31:49.885882Z","shell.execute_reply":"2023-06-13T08:31:55.779186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(layers.Flatten() )\nmodel.add(layers.Dropout(0.50))\nmodel.add(layers.BatchNormalization(axis=1))\nmodel.add(layers.Dense( 1024 ,activation='relu' ))\nmodel.add(layers.Dropout(0.50))\nmodel.add(layers.BatchNormalization(axis=1))\nmodel.add(layers.Dense( 512 ,activation='relu' ))\nmodel.add(layers.Dropout(0.50))\nmodel.add(layers.BatchNormalization(axis=1))\nmodel.add(layers.Dense( 1 , activation='sigmoid' ) )","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:55.781432Z","iopub.execute_input":"2023-06-13T08:31:55.781876Z","iopub.status.idle":"2023-06-13T08:31:55.886365Z","shell.execute_reply.started":"2023-06-13T08:31:55.781828Z","shell.execute_reply":"2023-06-13T08:31:55.885507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:55.887778Z","iopub.execute_input":"2023-06-13T08:31:55.888156Z","iopub.status.idle":"2023-06-13T08:31:55.937149Z","shell.execute_reply.started":"2023-06-13T08:31:55.888123Z","shell.execute_reply":"2023-06-13T08:31:55.936506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping\nearly_stopping = EarlyStopping(monitor=\"val_accuracy\", patience=3)","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:55.938066Z","iopub.execute_input":"2023-06-13T08:31:55.938400Z","iopub.status.idle":"2023-06-13T08:31:55.943894Z","shell.execute_reply.started":"2023-06-13T08:31:55.938367Z","shell.execute_reply":"2023-06-13T08:31:55.942782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import optimizers\nmodel.compile(loss='binary_crossentropy' , optimizer='Adam' , metrics=['accuracy'] )","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:55.944975Z","iopub.execute_input":"2023-06-13T08:31:55.945272Z","iopub.status.idle":"2023-06-13T08:31:55.976051Z","shell.execute_reply.started":"2023-06-13T08:31:55.945237Z","shell.execute_reply":"2023-06-13T08:31:55.975224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train_generator  , epochs=20 , validation_data=valid_generator ,callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:31:55.977813Z","iopub.execute_input":"2023-06-13T08:31:55.978417Z","iopub.status.idle":"2023-06-13T08:54:40.304073Z","shell.execute_reply.started":"2023-06-13T08:31:55.978385Z","shell.execute_reply":"2023-06-13T08:54:40.303158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  Accuracy and Loss Curves \nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs   = range(len(acc))\n\nplt.plot(epochs, acc, label=\"Training accuracy\")\nplt.plot(epochs, val_acc, label=\"Validation accuracy\")\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.show()\n\nplt.plot(epochs, loss, label=\"Training loss\")\nplt.plot(epochs, val_loss, label=\"Validation loss\")\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:54:40.305574Z","iopub.execute_input":"2023-06-13T08:54:40.305969Z","iopub.status.idle":"2023-06-13T08:54:41.179141Z","shell.execute_reply.started":"2023-06-13T08:54:40.305934Z","shell.execute_reply":"2023-06-13T08:54:41.177060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model Evaluation\nloss, accuracy = model.evaluate(valid_generator)\nprint(\"Test: accuracy = %f  ;  loss = %f \" % (accuracy, loss))","metadata":{"execution":{"iopub.status.busy":"2023-06-13T08:54:41.180355Z","iopub.execute_input":"2023-06-13T08:54:41.180711Z","iopub.status.idle":"2023-06-13T08:55:01.963514Z","shell.execute_reply.started":"2023-06-13T08:54:41.180677Z","shell.execute_reply":"2023-06-13T08:55:01.962519Z"},"trusted":true},"execution_count":null,"outputs":[]}]}