{"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\nimport time\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nprint(f'TensorFlow Version : {tf.__version__}')\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":"2021-09-30T01:46:47.564323Z","iopub.execute_input":"2021-09-30T01:46:47.564704Z","iopub.status.idle":"2021-09-30T01:46:47.572319Z","shell.execute_reply.started":"2021-09-30T01:46:47.564663Z","shell.execute_reply":"2021-09-30T01:46:47.571639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path = '../input/landmark-recognition-2021/train/'\ntest_img_path = '../input/landmark-recognition-2021/test/'","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:47.574899Z","iopub.execute_input":"2021-09-30T01:46:47.575505Z","iopub.status.idle":"2021-09-30T01:46:47.583085Z","shell.execute_reply.started":"2021-09-30T01:46:47.575470Z","shell.execute_reply":"2021-09-30T01:46:47.581991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traincsv = pd.read_csv('../input/landmark-recognition-2021/train.csv')\ntraincsv['path'] = traincsv['id'].transform(lambda x : train_img_path+x[0]+'/'+x[1]+'/'+x[2]+'/'+x+'.jpg')\ntraincsv.head(1)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:47.583893Z","iopub.execute_input":"2021-09-30T01:46:47.584090Z","iopub.status.idle":"2021-09-30T01:46:49.949793Z","shell.execute_reply.started":"2021-09-30T01:46:47.584069Z","shell.execute_reply":"2021-09-30T01:46:49.949118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_freq = traincsv['landmark_id'].value_counts().head(5).index\ndf = traincsv.loc[traincsv['landmark_id'].isin(top_freq)].copy()\ndf.head(1)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:49.950958Z","iopub.execute_input":"2021-09-30T01:46:49.951280Z","iopub.status.idle":"2021-09-30T01:46:50.094712Z","shell.execute_reply.started":"2021-09-30T01:46:49.951243Z","shell.execute_reply":"2021-09-30T01:46:50.093889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = LabelEncoder()\ndf['label'] = encoder.fit_transform(df['landmark_id'])\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:50.096927Z","iopub.execute_input":"2021-09-30T01:46:50.097266Z","iopub.status.idle":"2021-09-30T01:46:50.111134Z","shell.execute_reply.started":"2021-09-30T01:46:50.097229Z","shell.execute_reply":"2021-09-30T01:46:50.110282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dftrain, dfval = train_test_split(df, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:50.112376Z","iopub.execute_input":"2021-09-30T01:46:50.112948Z","iopub.status.idle":"2021-09-30T01:46:50.120882Z","shell.execute_reply.started":"2021-09-30T01:46:50.112912Z","shell.execute_reply":"2021-09-30T01:46:50.120106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_img(dataset, trainable):\n    if trainable:\n        return dataset.map(lambda x, y : (tf.image.resize_with_pad(\n                    tf.io.decode_image(tf.io.read_file(x), channels=3), \n                                    150, 150)/255., y),\n        num_parallel_calls=tf.data.experimental.AUTOTUNE\n        )\n    else:\n        return dataset.map(lambda x : tf.image.resize_with_pad(\n                    tf.io.decode_image(tf.io.read_file(x), channels=3), \n                                    150, 150)/255.,\n        num_parallel_calls=tf.data.experimental.AUTOTUNE\n        )","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:50.122157Z","iopub.execute_input":"2021-09-30T01:46:50.122609Z","iopub.status.idle":"2021-09-30T01:46:50.129881Z","shell.execute_reply.started":"2021-09-30T01:46:50.122573Z","shell.execute_reply":"2021-09-30T01:46:50.129179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_ds(df, shuffle=True, trainable=True):\n    if trainable:\n        labels = df['label']\n        ds = tf.data.Dataset.from_tensor_slices((df['path'], labels))\n    else:\n        ds = tf.data.Dataset.from_tensor_slices(df['path'])\n    ds = decode_img(ds, trainable)\n    if shuffle:\n        ds = ds.shuffle(len(df))\n    ds = ds.batch(64)\n    ds = ds.prefetch(2)    \n    return ds\n\ndstrain = get_ds(dftrain)\ndsval = get_ds(dfval, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:50.131387Z","iopub.execute_input":"2021-09-30T01:46:50.131681Z","iopub.status.idle":"2021-09-30T01:46:50.286562Z","shell.execute_reply.started":"2021-09-30T01:46:50.131645Z","shell.execute_reply":"2021-09-30T01:46:50.285881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.Input(shape=(150,150,3))\nx = tf.keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu')(inputs)\nx = tf.keras.layers.MaxPool2D()(x)\nx = tf.keras.layers.Conv2D(filters=128, kernel_size=3, activation='relu')(x)\nx = tf.keras.layers.MaxPool2D()(x)\nx = tf.keras.layers.Flatten()(x)\noutputs = tf.keras.layers.Dense(5, activation='softmax')(x)\n\nmodel = tf.keras.Model(inputs=inputs, outputs=outputs)\nmodel.compile(loss='sparse_categorical_crossentropy',\n              optimizer='Adam',\n              metrics=['sparse_categorical_accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:50.288107Z","iopub.execute_input":"2021-09-30T01:46:50.288538Z","iopub.status.idle":"2021-09-30T01:46:50.338866Z","shell.execute_reply.started":"2021-09-30T01:46:50.288503Z","shell.execute_reply":"2021-09-30T01:46:50.338067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(dstrain, epochs=5)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:46:50.340260Z","iopub.execute_input":"2021-09-30T01:46:50.340541Z","iopub.status.idle":"2021-09-30T01:49:08.937601Z","shell.execute_reply.started":"2021-09-30T01:46:50.340508Z","shell.execute_reply":"2021-09-30T01:49:08.936881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(dsval)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:49:08.938945Z","iopub.execute_input":"2021-09-30T01:49:08.939197Z","iopub.status.idle":"2021-09-30T01:49:15.300662Z","shell.execute_reply.started":"2021-09-30T01:49:08.939163Z","shell.execute_reply":"2021-09-30T01:49:15.299985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forecast = model.predict(dsval)\nprint(f'First Data Label : {np.argmax(forecast[0])}')\ndfval['label'].head(1)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T01:49:15.304673Z","iopub.execute_input":"2021-09-30T01:49:15.306641Z","iopub.status.idle":"2021-09-30T01:49:20.492565Z","shell.execute_reply.started":"2021-09-30T01:49:15.306604Z","shell.execute_reply":"2021-09-30T01:49:20.491889Z"},"trusted":true},"execution_count":null,"outputs":[]}]}