{"cells":[{"metadata":{"trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"id":"QYp_UCh6zHlp","trusted":true},"cell_type":"code","source":"import zipfile\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport random\nimport os\nimport time\nimport csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"id":"vZ3x4O9R-qEV","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n#keras / tensorflow\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.layers import MaxPooling2D, BatchNormalization, Conv2D, Dropout, Flatten, Dense\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.callbacks import EarlyStopping\n\nfrom keras.models import load_model\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import shutil\ndef make_directory(dir_path):\n    if os.path.exists(dir_path):\n        shutil.rmtree(dir_path)\n    os.makedirs(dir_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_path = '../output/dogs-vs-cats'","execution_count":null,"outputs":[]},{"metadata":{"id":"ssoYVAcI7IwR","trusted":true},"cell_type":"code","source":"file_test = '../input/dogs-vs-cats/test1.zip'\nfile_train = '../input/dogs-vs-cats/train.zip'","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(data_path)\n\n# extract test data\nwith zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall(data_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path = os.path.sep.join([data_path, 'train/'])\ntest_path = os.path.sep.join([data_path, 'test1/'])\ntrain_files = os.listdir(train_path)\ntest_files = os.listdir(test_path)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filelist = os.listdir(train_path)","execution_count":null,"outputs":[]},{"metadata":{"id":"AVqt7_3Qae1I","trusted":true},"cell_type":"code","source":"categories = []\nfor filename in filelist:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\ndf = pd.DataFrame({\n    'filename': filelist,\n    'category': categories\n})","execution_count":null,"outputs":[]},{"metadata":{"id":"e9j4DXSPF6mT","outputId":"37645ea2-7fad-4abd-d90c-fd6a098ed882","trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"BFiXxgRQGapi","outputId":"41678398-919d-43e3-f621-ffb3fdebcf82","trusted":true},"cell_type":"code","source":"df.tail()","execution_count":null,"outputs":[]},{"metadata":{"id":"PKlc1FTabOtF","trusted":true},"cell_type":"code","source":"df[\"category\"] = df[\"category\"].replace({0: 'cat', 1: 'dog'}) \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path","execution_count":null,"outputs":[]},{"metadata":{"id":"4T4ZWltcGbmk","outputId":"d84d6859-8f67-45bc-d287-7a9c41bdb68d","trusted":true},"cell_type":"code","source":"sample = random.choice(filelist)\nimg_sample = load_img(train_path+sample)\nplt.imshow(img_sample)","execution_count":null,"outputs":[]},{"metadata":{"id":"gcDkeh9JIbe_","trusted":true},"cell_type":"code","source":"#splitting the data in train and validation\n\ntrain, val = train_test_split(df, test_size = 0.2, random_state = 1)","execution_count":null,"outputs":[]},{"metadata":{"id":"pzs3Wo_xKJ2y","trusted":true},"cell_type":"code","source":"train = train.reset_index(drop = True)\nval = val.reset_index(drop = True)","execution_count":null,"outputs":[]},{"metadata":{"id":"BRNXR6BFJ9wW","trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rotation_range = 10,\n                                   width_shift_range = 0.2,\n                                   height_shift_range = 0.1,\n                                   shear_range = 0.1,\n                                   zoom_range = 0.1,\n                                   rescale = 1./255)","execution_count":null,"outputs":[]},{"metadata":{"id":"CFY5uC3RW8Xv","trusted":true},"cell_type":"code","source":"img_res = (200, 200)\nbatch_size = 64","execution_count":null,"outputs":[]},{"metadata":{"id":"JZt3LUryJ_dc","trusted":true},"cell_type":"code","source":"val_datagen = ImageDataGenerator(rescale = 1./255)","execution_count":null,"outputs":[]},{"metadata":{"id":"h-j8uSBsKAUC","outputId":"827a2c0e-948c-436c-91b5-e830f19211ba","trusted":true},"cell_type":"code","source":"train_gen = train_datagen.flow_from_dataframe(train, directory = train_path,\n                                              x_col = 'filename',\n                                              y_col = 'category',\n                                              target_size = img_res,\n                                              class_mode = 'binary',\n                                              seed = 1,\n                                              batch_size = batch_size)","execution_count":null,"outputs":[]},{"metadata":{"id":"kPt-nHyTXUg3","outputId":"a445bf3a-0e5a-412b-f6d0-d2d2ab2e4c8d","trusted":true},"cell_type":"code","source":"val_gen = val_datagen.flow_from_dataframe(val, directory = train_path,\n                                          class_mode = 'binary',\n                                          x_col = 'filename',\n                                          y_col = 'category',\n                                          target_size = img_res,\n                                          seed = 1,\n                                          batch_size =batch_size\n                                          )","execution_count":null,"outputs":[]},{"metadata":{"id":"DZFeRIBf_dsf"},"cell_type":"markdown","source":"# Model\n- We will use convolutional networks as they are good for image classification\n- Total 4 hidden layers are used with BatchNormalizatio, MaxPooling2D and dropouts for each block\n- HeUniform initialization is used for all the layers\n- activation used is relu for all layers except output layer which uses sigmoid"},{"metadata":{"id":"BhpkZWF4_aXo","trusted":true},"cell_type":"code","source":"model = tf.keras.Sequential([\n                             Conv2D(32, (3,3), activation = 'relu', input_shape = (200, 200, 3),\n                                    kernel_initializer = tf.keras.initializers.HeUniform()),\n                             BatchNormalization(),\n                             MaxPooling2D(),\n                             Dropout(0.2),\n\n                             Conv2D(64, (3,3), activation = 'relu',\n                                    kernel_initializer = tf.keras.initializers.HeUniform(), kernel_regularizer = tf.keras.regularizers.l2(l2=0.01)),\n                             BatchNormalization(),\n                             MaxPooling2D(),\n                             Dropout(0.3),\n\n                             Conv2D(128, (3,3), activation = 'relu', kernel_initializer = tf.keras.initializers.HeUniform(), kernel_regularizer = tf.keras.regularizers.l2(l2=0.01,)),\n                             BatchNormalization(),\n                             MaxPooling2D(),\n                             Dropout(0.3),\n\n                             Conv2D(128, (3,3), activation = 'relu', kernel_initializer= tf.keras.initializers.HeUniform(), kernel_regularizer = tf.keras.regularizers.l2(l2=0.01)),\n                             BatchNormalization(),\n                             MaxPooling2D(),\n                             Dropout(0.3),\n\n                             Conv2D(256, (3,3), activation = 'relu', kernel_initializer= tf.keras.initializers.HeUniform()),\n                             BatchNormalization(),\n                             MaxPooling2D(),\n                             Dropout(0.5),\n\n                             Flatten(),\n                             Dense(1, activation= 'sigmoid')\n\n\n])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Optimizer used is RMSprop as it gave better accuracy compared to Adam"},{"metadata":{"id":"VVSFs3q9YH8e","trusted":true},"cell_type":"code","source":"optimizer = tf.keras.optimizers.RMSprop(learning_rate = 0.0001, momentum= 0.9)","execution_count":null,"outputs":[]},{"metadata":{"id":"VsyHHd9Cbvop","trusted":true},"cell_type":"code","source":"model.compile(optimizer = optimizer, loss = 'binary_crossentropy', metrics = ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"id":"a5CouFRbcDHh","outputId":"d89afd0b-543e-4dac-b966-d45b1e74ddf0","trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"id":"4VGt9pvvKh-N","trusted":false},"cell_type":"code","source":"#callbacks\ncallbacks_list = []\n\n#checkpoint all\n# checkpoint_path = '/content/gdrive/My Drive/dataset/checkpoints'\n# best = '/best'\n# all = '/all'\n\n# checkpoint_all = ModelCheckpoint(checkpoint_path+all, monitor = 'loss', verbose = 1,\n#                              mode = 'min')\n# callbacks_list.append(checkpoint_all)\n\n# #checkpoint_best\n# checkpoint_best = ModelCheckpoint(checkpoint_path+best, monitor = 'loss', verbose = 1, save_best_only = True,\n#                                   mode = 'auto', save_freq = 4, )\n\n# callbacks_list.append(checkpoint_best)\n\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3,\n                              patience=2, min_lr=0.000001)\ncallbacks_list.append(reduce_lr)\n\n\nearly_stop = EarlyStopping(monitor = 'val_loss', patience = 10, verbose = 1)\ncallbacks_list.append(early_stop)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"I-Ak9ZiqcL6F","outputId":"f041b22d-57c7-4b32-e909-1631698cf185","trusted":false},"cell_type":"code","source":"callbacks_list","execution_count":null,"outputs":[]},{"metadata":{"id":"W7WzCTO0dQkL","trusted":false},"cell_type":"code","source":"e = 50","execution_count":null,"outputs":[]},{"metadata":{"id":"_OzKdsQbcHOM","outputId":"7fd9b32a-db70-44fc-dc57-6027624588a8","trusted":false},"cell_type":"code","source":"\n\nhistory = model.fit(train_gen,\n                    epochs = e,\n                    validation_data = val_gen,\n                    steps_per_epoch = len(train_gen),\n                    validation_steps = len(val_gen),\n                    callbacks = callbacks_list\n                    )\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"id":"TLmonqu8CQ_e","trusted":false},"cell_type":"code","source":"model.save('model.h5')","execution_count":null,"outputs":[]},{"metadata":{"id":"t9eRD_syCUcG","trusted":false},"cell_type":"code","source":"def summary_plot(history):\n  acc = history.history['accuracy']\n  val_acc = history.history['val_accuracy']\n\n  loss = history.history['loss']\n  val_loss = history.history['val_loss']\n\n  plt.figure(figsize=(10,10))\n  plt.subplot(211)\n  plt.title('Loss')\n  plt.plot(loss, color = 'red', label = 'loss')\n  plt.plot(loss, val_loss, color = 'blue', label = 'val_loss')\n  plt.legend()\n  \n\n  plt.subplot(212)\n  plt.title('Accuracy')\n  plt.plot(acc, color = 'red', label = 'acc')\n  plt.plot(val_acc, color = 'blue', label = 'val_acc')\n  plt.legend()","execution_count":null,"outputs":[]},{"metadata":{"id":"5aBN6nzUCqpb","outputId":"ef2ae8d1-0c05-4322-a64a-99fca155c656","trusted":false},"cell_type":"code","source":"summary_plot(history)","execution_count":null,"outputs":[]},{"metadata":{"id":"c2Kj1ihWB1hI","trusted":true},"cell_type":"code","source":"#test data preparation\n\ntest_files = os.listdir(test_path)\ndf_test = pd.DataFrame({\n    'filename': test_files\n})\nsamples = df_test.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"id":"xJV1pygVB1z3","outputId":"14227a17-4c61-4d03-fafd-a11e122ccee3","trusted":true},"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale = 1./255)\n\ntest_gen = test_datagen.flow_from_dataframe(df_test,\n                                            test_path,\n                                            x_col = 'filename',\n                                            y_col = None,\n                                            class_mode = None,\n                                            target_size = img_res, \n                                            batch_size = batch_size,\n                                            shuffle = False)\n","execution_count":null,"outputs":[]},{"metadata":{"id":"FjsLLkxmB1o1","outputId":"1feded2c-7528-4325-8d62-e291d3bf05e3","trusted":false},"cell_type":"code","source":"predict = model.predict(test_gen, steps = np.ceil(samples/batch_size), verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"id":"HcOCGzSyce4r","outputId":"cf3f28b0-a7d0-4f59-8165-6a90f708a86f","trusted":false},"cell_type":"code","source":"predict","execution_count":null,"outputs":[]},{"metadata":{"id":"6AryG9YbsXff","trusted":false},"cell_type":"code","source":"prediction = [1 if y > 0.5 else 0 for y in predict]","execution_count":null,"outputs":[]},{"metadata":{"id":"5bW8M_l2B1V3","trusted":false},"cell_type":"code","source":"test_df = df_test.copy()","execution_count":null,"outputs":[]},{"metadata":{"id":"_O5ObxPCsxkv","outputId":"02deb843-eeb0-4146-c045-472379c5e45d","trusted":false},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"rWNkK1w3cLz4","trusted":false},"cell_type":"code","source":"test_df['category'] = prediction","execution_count":null,"outputs":[]},{"metadata":{"id":"66n0bmWZjHWS","outputId":"8633d90d-4f3c-4c01-cf5f-b8527a6f146d","trusted":false},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"lXaI6NEhcWhN","outputId":"9732d425-f3fd-4219-eaa0-6960e245288d","trusted":false},"cell_type":"code","source":"test_df['category'].unique()","execution_count":null,"outputs":[]},{"metadata":{"id":"bNW76zwMcMHW","trusted":false},"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_gen.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","execution_count":null,"outputs":[]},{"metadata":{"id":"-r43W8UGcMQ_","trusted":false},"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })\n","execution_count":null,"outputs":[]},{"metadata":{"id":"4y43nZX_cL6R","outputId":"7eeec4f1-2828-4f99-90c1-531ca4211b04","trusted":false},"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()\n","execution_count":null,"outputs":[]},{"metadata":{"id":"pzTjj0_rcLro","outputId":"7257b897-a723-4e23-be52-c3cde49e5291","trusted":true,"collapsed":true},"cell_type":"code","source":"start = 36\nend = start + 18\nsample_test = test_df[start:end]\nsample_test.reset_index(inplace = True)\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = row['category']\n    img = load_img(test_path+filename, target_size=img_res)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')' )\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"id":"Aw0dYn3hFSlG","outputId":"ee7232b5-0b06-45d3-f3f8-96d0611ec068","trusted":false},"cell_type":"code","source":"#submission\n\nsubmission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","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}