{"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":"!pip install -q efficientnet","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-07T10:09:50.162979Z","iopub.execute_input":"2021-09-07T10:09:50.163595Z","iopub.status.idle":"2021-09-07T10:09:57.821810Z","shell.execute_reply.started":"2021-09-07T10:09:50.163457Z","shell.execute_reply":"2021-09-07T10:09:57.820771Z"},"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-07T10:09:57.823314Z","iopub.execute_input":"2021-09-07T10:09:57.823607Z","iopub.status.idle":"2021-09-07T10:10:04.262533Z","shell.execute_reply.started":"2021-09-07T10:09:57.823563Z","shell.execute_reply":"2021-09-07T10:10:04.261436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = Path('../input/landmark-recognition-2021')\ntest_data_dir = data_dir / 'test'\nsample_file = data_dir / 'sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:04.264462Z","iopub.execute_input":"2021-09-07T10:10:04.264782Z","iopub.status.idle":"2021-09-07T10:10:04.268908Z","shell.execute_reply.started":"2021-09-07T10:10:04.264751Z","shell.execute_reply":"2021-09-07T10:10:04.267965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(sample_file, index_col='id')","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:04.270804Z","iopub.execute_input":"2021-09-07T10:10:04.271258Z","iopub.status.idle":"2021-09-07T10:10:04.312129Z","shell.execute_reply.started":"2021-09-07T10:10:04.271227Z","shell.execute_reply":"2021-09-07T10:10:04.311258Z"},"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-07T10:10:04.315287Z","iopub.execute_input":"2021-09-07T10:10:04.315591Z","iopub.status.idle":"2021-09-07T10:10:04.320053Z","shell.execute_reply.started":"2021-09-07T10:10:04.315549Z","shell.execute_reply":"2021-09-07T10:10:04.319077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_size = (300, 300, 3)\nbatch_size = 32\nseed = 42","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:04.321459Z","iopub.execute_input":"2021-09-07T10:10:04.321847Z","iopub.status.idle":"2021-09-07T10:10:04.330524Z","shell.execute_reply.started":"2021-09-07T10:10:04.321808Z","shell.execute_reply":"2021-09-07T10:10:04.329631Z"},"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('00084cdf8f600d00',0)).resize(newsize))","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:04.331836Z","iopub.execute_input":"2021-09-07T10:10:04.332177Z","iopub.status.idle":"2021-09-07T10:10:04.341305Z","shell.execute_reply.started":"2021-09-07T10:10:04.332146Z","shell.execute_reply":"2021-09-07T10:10:04.340371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:04.343185Z","iopub.execute_input":"2021-09-07T10:10:04.343509Z","iopub.status.idle":"2021-09-07T10:10:04.351290Z","shell.execute_reply.started":"2021-09-07T10:10:04.343480Z","shell.execute_reply":"2021-09-07T10:10:04.350471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(x)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:04.352953Z","iopub.execute_input":"2021-09-07T10:10:04.353293Z","iopub.status.idle":"2021-09-07T10:10:04.362339Z","shell.execute_reply.started":"2021-09-07T10:10:04.353265Z","shell.execute_reply":"2021-09-07T10:10:04.361452Z"},"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-07T10:10:04.364135Z","iopub.execute_input":"2021-09-07T10:10:04.364648Z","iopub.status.idle":"2021-09-07T10:10:04.374411Z","shell.execute_reply.started":"2021-09-07T10:10:04.364607Z","shell.execute_reply":"2021-09-07T10:10:04.373119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x1 = sub.index.values\ntest = DataGenerator(x1, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:04.376364Z","iopub.execute_input":"2021-09-07T10:10:04.376861Z","iopub.status.idle":"2021-09-07T10:10:04.384275Z","shell.execute_reply.started":"2021-09-07T10:10:04.376746Z","shell.execute_reply":"2021-09-07T10:10:04.383328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"../input/my-google-dataset/model_name.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:04.386003Z","iopub.execute_input":"2021-09-07T10:10:04.386469Z","iopub.status.idle":"2021-09-07T10:10:12.141950Z","shell.execute_reply.started":"2021-09-07T10:10:04.386431Z","shell.execute_reply":"2021-09-07T10:10:12.141010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prediction = model.predict(test).flatten()\nprediction = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:10:12.143382Z","iopub.execute_input":"2021-09-07T10:10:12.143793Z","iopub.status.idle":"2021-09-07T10:36:19.031896Z","shell.execute_reply.started":"2021-09-07T10:10:12.143749Z","shell.execute_reply":"2021-09-07T10:36:19.030245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(prediction, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.035074Z","iopub.execute_input":"2021-09-07T10:36:19.035653Z","iopub.status.idle":"2021-09-07T10:36:19.041420Z","shell.execute_reply.started":"2021-09-07T10:36:19.035615Z","shell.execute_reply":"2021-09-07T10:36:19.040610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.042644Z","iopub.execute_input":"2021-09-07T10:36:19.043105Z","iopub.status.idle":"2021-09-07T10:36:19.067413Z","shell.execute_reply.started":"2021-09-07T10:36:19.043077Z","shell.execute_reply":"2021-09-07T10:36:19.066377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.068730Z","iopub.execute_input":"2021-09-07T10:36:19.069205Z","iopub.status.idle":"2021-09-07T10:36:19.077507Z","shell.execute_reply.started":"2021-09-07T10:36:19.069169Z","shell.execute_reply":"2021-09-07T10:36:19.076615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = prediction.max(axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.078762Z","iopub.execute_input":"2021-09-07T10:36:19.079096Z","iopub.status.idle":"2021-09-07T10:36:19.085361Z","shell.execute_reply.started":"2021-09-07T10:36:19.079069Z","shell.execute_reply":"2021-09-07T10:36:19.084410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.086778Z","iopub.execute_input":"2021-09-07T10:36:19.087100Z","iopub.status.idle":"2021-09-07T10:36:19.095264Z","shell.execute_reply.started":"2021-09-07T10:36:19.087037Z","shell.execute_reply":"2021-09-07T10:36:19.094262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = h5py.File('../input/my-google-dataset/data.h5', 'r')\nindex_numpy = file.get('dataset').value\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.096813Z","iopub.execute_input":"2021-09-07T10:36:19.097175Z","iopub.status.idle":"2021-09-07T10:36:19.121681Z","shell.execute_reply.started":"2021-09-07T10:36:19.097146Z","shell.execute_reply":"2021-09-07T10:36:19.120678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_numpy","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.123188Z","iopub.execute_input":"2021-09-07T10:36:19.123815Z","iopub.status.idle":"2021-09-07T10:36:19.133708Z","shell.execute_reply.started":"2021-09-07T10:36:19.123772Z","shell.execute_reply":"2021-09-07T10:36:19.132569Z"},"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-07T10:36:19.135300Z","iopub.execute_input":"2021-09-07T10:36:19.135744Z","iopub.status.idle":"2021-09-07T10:36:19.142977Z","shell.execute_reply.started":"2021-09-07T10:36:19.135714Z","shell.execute_reply":"2021-09-07T10:36:19.142162Z"},"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-07T10:36:19.144142Z","iopub.execute_input":"2021-09-07T10:36:19.144402Z","iopub.status.idle":"2021-09-07T10:36:19.207598Z","shell.execute_reply.started":"2021-09-07T10:36:19.144377Z","shell.execute_reply":"2021-09-07T10:36:19.206528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.210540Z","iopub.execute_input":"2021-09-07T10:36:19.210853Z","iopub.status.idle":"2021-09-07T10:36:19.216537Z","shell.execute_reply.started":"2021-09-07T10:36:19.210824Z","shell.execute_reply":"2021-09-07T10:36:19.215756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:36:19.218216Z","iopub.execute_input":"2021-09-07T10:36:19.218806Z","iopub.status.idle":"2021-09-07T10:36:19.249957Z","shell.execute_reply.started":"2021-09-07T10:36:19.218765Z","shell.execute_reply":"2021-09-07T10:36:19.249005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:46:04.305466Z","iopub.execute_input":"2021-09-07T10:46:04.305904Z","iopub.status.idle":"2021-09-07T10:46:04.330175Z","shell.execute_reply.started":"2021-09-07T10:46:04.305870Z","shell.execute_reply":"2021-09-07T10:46:04.329311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}