{"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        break\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":"2022-02-24T09:10:19.601929Z","iopub.execute_input":"2022-02-24T09:10:19.602205Z","iopub.status.idle":"2022-02-24T09:10:35.277945Z","shell.execute_reply.started":"2022-02-24T09:10:19.602112Z","shell.execute_reply":"2022-02-24T09:10:35.277253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport tensorflow as tf\nimport tensorflow_addons as tfa\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom tensorflow.keras import *\nfrom tensorflow.keras import layers\nfrom imgaug import augmenters as iaa","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:10:35.279723Z","iopub.execute_input":"2022-02-24T09:10:35.280136Z","iopub.status.idle":"2022-02-24T09:10:41.942888Z","shell.execute_reply.started":"2022-02-24T09:10:35.280099Z","shell.execute_reply":"2022-02-24T09:10:41.942194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_PATH = \"/kaggle/input/dog-vs-cat-classification/\"\nimg_train_path = list(glob.glob(os.path.join(DATA_PATH, \"train/train/**/*.jpg\")))\nimg_val_path = list(glob.glob(os.path.join(DATA_PATH, \"test/test/*.jpg\")))","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:10:41.944223Z","iopub.execute_input":"2022-02-24T09:10:41.944472Z","iopub.status.idle":"2022-02-24T09:10:42.055349Z","shell.execute_reply.started":"2022-02-24T09:10:41.944439Z","shell.execute_reply":"2022-02-24T09:10:42.054667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"labels = list(map(lambda x: os.path.split(os.path.split(x)[0])[1], img_train_path))\nfiles_path = pd.Series(img_train_path, name=\"train_Path\").astype('str')\nlabels = pd.Series(labels, name='Labels')\ndata_train = pd.concat([files_path, labels], axis=1)\ndata_train = data_train.sample(frac=1).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:10:42.056376Z","iopub.execute_input":"2022-02-24T09:10:42.056604Z","iopub.status.idle":"2022-02-24T09:10:42.146498Z","shell.execute_reply.started":"2022-02-24T09:10:42.056569Z","shell.execute_reply":"2022-02-24T09:10:42.145853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(5, 5, figsize = (8, 8))\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(plt.imread(data_train['train_Path'][i]))\n    ax.set_title(data_train['Labels'][i])\n    ax.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:10:42.148896Z","iopub.execute_input":"2022-02-24T09:10:42.149139Z","iopub.status.idle":"2022-02-24T09:10:43.771587Z","shell.execute_reply.started":"2022-02-24T09:10:42.149107Z","shell.execute_reply":"2022-02-24T09:10:43.769249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (5, 5))\nlabels_count = data_train['Labels'].value_counts()\nsns.barplot(x=labels_count.index, y=labels_count)\nplt.xlabel(\"Labels\")\nplt.ylabel(\"Samples\")\nplt.xticks(rotation=45)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:10:43.772512Z","iopub.execute_input":"2022-02-24T09:10:43.772778Z","iopub.status.idle":"2022-02-24T09:10:43.945271Z","shell.execute_reply.started":"2022-02-24T09:10:43.772737Z","shell.execute_reply":"2022-02-24T09:10:43.944481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.utils.image_dataset_from_directory(os.path.join(DATA_PATH, \"train/train/\"),\n                                                       seed = 42, \n                                                       validation_split=0.2, \n                                                       shuffle = True,\n                                                       image_size = (224, 224),\n                                                       subset=\"training\", batch_size = 64)\nval_ds = tf.keras.utils.image_dataset_from_directory(os.path.join(DATA_PATH, \"train/train/\"),\n                                                     seed = 42, \n                                                     validation_split=0.2, \n                                                     shuffle = True,\n                                                     image_size = (224, 224),\n                                                     subset=\"validation\", batch_size = 64)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:10:43.946594Z","iopub.execute_input":"2022-02-24T09:10:43.946822Z","iopub.status.idle":"2022-02-24T09:10:55.681563Z","shell.execute_reply.started":"2022-02-24T09:10:43.946790Z","shell.execute_reply":"2022-02-24T09:10:55.680850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"baseline_model = tf.keras.applications.efficientnet.EfficientNetB7(include_top = False, weights = \"imagenet\",\n                                                        input_shape = (224,224,3))\n# finetune_milestone = 172\n# for layer in baseline_model.layers[:finetune_milestone]:\n#     layer.trainable = False\nbaseline_model.trainable = False\nbaseline_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:10:55.682659Z","iopub.execute_input":"2022-02-24T09:10:55.684329Z","iopub.status.idle":"2022-02-24T09:11:03.008616Z","shell.execute_reply.started":"2022-02-24T09:10:55.684290Z","shell.execute_reply":"2022-02-24T09:11:03.007914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = tf.keras.Sequential([\n    tf.keras.layers.RandomRotation(0.25),\n    tf.keras.layers.RandomContrast(0.25),\n    tf.keras.layers.RandomFlip(\"horizontal\")\n])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:11:03.009842Z","iopub.execute_input":"2022-02-24T09:11:03.010098Z","iopub.status.idle":"2022-02-24T09:11:03.025233Z","shell.execute_reply.started":"2022-02-24T09:11:03.010064Z","shell.execute_reply":"2022-02-24T09:11:03.024573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.layers.Input(shape = (224, 224, 3))\nx = data_augmentation(inputs)\nx = tf.keras.applications.efficientnet.preprocess_input(x)\nx = baseline_model(x)\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx = tf.keras.layers.Flatten()(x)\nx = tf.keras.layers.Dense(64)(x)\nx = tf.keras.layers.Dropout(0.3)(x)\n\noutputs = tf.keras.layers.Dense(2, activation=\"softmax\")(x)\nmodel = tf.keras.Model(inputs, outputs)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:11:03.026470Z","iopub.execute_input":"2022-02-24T09:11:03.026776Z","iopub.status.idle":"2022-02-24T09:11:05.024282Z","shell.execute_reply.started":"2022-02-24T09:11:03.026742Z","shell.execute_reply":"2022-02-24T09:11:05.023562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(),\n             optimizer = tf.keras.optimizers.RMSprop(learning_rate = 2e-5, momentum = 0.4),\n             metrics = [\"accuracy\"])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:11:05.025576Z","iopub.execute_input":"2022-02-24T09:11:05.025822Z","iopub.status.idle":"2022-02-24T09:11:05.085528Z","shell.execute_reply.started":"2022-02-24T09:11:05.025790Z","shell.execute_reply":"2022-02-24T09:11:05.084843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = [\n            tf.keras.callbacks.ModelCheckpoint(\n            filepath=\"dog_cat_classification.h5\",\n            save_best_only=True,\n            monitor=\"val_loss\"),\n    \n            tf.keras.callbacks.EarlyStopping(\n            monitor='val_loss',\n            min_delta=0.0005,\n            patience=3)\n]","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:11:05.086540Z","iopub.execute_input":"2022-02-24T09:11:05.087283Z","iopub.status.idle":"2022-02-24T09:11:05.092344Z","shell.execute_reply.started":"2022-02-24T09:11:05.087247Z","shell.execute_reply":"2022-02-24T09:11:05.091561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 10\nmodel_history = model.fit(train_ds, epochs=num_epochs, validation_data=val_ds, callbacks=checkpoint)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:11:05.093664Z","iopub.execute_input":"2022-02-24T09:11:05.094022Z","iopub.status.idle":"2022-02-24T09:43:40.840154Z","shell.execute_reply.started":"2022-02-24T09:11:05.093987Z","shell.execute_reply":"2022-02-24T09:43:40.839425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(model_history.history['accuracy'], label='accuracy')\nplt.plot(model_history.history['val_accuracy'], label='val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Model val accuracy')\nplt.legend()\nplt.grid(True)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:43:40.842807Z","iopub.execute_input":"2022-02-24T09:43:40.843008Z","iopub.status.idle":"2022-02-24T09:43:41.071665Z","shell.execute_reply.started":"2022-02-24T09:43:40.842983Z","shell.execute_reply":"2022-02-24T09:43:41.071022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = os.listdir(DATA_PATH + \"/test/test\")\ntest_df = pd.DataFrame({\"id\": test_imgs})\nprint(test_df.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:43:41.072788Z","iopub.execute_input":"2022-02-24T09:43:41.073048Z","iopub.status.idle":"2022-02-24T09:43:41.082437Z","shell.execute_reply.started":"2022-02-24T09:43:41.073014Z","shell.execute_reply":"2022-02-24T09:43:41.081573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nimg_test_gen = ImageDataGenerator()\nimg_test_generator = img_test_gen.flow_from_dataframe(test_df,\n                                                     DATA_PATH + \"/test/test\",\n                                                     x_col = \"id\",\n                                                     y_col = None,\n                                                     target_size = (224, 224),\n                                                     batch_size = 64,\n                                                     class_mode = None,\n                                                     shuffle = False)\nprediction = model.predict(img_test_generator)\ntest_df['labels'] = np.argmax(prediction, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:43:41.083887Z","iopub.execute_input":"2022-02-24T09:43:41.084332Z","iopub.status.idle":"2022-02-24T09:44:50.452358Z","shell.execute_reply.started":"2022-02-24T09:43:41.084297Z","shell.execute_reply":"2022-02-24T09:44:50.451560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.set_index('id')\ntest_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:44:50.453470Z","iopub.execute_input":"2022-02-24T09:44:50.453667Z","iopub.status.idle":"2022-02-24T09:44:50.475111Z","shell.execute_reply.started":"2022-02-24T09:44:50.453643Z","shell.execute_reply":"2022-02-24T09:44:50.474484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2022-02-24T09:44:50.476337Z","iopub.execute_input":"2022-02-24T09:44:50.476575Z","iopub.status.idle":"2022-02-24T09:44:50.491016Z","shell.execute_reply.started":"2022-02-24T09:44:50.476544Z","shell.execute_reply":"2022-02-24T09:44:50.490396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}