{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport zipfile\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir(\"../input\"))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with zipfile.ZipFile('../input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/tmp/dogs-vs-cats/train')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall('/tmp/dogs-vs-cats/test')","execution_count":null,"outputs":[]},{"metadata":{"id":"1N6KBdsRdiMU","trusted":true},"cell_type":"code","source":"base_dir = \"/tmp/dogs-vs-cats\"\ntrain_dir = os.path.join(base_dir, 'train/train')\ntest_dir = os.path.join(base_dir, 'test/test')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filenames = os.listdir(\"/tmp/dogs-vs-cats/train/train\")","execution_count":null,"outputs":[]},{"metadata":{"id":"BQybzpqXJxe5","trusted":true},"cell_type":"code","source":"\ncategories = []\nfor filename in filenames:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append(\"dog\")\n    else:\n        categories.append(\"cat\")\n\ndf = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})","execution_count":null,"outputs":[]},{"metadata":{"id":"tva1G717nPTl","outputId":"f0c38778-a41f-4621-d3ad-18f9c55e2dc1","trusted":true},"cell_type":"code","source":"df.tail()","execution_count":null,"outputs":[]},{"metadata":{"id":"1P3x7YBVnPXX","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"id":"J7rBR4MnzG5Y","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,Reshape,Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","execution_count":null,"outputs":[]},{"metadata":{"id":"ZeRPQ8WkR0OP","trusted":true},"cell_type":"code","source":"model = Sequential([\n    Conv2D(filters=32,kernel_size=(3,3), input_shape = (64, 64, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n\n    Conv2D(filters=32,kernel_size=(3,3), activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.25),\n\n    Conv2D(filters=64,kernel_size=(3,3), activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n    Dropout(0.25),\n\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.25),\n    Dense(1, activation='sigmoid')\n])","execution_count":null,"outputs":[]},{"metadata":{"id":"4WR9o0tLS4dc","trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"id":"SEtzA3q6S51Q","trusted":true},"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, TensorBoard","execution_count":null,"outputs":[]},{"metadata":{"id":"XJmxv7nBugSU","trusted":true},"cell_type":"code","source":"rm -rf logs","execution_count":null,"outputs":[]},{"metadata":{"id":"CO4NEbidujbd","trusted":true},"cell_type":"code","source":"%load_ext tensorboard\nlog_folder = 'logs'","execution_count":null,"outputs":[]},{"metadata":{"id":"3BMM1NcbS74a","trusted":true},"cell_type":"code","source":"callbacks = [\n            # EarlyStopping(patience = 10,monitor=\"accuracy\",mode=\"max\"),\n            TensorBoard(log_dir=log_folder)\n    \n            ]","execution_count":null,"outputs":[]},{"metadata":{"id":"FkBk4zdJS92T","trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255, \n                                   shear_range=0.2,\n                                   zoom_range=0.2, \n                                   horizontal_flip=True,\n                                  width_shift_range=0.1,\n                                  height_shift_range=0.1\n                                   )","execution_count":null,"outputs":[]},{"metadata":{"id":"BGGv2AD2uv-x","trusted":true},"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"a5nJMbdXUQSO","trusted":true},"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"id":"lK58DfBwnxxo","outputId":"656dc4bd-3df9-409e-ce23-caea11725eaa","trusted":true},"cell_type":"code","source":"train_dir","execution_count":null,"outputs":[]},{"metadata":{"id":"gBIkVa9oUbNg","outputId":"191979b3-6372-4163-f364-a02b5796df49","trusted":true},"cell_type":"code","source":"training_set = train_datagen.flow_from_dataframe(\n                                                train_df,\n                                                train_dir,\n                                                 seed=101,                                                 \n                                                 target_size=(64, 64),\n                                                 batch_size=32,\n                                                 x_col='filename',\n                                                 y_col='category',\n                                                 class_mode='binary')","execution_count":null,"outputs":[]},{"metadata":{"id":"MNf4Mizhvxgx","outputId":"1524eef9-b590-4957-90eb-8a47e44d0183","trusted":true},"cell_type":"code","source":"validation_data = validation_datagen.flow_from_dataframe(\n                                                validate_df,\n                                                train_dir,\n                                                 seed=101,                                                 \n                                                 target_size=(64, 64),\n                                                 batch_size=32,\n                                                 x_col='filename',\n                                                 y_col='category',\n                                                 class_mode='binary')","execution_count":null,"outputs":[]},{"metadata":{"id":"JyHpIBqQXdlw","outputId":"d82db1e4-eadb-4fe7-854f-2584d3e2de30","trusted":true},"cell_type":"code","source":"history = model.fit(\n          training_set,\n          epochs=25, \n          validation_data=validation_data,\n          callbacks=callbacks)","execution_count":null,"outputs":[]},{"metadata":{"id":"2DA6OJQezwZY","trusted":false},"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']","execution_count":null,"outputs":[]},{"metadata":{"id":"53i_eRlHlPVt","trusted":false},"cell_type":"code","source":"import pandas as pd\nlosses = pd.DataFrame(history.history)","execution_count":null,"outputs":[]},{"metadata":{"id":"4v2s1SrQlSKt","outputId":"4450f7cc-5d32-4c68-a23e-95d6a6e5777c","trusted":false},"cell_type":"code","source":"losses.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"JDgdu0P5lTdl","outputId":"0c80327f-fd2c-4970-aa31-862e3cfa8d4e","trusted":false},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(8,6))\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nlosses['loss'].plot(label='Training Loss')\nlosses['val_loss'].plot(label='Validation Loss')\nplt.legend(loc='center')","execution_count":null,"outputs":[]},{"metadata":{"id":"mRTyiJ9yXtn9","trusted":false},"cell_type":"code","source":"import numpy as np\nfrom keras.preprocessing import image","execution_count":null,"outputs":[]},{"metadata":{"id":"wVVSv-ThwCdp","trusted":true},"cell_type":"code","source":"test_filenames = os.listdir(\"/tmp/dogs-vs-cats/test/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})","execution_count":null,"outputs":[]},{"metadata":{"id":"_oDkarVJJ1Vy","outputId":"4f89accc-5ffb-412c-ff0a-67b39f14734f","trusted":true},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"e5YcG3r7wCgz","trusted":true},"cell_type":"code","source":"nb_samples = test_df.shape[0]\n","execution_count":null,"outputs":[]},{"metadata":{"id":"u6ho8hSrwClO","outputId":"4b25f58a-f8d5-4c35-8631-0a40086725ae","trusted":true},"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/tmp/dogs-vs-cats/test/test1\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=(64,64),\n    batch_size=32,\n    shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{"id":"sjorOO5cx-YO","trusted":false},"cell_type":"code","source":"predictions = model.predict(test_generator)","execution_count":null,"outputs":[]},{"metadata":{"id":"8ArXDI8hJ7gl","outputId":"5251e758-0299-4678-a77c-27a6a75ced77","trusted":false},"cell_type":"code","source":"predictions","execution_count":null,"outputs":[]},{"metadata":{"id":"BjaYcZxrKPHD","trusted":false},"cell_type":"code","source":"categories = []\nfor prediction in predictions:\n  if prediction >= 0.5:\n    label=1\n    categories.append(label)\n  else:\n    label=0\n    categories.append(label)","execution_count":null,"outputs":[]},{"metadata":{"id":"1ql3OKNjNHjp","outputId":"ca75b285-238a-4b86-95e1-f15f82ce550d","trusted":false},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"PGvVKYG1NQW8","trusted":false},"cell_type":"code","source":"\ndef get_filenames(filename):\n  image = filename.split('.')[0]\n  return image","execution_count":null,"outputs":[]},{"metadata":{"id":"ejpvEOBdO1cq","trusted":false},"cell_type":"code","source":"images = list(test_df[\"filename\"].apply(get_filenames))","execution_count":null,"outputs":[]},{"metadata":{"id":"u3ls055YOWcO","trusted":false},"cell_type":"code","source":"submission_df = pd.DataFrame({\n    'id': images,\n    'label': categories\n})","execution_count":null,"outputs":[]},{"metadata":{"id":"dxNMxhhONx3H","outputId":"c8c13577-761b-4ac1-8c58-2582e3b8d833","trusted":false},"cell_type":"code","source":"submission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"V4EixPoWPCXV","trusted":false},"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"id":"rTL-m11WPCaL","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"o49t9QkUou1N","trusted":false},"cell_type":"code","source":"%tensorboard --logdir={log_folder}\n# %tensorboard line magic to start","execution_count":null,"outputs":[]},{"metadata":{"id":"PQJUgJNEuQaz","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}