{"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":"markdown","source":"# Intro\nWelcome to the [Google Landmark Recognition 2021](https://www.kaggle.com/c/landmark-recognition-2021) compedition\n![](https://storage.googleapis.com/kaggle-competitions/kaggle/29762/logos/header.png)\n\nThis notebook will give you a guideline to start step by step with this compedition. We focus on:\n* the underlying structure of the data,\n* a data generator to load the image data on demand during the prediction process.\n\nWe use a simple model with a pretrained model on a subset of the train data to clarify the workflow. Additionally we recommend to use the power of GPU.\n\n\n<span style=\"color: royalblue;\">Please vote the notebook up if it helps you. Feel free to leave a comment above the notebook. Thank you. </span>","metadata":{}},{"cell_type":"markdown","source":"# Libraries\nWe use some standard python packages and the libraries of scikit learn and keras. ","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\n\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.utils import to_categorical, Sequence\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.applications import VGG19, VGG16, ResNet50\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:22.386016Z","iopub.execute_input":"2021-09-07T11:03:22.386338Z","iopub.status.idle":"2021-09-07T11:03:26.804609Z","shell.execute_reply.started":"2021-09-07T11:03:22.386267Z","shell.execute_reply":"2021-09-07T11:03:26.803739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Path","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/landmark-recognition-2021/'\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:26.807712Z","iopub.execute_input":"2021-09-07T11:03:26.808000Z","iopub.status.idle":"2021-09-07T11:03:26.816939Z","shell.execute_reply.started":"2021-09-07T11:03:26.807974Z","shell.execute_reply":"2021-09-07T11:03:26.815963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv(path+'train.csv')\nsamp_subm = pd.read_csv(path+'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:26.819031Z","iopub.execute_input":"2021-09-07T11:03:26.819442Z","iopub.status.idle":"2021-09-07T11:03:28.154372Z","shell.execute_reply.started":"2021-09-07T11:03:26.819407Z","shell.execute_reply":"2021-09-07T11:03:28.153531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.156666Z","iopub.execute_input":"2021-09-07T11:03:28.156988Z","iopub.status.idle":"2021-09-07T11:03:28.176830Z","shell.execute_reply.started":"2021-09-07T11:03:28.156922Z","shell.execute_reply":"2021-09-07T11:03:28.175884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.178105Z","iopub.execute_input":"2021-09-07T11:03:28.178449Z","iopub.status.idle":"2021-09-07T11:03:28.187656Z","shell.execute_reply.started":"2021-09-07T11:03:28.178413Z","shell.execute_reply":"2021-09-07T11:03:28.186866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Functions","metadata":{}},{"cell_type":"code","source":"def plot_examples(landmark_id=1):\n    \"\"\" Plot 5 examples of images with the same landmark_id \"\"\"\n    \n    fig, axs = plt.subplots(1, 5, figsize=(25, 12))\n    fig.subplots_adjust(hspace = .2, wspace=.2)\n    axs = axs.ravel()\n    for i in range(5):\n        idx = train_data[train_data['landmark_id']==landmark_id].index[i]\n        image_id = train_data.loc[idx, 'id']\n        file = image_id+'.jpg'\n        subpath = '/'.join([char for char in image_id[0:3]])\n        img = cv2.imread(path+'train/'+subpath+'/'+file)\n        axs[i].imshow(img)\n        axs[i].set_title('landmark_id: '+str(landmark_id))\n        axs[i].set_xticklabels([])\n        axs[i].set_yticklabels([])","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.189011Z","iopub.execute_input":"2021-09-07T11:03:28.189590Z","iopub.status.idle":"2021-09-07T11:03:28.198712Z","shell.execute_reply.started":"2021-09-07T11:03:28.189551Z","shell.execute_reply":"2021-09-07T11:03:28.197862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview\nFirst we look on the size of the dataset:","metadata":{}},{"cell_type":"code","source":"print('Samples train:', len(train_data))\nprint('Samples test:', len(samp_subm))","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.200007Z","iopub.execute_input":"2021-09-07T11:03:28.200366Z","iopub.status.idle":"2021-09-07T11:03:28.208108Z","shell.execute_reply.started":"2021-09-07T11:03:28.200331Z","shell.execute_reply":"2021-09-07T11:03:28.207155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.211150Z","iopub.execute_input":"2021-09-07T11:03:28.211548Z","iopub.status.idle":"2021-09-07T11:03:28.219817Z","shell.execute_reply.started":"2021-09-07T11:03:28.211512Z","shell.execute_reply":"2021-09-07T11:03:28.218827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 81313 unique classes:","metadata":{"execution":{"iopub.status.busy":"2021-08-14T07:51:34.633814Z","iopub.execute_input":"2021-08-14T07:51:34.634214Z"}}},{"cell_type":"code","source":"len(train_data['landmark_id'].unique())","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.221721Z","iopub.execute_input":"2021-09-07T11:03:28.222307Z","iopub.status.idle":"2021-09-07T11:03:28.247080Z","shell.execute_reply.started":"2021-09-07T11:03:28.222272Z","shell.execute_reply":"2021-09-07T11:03:28.246183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For each test image, we have to predict one landmark label and a corresponding confidence score. ","metadata":{}},{"cell_type":"code","source":"samp_subm.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.248295Z","iopub.execute_input":"2021-09-07T11:03:28.248619Z","iopub.status.idle":"2021-09-07T11:03:28.258040Z","shell.execute_reply.started":"2021-09-07T11:03:28.248586Z","shell.execute_reply":"2021-09-07T11:03:28.256979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Find Image\nWe consider the first image of the train data set and plot it. The first 3 characters ares used for the subpath which is the location of the image. ","metadata":{}},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.259493Z","iopub.execute_input":"2021-09-07T11:03:28.260066Z","iopub.status.idle":"2021-09-07T11:03:28.270226Z","shell.execute_reply.started":"2021-09-07T11:03:28.260032Z","shell.execute_reply":"2021-09-07T11:03:28.269342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = train_data.loc[0, 'id']\nfile = image_id+'.jpg'\nsubpath = '/'.join([char for char in image_id[0:3]]) ","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.273105Z","iopub.execute_input":"2021-09-07T11:03:28.273357Z","iopub.status.idle":"2021-09-07T11:03:28.286804Z","shell.execute_reply.started":"2021-09-07T11:03:28.273335Z","shell.execute_reply":"2021-09-07T11:03:28.286073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.287855Z","iopub.execute_input":"2021-09-07T11:03:28.288205Z","iopub.status.idle":"2021-09-07T11:03:28.295471Z","shell.execute_reply.started":"2021-09-07T11:03:28.288160Z","shell.execute_reply":"2021-09-07T11:03:28.294527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subpath","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.296760Z","iopub.execute_input":"2021-09-07T11:03:28.297407Z","iopub.status.idle":"2021-09-07T11:03:28.302716Z","shell.execute_reply.started":"2021-09-07T11:03:28.297364Z","shell.execute_reply":"2021-09-07T11:03:28.301821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Is the file located in the subpath?","metadata":{}},{"cell_type":"code","source":"file in os.listdir(path+'train/'+subpath)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.304117Z","iopub.execute_input":"2021-09-07T11:03:28.304687Z","iopub.status.idle":"2021-09-07T11:03:28.388506Z","shell.execute_reply.started":"2021-09-07T11:03:28.304650Z","shell.execute_reply":"2021-09-07T11:03:28.387779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.389663Z","iopub.execute_input":"2021-09-07T11:03:28.390062Z","iopub.status.idle":"2021-09-07T11:03:28.398214Z","shell.execute_reply.started":"2021-09-07T11:03:28.390024Z","shell.execute_reply":"2021-09-07T11:03:28.397399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot the image:","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(path+'train/'+subpath+'/'+file)\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.399319Z","iopub.execute_input":"2021-09-07T11:03:28.399610Z","iopub.status.idle":"2021-09-07T11:03:28.601560Z","shell.execute_reply.started":"2021-09-07T11:03:28.399575Z","shell.execute_reply":"2021-09-07T11:03:28.600756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Look on the image shape:","metadata":{}},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:28.602501Z","iopub.execute_input":"2021-09-07T11:03:28.602818Z","iopub.status.idle":"2021-09-07T11:03:28.608666Z","shell.execute_reply.started":"2021-09-07T11:03:28.602762Z","shell.execute_reply":"2021-09-07T11:03:28.607683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot Some Examples\nWe plot some examples of images with the same **landmark_id** in a row.","metadata":{}},{"cell_type":"code","source":"plot_examples(landmark_id = 138982)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-07T11:03:28.609908Z","iopub.execute_input":"2021-09-07T11:03:28.610489Z","iopub.status.idle":"2021-09-07T11:03:29.938575Z","shell.execute_reply.started":"2021-09-07T11:03:28.610452Z","shell.execute_reply":"2021-09-07T11:03:29.937746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(landmark_id = 126637)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-07T11:03:29.939869Z","iopub.execute_input":"2021-09-07T11:03:29.940396Z","iopub.status.idle":"2021-09-07T11:03:30.886767Z","shell.execute_reply.started":"2021-09-07T11:03:29.940354Z","shell.execute_reply":"2021-09-07T11:03:30.885763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(landmark_id = 83144)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-09-07T11:03:30.888163Z","iopub.execute_input":"2021-09-07T11:03:30.888479Z","iopub.status.idle":"2021-09-07T11:03:31.819611Z","shell.execute_reply.started":"2021-09-07T11:03:30.888446Z","shell.execute_reply":"2021-09-07T11:03:31.818697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_examples(landmark_id = 83145)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:31.820925Z","iopub.execute_input":"2021-09-07T11:03:31.821247Z","iopub.status.idle":"2021-09-07T11:03:32.824577Z","shell.execute_reply.started":"2021-09-07T11:03:31.821213Z","shell.execute_reply":"2021-09-07T11:03:32.823716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = train_data[train_data['landmark_id']==83145].index[0]\nprint(idx)\nprint('-------')\nprint(train_data.loc[idx])\nprint('-------')\nimage_id = train_data.loc[idx, 'id']\nimage_id","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:32.828579Z","iopub.execute_input":"2021-09-07T11:03:32.829164Z","iopub.status.idle":"2021-09-07T11:03:32.844048Z","shell.execute_reply.started":"2021-09-07T11:03:32.829122Z","shell.execute_reply":"2021-09-07T11:03:32.843011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split Data\nWe define train, validation and test data.","metadata":{}},{"cell_type":"code","source":"train_data.index[0:3]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:32.846322Z","iopub.execute_input":"2021-09-07T11:03:32.846862Z","iopub.status.idle":"2021-09-07T11:03:32.853164Z","shell.execute_reply.started":"2021-09-07T11:03:32.846826Z","shell.execute_reply":"2021-09-07T11:03:32.852186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(train_data.index)[:15]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:32.854403Z","iopub.execute_input":"2021-09-07T11:03:32.855120Z","iopub.status.idle":"2021-09-07T11:03:32.953456Z","shell.execute_reply.started":"2021-09-07T11:03:32.855070Z","shell.execute_reply":"2021-09-07T11:03:32.952619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_IDs_trainA, list_IDs_valA = train_test_split(list(train_data.index)[:15], test_size=0.33, random_state=2021)\nprint('---- list_IDs_trainA -----')\nprint(list_IDs_trainA)\nprint('---list_IDs_valA-----')\nprint(list_IDs_valA)\nprint('-----------')\nlist_IDs_testA = list(samp_subm.index)[:15]\nprint('---- list_IDs_testA-----')\nprint(list_IDs_testA)\nprint('-----------')","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:32.954584Z","iopub.execute_input":"2021-09-07T11:03:32.954925Z","iopub.status.idle":"2021-09-07T11:03:33.049083Z","shell.execute_reply.started":"2021-09-07T11:03:32.954885Z","shell.execute_reply":"2021-09-07T11:03:33.048049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.iloc[train_data.index[0:3]]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.050584Z","iopub.execute_input":"2021-09-07T11:03:33.050974Z","iopub.status.idle":"2021-09-07T11:03:33.062360Z","shell.execute_reply.started":"2021-09-07T11:03:33.050936Z","shell.execute_reply":"2021-09-07T11:03:33.061400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#500000\nlist_IDs_train, list_IDs_val = train_test_split(list(train_data.index)[:100000], test_size=0.33, random_state=2021)\nlist_IDs_test = list(samp_subm.index)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.063979Z","iopub.execute_input":"2021-09-07T11:03:33.064308Z","iopub.status.idle":"2021-09-07T11:03:33.189525Z","shell.execute_reply.started":"2021-09-07T11:03:33.064274Z","shell.execute_reply":"2021-09-07T11:03:33.188722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number train samples:', len(list_IDs_train))\nprint('Number val samples:', len(list_IDs_val))\nprint('Number test samples:', len(list_IDs_test))","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.190714Z","iopub.execute_input":"2021-09-07T11:03:33.191226Z","iopub.status.idle":"2021-09-07T11:03:33.197782Z","shell.execute_reply.started":"2021-09-07T11:03:33.191188Z","shell.execute_reply":"2021-09-07T11:03:33.196728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generator\n\nWe use a data generator to load the data on demand.","metadata":{}},{"cell_type":"code","source":"img_size = 32\nimg_channel = 3\nbatch_size = 64\n\nnum_classes = len(train_data['landmark_id'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.199230Z","iopub.execute_input":"2021-09-07T11:03:33.199728Z","iopub.status.idle":"2021-09-07T11:03:33.233576Z","shell.execute_reply.started":"2021-09-07T11:03:33.199694Z","shell.execute_reply":"2021-09-07T11:03:33.232839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.234885Z","iopub.execute_input":"2021-09-07T11:03:33.235234Z","iopub.status.idle":"2021-09-07T11:03:33.240836Z","shell.execute_reply.started":"2021-09-07T11:03:33.235199Z","shell.execute_reply":"2021-09-07T11:03:33.239872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DataGenerator","metadata":{}},{"cell_type":"code","source":"class DataGenerator(Sequence):\n    def __init__(self, path, list_IDs, data, img_size, img_channel, batch_size):\n        self.path = path\n        self.list_IDs = list_IDs\n        self.data = data\n        self.img_size = img_size\n        self.img_channel = img_channel\n        self.batch_size = batch_size\n        self.indexes = np.arange(len(self.list_IDs))\n        \n    def __len__(self):\n        len_ = int(len(self.list_IDs)/self.batch_size)\n        if len_*self.batch_size < len(self.list_IDs):\n            len_ += 1\n        return len_\n    \n    def __getitem__(self, index):\n        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n        list_IDs_temp = [self.list_IDs[k] for k in indexes]\n        X, y = self.__data_generation(list_IDs_temp)\n        return X, y\n            \n    \n    def __data_generation(self, list_IDs_temp):\n        X = np.zeros((self.batch_size, self.img_size, self.img_size, self.img_channel))\n        y = np.zeros((self.batch_size, 1), dtype=int)\n        for i, ID in enumerate(list_IDs_temp):\n            \n            image_id = self.data.loc[ID, 'id']\n            file = image_id+'.jpg'\n            subpath = '/'.join([char for char in image_id[0:3]]) \n            \n            img = cv2.imread(self.path+subpath+'/'+file)\n            \n            img = cv2.resize(img, (self.img_size, self.img_size))\n            X[i, ] = img/255\n            if self.path.find('train')>=0:\n                y[i, ] = self.data.loc[ID, 'landmark_id']\n            else:\n                y[i, ] = 0\n        return X, y","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.242600Z","iopub.execute_input":"2021-09-07T11:03:33.243042Z","iopub.status.idle":"2021-09-07T11:03:33.258503Z","shell.execute_reply.started":"2021-09-07T11:03:33.243006Z","shell.execute_reply":"2021-09-07T11:03:33.257673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Use the DataGenerator class to define the data generators for train, validation and test data:","metadata":{}},{"cell_type":"code","source":"train_generator = DataGenerator(path+'train/', list_IDs_train, train_data, img_size, img_channel, batch_size)\nval_generator = DataGenerator(path+'train/', list_IDs_val, train_data, img_size, img_channel, batch_size)\ntest_generator = DataGenerator(path+'test/', list_IDs_test, samp_subm, img_size, img_channel, batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.259637Z","iopub.execute_input":"2021-09-07T11:03:33.260029Z","iopub.status.idle":"2021-09-07T11:03:33.271302Z","shell.execute_reply.started":"2021-09-07T11:03:33.259993Z","shell.execute_reply":"2021-09-07T11:03:33.270392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.data[:5]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.272439Z","iopub.execute_input":"2021-09-07T11:03:33.272809Z","iopub.status.idle":"2021-09-07T11:03:33.285676Z","shell.execute_reply.started":"2021-09-07T11:03:33.272756Z","shell.execute_reply":"2021-09-07T11:03:33.284706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data_generation(list_IDs_train)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.286879Z","iopub.execute_input":"2021-09-07T11:03:33.287219Z","iopub.status.idle":"2021-09-07T11:03:33.294932Z","shell.execute_reply.started":"2021-09-07T11:03:33.287186Z","shell.execute_reply":"2021-09-07T11:03:33.293921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"markdown","source":"Load pretrained model:","metadata":{}},{"cell_type":"code","source":"weights='../input/models/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\nconv_base = ResNet50(weights=weights,\n                     include_top=False,\n                     input_shape=(img_size, img_size, img_channel))\nconv_base.trainable = True","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:33.296266Z","iopub.execute_input":"2021-09-07T11:03:33.296684Z","iopub.status.idle":"2021-09-07T11:03:38.731476Z","shell.execute_reply.started":"2021-09-07T11:03:33.296649Z","shell.execute_reply":"2021-09-07T11:03:38.730633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define Model","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(conv_base)\nmodel.add(Flatten())\n#model.add(Dense(64, activation='relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(num_classes, activation='softmax'))\n\nmodel.compile(optimizer = Adam(lr=1e-4),\n              loss=\"sparse_categorical_crossentropy\",\n              metrics=['sparse_categorical_accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:38.732714Z","iopub.execute_input":"2021-09-07T11:03:38.733053Z","iopub.status.idle":"2021-09-07T11:03:39.148373Z","shell.execute_reply.started":"2021-09-07T11:03:38.733017Z","shell.execute_reply":"2021-09-07T11:03:39.147511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 1","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:39.149653Z","iopub.execute_input":"2021-09-07T11:03:39.150001Z","iopub.status.idle":"2021-09-07T11:03:39.155018Z","shell.execute_reply.started":"2021-09-07T11:03:39.149966Z","shell.execute_reply":"2021-09-07T11:03:39.153477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(generator=train_generator,\n                              validation_data=val_generator,\n                              epochs = epochs, workers=4)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:03:39.156597Z","iopub.execute_input":"2021-09-07T11:03:39.157031Z","iopub.status.idle":"2021-09-07T11:12:46.364712Z","shell.execute_reply.started":"2021-09-07T11:03:39.156994Z","shell.execute_reply":"2021-09-07T11:12:46.363803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train done\")","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:12:46.366616Z","iopub.execute_input":"2021-09-07T11:12:46.367169Z","iopub.status.idle":"2021-09-07T11:12:46.381593Z","shell.execute_reply.started":"2021-09-07T11:12:46.367131Z","shell.execute_reply":"2021-09-07T11:12:46.378547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# list all data in history\nprint(history.history.keys())","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:12:46.385391Z","iopub.execute_input":"2021-09-07T11:12:46.385728Z","iopub.status.idle":"2021-09-07T11:12:46.397896Z","shell.execute_reply.started":"2021-09-07T11:12:46.385694Z","shell.execute_reply":"2021-09-07T11:12:46.395957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history['loss']","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:12:46.399879Z","iopub.execute_input":"2021-09-07T11:12:46.400198Z","iopub.status.idle":"2021-09-07T11:12:46.413258Z","shell.execute_reply.started":"2021-09-07T11:12:46.400167Z","shell.execute_reply":"2021-09-07T11:12:46.412366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:12:46.419450Z","iopub.execute_input":"2021-09-07T11:12:46.422374Z","iopub.status.idle":"2021-09-07T11:12:46.843033Z","shell.execute_reply.started":"2021-09-07T11:12:46.422337Z","shell.execute_reply":"2021-09-07T11:12:46.835542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict Test Data","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict_generator(test_generator, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:12:46.844445Z","iopub.execute_input":"2021-09-07T11:12:46.844823Z","iopub.status.idle":"2021-09-07T11:14:48.130715Z","shell.execute_reply.started":"2021-09-07T11:12:46.844764Z","shell.execute_reply":"2021-09-07T11:14:48.129749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:14:48.132263Z","iopub.execute_input":"2021-09-07T11:14:48.132584Z","iopub.status.idle":"2021-09-07T11:14:48.137806Z","shell.execute_reply.started":"2021-09-07T11:14:48.132548Z","shell.execute_reply":"2021-09-07T11:14:48.136999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(samp_subm.index)):\n    category = np.argmax(y_pred[i])\n    score = y_pred[i][np.argmax(y_pred[i])].round(2)\n    samp_subm.loc[i, 'landmarks'] = str(category)+' '+str(score)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:14:48.139292Z","iopub.execute_input":"2021-09-07T11:14:48.139769Z","iopub.status.idle":"2021-09-07T11:14:51.542670Z","shell.execute_reply.started":"2021-09-07T11:14:48.139732Z","shell.execute_reply":"2021-09-07T11:14:51.540859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samp_subm.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:14:51.546579Z","iopub.execute_input":"2021-09-07T11:14:51.546955Z","iopub.status.idle":"2021-09-07T11:14:51.557777Z","shell.execute_reply.started":"2021-09-07T11:14:51.546927Z","shell.execute_reply":"2021-09-07T11:14:51.556882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Export","metadata":{}},{"cell_type":"code","source":"samp_subm.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:14:51.559184Z","iopub.execute_input":"2021-09-07T11:14:51.559683Z","iopub.status.idle":"2021-09-07T11:14:51.600971Z","shell.execute_reply.started":"2021-09-07T11:14:51.559637Z","shell.execute_reply":"2021-09-07T11:14:51.600158Z"},"trusted":true},"execution_count":null,"outputs":[]}]}