{"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":"\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\n\nimport os\nfor 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 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","_kg_hide-input":true,"_kg_hide-output":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport PIL\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:23:17.400827Z","iopub.execute_input":"2023-05-23T23:23:17.401172Z","iopub.status.idle":"2023-05-23T23:23:23.580819Z","shell.execute_reply.started":"2023-05-23T23:23:17.401146Z","shell.execute_reply":"2023-05-23T23:23:23.579954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Using Pandas to open the training data in a dataframe. \ntrainingDataset = pd.read_csv('/kaggle/input/landmark-recognition-2020/train.csv')\n\nprint(trainingDataset.head())\nprint(trainingDataset.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:23:24.877431Z","iopub.execute_input":"2023-05-23T23:23:24.878512Z","iopub.status.idle":"2023-05-23T23:23:26.239332Z","shell.execute_reply.started":"2023-05-23T23:23:24.878479Z","shell.execute_reply":"2023-05-23T23:23:26.238375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlabel_counts = trainingDataset['landmark_id'].value_counts()\n\n\n#finding the top 10 labels \ntop_10_labels = label_counts.head(10)\n\ntop10 = top_10_labels.index.to_list()\nprint(top10)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:23:27.279157Z","iopub.execute_input":"2023-05-23T23:23:27.279506Z","iopub.status.idle":"2023-05-23T23:23:27.325500Z","shell.execute_reply.started":"2023-05-23T23:23:27.279477Z","shell.execute_reply":"2023-05-23T23:23:27.324560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#reducing the dataset to only the top 10 labels \nreduced_df = trainingDataset[trainingDataset['landmark_id'].isin(top10)].copy()\nprint(reduced_df.head())\nprint(reduced_df.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:23:32.371062Z","iopub.execute_input":"2023-05-23T23:23:32.371413Z","iopub.status.idle":"2023-05-23T23:23:32.399276Z","shell.execute_reply.started":"2023-05-23T23:23:32.371385Z","shell.execute_reply":"2023-05-23T23:23:32.398302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n#reducing the dataset to a smaller count but with same proportions of lables. \nclass_proportions = reduced_df['landmark_id'].value_counts(normalize=True)\n\ndesired_sample_count = 10000  \n\nclass_sample_counts = (class_proportions * len(reduced_df)).round().astype(int)\nclass_sample_counts = class_sample_counts.apply(lambda count: min(count, desired_sample_count))\n\nreduced_df = reduced_df.groupby('landmark_id').apply(lambda x: x.sample(class_sample_counts[x.name])).reset_index(drop=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:29:36.298424Z","iopub.execute_input":"2023-05-23T23:29:36.298806Z","iopub.status.idle":"2023-05-23T23:29:36.317269Z","shell.execute_reply.started":"2023-05-23T23:29:36.298758Z","shell.execute_reply":"2023-05-23T23:29:36.316210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(reduced_df.shape[0])","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:27:10.466855Z","iopub.execute_input":"2023-05-23T23:27:10.467531Z","iopub.status.idle":"2023-05-23T23:27:10.472821Z","shell.execute_reply.started":"2023-05-23T23:27:10.467497Z","shell.execute_reply":"2023-05-23T23:27:10.471782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimages = []\nlabels = []\n\n#Converting the dataset to have the actual image in the image column not just the name \nfor index, row in reduced_df.iterrows():\n    imageName = row['id']\n    \n    image_path = \"/kaggle/input/landmark-recognition-2020/train/\"+imageName[0] + \"/\" +imageName[1] + \"/\" + imageName[2] + \"/\" + imageName +\".jpg\"\n    print(image_path)\n    image = cv2.imread(image_path)\n    #Ensure images are in RGB \n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # Convert BGR to RGB\n    \n    images.append(image)\n    labels.append(row['landmark_id'])  \nimages = np.array(images)\nlabels = np.array(labels)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:29:39.205130Z","iopub.execute_input":"2023-05-23T23:29:39.205500Z","iopub.status.idle":"2023-05-23T23:29:40.250357Z","shell.execute_reply.started":"2023-05-23T23:29:39.205464Z","shell.execute_reply":"2023-05-23T23:29:40.248956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#making sure images array is containing the real images. \nimage_index = 2 \n\n# Display the image\nplt.imshow(images[image_index])\nplt.axis('off')  # Remove axis labels\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:29:43.340017Z","iopub.execute_input":"2023-05-23T23:29:43.340378Z","iopub.status.idle":"2023-05-23T23:29:43.597211Z","shell.execute_reply.started":"2023-05-23T23:29:43.340350Z","shell.execute_reply":"2023-05-23T23:29:43.596347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Encoding the labels to be from 0 - 9\nfrom sklearn.preprocessing import LabelEncoder\n\n\nlabel_encoder = LabelEncoder()\n\nlabels_encoded = label_encoder.fit_transform(labels)\n\nprint(dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_))))\n\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:29:45.682741Z","iopub.execute_input":"2023-05-23T23:29:45.683594Z","iopub.status.idle":"2023-05-23T23:29:45.692897Z","shell.execute_reply.started":"2023-05-23T23:29:45.683560Z","shell.execute_reply":"2023-05-23T23:29:45.690613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#resizing all the images to now be the same size. \n\nimport cv2\n\n\nresized_images = []\n\nfor img in images:\n    img_resized = cv2.resize(img, (800, 593))\n    resized_images.append(img_resized)\n\ndata_resized = np.array(resized_images)\n\nprint(data_resized.shape)  # This should now be (100, 593, 800, 3).\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:29:47.893526Z","iopub.execute_input":"2023-05-23T23:29:47.893908Z","iopub.status.idle":"2023-05-23T23:29:48.131076Z","shell.execute_reply.started":"2023-05-23T23:29:47.893878Z","shell.execute_reply":"2023-05-23T23:29:48.130048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#images = np.asarray(images).astype('float32')\n\n#printing the shape of all the images, making sure they are all the same size. \nprint(data_resized[0].shape)\nfor i in data_resized:\n    print(i.shape)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:29:50.584099Z","iopub.execute_input":"2023-05-23T23:29:50.584449Z","iopub.status.idle":"2023-05-23T23:29:50.591866Z","shell.execute_reply.started":"2023-05-23T23:29:50.584421Z","shell.execute_reply":"2023-05-23T23:29:50.590726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data_resized[0])\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:29:53.317341Z","iopub.execute_input":"2023-05-23T23:29:53.317699Z","iopub.status.idle":"2023-05-23T23:29:53.743266Z","shell.execute_reply.started":"2023-05-23T23:29:53.317669Z","shell.execute_reply":"2023-05-23T23:29:53.740326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(data_resized[1])","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:27:30.765724Z","iopub.execute_input":"2023-05-23T23:27:30.766722Z","iopub.status.idle":"2023-05-23T23:27:31.167725Z","shell.execute_reply.started":"2023-05-23T23:27:30.766680Z","shell.execute_reply":"2023-05-23T23:27:31.166727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n \n\n#converting the images array to a tensor for the deep learning network. \ntensor = tf.convert_to_tensor(data_resized)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:29:58.719521Z","iopub.execute_input":"2023-05-23T23:29:58.719920Z","iopub.status.idle":"2023-05-23T23:29:58.876665Z","shell.execute_reply.started":"2023-05-23T23:29:58.719887Z","shell.execute_reply":"2023-05-23T23:29:58.875626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\n#doing label encoading here so I can run the training in one run. \nlabel_encoder = LabelEncoder()\n\nlabels_encoded = label_encoder.fit_transform(labels)\n\n\n#normalizing all pixel values. \ndata_resized = data_resized / 255.0\n\n# Split the data into a training set and a test set.\n# I'm using 80% of the data for training and 20% for testing.\ntrain_images, test_images, train_labels, test_labels = train_test_split(data_resized, labels_encoded, test_size=0.2, random_state=42)\n\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(593, 800, 3)),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    Flatten(),\n    Dense(64, activation='relu'),\n    Dense(len(set(labels_encoded)), activation='softmax')  # The number of neurons should match the number of classes\n])\n\nmodel.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n              metrics=['accuracy'])\n\nmodel.fit(train_images, train_labels, epochs=10)\n\n# Evaluate the model on the test set.\ntest_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)\nprint('\\nTest accuracy:', test_acc)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-23T23:30:00.712496Z","iopub.execute_input":"2023-05-23T23:30:00.712877Z","iopub.status.idle":"2023-05-23T23:30:15.184030Z","shell.execute_reply.started":"2023-05-23T23:30:00.712845Z","shell.execute_reply":"2023-05-23T23:30:15.182483Z"},"trusted":true},"execution_count":null,"outputs":[]}]}