{"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-16T04:59:00.919769Z","iopub.execute_input":"2022-05-16T04:59:00.920114Z","iopub.status.idle":"2022-05-16T04:59:07.743082Z","shell.execute_reply.started":"2022-05-16T04:59:00.920029Z","shell.execute_reply":"2022-05-16T04:59:07.742280Z"},"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-16T04:59:07.744886Z","iopub.execute_input":"2022-05-16T04:59:07.745152Z","iopub.status.idle":"2022-05-16T04:59:07.752155Z","shell.execute_reply.started":"2022-05-16T04:59:07.745118Z","shell.execute_reply":"2022-05-16T04:59:07.751481Z"},"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-16T04:59:07.753660Z","iopub.execute_input":"2022-05-16T04:59:07.754136Z","iopub.status.idle":"2022-05-16T04:59:19.625315Z","shell.execute_reply.started":"2022-05-16T04:59:07.754096Z","shell.execute_reply":"2022-05-16T04:59:19.624414Z"},"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-16T04:59:19.627837Z","iopub.execute_input":"2022-05-16T04:59:19.628145Z","iopub.status.idle":"2022-05-16T04:59:19.657204Z","shell.execute_reply.started":"2022-05-16T04:59:19.628102Z","shell.execute_reply":"2022-05-16T04:59:19.656263Z"},"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-16T04:59:19.658872Z","iopub.execute_input":"2022-05-16T04:59:19.659204Z","iopub.status.idle":"2022-05-16T04:59:19.751746Z","shell.execute_reply.started":"2022-05-16T04:59:19.659168Z","shell.execute_reply":"2022-05-16T04:59:19.750673Z"},"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-16T04:59:19.753101Z","iopub.execute_input":"2022-05-16T04:59:19.754534Z","iopub.status.idle":"2022-05-16T04:59:19.781171Z","shell.execute_reply.started":"2022-05-16T04:59:19.754443Z","shell.execute_reply":"2022-05-16T04:59:19.780289Z"},"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-16T04:59:19.783716Z","iopub.execute_input":"2022-05-16T04:59:19.783953Z","iopub.status.idle":"2022-05-16T04:59:19.809976Z","shell.execute_reply.started":"2022-05-16T04:59:19.783927Z","shell.execute_reply":"2022-05-16T04:59:19.809150Z"},"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-16T04:59:19.813287Z","iopub.execute_input":"2022-05-16T04:59:19.813537Z","iopub.status.idle":"2022-05-16T04:59:25.105022Z","shell.execute_reply.started":"2022-05-16T04:59:19.813500Z","shell.execute_reply":"2022-05-16T04:59:25.104179Z"},"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-16T04:59:25.106526Z","iopub.execute_input":"2022-05-16T04:59:25.106800Z","iopub.status.idle":"2022-05-16T04:59:25.121189Z","shell.execute_reply.started":"2022-05-16T04:59:25.106764Z","shell.execute_reply":"2022-05-16T04:59:25.120295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"load_img(path1+'5225.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-05-16T04:59:25.124429Z","iopub.execute_input":"2022-05-16T04:59:25.124698Z","iopub.status.idle":"2022-05-16T04:59:25.217826Z","shell.execute_reply.started":"2022-05-16T04:59:25.124663Z","shell.execute_reply":"2022-05-16T04:59:25.216989Z"},"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-16T04:59:25.219378Z","iopub.execute_input":"2022-05-16T04:59:25.219627Z","iopub.status.idle":"2022-05-16T04:59:25.273269Z","shell.execute_reply.started":"2022-05-16T04:59:25.219589Z","shell.execute_reply":"2022-05-16T04:59:25.272433Z"},"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-16T04:59:25.275589Z","iopub.execute_input":"2022-05-16T04:59:25.276139Z","iopub.status.idle":"2022-05-16T04:59:25.491574Z","shell.execute_reply.started":"2022-05-16T04:59:25.276099Z","shell.execute_reply":"2022-05-16T04:59:25.490222Z"},"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-16T04:59:25.492978Z","iopub.execute_input":"2022-05-16T04:59:25.493222Z","iopub.status.idle":"2022-05-16T04:59:25.529513Z","shell.execute_reply.started":"2022-05-16T04:59:25.493188Z","shell.execute_reply":"2022-05-16T04:59:25.528644Z"},"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-16T04:59:25.531037Z","iopub.execute_input":"2022-05-16T04:59:25.531303Z","iopub.status.idle":"2022-05-16T04:59:28.600864Z","shell.execute_reply.started":"2022-05-16T04:59:25.531266Z","shell.execute_reply":"2022-05-16T04:59:28.600078Z"},"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-16T04:59:28.602235Z","iopub.execute_input":"2022-05-16T04:59:28.602685Z","iopub.status.idle":"2022-05-16T04:59:28.615555Z","shell.execute_reply.started":"2022-05-16T04:59:28.602649Z","shell.execute_reply":"2022-05-16T04:59:28.614759Z"},"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-16T04:59:28.617509Z","iopub.execute_input":"2022-05-16T04:59:28.618013Z","iopub.status.idle":"2022-05-16T07:56:56.842147Z","shell.execute_reply.started":"2022-05-16T04:59:28.617966Z","shell.execute_reply":"2022-05-16T07:56:56.841283Z"},"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-16T07:56:56.843780Z","iopub.execute_input":"2022-05-16T07:56:56.844129Z","iopub.status.idle":"2022-05-16T07:56:57.089886Z","shell.execute_reply.started":"2022-05-16T07:56:56.844088Z","shell.execute_reply":"2022-05-16T07:56:57.089156Z"},"trusted":true},"execution_count":null,"outputs":[]}]}