{"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-11T17:25:53.5935Z","iopub.execute_input":"2023-06-11T17:25:53.593843Z","iopub.status.idle":"2023-06-11T17:26:01.759582Z","shell.execute_reply.started":"2023-06-11T17:25:53.5938Z","shell.execute_reply":"2023-06-11T17:26:01.758507Z"},"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-11T17:26:01.761524Z","iopub.execute_input":"2023-06-11T17:26:01.762274Z","iopub.status.idle":"2023-06-11T17:26:16.173762Z","shell.execute_reply.started":"2023-06-11T17:26:01.762236Z","shell.execute_reply":"2023-06-11T17:26:16.172784Z"},"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-11T17:26:16.175348Z","iopub.execute_input":"2023-06-11T17:26:16.175686Z","iopub.status.idle":"2023-06-11T17:26:16.182227Z","shell.execute_reply.started":"2023-06-11T17:26:16.175654Z","shell.execute_reply":"2023-06-11T17:26:16.18123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path='/kaggle/working/train_images/train/'","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:26:16.185269Z","iopub.execute_input":"2023-06-11T17:26:16.186072Z","iopub.status.idle":"2023-06-11T17:26:16.190732Z","shell.execute_reply.started":"2023-06-11T17:26:16.186039Z","shell.execute_reply":"2023-06-11T17:26:16.189698Z"},"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-11T17:26:16.192299Z","iopub.execute_input":"2023-06-11T17:26:16.192639Z","iopub.status.idle":"2023-06-11T17:26:16.21475Z","shell.execute_reply.started":"2023-06-11T17:26:16.192607Z","shell.execute_reply":"2023-06-11T17:26:16.213784Z"},"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-11T17:26:16.216815Z","iopub.execute_input":"2023-06-11T17:26:16.217146Z","iopub.status.idle":"2023-06-11T17:26:16.222778Z","shell.execute_reply.started":"2023-06-11T17:26:16.217117Z","shell.execute_reply":"2023-06-11T17:26:16.221801Z"},"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-11T17:26:16.224435Z","iopub.execute_input":"2023-06-11T17:26:16.224796Z","iopub.status.idle":"2023-06-11T17:26:16.250869Z","shell.execute_reply.started":"2023-06-11T17:26:16.224767Z","shell.execute_reply":"2023-06-11T17:26:16.250042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, val = train_test_split(train,test_size=0.20,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:26:16.252309Z","iopub.execute_input":"2023-06-11T17:26:16.252625Z","iopub.status.idle":"2023-06-11T17:26:16.266891Z","shell.execute_reply.started":"2023-06-11T17:26:16.252597Z","shell.execute_reply":"2023-06-11T17:26:16.266099Z"},"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-11T17:26:16.26914Z","iopub.execute_input":"2023-06-11T17:26:16.269384Z","iopub.status.idle":"2023-06-11T17:26:17.495069Z","shell.execute_reply.started":"2023-06-11T17:26:16.269363Z","shell.execute_reply":"2023-06-11T17:26:17.494027Z"},"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./255)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:26:17.500188Z","iopub.execute_input":"2023-06-11T17:26:17.500467Z","iopub.status.idle":"2023-06-11T17:26:17.506805Z","shell.execute_reply.started":"2023-06-11T17:26:17.500442Z","shell.execute_reply":"2023-06-11T17:26:17.505868Z"},"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-11T17:26:17.508418Z","iopub.execute_input":"2023-06-11T17:26:17.509024Z","iopub.status.idle":"2023-06-11T17:26:18.029145Z","shell.execute_reply.started":"2023-06-11T17:26:17.508994Z","shell.execute_reply":"2023-06-11T17:26:18.028214Z"},"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-11T17:26:18.030552Z","iopub.execute_input":"2023-06-11T17:26:18.030931Z","iopub.status.idle":"2023-06-11T17:26:18.168256Z","shell.execute_reply.started":"2023-06-11T17:26:18.030899Z","shell.execute_reply":"2023-06-11T17:26:18.167323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building the model \nfrom keras.applications import VGG16\n\nmodel=models.Sequential()\n\nconv_base=VGG16( 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-11T17:26:18.169452Z","iopub.execute_input":"2023-06-11T17:26:18.169823Z","iopub.status.idle":"2023-06-11T17:26:24.196162Z","shell.execute_reply.started":"2023-06-11T17:26:18.169778Z","shell.execute_reply":"2023-06-11T17:26:24.195232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(layers.Flatten() )\nmodel.add(layers.Dropout(0.25))\nmodel.add(layers.Dense( 512 ,activation='relu' ))\nmodel.add(layers.Dropout(0.25))\nmodel.add(layers.Dense( 1 , activation='sigmoid' ) )","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:26:24.197592Z","iopub.execute_input":"2023-06-11T17:26:24.19793Z","iopub.status.idle":"2023-06-11T17:26:24.244739Z","shell.execute_reply.started":"2023-06-11T17:26:24.1979Z","shell.execute_reply":"2023-06-11T17:26:24.243866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:26:24.246061Z","iopub.execute_input":"2023-06-11T17:26:24.246426Z","iopub.status.idle":"2023-06-11T17:26:24.272795Z","shell.execute_reply.started":"2023-06-11T17:26:24.246396Z","shell.execute_reply":"2023-06-11T17:26:24.272088Z"},"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-11T17:26:24.274101Z","iopub.execute_input":"2023-06-11T17:26:24.274431Z","iopub.status.idle":"2023-06-11T17:26:24.302307Z","shell.execute_reply.started":"2023-06-11T17:26:24.274401Z","shell.execute_reply":"2023-06-11T17:26:24.301435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train_generator  , epochs=5 , validation_data=valid_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:26:24.303627Z","iopub.execute_input":"2023-06-11T17:26:24.3044Z","iopub.status.idle":"2023-06-11T17:50:01.995696Z","shell.execute_reply.started":"2023-06-11T17:26:24.304368Z","shell.execute_reply":"2023-06-11T17:50:01.994655Z"},"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-11T17:50:01.997216Z","iopub.execute_input":"2023-06-11T17:50:01.997557Z","iopub.status.idle":"2023-06-11T17:50:02.65059Z","shell.execute_reply.started":"2023-06-11T17:50:01.997526Z","shell.execute_reply":"2023-06-11T17:50:02.649695Z"},"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-11T17:50:02.652003Z","iopub.execute_input":"2023-06-11T17:50:02.653042Z","iopub.status.idle":"2023-06-11T17:50:18.214736Z","shell.execute_reply.started":"2023-06-11T17:50:02.653008Z","shell.execute_reply":"2023-06-11T17:50:18.213878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict\npredict = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-11T17:50:18.216475Z","iopub.execute_input":"2023-06-11T17:50:18.216903Z","iopub.status.idle":"2023-06-11T17:50:18.84495Z","shell.execute_reply.started":"2023-06-11T17:50:18.216867Z","shell.execute_reply":"2023-06-11T17:50:18.843664Z"},"trusted":true},"execution_count":null,"outputs":[]}]}