{"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\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-12-15T00:29:55.933277Z","iopub.execute_input":"2022-12-15T00:29:55.934199Z","iopub.status.idle":"2022-12-15T00:29:55.957776Z","shell.execute_reply.started":"2022-12-15T00:29:55.934096Z","shell.execute_reply":"2022-12-15T00:29:55.956893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/train-csv-processed/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:29:55.959561Z","iopub.execute_input":"2022-12-15T00:29:55.959904Z","iopub.status.idle":"2022-12-15T00:29:57.813068Z","shell.execute_reply.started":"2022-12-15T00:29:55.959872Z","shell.execute_reply":"2022-12-15T00:29:57.811996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TOP_K = 100\nNUM_OF_CLASSES = df[\"landmark_id\"].max() + 1\nIMG_SIZE = (512, 512)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:29:57.814450Z","iopub.execute_input":"2022-12-15T00:29:57.815050Z","iopub.status.idle":"2022-12-15T00:29:57.828731Z","shell.execute_reply.started":"2022-12-15T00:29:57.815014Z","shell.execute_reply":"2022-12-15T00:29:57.827640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:29:57.833562Z","iopub.execute_input":"2022-12-15T00:29:57.834002Z","iopub.status.idle":"2022-12-15T00:30:03.481169Z","shell.execute_reply.started":"2022-12-15T00:29:57.833966Z","shell.execute_reply":"2022-12-15T00:30:03.478895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model = keras.applications.resnet_v2.ResNet152V2(\n#     include_top=False, \n#     weights='imagenet', \n#     input_shape=(224, 224, 3)\n# )\n\n# base_model.trainable = True\n\n# inputs = keras.Input((224, 224, 3))\n# x = base_model(inputs, training=False)\n# x = keras.layers.GlobalAveragePooling2D()(x)\n# outputs = keras.layers.Dense(NUM_OF_CLASSES, activation='softmax')(x)\n\n# model = keras.Model(inputs, outputs)\n\n# adam = keras.optimizers.Adam(learning_rate=0.00005, amsgrad=True)\n\n# model.compile(\n#     optimizer=adam,\n#     loss=\"categorical_crossentropy\",\n#     metrics=[\"categorical_accuracy\"]\n# )\n\n# model.summary()\n\n# model.load_weights('../input/modeltrained/model_weight.h5')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:30:03.483566Z","iopub.execute_input":"2022-12-15T00:30:03.485451Z","iopub.status.idle":"2022-12-15T00:30:03.494734Z","shell.execute_reply.started":"2022-12-15T00:30:03.485412Z","shell.execute_reply":"2022-12-15T00:30:03.493068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\nmodel = load_model('../input/modelrgdd/model512_3.h5')\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:30:03.498578Z","iopub.execute_input":"2022-12-15T00:30:03.499252Z","iopub.status.idle":"2022-12-15T00:30:35.384024Z","shell.execute_reply.started":"2022-12-15T00:30:03.499216Z","shell.execute_reply":"2022-12-15T00:30:35.383036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Model\n\ngap_layer_model = Model(\n    inputs=model.input,\n    outputs=model.get_layer('fc').output\n)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:30:35.387904Z","iopub.execute_input":"2022-12-15T00:30:35.390376Z","iopub.status.idle":"2022-12-15T00:30:35.406688Z","shell.execute_reply.started":"2022-12-15T00:30:35.390339Z","shell.execute_reply":"2022-12-15T00:30:35.405723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\n\ntest_path = glob.glob('../input/landmark-retrieval-2021/test/*/*/*/*.jpg')\nindex_path = glob.glob('../input/landmark-retrieval-2021/index/*/*/*/*.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:30:35.410992Z","iopub.execute_input":"2022-12-15T00:30:35.413690Z","iopub.status.idle":"2022-12-15T00:31:33.175931Z","shell.execute_reply.started":"2022-12-15T00:30:35.413657Z","shell.execute_reply":"2022-12-15T00:31:33.174894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = [x.split('/')[-1].split('.')[0] for x in test_path]\nindex_ids = [x.split('/')[-1].split('.')[0] for x in index_path]","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:31:33.179772Z","iopub.execute_input":"2022-12-15T00:31:33.180227Z","iopub.status.idle":"2022-12-15T00:31:33.230787Z","shell.execute_reply.started":"2022-12-15T00:31:33.180193Z","shell.execute_reply":"2022-12-15T00:31:33.229932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import Sequence\n\nclass DataGenerator(Sequence):\n    def __init__(self, data_paths, batch_size=32, img_size=(224, 224)):\n        self.data_paths = data_paths\n        self.num_of_data = len(data_paths)\n        self.batch_size = batch_size\n        self.height, self.width = img_size\n    \n    def __len__(self):\n        return int(self.num_of_data / self.batch_size)+1\n    \n    def get_img_batch(self, batch_img_paths):\n        batch_img = []\n        for path in batch_img_paths:\n            img = tf.io.read_file(path)\n            img = tf.image.decode_jpeg(img, channels=3)\n            img = tf.image.resize(img, [self.height, self.width])\n            img /= 255.0\n            batch_img.append(img)\n        return batch_img\n    \n    def __getitem__(self, idx):\n        l = idx * self.batch_size\n        r = (idx+1) * self.batch_size\n        batch_img_paths = self.data_paths[l:r]\n        \n        batch_img = self.get_img_batch(batch_img_paths)\n        return np.array(batch_img)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:31:33.232708Z","iopub.execute_input":"2022-12-15T00:31:33.233271Z","iopub.status.idle":"2022-12-15T00:31:33.530638Z","shell.execute_reply.started":"2022-12-15T00:31:33.233235Z","shell.execute_reply":"2022-12-15T00:31:33.529614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 128\n\ntest_img = DataGenerator(test_path, batch_size=BATCH_SIZE, img_size=IMG_SIZE)\nindex_img = DataGenerator(index_path, batch_size=BATCH_SIZE, img_size=IMG_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:31:33.531977Z","iopub.execute_input":"2022-12-15T00:31:33.532586Z","iopub.status.idle":"2022-12-15T00:31:33.541760Z","shell.execute_reply.started":"2022-12-15T00:31:33.532551Z","shell.execute_reply":"2022-12-15T00:31:33.540048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_output = gap_layer_model.predict(test_img)\nindex_output = gap_layer_model.predict(index_img)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T00:31:33.544377Z","iopub.execute_input":"2022-12-15T00:31:33.545022Z","iopub.status.idle":"2022-12-15T01:17:21.574350Z","shell.execute_reply.started":"2022-12-15T00:31:33.544978Z","shell.execute_reply":"2022-12-15T01:17:21.573213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_output[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:17:21.576041Z","iopub.execute_input":"2022-12-15T01:17:21.577405Z","iopub.status.idle":"2022-12-15T01:17:21.586461Z","shell.execute_reply.started":"2022-12-15T01:17:21.577369Z","shell.execute_reply":"2022-12-15T01:17:21.585382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import trange\nfrom scipy.spatial.distance import cdist\n\npredict_list = []\nfor test_i in trange(test_output.shape[0]):\n    distances = cdist(test_output[np.newaxis, test_i, :], index_output, 'cosine')[0]\n    partition = np.argpartition(distances, TOP_K)[:TOP_K]\n    nearest = sorted([(index_ids[p], distances[p]) for p in partition], key=lambda x: x[1])\n    pred_str = ''\n    for index_id, dis in nearest:\n        pred_str += index_id + ' '\n#         if test_i == 1:\n#             print(dis)\n    predict_list.append(pred_str)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:17:21.587837Z","iopub.execute_input":"2022-12-15T01:17:21.588512Z","iopub.status.idle":"2022-12-15T01:23:08.440640Z","shell.execute_reply.started":"2022-12-15T01:17:21.588472Z","shell.execute_reply":"2022-12-15T01:23:08.439432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.DataFrame(data={'id': test_ids, 'images': predict_list})\nsub_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T01:23:08.442349Z","iopub.execute_input":"2022-12-15T01:23:08.443025Z","iopub.status.idle":"2022-12-15T01:23:08.480249Z","shell.execute_reply.started":"2022-12-15T01:23:08.442987Z","shell.execute_reply":"2022-12-15T01:23:08.479348Z"},"trusted":true},"execution_count":null,"outputs":[]}]}