{"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":"# Imports","metadata":{}},{"cell_type":"code","source":"!pip install -q efficientnet","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:44:55.373870Z","iopub.execute_input":"2021-09-07T09:44:55.374194Z","iopub.status.idle":"2021-09-07T09:45:03.399394Z","shell.execute_reply.started":"2021-09-07T09:44:55.374120Z","shell.execute_reply":"2021-09-07T09:45:03.398351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom pathlib import Path\nimport math\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn import model_selection\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport tensorflow_addons as tfa\nimport efficientnet.tfkeras as efn\nimport h5py","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:03.402453Z","iopub.execute_input":"2021-09-07T09:45:03.402823Z","iopub.status.idle":"2021-09-07T09:45:08.352839Z","shell.execute_reply.started":"2021-09-07T09:45:03.402783Z","shell.execute_reply":"2021-09-07T09:45:08.351866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Directories","metadata":{}},{"cell_type":"code","source":"data_dir = Path('../input/landmark-recognition-2021')\ntrain_data_dir = data_dir / 'train'\ntest_data_dir = data_dir / 'test'\ntrain_label_file = data_dir / 'train.csv'\nsample_file = data_dir / 'sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:08.364392Z","iopub.execute_input":"2021-09-07T09:45:08.364748Z","iopub.status.idle":"2021-09-07T09:45:08.370134Z","shell.execute_reply.started":"2021-09-07T09:45:08.364712Z","shell.execute_reply":"2021-09-07T09:45:08.369133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_label = pd.read_csv(train_label_file, index_col='id')\nsub = pd.read_csv(sample_file, index_col='id')","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:08.371528Z","iopub.execute_input":"2021-09-07T09:45:08.371906Z","iopub.status.idle":"2021-09-07T09:45:10.638698Z","shell.execute_reply.started":"2021-09-07T09:45:08.371870Z","shell.execute_reply":"2021-09-07T09:45:10.637811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing of the Data","metadata":{}},{"cell_type":"code","source":"original_label","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:10.640824Z","iopub.execute_input":"2021-09-07T09:45:10.641088Z","iopub.status.idle":"2021-09-07T09:45:10.661702Z","shell.execute_reply.started":"2021-09-07T09:45:10.641063Z","shell.execute_reply":"2021-09-07T09:45:10.660803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_label = original_label.sample(frac=1) # Shuffling the data","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:10.664657Z","iopub.execute_input":"2021-09-07T09:45:10.664993Z","iopub.status.idle":"2021-09-07T09:45:11.000281Z","shell.execute_reply.started":"2021-09-07T09:45:10.664958Z","shell.execute_reply":"2021-09-07T09:45:10.999333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_label # Exhibition of the shuffled Data","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.002544Z","iopub.execute_input":"2021-09-07T09:45:11.002946Z","iopub.status.idle":"2021-09-07T09:45:11.013941Z","shell.execute_reply.started":"2021-09-07T09:45:11.002908Z","shell.execute_reply":"2021-09-07T09:45:11.012912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"leny = len(original_label)\nborder = int(0.2*leny)\ntest_label = original_label[:border]\nlabel = original_label[border:]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.015767Z","iopub.execute_input":"2021-09-07T09:45:11.016244Z","iopub.status.idle":"2021-09-07T09:45:11.137955Z","shell.execute_reply.started":"2021-09-07T09:45:11.016207Z","shell.execute_reply":"2021-09-07T09:45:11.136735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(label)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.139816Z","iopub.execute_input":"2021-09-07T09:45:11.140268Z","iopub.status.idle":"2021-09-07T09:45:11.149907Z","shell.execute_reply.started":"2021-09-07T09:45:11.140222Z","shell.execute_reply":"2021-09-07T09:45:11.148757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label['landmark_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.151550Z","iopub.execute_input":"2021-09-07T09:45:11.151940Z","iopub.status.idle":"2021-09-07T09:45:11.201373Z","shell.execute_reply.started":"2021-09-07T09:45:11.151903Z","shell.execute_reply":"2021-09-07T09:45:11.200377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = label['landmark_id'].value_counts().values\nindexes = label['landmark_id'].value_counts().index","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.202817Z","iopub.execute_input":"2021-09-07T09:45:11.203182Z","iopub.status.idle":"2021-09-07T09:45:11.263318Z","shell.execute_reply.started":"2021-09-07T09:45:11.203147Z","shell.execute_reply":"2021-09-07T09:45:11.262464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = np.array(values)\nindexes = np.array(indexes)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.264642Z","iopub.execute_input":"2021-09-07T09:45:11.264998Z","iopub.status.idle":"2021-09-07T09:45:11.270029Z","shell.execute_reply.started":"2021-09-07T09:45:11.264955Z","shell.execute_reply":"2021-09-07T09:45:11.269039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.271520Z","iopub.execute_input":"2021-09-07T09:45:11.271894Z","iopub.status.idle":"2021-09-07T09:45:11.280825Z","shell.execute_reply.started":"2021-09-07T09:45:11.271859Z","shell.execute_reply":"2021-09-07T09:45:11.279801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cutter = 50 # How many labels we are trying to predict correctly","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.282390Z","iopub.execute_input":"2021-09-07T09:45:11.282808Z","iopub.status.idle":"2021-09-07T09:45:11.288129Z","shell.execute_reply.started":"2021-09-07T09:45:11.282774Z","shell.execute_reply":"2021-09-07T09:45:11.287149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values[:cutter]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.289644Z","iopub.execute_input":"2021-09-07T09:45:11.289989Z","iopub.status.idle":"2021-09-07T09:45:11.298635Z","shell.execute_reply.started":"2021-09-07T09:45:11.289956Z","shell.execute_reply":"2021-09-07T09:45:11.297652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indexes[:cutter]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.300241Z","iopub.execute_input":"2021-09-07T09:45:11.300673Z","iopub.status.idle":"2021-09-07T09:45:11.307780Z","shell.execute_reply.started":"2021-09-07T09:45:11.300637Z","shell.execute_reply":"2021-09-07T09:45:11.306776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get_rid = indexes[values < 1000]\nget_rid = indexes[cutter:]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.309284Z","iopub.execute_input":"2021-09-07T09:45:11.309676Z","iopub.status.idle":"2021-09-07T09:45:11.314188Z","shell.execute_reply.started":"2021-09-07T09:45:11.309617Z","shell.execute_reply":"2021-09-07T09:45:11.313212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_rid","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.315711Z","iopub.execute_input":"2021-09-07T09:45:11.316092Z","iopub.status.idle":"2021-09-07T09:45:11.324400Z","shell.execute_reply.started":"2021-09-07T09:45:11.316026Z","shell.execute_reply":"2021-09-07T09:45:11.323315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.326018Z","iopub.execute_input":"2021-09-07T09:45:11.326359Z","iopub.status.idle":"2021-09-07T09:45:11.341455Z","shell.execute_reply.started":"2021-09-07T09:45:11.326326Z","shell.execute_reply":"2021-09-07T09:45:11.340462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(get_rid)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.343020Z","iopub.execute_input":"2021-09-07T09:45:11.343442Z","iopub.status.idle":"2021-09-07T09:45:11.351731Z","shell.execute_reply.started":"2021-09-07T09:45:11.343404Z","shell.execute_reply":"2021-09-07T09:45:11.350743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_numpy = label['landmark_id'].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.358657Z","iopub.execute_input":"2021-09-07T09:45:11.359213Z","iopub.status.idle":"2021-09-07T09:45:11.363482Z","shell.execute_reply.started":"2021-09-07T09:45:11.359177Z","shell.execute_reply":"2021-09-07T09:45:11.362466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_numpy","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.366842Z","iopub.execute_input":"2021-09-07T09:45:11.367450Z","iopub.status.idle":"2021-09-07T09:45:11.373649Z","shell.execute_reply.started":"2021-09-07T09:45:11.367412Z","shell.execute_reply":"2021-09-07T09:45:11.372680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = np.isin(label_numpy, get_rid)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.375131Z","iopub.execute_input":"2021-09-07T09:45:11.375795Z","iopub.status.idle":"2021-09-07T09:45:11.569274Z","shell.execute_reply.started":"2021-09-07T09:45:11.375759Z","shell.execute_reply":"2021-09-07T09:45:11.568394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.570730Z","iopub.execute_input":"2021-09-07T09:45:11.571086Z","iopub.status.idle":"2021-09-07T09:45:11.577418Z","shell.execute_reply.started":"2021-09-07T09:45:11.571047Z","shell.execute_reply":"2021-09-07T09:45:11.576336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_numpy[mask == 1] = 0","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.579067Z","iopub.execute_input":"2021-09-07T09:45:11.579418Z","iopub.status.idle":"2021-09-07T09:45:11.589761Z","shell.execute_reply.started":"2021-09-07T09:45:11.579382Z","shell.execute_reply":"2021-09-07T09:45:11.588894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label['landmark_id'] = label_numpy","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.591089Z","iopub.execute_input":"2021-09-07T09:45:11.591417Z","iopub.status.idle":"2021-09-07T09:45:11.600160Z","shell.execute_reply.started":"2021-09-07T09:45:11.591383Z","shell.execute_reply":"2021-09-07T09:45:11.599175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.601526Z","iopub.execute_input":"2021-09-07T09:45:11.602103Z","iopub.status.idle":"2021-09-07T09:45:11.614753Z","shell.execute_reply.started":"2021-09-07T09:45:11.602050Z","shell.execute_reply":"2021-09-07T09:45:11.613712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = label.groupby('landmark_id').tail(100)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.616277Z","iopub.execute_input":"2021-09-07T09:45:11.616745Z","iopub.status.idle":"2021-09-07T09:45:11.674074Z","shell.execute_reply.started":"2021-09-07T09:45:11.616631Z","shell.execute_reply":"2021-09-07T09:45:11.673113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label.landmark_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.675371Z","iopub.execute_input":"2021-09-07T09:45:11.675748Z","iopub.status.idle":"2021-09-07T09:45:11.685029Z","shell.execute_reply.started":"2021-09-07T09:45:11.675710Z","shell.execute_reply":"2021-09-07T09:45:11.683950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.686525Z","iopub.execute_input":"2021-09-07T09:45:11.687111Z","iopub.status.idle":"2021-09-07T09:45:11.700097Z","shell.execute_reply.started":"2021-09-07T09:45:11.687074Z","shell.execute_reply":"2021-09-07T09:45:11.699177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label.drop(label.index[label['landmark_id'] == 0], inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.701350Z","iopub.execute_input":"2021-09-07T09:45:11.701766Z","iopub.status.idle":"2021-09-07T09:45:11.725457Z","shell.execute_reply.started":"2021-09-07T09:45:11.701732Z","shell.execute_reply":"2021-09-07T09:45:11.723610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label.landmark_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.726707Z","iopub.execute_input":"2021-09-07T09:45:11.727036Z","iopub.status.idle":"2021-09-07T09:45:11.736384Z","shell.execute_reply.started":"2021-09-07T09:45:11.727001Z","shell.execute_reply":"2021-09-07T09:45:11.735365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indexes = label.landmark_id.value_counts().index","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.737657Z","iopub.execute_input":"2021-09-07T09:45:11.738207Z","iopub.status.idle":"2021-09-07T09:45:11.743088Z","shell.execute_reply.started":"2021-09-07T09:45:11.738172Z","shell.execute_reply":"2021-09-07T09:45:11.742188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indexes","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.744674Z","iopub.execute_input":"2021-09-07T09:45:11.745397Z","iopub.status.idle":"2021-09-07T09:45:11.754877Z","shell.execute_reply.started":"2021-09-07T09:45:11.745357Z","shell.execute_reply":"2021-09-07T09:45:11.753932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_numpy = np.array(indexes)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.756605Z","iopub.execute_input":"2021-09-07T09:45:11.757218Z","iopub.status.idle":"2021-09-07T09:45:11.761615Z","shell.execute_reply.started":"2021-09-07T09:45:11.757181Z","shell.execute_reply":"2021-09-07T09:45:11.760674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_id_numpy = label.landmark_id.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.763166Z","iopub.execute_input":"2021-09-07T09:45:11.763833Z","iopub.status.idle":"2021-09-07T09:45:11.769113Z","shell.execute_reply.started":"2021-09-07T09:45:11.763795Z","shell.execute_reply":"2021-09-07T09:45:11.768106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"landmark_id_numpy","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.770538Z","iopub.execute_input":"2021-09-07T09:45:11.771297Z","iopub.status.idle":"2021-09-07T09:45:11.778867Z","shell.execute_reply.started":"2021-09-07T09:45:11.771260Z","shell.execute_reply":"2021-09-07T09:45:11.777867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, index in enumerate(index_numpy):\n    landmark_id_numpy[landmark_id_numpy==index] = i","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.780467Z","iopub.execute_input":"2021-09-07T09:45:11.781256Z","iopub.status.idle":"2021-09-07T09:45:11.786487Z","shell.execute_reply.started":"2021-09-07T09:45:11.781220Z","shell.execute_reply":"2021-09-07T09:45:11.785515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label['landmark_id'] = landmark_id_numpy","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.788011Z","iopub.execute_input":"2021-09-07T09:45:11.788827Z","iopub.status.idle":"2021-09-07T09:45:11.796692Z","shell.execute_reply.started":"2021-09-07T09:45:11.788787Z","shell.execute_reply":"2021-09-07T09:45:11.795696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label.landmark_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.798195Z","iopub.execute_input":"2021-09-07T09:45:11.798870Z","iopub.status.idle":"2021-09-07T09:45:11.808783Z","shell.execute_reply.started":"2021-09-07T09:45:11.798834Z","shell.execute_reply":"2021-09-07T09:45:11.807932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_classes = len(label.landmark_id.value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.810088Z","iopub.execute_input":"2021-09-07T09:45:11.810688Z","iopub.status.idle":"2021-09-07T09:45:11.816018Z","shell.execute_reply.started":"2021-09-07T09:45:11.810651Z","shell.execute_reply":"2021-09-07T09:45:11.815031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def id_to_path(s, train=True):\n    data_dir = train_data_dir if train else test_data_dir\n    return data_dir / s[0] / s[1] / s[2] / f'{s}.jpg'","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.817481Z","iopub.execute_input":"2021-09-07T09:45:11.818260Z","iopub.status.idle":"2021-09-07T09:45:11.823783Z","shell.execute_reply.started":"2021-09-07T09:45:11.818098Z","shell.execute_reply":"2021-09-07T09:45:11.822850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_size = (300, 300, 3)\nbatch_size = 32\nn_epoch = 10\nseed = 42","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.825579Z","iopub.execute_input":"2021-09-07T09:45:11.826396Z","iopub.status.idle":"2021-09-07T09:45:11.831717Z","shell.execute_reply.started":"2021-09-07T09:45:11.826358Z","shell.execute_reply":"2021-09-07T09:45:11.830833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newsize = (300, 300)\n# # x = np.asarray((Image.open(id_to_path('fd80b73a476ae8a1')).resize(newsize)))\n# x = np.array(Image.open(id_to_path('fd80b73a476ae8a1')).resize(newsize))","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.833305Z","iopub.execute_input":"2021-09-07T09:45:11.834099Z","iopub.status.idle":"2021-09-07T09:45:11.839612Z","shell.execute_reply.started":"2021-09-07T09:45:11.834061Z","shell.execute_reply":"2021-09-07T09:45:11.838740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.841055Z","iopub.execute_input":"2021-09-07T09:45:11.841755Z","iopub.status.idle":"2021-09-07T09:45:11.846925Z","shell.execute_reply.started":"2021-09-07T09:45:11.841720Z","shell.execute_reply":"2021-09-07T09:45:11.846061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(x)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.848399Z","iopub.execute_input":"2021-09-07T09:45:11.849141Z","iopub.status.idle":"2021-09-07T09:45:11.854146Z","shell.execute_reply.started":"2021-09-07T09:45:11.849104Z","shell.execute_reply":"2021-09-07T09:45:11.853215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataGenerator(keras.utils.Sequence):\n    def __init__(self, x_set, y_set=None, batch_size=32):\n        self.x , self.y = x_set, y_set\n        self.batch_size = batch_size\n        self.is_train = False if y_set is None else True\n        \n    def __len__(self):\n        return math.ceil(len(self.x) / self.batch_size)\n    \n    def __getitem__(self, idx):\n        batch_ids = self.x[idx * self.batch_size: (idx + 1) * self.batch_size]\n        if self.y is not None:\n            batch_y = self.y[idx * self.batch_size: (idx + 1) * self.batch_size]\n        \n#         list_x = [np.load(id_to_path(x, self.is_train))[::2] for x in batch_ids]\n        newsize = (300, 300)\n        list_x = np.array([np.asarray(Image.open(id_to_path(x, self.is_train)).resize(newsize)) for x in batch_ids])\n#         list_x = np.concatenate(list_x, axis=0)\n        batch_x = np.moveaxis(list_x,1,1)\n        batch_x = batch_x.astype(\"float\") / 255\n        \n        if self.is_train:\n            return batch_x, batch_y\n        else:\n            return batch_x","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.855729Z","iopub.execute_input":"2021-09-07T09:45:11.856343Z","iopub.status.idle":"2021-09-07T09:45:11.867867Z","shell.execute_reply.started":"2021-09-07T09:45:11.856307Z","shell.execute_reply":"2021-09-07T09:45:11.866862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label.nunique()[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.869402Z","iopub.execute_input":"2021-09-07T09:45:11.870062Z","iopub.status.idle":"2021-09-07T09:45:11.882725Z","shell.execute_reply.started":"2021-09-07T09:45:11.870026Z","shell.execute_reply":"2021-09-07T09:45:11.882005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Architecture of the Model","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential([\n        efn.EfficientNetB3(input_shape=input_size,weights='imagenet',include_top=False),\n        keras.layers.GlobalAveragePooling2D(),\n        keras.layers.Dense(no_classes, activation='softmax')\n        ])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:11.883666Z","iopub.execute_input":"2021-09-07T09:45:11.884006Z","iopub.status.idle":"2021-09-07T09:45:18.217146Z","shell.execute_reply.started":"2021-09-07T09:45:11.883972Z","shell.execute_reply":"2021-09-07T09:45:18.216309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.models import Sequential\n# from tensorflow.keras.layers import Dense, Dropout, Flatten\n# from tensorflow.keras.layers import Conv2D, MaxPooling2D\n\n# input_shape = x.shape\n\n# # Create the model\n# model = Sequential()\n# model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))\n# model.add(MaxPooling2D(pool_size=(2, 2)))\n# model.add(Dropout(0.25))\n# model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))\n# model.add(MaxPooling2D(pool_size=(2, 2)))\n# model.add(Dropout(0.25))\n# model.add(Flatten())\n# model.add(Dense(256, activation='relu'))\n# model.add(Dense(no_classes, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:18.218445Z","iopub.execute_input":"2021-09-07T09:45:18.218816Z","iopub.status.idle":"2021-09-07T09:45:18.223734Z","shell.execute_reply.started":"2021-09-07T09:45:18.218776Z","shell.execute_reply":"2021-09-07T09:45:18.222523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-3),\n#               loss= tf.keras.losses.SparseCategoricalCrossentropy(\n#     from_logits=False, reduction=\"auto\", name=\"sparse_categorical_crossentropy\"\n# ), metrics=[keras.metrics.AUC()])","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:18.225183Z","iopub.execute_input":"2021-09-07T09:45:18.225685Z","iopub.status.idle":"2021-09-07T09:45:18.234668Z","shell.execute_reply.started":"2021-09-07T09:45:18.225649Z","shell.execute_reply":"2021-09-07T09:45:18.233682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(optimizer='adam',\n# #               loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n#               loss=tfa.losses.SigmoidFocalCrossEntropy(),\n#               metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:18.236013Z","iopub.execute_input":"2021-09-07T09:45:18.236431Z","iopub.status.idle":"2021-09-07T09:45:18.242788Z","shell.execute_reply.started":"2021-09-07T09:45:18.236392Z","shell.execute_reply":"2021-09-07T09:45:18.241877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Compile of the Model","metadata":{}},{"cell_type":"code","source":"# Compile the model\nmodel.compile(loss=tf.keras.losses.sparse_categorical_crossentropy,\n              optimizer=tf.keras.optimizers.Adam(),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:18.245028Z","iopub.execute_input":"2021-09-07T09:45:18.245383Z","iopub.status.idle":"2021-09-07T09:45:18.269864Z","shell.execute_reply.started":"2021-09-07T09:45:18.245356Z","shell.execute_reply":"2021-09-07T09:45:18.268867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fit of the Model","metadata":{}},{"cell_type":"code","source":"x0 = label.index.values\ny0 = label['landmark_id'].values\nx2 = test_label.index.values\ny2 = test_label['landmark_id'].values\n\nx1 = sub.index.values\n\nx_train, x_val, y_train, y_val = model_selection.train_test_split(x0, y0, test_size=.2, random_state=seed)\n# x_train, y_train = x0, y0\n# x_val, y_val = x2, y2\n\ntrain = DataGenerator(x_train, y_train, batch_size=batch_size)\nval = DataGenerator(x_val, y_val, batch_size=batch_size)\ntest = DataGenerator(x1, batch_size=batch_size)\n\n# history = model.fit(train, validation_data=val, epochs=n_epoch)\nhistory = model.fit(train, validation_data=val, epochs=5)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:45:18.270816Z","iopub.execute_input":"2021-09-07T09:45:18.271280Z","iopub.status.idle":"2021-09-07T09:55:01.776717Z","shell.execute_reply.started":"2021-09-07T09:45:18.271244Z","shell.execute_reply":"2021-09-07T09:55:01.775683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exhibition of the Learning of the Model","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(num=0)\nplt.title('loss')\nplt.plot(history.history['loss'], label='train')\nplt.plot(history.history['val_loss'], label='validation')\nplt.legend()\n\nplt.figure(num=1)\nplt.title('accuracy')\nplt.plot(history.history['accuracy'], label='train')\nplt.plot(history.history['val_accuracy'], label='validation')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:01.778985Z","iopub.execute_input":"2021-09-07T09:55:01.779351Z","iopub.status.idle":"2021-09-07T09:55:02.130520Z","shell.execute_reply.started":"2021-09-07T09:55:01.779308Z","shell.execute_reply":"2021-09-07T09:55:02.129621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"model_name.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:02.131873Z","iopub.execute_input":"2021-09-07T09:55:02.132220Z","iopub.status.idle":"2021-09-07T09:55:03.091790Z","shell.execute_reply.started":"2021-09-07T09:55:02.132184Z","shell.execute_reply":"2021-09-07T09:55:03.090876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:03.093425Z","iopub.execute_input":"2021-09-07T09:55:03.093754Z","iopub.status.idle":"2021-09-07T09:55:03.106726Z","shell.execute_reply.started":"2021-09-07T09:55:03.093726Z","shell.execute_reply":"2021-09-07T09:55:03.105679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:03.108385Z","iopub.execute_input":"2021-09-07T09:55:03.109183Z","iopub.status.idle":"2021-09-07T09:55:03.122669Z","shell.execute_reply.started":"2021-09-07T09:55:03.109145Z","shell.execute_reply":"2021-09-07T09:55:03.121763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_label = test_label[:1000]\nx2 = valid_label.index.values\ny2 = valid_label['landmark_id'].values\nx_val, y_val = x2, y2\nval = DataGenerator(x_val, y_val, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:03.123947Z","iopub.execute_input":"2021-09-07T09:55:03.124293Z","iopub.status.idle":"2021-09-07T09:55:03.184407Z","shell.execute_reply.started":"2021-09-07T09:55:03.124258Z","shell.execute_reply":"2021-09-07T09:55:03.183534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions on the validation set","metadata":{}},{"cell_type":"code","source":"# prediction = model.predict(test).flatten()\nprediction = model.predict(val)\npredictions = np.argmax(prediction, axis=1)\nfor i, index in enumerate(index_numpy):\n    predictions[predictions==i] = index","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:03.185811Z","iopub.execute_input":"2021-09-07T09:55:03.186353Z","iopub.status.idle":"2021-09-07T09:55:28.777420Z","shell.execute_reply.started":"2021-09-07T09:55:03.186314Z","shell.execute_reply":"2021-09-07T09:55:28.776500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_label.nunique()[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:28.779758Z","iopub.execute_input":"2021-09-07T09:55:28.780019Z","iopub.status.idle":"2021-09-07T09:55:28.795632Z","shell.execute_reply.started":"2021-09-07T09:55:28.779993Z","shell.execute_reply":"2021-09-07T09:55:28.794840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"1 / valid_label.nunique()[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:28.796987Z","iopub.execute_input":"2021-09-07T09:55:28.797697Z","iopub.status.idle":"2021-09-07T09:55:28.805939Z","shell.execute_reply.started":"2021-09-07T09:55:28.797659Z","shell.execute_reply":"2021-09-07T09:55:28.804981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(predictions == y_val).sum()/len(predictions)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:28.808073Z","iopub.execute_input":"2021-09-07T09:55:28.808958Z","iopub.status.idle":"2021-09-07T09:55:28.817431Z","shell.execute_reply.started":"2021-09-07T09:55:28.808838Z","shell.execute_reply":"2021-09-07T09:55:28.816067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions on the test set","metadata":{}},{"cell_type":"code","source":"# prediction = model.predict(test).flatten()\nprediction = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:55:28.819171Z","iopub.execute_input":"2021-09-07T09:55:28.820147Z","iopub.status.idle":"2021-09-07T09:58:43.071390Z","shell.execute_reply.started":"2021-09-07T09:55:28.820109Z","shell.execute_reply":"2021-09-07T09:58:43.070299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = prediction.max(axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.075782Z","iopub.execute_input":"2021-09-07T09:58:43.076075Z","iopub.status.idle":"2021-09-07T09:58:43.084457Z","shell.execute_reply.started":"2021-09-07T09:58:43.076047Z","shell.execute_reply":"2021-09-07T09:58:43.083603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.092070Z","iopub.execute_input":"2021-09-07T09:58:43.092713Z","iopub.status.idle":"2021-09-07T09:58:43.098599Z","shell.execute_reply.started":"2021-09-07T09:58:43.092677Z","shell.execute_reply":"2021-09-07T09:58:43.097749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(prediction, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.100857Z","iopub.execute_input":"2021-09-07T09:58:43.101712Z","iopub.status.idle":"2021-09-07T09:58:43.109875Z","shell.execute_reply.started":"2021-09-07T09:58:43.101673Z","shell.execute_reply":"2021-09-07T09:58:43.108992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.111243Z","iopub.execute_input":"2021-09-07T09:58:43.112057Z","iopub.status.idle":"2021-09-07T09:58:43.119794Z","shell.execute_reply.started":"2021-09-07T09:58:43.112020Z","shell.execute_reply":"2021-09-07T09:58:43.118943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.121289Z","iopub.execute_input":"2021-09-07T09:58:43.122030Z","iopub.status.idle":"2021-09-07T09:58:43.131489Z","shell.execute_reply.started":"2021-09-07T09:58:43.121971Z","shell.execute_reply":"2021-09-07T09:58:43.130322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_numpy","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.133024Z","iopub.execute_input":"2021-09-07T09:58:43.133629Z","iopub.status.idle":"2021-09-07T09:58:43.141605Z","shell.execute_reply.started":"2021-09-07T09:58:43.133580Z","shell.execute_reply":"2021-09-07T09:58:43.140748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, index in enumerate(index_numpy):\n    predictions[predictions==i] = index","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.143162Z","iopub.execute_input":"2021-09-07T09:58:43.143787Z","iopub.status.idle":"2021-09-07T09:58:43.150358Z","shell.execute_reply.started":"2021-09-07T09:58:43.143749Z","shell.execute_reply":"2021-09-07T09:58:43.149408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.151763Z","iopub.execute_input":"2021-09-07T09:58:43.152445Z","iopub.status.idle":"2021-09-07T09:58:43.160652Z","shell.execute_reply.started":"2021-09-07T09:58:43.152407Z","shell.execute_reply":"2021-09-07T09:58:43.159695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_pred = str(predictions) + ' ' + str(scores)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.162159Z","iopub.execute_input":"2021-09-07T09:58:43.162888Z","iopub.status.idle":"2021-09-07T09:58:43.169246Z","shell.execute_reply.started":"2021-09-07T09:58:43.162852Z","shell.execute_reply":"2021-09-07T09:58:43.168173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['landmarks'] = predictions\nsub['scores'] = scores\nsub['space'] = scores\nsub['space'] = sub['space'].apply(lambda x: ' ')\n\nsub['landmarks'] = sub['landmarks'].apply(lambda x: str(x))\nsub['scores'] = sub['scores'].apply(lambda x: str(\"%.2f\" % x))\nsub['landmarks'] = sub['landmarks'] + sub['space'] + sub['scores']\n\nsub.drop('scores', inplace=True, axis=1)\nsub.drop('space', inplace=True, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.171148Z","iopub.execute_input":"2021-09-07T09:58:43.171850Z","iopub.status.idle":"2021-09-07T09:58:43.207909Z","shell.execute_reply.started":"2021-09-07T09:58:43.171810Z","shell.execute_reply":"2021-09-07T09:58:43.207087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.209318Z","iopub.execute_input":"2021-09-07T09:58:43.209669Z","iopub.status.idle":"2021-09-07T09:58:43.221777Z","shell.execute_reply.started":"2021-09-07T09:58:43.209633Z","shell.execute_reply":"2021-09-07T09:58:43.220685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.223642Z","iopub.execute_input":"2021-09-07T09:58:43.224376Z","iopub.status.idle":"2021-09-07T09:58:43.231783Z","shell.execute_reply.started":"2021-09-07T09:58:43.224334Z","shell.execute_reply":"2021-09-07T09:58:43.230729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.233271Z","iopub.execute_input":"2021-09-07T09:58:43.234066Z","iopub.status.idle":"2021-09-07T09:58:43.254403Z","shell.execute_reply.started":"2021-09-07T09:58:43.234024Z","shell.execute_reply":"2021-09-07T09:58:43.253471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.255676Z","iopub.execute_input":"2021-09-07T09:58:43.256061Z","iopub.status.idle":"2021-09-07T09:58:43.297807Z","shell.execute_reply.started":"2021-09-07T09:58:43.256010Z","shell.execute_reply":"2021-09-07T09:58:43.297064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = pd.read_csv('../input/landmark-recognition-2021/sample_submission.csv')\ns","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.298905Z","iopub.execute_input":"2021-09-07T09:58:43.299343Z","iopub.status.idle":"2021-09-07T09:58:43.322972Z","shell.execute_reply.started":"2021-09-07T09:58:43.299306Z","shell.execute_reply":"2021-09-07T09:58:43.321987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = h5py.File('data.h5', 'w')\nfile.create_dataset('dataset', data=index_numpy)\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T09:58:43.324562Z","iopub.execute_input":"2021-09-07T09:58:43.324934Z","iopub.status.idle":"2021-09-07T09:58:43.331216Z","shell.execute_reply.started":"2021-09-07T09:58:43.324899Z","shell.execute_reply":"2021-09-07T09:58:43.330331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}