{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-29T14:47:50.659342Z","iopub.execute_input":"2021-07-29T14:47:50.659999Z","iopub.status.idle":"2021-07-29T14:47:50.676580Z","shell.execute_reply.started":"2021-07-29T14:47:50.659875Z","shell.execute_reply":"2021-07-29T14:47:50.675358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\n\n# keras\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\nfrom keras import optimizers\nfrom keras import models\nfrom keras import layers\nfrom keras.losses import SparseCategoricalCrossentropy\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# sklearn library\nfrom sklearn.preprocessing import StandardScaler \nfrom sklearn.model_selection import train_test_split \nfrom sklearn.metrics import classification_report ","metadata":{"execution":{"iopub.status.busy":"2021-07-29T14:47:50.678386Z","iopub.execute_input":"2021-07-29T14:47:50.679345Z","iopub.status.idle":"2021-07-29T14:47:59.913612Z","shell.execute_reply.started":"2021-07-29T14:47:50.679284Z","shell.execute_reply":"2021-07-29T14:47:59.912412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip \"../input/dogs-vs-cats-redux-kernels-edition/train.zip\" -d train\n! unzip \"../input/dogs-vs-cats-redux-kernels-edition/test.zip\" -d test","metadata":{"execution":{"iopub.status.busy":"2021-07-29T14:47:59.915931Z","iopub.execute_input":"2021-07-29T14:47:59.916293Z","iopub.status.idle":"2021-07-29T14:48:19.661290Z","shell.execute_reply.started":"2021-07-29T14:47:59.916259Z","shell.execute_reply":"2021-07-29T14:48:19.659858Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# labels\nfile = os.listdir(\"./train/train\")\nLabels = list(map(lambda x: x.split('.')[0], file))\n\n# for filenames\nf = Path(\"./train/train\")\nFile_Path = list(f.glob(r\"**/*.jpg\"))\n\n# dataframe\nFile_Path = pd.Series(File_Path).astype(str)\nLabels = pd.Series(Labels)\ndf = pd.concat([File_Path,Labels],axis=1)\ndf.columns = ['filename', 'category']","metadata":{"execution":{"iopub.status.busy":"2021-07-29T14:48:19.666905Z","iopub.execute_input":"2021-07-29T14:48:19.667521Z","iopub.status.idle":"2021-07-29T14:48:20.291952Z","shell.execute_reply.started":"2021-07-29T14:48:19.667469Z","shell.execute_reply":"2021-07-29T14:48:20.290988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T14:48:20.293187Z","iopub.execute_input":"2021-07-29T14:48:20.293675Z","iopub.status.idle":"2021-07-29T14:48:20.325322Z","shell.execute_reply.started":"2021-07-29T14:48:20.293635Z","shell.execute_reply":"2021-07-29T14:48:20.324419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set, test_data = train_test_split(df, test_size= 1/3, random_state = 42)\ntrain_data, val_data = train_test_split(train_set, test_size= 1/3, random_state = 42)\n\n\nprint(train_data.shape)\nprint(test_data.shape)\nprint(val_data.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:55:22.310939Z","iopub.execute_input":"2021-07-29T15:55:22.311644Z","iopub.status.idle":"2021-07-29T15:55:22.334630Z","shell.execute_reply.started":"2021-07-29T15:55:22.311566Z","shell.execute_reply":"2021-07-29T15:55:22.333350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data.reset_index(drop=True)\ntest_data = test_data.reset_index(drop=True)\nval_data = val_data.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:55:26.639902Z","iopub.execute_input":"2021-07-29T15:55:26.640272Z","iopub.status.idle":"2021-07-29T15:55:26.648343Z","shell.execute_reply.started":"2021-07-29T15:55:26.640241Z","shell.execute_reply":"2021-07-29T15:55:26.647068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = (128, 128)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:55:28.976153Z","iopub.execute_input":"2021-07-29T15:55:28.976672Z","iopub.status.idle":"2021-07-29T15:55:28.981749Z","shell.execute_reply.started":"2021-07-29T15:55:28.976627Z","shell.execute_reply":"2021-07-29T15:55:28.980655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_gen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:55:30.926207Z","iopub.execute_input":"2021-07-29T15:55:30.926886Z","iopub.status.idle":"2021-07-29T15:55:30.934156Z","shell.execute_reply.started":"2021-07-29T15:55:30.926827Z","shell.execute_reply":"2021-07-29T15:55:30.932649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = img_gen.flow_from_dataframe(\n    train_data, \n    x_col='filename',\n    y_col='category',\n    target_size=img_size,\n    class_mode='binary',\n    batch_size=32,\n    shuffle = False\n)\nvalidation_gen = img_gen.flow_from_dataframe(\n    val_data, \n    x_col='filename',\n    y_col='category',\n    target_size=img_size,\n    class_mode='binary',\n    batch_size=32,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:55:32.309391Z","iopub.execute_input":"2021-07-29T15:55:32.309890Z","iopub.status.idle":"2021-07-29T15:55:32.564830Z","shell.execute_reply.started":"2021-07-29T15:55:32.309847Z","shell.execute_reply":"2021-07-29T15:55:32.563342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)))\nmodel.add(layers.MaxPool2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPool2D((2, 2)))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPool2D((2, 2)))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPool2D((2, 2)))\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:55:34.297829Z","iopub.execute_input":"2021-07-29T15:55:34.298333Z","iopub.status.idle":"2021-07-29T15:55:34.453500Z","shell.execute_reply.started":"2021-07-29T15:55:34.298286Z","shell.execute_reply":"2021-07-29T15:55:34.452237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:55:37.962177Z","iopub.execute_input":"2021-07-29T15:55:37.962556Z","iopub.status.idle":"2021-07-29T15:55:37.976435Z","shell.execute_reply.started":"2021-07-29T15:55:37.962523Z","shell.execute_reply":"2021-07-29T15:55:37.975392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_gen,\n                    validation_data = validation_gen,\n                    validation_steps=50,\n                    steps_per_epoch=100,\n                    epochs = 50,\n                    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:56:18.998080Z","iopub.execute_input":"2021-07-29T15:56:18.998475Z","iopub.status.idle":"2021-07-29T16:57:25.875820Z","shell.execute_reply.started":"2021-07-29T15:56:18.998426Z","shell.execute_reply":"2021-07-29T16:57:25.874421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#accuracy\nacc = history.history['acc']\nval_acc = history.history['val_acc']\n\n#loss\nloss = history.history['loss'] \nval_loss = history.history['val_loss']","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:40:30.435768Z","iopub.execute_input":"2021-07-29T15:40:30.436395Z","iopub.status.idle":"2021-07-29T15:40:30.443036Z","shell.execute_reply.started":"2021-07-29T15:40:30.436356Z","shell.execute_reply":"2021-07-29T15:40:30.441433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\n\n# visualising Accuracy \nplt.subplot(2, 1, 1)\n\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\n\nplt.ylabel('Accuracy') \nplt.title('Training and Validation Accuracy') \n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:40:30.445005Z","iopub.execute_input":"2021-07-29T15:40:30.445548Z","iopub.status.idle":"2021-07-29T15:40:30.735044Z","shell.execute_reply.started":"2021-07-29T15:40:30.445497Z","shell.execute_reply":"2021-07-29T15:40:30.733175Z"},"trusted":true},"execution_count":null,"outputs":[]}]}