{"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)\nimport matplotlib.pyplot as plt\n\nimport os\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-05-24T06:11:57.542909Z","iopub.execute_input":"2022-05-24T06:11:57.543226Z","iopub.status.idle":"2022-05-24T06:11:57.640898Z","shell.execute_reply.started":"2022-05-24T06:11:57.543145Z","shell.execute_reply":"2022-05-24T06:11:57.640061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set Path","metadata":{}},{"cell_type":"code","source":"input_dir = os.path.join('..', 'input')\ndata_dir = os.path.join(input_dir, 'landmark-recognition-2021')\n\ntrain_label_info = os.path.join(data_dir, 'train.csv')\ntrain_dir = os.path.join(data_dir, 'train')\ntest_dir = os.path.join(data_dir, 'test')","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:01.494274Z","iopub.execute_input":"2022-05-24T06:12:01.495076Z","iopub.status.idle":"2022-05-24T06:12:01.501494Z","shell.execute_reply.started":"2022-05-24T06:12:01.495034Z","shell.execute_reply":"2022-05-24T06:12:01.50026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traincsv = pd.read_csv(train_label_info)\ntraincsv.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:04.012899Z","iopub.execute_input":"2022-05-24T06:12:04.013168Z","iopub.status.idle":"2022-05-24T06:12:05.291072Z","shell.execute_reply.started":"2022-05-24T06:12:04.013138Z","shell.execute_reply":"2022-05-24T06:12:05.290429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Add Files' path to dataframe\n \n- Because I'm going to use .flow_from_dataframe","metadata":{}},{"cell_type":"code","source":"traincsv['path'] = traincsv['id'].transform(lambda x: train_dir+'/'+str(x[0])+'/'+str(x[1])+'/'+\n                                            str(x[2])+'/'+str(x)+'.jpg')\ntraincsv.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:08.010532Z","iopub.execute_input":"2022-05-24T06:12:08.010789Z","iopub.status.idle":"2022-05-24T06:12:10.635559Z","shell.execute_reply.started":"2022-05-24T06:12:08.010762Z","shell.execute_reply":"2022-05-24T06:12:10.634862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traincsv.info()","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:13.158841Z","iopub.execute_input":"2022-05-24T06:12:13.159123Z","iopub.status.idle":"2022-05-24T06:12:13.463843Z","shell.execute_reply.started":"2022-05-24T06:12:13.159093Z","shell.execute_reply":"2022-05-24T06:12:13.463113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## First I'm going to check the model works well by using only ten labels","metadata":{}},{"cell_type":"code","source":"topten = traincsv['landmark_id'].value_counts().head(100).index\ndftrain = traincsv.loc[traincsv['landmark_id'].isin(topten)].copy()","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:18.841693Z","iopub.execute_input":"2022-05-24T06:12:18.842368Z","iopub.status.idle":"2022-05-24T06:12:18.891159Z","shell.execute_reply.started":"2022-05-24T06:12:18.84232Z","shell.execute_reply":"2022-05-24T06:12:18.890437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Set labels 0 to N","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nencoder = LabelEncoder()\ndftrain['label'] = encoder.fit_transform(dftrain['landmark_id'])\ndftrain.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:21.526527Z","iopub.execute_input":"2022-05-24T06:12:21.527222Z","iopub.status.idle":"2022-05-24T06:12:22.315857Z","shell.execute_reply.started":"2022-05-24T06:12:21.527181Z","shell.execute_reply":"2022-05-24T06:12:22.315168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nprint(f'TensorFlow Version : {tf.__version__}')","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:24.608828Z","iopub.execute_input":"2022-05-24T06:12:24.609093Z","iopub.status.idle":"2022-05-24T06:12:29.453631Z","shell.execute_reply.started":"2022-05-24T06:12:24.609064Z","shell.execute_reply":"2022-05-24T06:12:29.452876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(dftrain)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:30.904072Z","iopub.execute_input":"2022-05-24T06:12:30.904619Z","iopub.status.idle":"2022-05-24T06:12:30.90993Z","shell.execute_reply.started":"2022-05-24T06:12:30.904581Z","shell.execute_reply":"2022-05-24T06:12:30.909184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Split train and val dataset","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain, val = train_test_split(dftrain, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:33.940002Z","iopub.execute_input":"2022-05-24T06:12:33.940274Z","iopub.status.idle":"2022-05-24T06:12:33.998138Z","shell.execute_reply.started":"2022-05-24T06:12:33.940243Z","shell.execute_reply":"2022-05-24T06:12:33.997373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Using datagen for train set(Add noise into train set will be better for the Model)","metadata":{}},{"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n            rescale=1./255,\n            rotation_range=20,\n            width_shift_range=0.2,\n            height_shift_range=0.2,\n            horizontal_flip=True)\n    \n\ntrain_data = datagen.flow_from_dataframe(train, x_col='path', y_col='label', class_mode='raw',\n                                             target_size=(256,256))\n\nval_data = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.).flow_from_dataframe(\n                    val, x_col='path', y_col='label', class_mode='raw', target_size=(256,256))","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:12:37.761703Z","iopub.execute_input":"2022-05-24T06:12:37.76198Z","iopub.status.idle":"2022-05-24T06:13:16.123109Z","shell.execute_reply.started":"2022-05-24T06:12:37.761951Z","shell.execute_reply":"2022-05-24T06:13:16.122358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.Input(shape=(256,256,3))\nbase = tf.keras.applications.EfficientNetB5(\n            include_top=False,\n            weights='imagenet',\n            input_tensor=tf.keras.Input((256, 256, 3))\n        )\nx = base(inputs)\nx = tf.keras.layers.Flatten()(x)\noutputs = tf.keras.layers.Dense(100, activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs=inputs, outputs=outputs)\nmodel.compile(optimizer='Adam', loss='sparse_categorical_crossentropy',\n             metrics=['sparse_categorical_accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:13:44.924088Z","iopub.execute_input":"2022-05-24T06:13:44.924392Z","iopub.status.idle":"2022-05-24T06:13:55.085148Z","shell.execute_reply.started":"2022-05-24T06:13:44.924359Z","shell.execute_reply":"2022-05-24T06:13:55.084268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_data, epochs=10,validation_data=val_data, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-24T06:25:41.625276Z","iopub.execute_input":"2022-05-24T06:25:41.6256Z","iopub.status.idle":"2022-05-24T07:50:01.987783Z","shell.execute_reply.started":"2022-05-24T06:25:41.62557Z","shell.execute_reply":"2022-05-24T07:50:01.987042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(val_data)","metadata":{"execution":{"iopub.status.busy":"2021-09-28T05:27:45.012469Z","iopub.execute_input":"2021-09-28T05:27:45.012841Z","iopub.status.idle":"2021-09-28T05:28:47.717067Z","shell.execute_reply.started":"2021-09-28T05:27:45.012804Z","shell.execute_reply":"2021-09-28T05:28:47.71599Z"},"trusted":true},"execution_count":null,"outputs":[]}]}