{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":19231,"databundleVersionId":1413778},{"sourceType":"datasetVersion","sourceId":1383460,"datasetId":807335,"databundleVersionId":1416050}],"dockerImageVersionId":29987,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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#import os\n#for 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 5GB 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","trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:27:47.020336Z","iopub.execute_input":"2025-07-05T14:27:47.020655Z","iopub.status.idle":"2025-07-05T14:27:47.025469Z","shell.execute_reply.started":"2025-07-05T14:27:47.020621Z","shell.execute_reply":"2025-07-05T14:27:47.024521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install pynvml -q\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-06T07:29:56.627818Z","iopub.execute_input":"2025-07-06T07:29:56.628144Z","iopub.status.idle":"2025-07-06T07:33:51.067404Z","shell.execute_reply.started":"2025-07-06T07:29:56.628117Z","shell.execute_reply":"2025-07-06T07:33:51.066277Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications import MobileNetV2\nfrom keras.utils import to_categorical\nfrom keras.layers import Dense\nfrom keras import Model\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.models import load_model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:27:47.027319Z","iopub.execute_input":"2025-07-05T14:27:47.027546Z","iopub.status.idle":"2025-07-05T14:27:52.109964Z","shell.execute_reply.started":"2025-07-05T14:27:47.02752Z","shell.execute_reply":"2025-07-05T14:27:52.109054Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/landmark-recognition-2020/train.csv\")\ntrain[\"filename\"] = train.id.str[0]+\"/\"+train.id.str[1]+\"/\"+train.id.str[2]+\"/\"+train.id+\".jpg\"\ntrain[\"label\"] = train.landmark_id.astype(str)\ntrain","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:27:52.111675Z","iopub.execute_input":"2025-07-05T14:27:52.111921Z","iopub.status.idle":"2025-07-05T14:27:57.928076Z","shell.execute_reply.started":"2025-07-05T14:27:52.111894Z","shell.execute_reply":"2025-07-05T14:27:57.927312Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/landmark-recognition-2020/sample_submission.csv\")\nsub[\"filename\"] = sub.id.str[0]+\"/\"+sub.id.str[1]+\"/\"+sub.id.str[2]+\"/\"+sub.id+\".jpg\"\nsub","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:27:57.929114Z","iopub.execute_input":"2025-07-05T14:27:57.929334Z","iopub.status.idle":"2025-07-05T14:27:57.979917Z","shell.execute_reply.started":"2025-07-05T14:27:57.929312Z","shell.execute_reply":"2025-07-05T14:27:57.979256Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = train.landmark_id.values\nn_classes = len(np.unique(y))\nprint(n_classes)\nplt.hist(y)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:27:57.98095Z","iopub.execute_input":"2025-07-05T14:27:57.981201Z","iopub.status.idle":"2025-07-05T14:27:58.208826Z","shell.execute_reply.started":"2025-07-05T14:27:57.981177Z","shell.execute_reply":"2025-07-05T14:27:58.207966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_rate = 0.3\nbatch_size = 32","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:27:58.211221Z","iopub.execute_input":"2025-07-05T14:27:58.211437Z","iopub.status.idle":"2025-07-05T14:27:58.215203Z","shell.execute_reply.started":"2025-07-05T14:27:58.211415Z","shell.execute_reply":"2025-07-05T14:27:58.214164Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"gen = ImageDataGenerator(validation_split=val_rate)\n\ntrain_gen = gen.flow_from_dataframe(\n    train,\n    directory=\"/kaggle/input/landmark-recognition-2020/train/\",\n    x_col=\"filename\",\n    y_col=\"label\",\n    weight_col=None,\n    target_size=(256, 256),\n    color_mode=\"rgb\",\n    classes=None,\n    class_mode=\"categorical\",\n    batch_size=batch_size,\n    shuffle=True,\n    subset=\"training\",\n    interpolation=\"nearest\",\n    validate_filenames=False)\n    \nval_gen = gen.flow_from_dataframe(\n    train,\n    directory=\"/kaggle/input/landmark-recognition-2020/train/\",\n    x_col=\"filename\",\n    y_col=\"label\",\n    weight_col=None,\n    target_size=(256, 256),\n    color_mode=\"rgb\",\n    classes=None,\n    class_mode=\"categorical\",\n    batch_size=batch_size,\n    shuffle=True,\n    subset=\"validation\",\n    interpolation=\"nearest\",\n    validate_filenames=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:27:58.216706Z","iopub.execute_input":"2025-07-05T14:27:58.217058Z","iopub.status.idle":"2025-07-05T14:28:04.956213Z","shell.execute_reply.started":"2025-07-05T14:27:58.217012Z","shell.execute_reply":"2025-07-05T14:28:04.955509Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pre  = load_model(\"/kaggle/input/common-keras-pretrained-models/MobileNetV2.h5\")\nfor layer in pre.layers:\n    layer.trainable = False\npre.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:28:04.957279Z","iopub.execute_input":"2025-07-05T14:28:04.957515Z","iopub.status.idle":"2025-07-05T14:28:10.882683Z","shell.execute_reply.started":"2025-07-05T14:28:04.95749Z","shell.execute_reply":"2025-07-05T14:28:10.881884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = pre.layers[-2].output\nx = Dense(n_classes, activation='softmax', name='pred')(x)\nmodel = Model(inputs=pre.layers[0].output,outputs=x)\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:28:10.884343Z","iopub.execute_input":"2025-07-05T14:28:10.884686Z","iopub.status.idle":"2025-07-05T14:28:10.961517Z","shell.execute_reply.started":"2025-07-05T14:28:10.884651Z","shell.execute_reply":"2025-07-05T14:28:10.960796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"categorical_accuracy\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:28:10.962911Z","iopub.execute_input":"2025-07-05T14:28:10.963301Z","iopub.status.idle":"2025-07-05T14:28:10.980581Z","shell.execute_reply.started":"2025-07-05T14:28:10.963265Z","shell.execute_reply":"2025-07-05T14:28:10.979703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# training parameters\nepochs = 2 # maximum number of epochs\ntrain_steps = int(len(train)*(1-val_rate))//batch_size\nval_steps = int(len(train)*val_rate)//batch_size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:28:10.981574Z","iopub.execute_input":"2025-07-05T14:28:10.981809Z","iopub.status.idle":"2025-07-05T14:28:10.987551Z","shell.execute_reply.started":"2025-07-05T14:28:10.981786Z","shell.execute_reply":"2025-07-05T14:28:10.986848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_checkpoint = ModelCheckpoint(\"best_model.h5\", save_best_only=True, verbose=1)\n\nhistory = model.fit_generator(train_gen, steps_per_epoch=train_steps, epochs=epochs,\n                              validation_data=val_gen, validation_steps=val_steps, callbacks=[model_checkpoint])\n\nmodel.save(\"model.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-05T14:28:10.989702Z","iopub.execute_input":"2025-07-05T14:28:10.989924Z","execution_failed":"2025-07-05T15:19:14.17Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.models import load_model\nbest_model = load_model(\"best_model.h5\")","metadata":{"trusted":true,"execution":{"execution_failed":"2025-07-05T15:19:14.172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_gen = ImageDataGenerator().flow_from_dataframe(\n    sub,\n    directory=\"/kaggle/input/landmark-recognition-2020/test/\",\n    x_col=\"filename\",\n    y_col=None,\n    weight_col=None,\n    target_size=(256, 256),\n    color_mode=\"rgb\",\n    classes=None,\n    class_mode=None,\n    batch_size=1,\n    shuffle=True,\n    subset=None,\n    interpolation=\"nearest\",\n    validate_filenames=False)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-07-05T15:19:14.172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Predicting on all available data...\")\ny_pred_one_hot = best_model.predict_generator(test_gen, verbose=1, steps=len(sub))","metadata":{"trusted":true,"execution":{"execution_failed":"2025-07-05T15:19:14.172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = np.argmax(y_pred_one_hot, axis=-1)\ny_prob = np.max(y_pred_one_hot, axis=-1)\nprint(y_pred.shape, y_prob.shape)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-07-05T15:19:14.172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_uniq = np.unique(train_keep.landmark_id.values)\nprint(y_uniq)\ny_pred = [y_uniq[Y] for Y in y_pred]","metadata":{"trusted":true,"execution":{"execution_failed":"2025-07-05T15:19:14.172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(len(sub)):\n    sub.loc[i, \"landmarks\"] = str(y_pred[i])+\" \"+str(y_prob[i])\nsub = sub.drop(columns=\"filename\")\nsub.to_csv(\"submission.csv\", index=False)\nsub","metadata":{"trusted":true,"execution":{"execution_failed":"2025-07-05T15:19:14.172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}