{"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":"import os \nimport zipfile\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import models\nfrom tensorflow.keras import regularizers\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.callbacks import EarlyStopping\n\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-20T16:20:49.348275Z","iopub.execute_input":"2022-05-20T16:20:49.348873Z","iopub.status.idle":"2022-05-20T16:20:55.749705Z","shell.execute_reply.started":"2022-05-20T16:20:49.348787Z","shell.execute_reply":"2022-05-20T16:20:55.748896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 0\nnp.random.seed(seed)\ntf.random.set_seed(3)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:20:55.751493Z","iopub.execute_input":"2022-05-20T16:20:55.751805Z","iopub.status.idle":"2022-05-20T16:20:55.758596Z","shell.execute_reply.started":"2022-05-20T16:20:55.751764Z","shell.execute_reply":"2022-05-20T16:20:55.757894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",'r') as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:20:55.759847Z","iopub.execute_input":"2022-05-20T16:20:55.760376Z","iopub.status.idle":"2022-05-20T16:21:08.375472Z","shell.execute_reply.started":"2022-05-20T16:20:55.760333Z","shell.execute_reply":"2022-05-20T16:21:08.374645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/working/train/'\nfilenames = os.listdir(path)\nfilenames[:10]","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:08.379702Z","iopub.execute_input":"2022-05-20T16:21:08.379902Z","iopub.status.idle":"2022-05-20T16:21:08.402971Z","shell.execute_reply.started":"2022-05-20T16:21:08.379877Z","shell.execute_reply":"2022-05-20T16:21:08.40221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nload_img(path+'dog.4841.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:08.404432Z","iopub.execute_input":"2022-05-20T16:21:08.404687Z","iopub.status.idle":"2022-05-20T16:21:08.484383Z","shell.execute_reply.started":"2022-05-20T16:21:08.404653Z","shell.execute_reply":"2022-05-20T16:21:08.482063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = []\nfor filename in filenames:\n    if filename.split('.')[0] =='cat':\n        label.append('cat')\n    else:\n        label.append('dog')","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:08.485347Z","iopub.execute_input":"2022-05-20T16:21:08.485605Z","iopub.status.idle":"2022-05-20T16:21:08.506685Z","shell.execute_reply.started":"2022-05-20T16:21:08.485573Z","shell.execute_reply":"2022-05-20T16:21:08.50602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'name':filenames,'label':label})\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:08.507838Z","iopub.execute_input":"2022-05-20T16:21:08.508622Z","iopub.status.idle":"2022-05-20T16:21:08.53074Z","shell.execute_reply.started":"2022-05-20T16:21:08.50858Z","shell.execute_reply":"2022-05-20T16:21:08.530041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",'r') as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:08.531988Z","iopub.execute_input":"2022-05-20T16:21:08.532239Z","iopub.status.idle":"2022-05-20T16:21:15.19527Z","shell.execute_reply.started":"2022-05-20T16:21:08.532206Z","shell.execute_reply":"2022-05-20T16:21:15.194508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path1 = '/kaggle/working/test1/'\nfilenames_test = os.listdir(path1)\nfilenames_test[:5]","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:15.196594Z","iopub.execute_input":"2022-05-20T16:21:15.196891Z","iopub.status.idle":"2022-05-20T16:21:15.211478Z","shell.execute_reply.started":"2022-05-20T16:21:15.196853Z","shell.execute_reply":"2022-05-20T16:21:15.210738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-05-20T18:32:36.58996Z","iopub.execute_input":"2022-05-20T18:32:36.590214Z","iopub.status.idle":"2022-05-20T18:32:36.673103Z","shell.execute_reply.started":"2022-05-20T18:32:36.590181Z","shell.execute_reply":"2022-05-20T18:32:36.672354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, test_val = train_test_split(df, test_size=0.2, stratify=df['label'], random_state=17)\ntest, val = train_test_split(test_val, test_size=0.5,  stratify=test_val['label'], random_state=17)","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:15.307154Z","iopub.execute_input":"2022-05-20T16:21:15.307467Z","iopub.status.idle":"2022-05-20T16:21:15.356238Z","shell.execute_reply.started":"2022-05-20T16:21:15.307432Z","shell.execute_reply":"2022-05-20T16:21:15.355508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aug_gen = ImageDataGenerator(rescale = 1./255,\n                               shear_range = 0.2,\n                               zoom_range = 0.2,\n                               rotation_range=40,\n                               width_shift_range=0.2,\n                               height_shift_range=0.2,\n                               horizontal_flip=True,\n                               fill_mode='nearest'\n                              )\ntrain_data = aug_gen.flow_from_dataframe(train,\n                                         directory=path,\n                                         x_col='name',\n                                         y_col='label',\n                                         class_mode='binary',\n                                         target_size=(224,224),\n                                         seed=17\n                                        )","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:15.357806Z","iopub.execute_input":"2022-05-20T16:21:15.358104Z","iopub.status.idle":"2022-05-20T16:21:15.563071Z","shell.execute_reply.started":"2022-05-20T16:21:15.358068Z","shell.execute_reply":"2022-05-20T16:21:15.56173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_gen = ImageDataGenerator(rescale=1./255)\nval_data = val_gen.flow_from_dataframe(val,\n                                       directory=path,\n                                       x_col='name',\n                                       y_col='label',\n                                       class_mode='binary',\n                                       target_size=(224,224),\n                                       seed=17  \n                                      )","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:15.564509Z","iopub.execute_input":"2022-05-20T16:21:15.564802Z","iopub.status.idle":"2022-05-20T16:21:15.600988Z","shell.execute_reply.started":"2022-05-20T16:21:15.564766Z","shell.execute_reply":"2022-05-20T16:21:15.60024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\n\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512, activation='relu', kernel_regularizer=regularizers.l2(0.001)))\nmodel.add(layers.Dropout(0.2))\nmodel.add(layers.Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:15.602294Z","iopub.execute_input":"2022-05-20T16:21:15.602625Z","iopub.status.idle":"2022-05-20T16:21:18.84309Z","shell.execute_reply.started":"2022-05-20T16:21:15.602584Z","shell.execute_reply":"2022-05-20T16:21:18.842317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics='acc')","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:18.844343Z","iopub.execute_input":"2022-05-20T16:21:18.844732Z","iopub.status.idle":"2022-05-20T16:21:18.858149Z","shell.execute_reply.started":"2022-05-20T16:21:18.844694Z","shell.execute_reply":"2022-05-20T16:21:18.857261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_data,validation_data = val_data, epochs = 60,callbacks = [EarlyStopping(monitor='val_acc', min_delta=0.001, patience=5, verbose=1)])","metadata":{"execution":{"iopub.status.busy":"2022-05-20T16:21:18.860581Z","iopub.execute_input":"2022-05-20T16:21:18.860802Z","iopub.status.idle":"2022-05-20T18:32:36.207077Z","shell.execute_reply.started":"2022-05-20T16:21:18.860776Z","shell.execute_reply":"2022-05-20T18:32:36.206402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['acc']\nval_acc = history.history['val_acc']\n\nplt.figure(figsize=(15,8))\nplt.plot(acc, label='Train acc')\nplt.plot(val_acc,'--', label='Val acc')\nplt.title('Training and validation accuracy')\nplt.legend();","metadata":{"execution":{"iopub.status.busy":"2022-05-20T18:32:36.208749Z","iopub.execute_input":"2022-05-20T18:32:36.209017Z","iopub.status.idle":"2022-05-20T18:32:36.432604Z","shell.execute_reply.started":"2022-05-20T18:32:36.208965Z","shell.execute_reply":"2022-05-20T18:32:36.431844Z"},"trusted":true},"execution_count":null,"outputs":[]}]}