{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from zipfile import ZipFile\nimport shutil\nimport random\nimport cv2\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras import layers\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Extract and read images"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_path = '/kaggle/input/dogs-vs-cats/train.zip'\ntest_path = '/kaggle/input/dogs-vs-cats/test1.zip'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DATA_PATH = './dogs-vs-cats'\n\n# extract train data\nwith ZipFile(train_path, 'r') as zip_ref:\n    zip_ref.extractall(DATA_PATH)\n    \n# extract test data\nwith ZipFile(test_path, 'r') as zip_ref:\n    zip_ref.extractall(DATA_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Number of training images: {len(os.listdir(DATA_PATH + \"/train\"))}')\nprint(f'Number of test images: {len(os.listdir(DATA_PATH + \"/test1\"))}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Display few images"},{"metadata":{"trusted":true},"cell_type":"code","source":"# path of extracted train images\npath = DATA_PATH + '/train'\n\nfig, axis = plt.subplots(2, 5, figsize = (14, 5))\nfor ax, file in zip(axis.flatten(), os.listdir(path)[:10]):\n    img = cv2.imread(os.path.join(path, file))\n    img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (224, 224))\n    ax.imshow(img)\n    ax.get_xaxis().set_visible(False)\n    ax.get_yaxis().set_visible(False)\nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Prepare training and validation data"},{"metadata":{"trusted":true},"cell_type":"code","source":"# list of training files\ntrain_files = os.listdir(path)\nlabels = []\n\nfor file in train_files:\n    label = file.split('.')[0]\n    if label == 'dog':\n        labels.append(1)\n    else:\n        labels.append(0)\n        \ntrain_df = pd.DataFrame({'file_names': train_files, 'labels': labels})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.sample(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check the number of images in each category\ntrain_df.labels.value_counts().plot(kind = 'bar', title = 'Number of images in each category', \n                                    xlabel = 'category', ylabel = 'count');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['labels'] = train_df['labels'].replace({0: 'cat', 1: 'dog'})\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# split dataframe \ndf_train, df_val = train_test_split(train_df, test_size = 0.12, stratify = train_df['labels'], random_state = 42)\n\ndf_train = df_train.reset_index(drop = True)\ndf_val = df_val.reset_index(drop = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Shape of df_train: {df_train.shape}\\nShape of df_val: {df_val.shape}')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Check the distribution of images in validation dataframe"},{"metadata":{"trusted":true},"cell_type":"code","source":"df_val.labels.value_counts().plot(kind = 'bar', title = 'Number of images in each category', \n                                    xlabel = 'category', ylabel = 'count');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_SIZE = (224, 224)\nBATCH_SIZE = 64","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Create training and validation generators"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_aug = ImageDataGenerator(rescale = 1 / 255.0, rotation_range = 30, zoom_range = 0.2, width_shift_range = 0.2,\n                               height_shift_range = 0.2, shear_range = 0.2, brightness_range = (0.1, 0.9),\n                               fill_mode = 'nearest', horizontal_flip = True)\n\ntrain_gen = train_aug.flow_from_dataframe(df_train, path, x_col = 'file_names', y_col = 'labels', class_mode = 'categorical',\n                                         target_size = IMAGE_SIZE, batch_size = BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# validation generator\nval_aug = ImageDataGenerator(rescale = 1 / 255.0)\n\nval_gen = val_aug.flow_from_dataframe(df_val, path, x_col = 'file_names', y_col = 'labels', class_mode = 'categorical',\n                                     target_size = IMAGE_SIZE, batch_size = BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Display random augmented image"},{"metadata":{"trusted":true},"cell_type":"code","source":"random_image = train_df.sample(1).reset_index(drop = True)\nrandom_gen = train_aug.flow_from_dataframe(random_image, path, x_col = 'file_names', y_col = 'labels', \n                                           target_size = IMAGE_SIZE, class_mode = 'categorical')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axis = plt.subplots(2, 5, figsize = (14, 5))\nfor ax in axis.flatten():\n    for image_batch, label_batch in random_gen:\n        ax.imshow(image_batch[0])\n        ax.get_xaxis().set_visible(False)\n        ax.get_yaxis().set_visible(False)\n        break\nplt.tight_layout()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Load pretrained InceptionV3 model"},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model = InceptionV3(include_top = False, weights = 'imagenet', input_shape = (224, 224, 3))\n\n# freeze trained network\nfor layer in base_model.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# check the shape of the last layer\nlast_layer = base_model.layers[-1].output\nprint(f'Last Layer output shape: {last_layer.shape}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = layers.GlobalAveragePooling2D()(last_layer)\nx = layers.Dense(1024, activation = 'relu')(x)\nx = layers.Dropout(0.2)(x)\nx = layers.Dense(512, activation = 'relu')(x)\nx = layers.Dense(2, activation = 'softmax')(x)\n\nmodel = tf.keras.Model(inputs = base_model.input,outputs = x)\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Train the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# compile the model\nmodel.compile(optimizer = tf.keras.optimizers.RMSprop(lr = 0.0001), loss = 'binary_crossentropy', metrics = ['acc'])\n\n# train the model\nhistory = model.fit(train_gen, validation_data = val_gen, epochs = 8, verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Fine tune the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in base_model.layers[:197]:\n    layer.trainable = False\n    \nfor layer in base_model.layers[197:]:\n    layer.trainable = True\n    \n    \n# compile the model\nmodel.compile(optimizer = tf.keras.optimizers.RMSprop(lr = 0.0001), loss = 'binary_crossentropy', metrics = ['acc'])\n\n# train the model\nhistory = model.fit(train_gen, validation_data = val_gen, epochs = 6, verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Create test generator"},{"metadata":{"trusted":true},"cell_type":"code","source":"# path of test images\npath = DATA_PATH + '/test1'\n\ndf_test = pd.DataFrame({'file_names': os.listdir(path)})\n\n\ntest_aug = ImageDataGenerator(rescale = 1 / 255.0)\n\ntest_gen = test_aug.flow_from_dataframe(df_test, path, x_col = 'file_names', y_col = None, class_mode = None, \n                                        target_size = IMAGE_SIZE, batch_size = BATCH_SIZE, shuffle = False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Make predictions on test images"},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model.predict(test_gen, steps = np.ceil(df_test.shape[0] / BATCH_SIZE))\n\ndf_test['labels'] = np.argmax(predict, axis = -1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_map = dict((v, k) for k, v in train_gen.class_indices.items())\ndf_test['labels'] = df_test['labels'].replace(label_map)\ndf_test['labels'] = df_test['labels'].replace({'dog': 1, 'cat': 0 })","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = df_test.copy()\nsubmission_df['id'] = submission_df['file_names'].str.split('.').str[0]\nsubmission_df.drop('file_names', axis = 1, inplace = True)\nsubmission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_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}