{"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-27T15:23:11.783884Z","iopub.execute_input":"2022-05-27T15:23:11.784272Z","iopub.status.idle":"2022-05-27T15:23:20.114085Z","shell.execute_reply.started":"2022-05-27T15:23:11.784184Z","shell.execute_reply":"2022-05-27T15:23:20.113166Z"},"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-26T13:25:18.581193Z","iopub.execute_input":"2022-05-26T13:25:18.581481Z","iopub.status.idle":"2022-05-26T13:25:18.586127Z","shell.execute_reply.started":"2022-05-26T13:25:18.581448Z","shell.execute_reply":"2022-05-26T13:25:18.585316Z"},"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-26T13:27:04.883689Z","iopub.execute_input":"2022-05-26T13:27:04.884202Z","iopub.status.idle":"2022-05-26T13:27:16.693296Z","shell.execute_reply.started":"2022-05-26T13:27:04.884157Z","shell.execute_reply":"2022-05-26T13:27:16.69246Z"},"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-26T13:27:23.416157Z","iopub.execute_input":"2022-05-26T13:27:23.417061Z","iopub.status.idle":"2022-05-26T13:27:23.441776Z","shell.execute_reply.started":"2022-05-26T13:27:23.417009Z","shell.execute_reply":"2022-05-26T13:27:23.440858Z"},"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-26T13:27:43.616809Z","iopub.execute_input":"2022-05-26T13:27:43.617351Z","iopub.status.idle":"2022-05-26T13:27:43.706148Z","shell.execute_reply.started":"2022-05-26T13:27:43.617309Z","shell.execute_reply":"2022-05-26T13:27:43.703981Z"},"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-26T13:27:53.200158Z","iopub.execute_input":"2022-05-26T13:27:53.200906Z","iopub.status.idle":"2022-05-26T13:27:53.216938Z","shell.execute_reply.started":"2022-05-26T13:27:53.200844Z","shell.execute_reply":"2022-05-26T13:27:53.215938Z"},"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-26T13:27:59.000118Z","iopub.execute_input":"2022-05-26T13:27:59.00046Z","iopub.status.idle":"2022-05-26T13:27:59.031625Z","shell.execute_reply.started":"2022-05-26T13:27:59.000408Z","shell.execute_reply":"2022-05-26T13:27:59.030949Z"},"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-26T13:28:04.431201Z","iopub.execute_input":"2022-05-26T13:28:04.431763Z","iopub.status.idle":"2022-05-26T13:28:11.243931Z","shell.execute_reply.started":"2022-05-26T13:28:04.431722Z","shell.execute_reply":"2022-05-26T13:28:11.243078Z"},"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-26T13:28:15.350571Z","iopub.execute_input":"2022-05-26T13:28:15.350861Z","iopub.status.idle":"2022-05-26T13:28:15.366524Z","shell.execute_reply.started":"2022-05-26T13:28:15.350828Z","shell.execute_reply":"2022-05-26T13:28:15.365596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"load_img(path1+'5225.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-05-26T13:28:20.248535Z","iopub.execute_input":"2022-05-26T13:28:20.249147Z","iopub.status.idle":"2022-05-26T13:28:20.361887Z","shell.execute_reply.started":"2022-05-26T13:28:20.249107Z","shell.execute_reply":"2022-05-26T13:28:20.361173Z"},"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-26T13:28:28.335068Z","iopub.execute_input":"2022-05-26T13:28:28.335511Z","iopub.status.idle":"2022-05-26T13:28:28.410404Z","shell.execute_reply.started":"2022-05-26T13:28:28.335465Z","shell.execute_reply":"2022-05-26T13:28:28.409481Z"},"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-26T13:29:25.470073Z","iopub.execute_input":"2022-05-26T13:29:25.470547Z","iopub.status.idle":"2022-05-26T13:29:25.722048Z","shell.execute_reply.started":"2022-05-26T13:29:25.470511Z","shell.execute_reply":"2022-05-26T13:29:25.721041Z"},"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-26T13:30:03.049154Z","iopub.execute_input":"2022-05-26T13:30:03.049442Z","iopub.status.idle":"2022-05-26T13:30:03.090429Z","shell.execute_reply.started":"2022-05-26T13:30:03.049412Z","shell.execute_reply":"2022-05-26T13:30:03.089595Z"},"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-26T13:30:08.06584Z","iopub.execute_input":"2022-05-26T13:30:08.066346Z","iopub.status.idle":"2022-05-26T13:30:10.742351Z","shell.execute_reply.started":"2022-05-26T13:30:08.066308Z","shell.execute_reply":"2022-05-26T13:30:10.741481Z"},"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-26T13:30:18.449803Z","iopub.execute_input":"2022-05-26T13:30:18.450118Z","iopub.status.idle":"2022-05-26T13:30:18.465312Z","shell.execute_reply.started":"2022-05-26T13:30:18.450085Z","shell.execute_reply":"2022-05-26T13:30:18.464448Z"},"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-26T13:30:22.430844Z","iopub.execute_input":"2022-05-26T13:30:22.43117Z","iopub.status.idle":"2022-05-26T16:04:28.202892Z","shell.execute_reply.started":"2022-05-26T13:30:22.431138Z","shell.execute_reply":"2022-05-26T16:04:28.202092Z"},"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-26T16:32:02.207233Z","iopub.execute_input":"2022-05-26T16:32:02.207527Z","iopub.status.idle":"2022-05-26T16:32:02.484687Z","shell.execute_reply.started":"2022-05-26T16:32:02.207495Z","shell.execute_reply":"2022-05-26T16:32:02.483924Z"},"trusted":true},"execution_count":null,"outputs":[]}]}