{"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":"markdown","source":"# Load all the required library","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport multiprocessing\nfrom PIL import Image\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras.layers import Activation, Dropout, Flatten, Dense, Conv2D, MaxPooling2D, SeparableConv2D, BatchNormalization, GlobalMaxPooling2D\nfrom keras import Model\nfrom keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.applications.xception import Xception\nfrom keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:44:48.394966Z","iopub.execute_input":"2022-04-09T20:44:48.395629Z","iopub.status.idle":"2022-04-09T20:44:53.974117Z","shell.execute_reply.started":"2022-04-09T20:44:48.395537Z","shell.execute_reply":"2022-04-09T20:44:53.973348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = pd.read_csv('../input/landmark-recognition-2020/train.csv', dtype=str)\ntest_label = pd.read_csv('../input/landmark-recognition-2020/sample_submission.csv', dtype=str)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:45:40.238711Z","iopub.execute_input":"2022-04-09T20:45:40.239512Z","iopub.status.idle":"2022-04-09T20:45:41.552136Z","shell.execute_reply.started":"2022-04-09T20:45:40.239456Z","shell.execute_reply":"2022-04-09T20:45:41.551431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The total number of pictures in the train dataset:\", len(train_label))\nprint(\"The total number of landmarks in the train dataset:\", train_label.landmark_id.nunique())\nprint(\"The total number of pictures in the test dataset:\", len(test_label))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:58:38.401473Z","iopub.execute_input":"2022-04-09T18:58:38.401764Z","iopub.status.idle":"2022-04-09T18:58:38.54051Z","shell.execute_reply.started":"2022-04-09T18:58:38.401732Z","shell.execute_reply":"2022-04-09T18:58:38.539296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sort the original train.csv by landmark_id frequency\ntrain_sort = pd.DataFrame(train_label['landmark_id'].value_counts(sort = True, ascending = False)) \ntrain_sort.reset_index(inplace=True) \ntrain_sort.columns=['landmark_id','count']\ntrain_sort","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:58:42.724778Z","iopub.execute_input":"2022-04-09T18:58:42.72524Z","iopub.status.idle":"2022-04-09T18:58:42.935956Z","shell.execute_reply.started":"2022-04-09T18:58:42.725202Z","shell.execute_reply":"2022-04-09T18:58:42.935318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Stratify the training data by landmark_id, with 30% sample\ntrain_label_stratified = train_label.groupby(\"landmark_id\", group_keys=False).apply(lambda x: x.sample(frac = 0.3, random_state = 123))\nprint(\"The total number of pictures in the stratified dataset:\", len(train_label_stratified))\nprint(\"The total number of landmarks in the stratified dataset:\", train_label_stratified.landmark_id.nunique())","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:59:40.566546Z","iopub.execute_input":"2022-04-09T18:59:40.567231Z","iopub.status.idle":"2022-04-09T19:00:33.482274Z","shell.execute_reply.started":"2022-04-09T18:59:40.567193Z","shell.execute_reply":"2022-04-09T19:00:33.480706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_label_stratified = train_label.loc[train_label['landmark_id'].isin([x for x in list(train_sort[0:2500].landmark_id)])]\n#print(\"The total number of pictures in the selected dataset:\", len(train_label_stratified))\n#print(\"The total number of landmarks in the selected dataset:\", train_label_stratified.landmark_id.nunique())\n","metadata":{"execution":{"iopub.status.busy":"2022-04-09T18:59:15.573396Z","iopub.execute_input":"2022-04-09T18:59:15.57369Z","iopub.status.idle":"2022-04-09T18:59:15.726423Z","shell.execute_reply.started":"2022-04-09T18:59:15.573652Z","shell.execute_reply":"2022-04-09T18:59:15.725697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sort_stratified = pd.DataFrame(train_label_stratified['landmark_id'].value_counts()) \ntrain_sort_stratified.reset_index(inplace=True) \ntrain_sort_stratified.columns=['landmark_id','count']\ntrain_sort_stratified","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:00:36.652787Z","iopub.execute_input":"2022-04-09T19:00:36.65304Z","iopub.status.idle":"2022-04-09T19:00:36.754725Z","shell.execute_reply.started":"2022-04-09T19:00:36.653011Z","shell.execute_reply":"2022-04-09T19:00:36.753919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rename the id to match with the image file path in the training/test folder\ntrain_label_stratified[\"id\"] = train_label_stratified.id.str[0]+\"/\"+train_label_stratified.id.str[1]+\"/\"+train_label_stratified.id.str[2]+\"/\"+train_label_stratified.id+\".jpg\"\ntest_label[\"id\"] = test_label.id.str[0]+\"/\"+test_label.id.str[1]+\"/\"+test_label.id.str[2]+\"/\"+test_label.id+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:00:42.512715Z","iopub.execute_input":"2022-04-09T19:00:42.513139Z","iopub.status.idle":"2022-04-09T19:00:44.034848Z","shell.execute_reply.started":"2022-04-09T19:00:42.513101Z","shell.execute_reply":"2022-04-09T19:00:44.034124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label_stratified.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:00:45.810822Z","iopub.execute_input":"2022-04-09T19:00:45.811495Z","iopub.status.idle":"2022-04-09T19:00:45.824357Z","shell.execute_reply.started":"2022-04-09T19:00:45.811459Z","shell.execute_reply":"2022-04-09T19:00:45.823702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_label.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:00:47.406934Z","iopub.execute_input":"2022-04-09T19:00:47.40739Z","iopub.status.idle":"2022-04-09T19:00:47.416758Z","shell.execute_reply.started":"2022-04-09T19:00:47.407352Z","shell.execute_reply":"2022-04-09T19:00:47.415288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# parameters\nval_split = 0.25\nbatch_size = 128\nimg_width = img_height = 256","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:00:53.484573Z","iopub.execute_input":"2022-04-09T19:00:53.48525Z","iopub.status.idle":"2022-04-09T19:00:53.488988Z","shell.execute_reply.started":"2022-04-09T19:00:53.485215Z","shell.execute_reply":"2022-04-09T19:00:53.488086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generate training data and validation data","metadata":{}},{"cell_type":"code","source":"datagen=ImageDataGenerator(validation_split=val_split, rescale=1. / 255)\n\ntrain_generator=datagen.flow_from_dataframe(dataframe=train_label_stratified,\n                                            directory=\"/kaggle/input/landmark-recognition-2020/train/\",\n                                            x_col=\"id\",\n                                            y_col=\"landmark_id\",\n                                            subset=\"training\",\n                                            batch_size=batch_size,\n                                            seed=123,\n                                            shuffle=True,\n                                            class_mode=\"categorical\",\n                                            target_size=(img_width,img_height),\n                                            color_mode=\"rgb\")\n\nvalid_generator=datagen.flow_from_dataframe(dataframe=train_label_stratified,\n                                            directory=\"/kaggle/input/landmark-recognition-2020/train/\",\n                                            x_col=\"id\",\n                                            y_col=\"landmark_id\",\n                                            subset=\"validation\",\n                                            batch_size=batch_size,\n                                            seed=123,\n                                            shuffle=True,\n                                            class_mode=\"categorical\",\n                                            target_size=(img_width,img_height),\n                                            color_mode=\"rgb\")\n\n#test_datagen=ImageDataGenerator(rescale=1. / 255)\n\n#test_generator=test_datagen.flow_from_dataframe(dataframe=test_label,\n#                                                directory=\"/kaggle/input/landmark-recognition-2020/test/\",\n#                                                x_col=\"id\",\n#                                                y_col=None,\n#                                                batch_size=batch_size,\n#                                                seed=123,\n#                                                shuffle=False,\n#                                                class_mode=None,\n#                                                target_size=(img_width,img_height))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:01:02.128832Z","iopub.execute_input":"2022-04-09T19:01:02.129084Z","iopub.status.idle":"2022-04-09T19:20:40.919058Z","shell.execute_reply.started":"2022-04-09T19:01:02.129055Z","shell.execute_reply":"2022-04-09T19:20:40.91747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Define the model with pretrained Xception plus extra layers","metadata":{}},{"cell_type":"code","source":"# model \ndef my_model(input_shape, num_classes, dropout, learning_rate = 0.0002):\n\n    base_model = Xception(input_shape=input_shape,weights='imagenet', include_top=False)\n    #base_model.load_weights(\"../input/keraspretrainedmodel/xception_weights_tf_dim_ordering_tf_kernels_notop.h5\")\n    base_model.trainable = False\n    x = Sequential()\n    x.add(base_model)\n    \n    x.add(SeparableConv2D(64, kernel_size=(3, 3), activation='relu',kernel_initializer = tf.keras.initializers.he_uniform(seed=1)))\n    x.add(BatchNormalization())\n    x.add(SeparableConv2D(32, kernel_size=(3, 3), activation='relu',kernel_initializer = tf.keras.initializers.he_uniform(seed=3)))\n    x.add(BatchNormalization())\n    x.add(SeparableConv2D(num_classes,kernel_size = (3,3), depth_multiplier=1, activation = 'relu',\n                kernel_initializer = tf.keras.initializers.he_uniform(seed=0),\n                kernel_regularizer=tf.keras.regularizers.l1_l2(l1=0.1, l2=0.01)\n                ))\n    x.add(GlobalMaxPooling2D())\n    x.add(BatchNormalization())\n    x.add(Dropout(dropout))\n    \n    x.add(Flatten())\n    x.add(Dense(512, activation = 'relu'))\n    x.add(Dropout(dropout))\n    x.add(Dense(num_classes, activation = 'softmax'))\n\n    x.compile(loss='categorical_crossentropy',experimental_steps_per_execution=8, optimizer = tf.keras.optimizers.Adagrad(learning_rate=learning_rate), metrics='categorical_accuracy')\n    x.summary()\n    return x","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:23:05.42962Z","iopub.execute_input":"2022-04-09T19:23:05.429915Z","iopub.status.idle":"2022-04-09T19:23:05.441394Z","shell.execute_reply.started":"2022-04-09T19:23:05.429885Z","shell.execute_reply":"2022-04-09T19:23:05.440539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(train_sort_stratified)\nmodel = my_model(input_shape = (img_width, img_height, 3), num_classes = num_classes, dropout = 0.3)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:23:10.315798Z","iopub.execute_input":"2022-04-09T19:23:10.31607Z","iopub.status.idle":"2022-04-09T19:23:11.812103Z","shell.execute_reply.started":"2022-04-09T19:23:10.316039Z","shell.execute_reply":"2022-04-09T19:23:11.811445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define call backs:\ncheckpointer = ModelCheckpoint('basic_cnn.h5', monitor='val_categorical_accuracy', verbose=1, save_best_only=True)\n\n# Early stopping\nearly_stopping = EarlyStopping(monitor='val_loss', verbose=1, patience=5)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:23:24.834861Z","iopub.execute_input":"2022-04-09T19:23:24.835127Z","iopub.status.idle":"2022-04-09T19:23:24.840236Z","shell.execute_reply.started":"2022-04-09T19:23:24.835097Z","shell.execute_reply":"2022-04-09T19:23:24.839473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20 # Defining epochs for the model\nbatch_size = 128\ntrain_samples  = int(len(train_label_stratified)*(1-val_split))//batch_size\nvalidation_samples  = int(len(train_label_stratified)*val_split)//batch_size\n\nprint(train_samples)\nprint(validation_samples)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:23:27.783308Z","iopub.execute_input":"2022-04-09T19:23:27.783983Z","iopub.status.idle":"2022-04-09T19:23:27.790422Z","shell.execute_reply.started":"2022-04-09T19:23:27.783946Z","shell.execute_reply":"2022-04-09T19:23:27.789691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    steps_per_epoch=40,\n                    epochs=epochs,\n                    callbacks=[checkpointer, early_stopping],\n                    use_multiprocessing=True,\n                    verbose=2,\n                    validation_data=valid_generator,\n                    validation_steps=10)\n\nmodel.save(\"basic_cnn.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-04-09T19:31:41.981863Z","iopub.execute_input":"2022-04-09T19:31:41.982509Z","iopub.status.idle":"2022-04-09T19:35:13.355298Z","shell.execute_reply.started":"2022-04-09T19:31:41.982471Z","shell.execute_reply":"2022-04-09T19:35:13.354117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.trainable = True\n    \nmodel.compile(loss='categorical_crossentropy', experimental_steps_per_execution=8, optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001), metrics='categorical_accuracy')\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-09T04:10:00.651354Z","iopub.execute_input":"2022-04-09T04:10:00.652649Z","iopub.status.idle":"2022-04-09T04:10:00.698258Z","shell.execute_reply.started":"2022-04-09T04:10:00.652599Z","shell.execute_reply":"2022-04-09T04:10:00.697564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n        train_generator,\n        steps_per_epoch=100,\n        epochs=epochs,\n        callbacks=[checkpointer, early_stopping],\n        use_multiprocessing=True,\n        verbose=1,\n        validation_data=valid_generator,\n        validation_steps=25)\n\nmodel.save(\"fined_cnn.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-04-09T04:10:12.375561Z","iopub.execute_input":"2022-04-09T04:10:12.37582Z","iopub.status.idle":"2022-04-09T04:22:42.327135Z","shell.execute_reply.started":"2022-04-09T04:10:12.375784Z","shell.execute_reply":"2022-04-09T04:22:42.326098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = load_model(\"../input/fine-cnn-model/fined_cnn.h5\")\ntest_label = pd.read_csv('../input/landmark-recognition-2020/sample_submission.csv', dtype=str)\ntest_label[\"id\"] = test_label.id.str[0]+\"/\"+test_label.id.str[1]+\"/\"+test_label.id.str[2]+\"/\"+test_label.id+\".jpg\"\n\ntest_datagen=ImageDataGenerator(rescale=1. / 255)\n\ntest_generator=test_datagen.flow_from_dataframe(dataframe=test_label,\n                                                directory=\"/kaggle/input/landmark-recognition-2020/test/\",\n                                                x_col=\"id\",\n                                                y_col=None,\n                                                batch_size=64,\n                                                seed=123,\n                                                shuffle=False,\n                                                class_mode=None,\n                                                target_size=(256,256))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:47:02.066605Z","iopub.execute_input":"2022-04-09T20:47:02.066879Z","iopub.status.idle":"2022-04-09T20:47:16.098484Z","shell.execute_reply.started":"2022-04-09T20:47:02.066849Z","shell.execute_reply":"2022-04-09T20:47:16.097675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = best_model.predict(test_generator, verbose=1, steps=len(test_label))","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:48:09.025749Z","iopub.execute_input":"2022-04-09T20:48:09.026123Z","iopub.status.idle":"2022-04-09T20:50:19.306863Z","shell.execute_reply.started":"2022-04-09T20:48:09.026068Z","shell.execute_reply":"2022-04-09T20:50:19.306114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_label = np.argmax(y_pred, axis=-1)\ny_prob = np.max(y_pred, axis=-1)\nprint(y_pred_label.shape, y_prob.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:50:22.920033Z","iopub.execute_input":"2022-04-09T20:50:22.920294Z","iopub.status.idle":"2022-04-09T20:50:22.931280Z","shell.execute_reply.started":"2022-04-09T20:50:22.920265Z","shell.execute_reply":"2022-04-09T20:50:22.930362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_test_image_path(img_id):\n    #image_path = f\"../input/landmark-recognition-2020/test/{img_id[0]}{img_id[1]}{img_id[2]}{img_id}\"\n    image_path = f\"../input/landmark-recognition-2020/test/{img_id}\"\n\n    img = np.array(Image.open(image_path).resize((224, 224), Image.LANCZOS))\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:50:24.696409Z","iopub.execute_input":"2022-04-09T20:50:24.696956Z","iopub.status.idle":"2022-04-09T20:50:24.702183Z","shell.execute_reply.started":"2022-04-09T20:50:24.696910Z","shell.execute_reply":"2022-04-09T20:50:24.701021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_image_path(img_id):\n    #image_path = f\"../input/landmark-recognition-2020/test/{img_id[0]}{img_id[1]}{img_id[2]}{img_id}\"\n    image_path = f\"../input/landmark-recognition-2020/train/{img_id}\"\n\n    img = np.array(Image.open(image_path).resize((224, 224), Image.LANCZOS))\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:55:55.638116Z","iopub.execute_input":"2022-04-09T20:55:55.638402Z","iopub.status.idle":"2022-04-09T20:55:55.643817Z","shell.execute_reply.started":"2022-04-09T20:55:55.638371Z","shell.execute_reply":"2022-04-09T20:55:55.642408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = pd.read_csv('../input/landmark-recognition-2020/train.csv', dtype=str)\ntrain_sort = pd.DataFrame(train_label['landmark_id'].value_counts(sort = True, ascending = False)) \ntrain_sort.reset_index(inplace=True) \ntrain_sort.columns=['landmark_id','count']\ntrain_label_stratified = train_label.loc[train_label['landmark_id'].isin([x for x in list(train_sort[0:200].landmark_id)])]\ntrain_sort_stratified = pd.DataFrame(train_label_stratified['landmark_id'].value_counts()) \ntrain_sort_stratified.reset_index(inplace=True) \ntrain_sort_stratified.columns=['landmark_id','count']\ntrain_label_stratified[\"id\"] = train_label_stratified.id.str[0]+\"/\"+train_label_stratified.id.str[1]+\"/\"+train_label_stratified.id.str[2]+\"/\"+train_label_stratified.id+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:52:27.798764Z","iopub.execute_input":"2022-04-09T20:52:27.799055Z","iopub.status.idle":"2022-04-09T20:52:29.447861Z","shell.execute_reply.started":"2022-04-09T20:52:27.799025Z","shell.execute_reply":"2022-04-09T20:52:29.446984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_uniq = np.unique(train_label_stratified.landmark_id.values)\ny_pred_label = [y_uniq[Y] for Y in y_pred_label]","metadata":{"execution":{"iopub.status.busy":"2022-04-09T20:52:35.188773Z","iopub.execute_input":"2022-04-09T20:52:35.189040Z","iopub.status.idle":"2022-04-09T20:52:35.287119Z","shell.execute_reply.started":"2022-04-09T20:52:35.189010Z","shell.execute_reply":"2022-04-09T20:52:35.286371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extracting best and worst classficiations from predictions","metadata":{}},{"cell_type":"code","source":"temp_sub","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:06:30.274934Z","iopub.execute_input":"2022-04-09T21:06:30.275394Z","iopub.status.idle":"2022-04-09T21:06:30.289709Z","shell.execute_reply.started":"2022-04-09T21:06:30.275355Z","shell.execute_reply":"2022-04-09T21:06:30.288768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_sub = test_label\n\nfor i in range(len(temp_sub)):\n    temp_sub.loc[i, \"landmarks\"] = str(y_pred_label[i])\n\n#temp_sub.insert(2, \"pred\", y_prob)    \n","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:07:15.573618Z","iopub.execute_input":"2022-04-09T21:07:15.573882Z","iopub.status.idle":"2022-04-09T21:07:18.208691Z","shell.execute_reply.started":"2022-04-09T21:07:15.573853Z","shell.execute_reply":"2022-04-09T21:07:18.207963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"worst_preds = temp_sub.sort_values(by=['pred'])\nworst_preds = worst_preds[0:12]\nworst_preds","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:08:27.820734Z","iopub.execute_input":"2022-04-09T21:08:27.821438Z","iopub.status.idle":"2022-04-09T21:08:27.839121Z","shell.execute_reply.started":"2022-04-09T21:08:27.821396Z","shell.execute_reply":"2022-04-09T21:08:27.838249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_preds = temp_sub.sort_values(by=['pred'], ascending=False)\nbest_preds = best_preds[0:12]\nbest_preds","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:08:29.478633Z","iopub.execute_input":"2022-04-09T21:08:29.478894Z","iopub.status.idle":"2022-04-09T21:08:29.494934Z","shell.execute_reply.started":"2022-04-09T21:08:29.478865Z","shell.execute_reply":"2022-04-09T21:08:29.494221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 12 Worst classifications","metadata":{}},{"cell_type":"code","source":"figure = plt.figure(figsize = (14, 14))\nworst_images = worst_preds.id.values\n\nfor i in range(len(worst_images)):\n    path = worst_images[i]\n    # Display the randomly selected images.\n    image = get_test_image_path(path)\n    figure.add_subplot(3, 4, i+1)\n    plt.title(worst_preds.pred.values[i])\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:08:36.769504Z","iopub.execute_input":"2022-04-09T21:08:36.769766Z","iopub.status.idle":"2022-04-09T21:08:38.496381Z","shell.execute_reply.started":"2022-04-09T21:08:36.769737Z","shell.execute_reply":"2022-04-09T21:08:38.495686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 12 Best classfications","metadata":{}},{"cell_type":"code","source":"figure = plt.figure(figsize = (14, 14))\nbest_images = best_preds.id.values\n\nfor i in range(len(best_images)):\n    path = best_images[i]\n    image = get_test_image_path(path)\n    figure.add_subplot(3, 4, i+1)\n    plt.title(best_preds.pred.values[i])\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:08:46.978055Z","iopub.execute_input":"2022-04-09T21:08:46.978388Z","iopub.status.idle":"2022-04-09T21:08:48.631742Z","shell.execute_reply.started":"2022-04-09T21:08:46.978354Z","shell.execute_reply":"2022-04-09T21:08:48.631100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visual Comparison","metadata":{}},{"cell_type":"code","source":"best_train = train_label_stratified[train_label_stratified.landmark_id == '96663'].reset_index()\nbest_train","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:09:06.445426Z","iopub.execute_input":"2022-04-09T21:09:06.445698Z","iopub.status.idle":"2022-04-09T21:09:06.474943Z","shell.execute_reply.started":"2022-04-09T21:09:06.445669Z","shell.execute_reply":"2022-04-09T21:09:06.473885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure = plt.figure(figsize = (14, 14))\nfor i in range(9):\n    path = best_train.id[i]\n    image = get_train_image_path(path)\n    figure.add_subplot(3, 3, i+1)\n    plt.title(best_train.id[i])\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:09:08.624930Z","iopub.execute_input":"2022-04-09T21:09:08.625187Z","iopub.status.idle":"2022-04-09T21:09:10.222747Z","shell.execute_reply.started":"2022-04-09T21:09:08.625159Z","shell.execute_reply":"2022-04-09T21:09:10.220397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_train_2 = train_label_stratified[train_label_stratified.landmark_id == '179959'].reset_index()\nbest_train_2","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:12:59.309349Z","iopub.execute_input":"2022-04-09T21:12:59.309914Z","iopub.status.idle":"2022-04-09T21:12:59.340288Z","shell.execute_reply.started":"2022-04-09T21:12:59.309874Z","shell.execute_reply":"2022-04-09T21:12:59.339459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure = plt.figure(figsize = (14, 14))\nfor i in range(9):\n    path = best_train_2.id[i]\n    image = get_train_image_path(path)\n    figure.add_subplot(3, 3, i+1)\n    plt.title(best_train_2.id[i])\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:13:00.924805Z","iopub.execute_input":"2022-04-09T21:13:00.925627Z","iopub.status.idle":"2022-04-09T21:13:02.463013Z","shell.execute_reply.started":"2022-04-09T21:13:00.925586Z","shell.execute_reply":"2022-04-09T21:13:02.462355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"worst_train = train_label_stratified[train_label_stratified.landmark_id == '126637'].reset_index()\nworst_train","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:10:26.983657Z","iopub.execute_input":"2022-04-09T21:10:26.983933Z","iopub.status.idle":"2022-04-09T21:10:27.013905Z","shell.execute_reply.started":"2022-04-09T21:10:26.983897Z","shell.execute_reply":"2022-04-09T21:10:27.013189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure = plt.figure(figsize = (14, 14))\nfor i in range(9):\n    path = worst_train.id[i]\n    image = get_train_image_path(path)\n    figure.add_subplot(3, 3, i+1)\n    plt.title(worst_train.id[i])\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-04-09T21:10:38.593716Z","iopub.execute_input":"2022-04-09T21:10:38.594173Z","iopub.status.idle":"2022-04-09T21:10:40.587473Z","shell.execute_reply.started":"2022-04-09T21:10:38.594129Z","shell.execute_reply":"2022-04-09T21:10:40.586677Z"},"trusted":true},"execution_count":null,"outputs":[]}]}