{"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":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport random\nimport keras\nfrom keras.models import Sequential\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.layers import Dense,Flatten,Dropout,Conv2D,MaxPooling2D,BatchNormalization\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.metrics import classification_report, log_loss, accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nimport cv2","metadata":{"papermill":{"duration":5.890905,"end_time":"2021-08-12T15:20:55.330215","exception":false,"start_time":"2021-08-12T15:20:49.43931","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:09:19.403677Z","iopub.execute_input":"2021-10-01T17:09:19.404045Z","iopub.status.idle":"2021-10-01T17:09:24.433419Z","shell.execute_reply.started":"2021-10-01T17:09:19.403996Z","shell.execute_reply":"2021-10-01T17:09:24.432582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/landmark-recognition-2021/train'\ntest_dir = '../input/landmark-recognition-2021/test'","metadata":{"papermill":{"duration":0.027035,"end_time":"2021-08-12T15:20:55.376863","exception":false,"start_time":"2021-08-12T15:20:55.349828","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:09:24.434938Z","iopub.execute_input":"2021-10-01T17:09:24.435292Z","iopub.status.idle":"2021-10-01T17:09:24.439850Z","shell.execute_reply.started":"2021-10-01T17:09:24.435254Z","shell.execute_reply":"2021-10-01T17:09:24.438934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('../input/landmark-recognition-2021/train.csv')\ntrain","metadata":{"execution":{"iopub.status.busy":"2021-10-01T17:09:24.458331Z","iopub.execute_input":"2021-10-01T17:09:24.458870Z","iopub.status.idle":"2021-10-01T17:09:25.738411Z","shell.execute_reply.started":"2021-10-01T17:09:24.458827Z","shell.execute_reply":"2021-10-01T17:09:25.737630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.read_csv('../input/landmark-recognition-2021/sample_submission.csv')\nsample","metadata":{"execution":{"iopub.status.busy":"2021-10-01T17:09:25.739720Z","iopub.execute_input":"2021-10-01T17:09:25.740064Z","iopub.status.idle":"2021-10-01T17:09:25.765483Z","shell.execute_reply.started":"2021-10-01T17:09:25.740027Z","shell.execute_reply":"2021-10-01T17:09:25.764425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Name=[]\nfor landmark in train['landmark_id'].unique():\n    Name+=[landmark]\nprint(len(Name))\nn=len(Name)","metadata":{"papermill":{"duration":0.054538,"end_time":"2021-08-12T15:20:55.450664","exception":false,"start_time":"2021-08-12T15:20:55.396126","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:09:25.766837Z","iopub.execute_input":"2021-10-01T17:09:25.767180Z","iopub.status.idle":"2021-10-01T17:09:25.812526Z","shell.execute_reply.started":"2021-10-01T17:09:25.767144Z","shell.execute_reply":"2021-10-01T17:09:25.811480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N=list(range(n))\nprint(n)\nnormal_mapping=dict(zip(Name,N)) \nreverse_mapping=dict(zip(N,Name)) ","metadata":{"papermill":{"duration":0.027332,"end_time":"2021-08-12T15:20:55.497997","exception":false,"start_time":"2021-08-12T15:20:55.470665","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:09:25.813821Z","iopub.execute_input":"2021-10-01T17:09:25.814156Z","iopub.status.idle":"2021-10-01T17:09:25.836405Z","shell.execute_reply.started":"2021-10-01T17:09:25.814121Z","shell.execute_reply":"2021-10-01T17:09:25.835431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainX0=[]\ntrainY0=[]\n#len(train)\nfor i in tqdm(range(10000)):\n    idt=train.loc[i,'id']+'.jpg'\n    landt=train.loc[i,'landmark_id']\n    path=os.path.join(train_dir,idt[0],idt[1],idt[2])\n    image=cv2.imread(os.path.join(path,idt))\n    image2=cv2.resize(image,dsize=(16,16),interpolation=cv2.INTER_CUBIC)\n    trainX0+=[image2]\n    trainY0+=[landt]    ","metadata":{"papermill":{"duration":4193.479323,"end_time":"2021-08-12T16:30:48.996776","exception":false,"start_time":"2021-08-12T15:20:55.517453","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:09:25.837509Z","iopub.execute_input":"2021-10-01T17:09:25.837952Z","iopub.status.idle":"2021-10-01T17:11:14.080480Z","shell.execute_reply.started":"2021-10-01T17:09:25.837876Z","shell.execute_reply":"2021-10-01T17:11:14.079618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainX=np.array(trainX0)\ntrainY1=pd.Series(trainY0).map(normal_mapping)\ntrainY2=np.array(trainY1)","metadata":{"papermill":{"duration":1.481607,"end_time":"2021-08-12T16:30:51.849726","exception":false,"start_time":"2021-08-12T16:30:50.368119","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:14.081757Z","iopub.execute_input":"2021-10-01T17:11:14.082140Z","iopub.status.idle":"2021-10-01T17:11:14.178538Z","shell.execute_reply.started":"2021-10-01T17:11:14.082108Z","shell.execute_reply":"2021-10-01T17:11:14.177737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainY3=to_categorical(trainY2)\nX_train=np.array(trainX0).reshape(-1,16,16,3)\ny_train=np.array(trainY3)","metadata":{"papermill":{"duration":1.013638,"end_time":"2021-08-12T16:30:54.17095","exception":false,"start_time":"2021-08-12T16:30:53.157312","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:14.179765Z","iopub.execute_input":"2021-10-01T17:11:14.180138Z","iopub.status.idle":"2021-10-01T17:11:14.211589Z","shell.execute_reply.started":"2021-10-01T17:11:14.180100Z","shell.execute_reply":"2021-10-01T17:11:14.210742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainx,testx,trainy,testy=train_test_split(X_train,y_train,test_size=0.2,random_state=44)","metadata":{"papermill":{"duration":0.771643,"end_time":"2021-08-12T16:30:55.307373","exception":false,"start_time":"2021-08-12T16:30:54.53573","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:14.212831Z","iopub.execute_input":"2021-10-01T17:11:14.213434Z","iopub.status.idle":"2021-10-01T17:11:14.231760Z","shell.execute_reply.started":"2021-10-01T17:11:14.213393Z","shell.execute_reply":"2021-10-01T17:11:14.230862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(trainx.shape)\nprint(testx.shape)\nprint(trainy.shape)\nprint(testy.shape)\nprint(trainy.shape[1])","metadata":{"papermill":{"duration":0.089413,"end_time":"2021-08-12T16:30:55.477237","exception":false,"start_time":"2021-08-12T16:30:55.387824","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:14.233069Z","iopub.execute_input":"2021-10-01T17:11:14.233436Z","iopub.status.idle":"2021-10-01T17:11:14.239830Z","shell.execute_reply.started":"2021-10-01T17:11:14.233395Z","shell.execute_reply":"2021-10-01T17:11:14.238769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32,(2,2),input_shape = (16,16,3),activation = 'relu'))\nmodel.add(Conv2D(32,(2,2),activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(trainy.shape[1], activation='softmax'))","metadata":{"papermill":{"duration":0.419664,"end_time":"2021-08-12T16:30:55.98255","exception":false,"start_time":"2021-08-12T16:30:55.562886","status":"completed"},"tags":[],"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-10-01T17:11:14.241183Z","iopub.execute_input":"2021-10-01T17:11:14.241528Z","iopub.status.idle":"2021-10-01T17:11:16.197575Z","shell.execute_reply.started":"2021-10-01T17:11:14.241492Z","shell.execute_reply":"2021-10-01T17:11:16.196689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])","metadata":{"papermill":{"duration":0.106575,"end_time":"2021-08-12T16:30:56.171685","exception":false,"start_time":"2021-08-12T16:30:56.06511","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:16.198800Z","iopub.execute_input":"2021-10-01T17:11:16.199127Z","iopub.status.idle":"2021-10-01T17:11:16.213862Z","shell.execute_reply.started":"2021-10-01T17:11:16.199094Z","shell.execute_reply":"2021-10-01T17:11:16.212672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"papermill":{"duration":0.093388,"end_time":"2021-08-12T16:30:56.346517","exception":false,"start_time":"2021-08-12T16:30:56.253129","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:16.215382Z","iopub.execute_input":"2021-10-01T17:11:16.215806Z","iopub.status.idle":"2021-10-01T17:11:16.226085Z","shell.execute_reply.started":"2021-10-01T17:11:16.215773Z","shell.execute_reply":"2021-10-01T17:11:16.224843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"his = model.fit(X_train, y_train, validation_split=0.2, epochs=50, batch_size=64, verbose=2)","metadata":{"papermill":{"duration":1953.382631,"end_time":"2021-08-12T17:03:29.811355","exception":false,"start_time":"2021-08-12T16:30:56.428724","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:16.227571Z","iopub.execute_input":"2021-10-01T17:11:16.227932Z","iopub.status.idle":"2021-10-01T17:11:42.650735Z","shell.execute_reply.started":"2021-10-01T17:11:16.227896Z","shell.execute_reply":"2021-10-01T17:11:42.649879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(testx)\npred=np.argmax(y_pred,axis=1)\nground = np.argmax(testy,axis=1)\nprint(classification_report(ground,pred))","metadata":{"papermill":{"duration":7.963623,"end_time":"2021-08-12T17:03:37.874028","exception":false,"start_time":"2021-08-12T17:03:29.910405","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:42.653820Z","iopub.execute_input":"2021-10-01T17:11:42.654090Z","iopub.status.idle":"2021-10-01T17:11:42.844215Z","shell.execute_reply.started":"2021-10-01T17:11:42.654063Z","shell.execute_reply":"2021-10-01T17:11:42.843386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_acc = his.history['accuracy']\nvalue_acc = his.history['val_accuracy']\nget_loss = his.history['loss']\nvalidation_loss = his.history['val_loss']\n\nepochs = range(len(get_acc))\nplt.plot(epochs, get_acc, 'r', label='Accuracy of Training data')\nplt.plot(epochs, value_acc, 'b', label='Accuracy of Validation data')\nplt.title('Training vs validation accuracy')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":0.321252,"end_time":"2021-08-12T17:03:38.298576","exception":false,"start_time":"2021-08-12T17:03:37.977324","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:42.845694Z","iopub.execute_input":"2021-10-01T17:11:42.846306Z","iopub.status.idle":"2021-10-01T17:11:43.022141Z","shell.execute_reply.started":"2021-10-01T17:11:42.846266Z","shell.execute_reply":"2021-10-01T17:11:43.021160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = range(len(get_loss))\nplt.plot(epochs, get_loss, 'r', label='Loss of Training data')\nplt.plot(epochs, validation_loss, 'b', label='Loss of Validation data')\nplt.title('Training vs validation loss')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"papermill":{"duration":0.28024,"end_time":"2021-08-12T17:03:38.679601","exception":false,"start_time":"2021-08-12T17:03:38.399361","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:11:43.023571Z","iopub.execute_input":"2021-10-01T17:11:43.023936Z","iopub.status.idle":"2021-10-01T17:11:43.185665Z","shell.execute_reply.started":"2021-10-01T17:11:43.023897Z","shell.execute_reply":"2021-10-01T17:11:43.184736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testX0=[]\nfor i in tqdm(range(len(sample))):\n    idt=sample.loc[i,'id']+'.jpg'\n    path=os.path.join(test_dir,idt[0],idt[1],idt[2])\n    image=cv2.imread(os.path.join(path,idt))\n    image2=cv2.resize(image,dsize=(16,16),interpolation=cv2.INTER_CUBIC)\n    testX0+=[image2]","metadata":{"execution":{"iopub.status.busy":"2021-10-01T17:11:43.186971Z","iopub.execute_input":"2021-10-01T17:11:43.187448Z","iopub.status.idle":"2021-10-01T17:13:49.886292Z","shell.execute_reply.started":"2021-10-01T17:11:43.187407Z","shell.execute_reply":"2021-10-01T17:13:49.884512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=np.array(testX0).reshape(-1,16,16,3)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T17:13:49.887783Z","iopub.execute_input":"2021-10-01T17:13:49.888201Z","iopub.status.idle":"2021-10-01T17:13:49.905959Z","shell.execute_reply.started":"2021-10-01T17:13:49.888157Z","shell.execute_reply":"2021-10-01T17:13:49.904991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2=model.predict(X_test)\nprint(pred2[0:10])\n\nPRED=[]\nfor item in pred2:\n    value=np.argmax(item)\n    score=item[value]\n    value2=reverse_mapping[value]\n    PRED+=[str(value2)+' '+str(score)]\nprint(PRED[0:10])","metadata":{"papermill":{"duration":12.751114,"end_time":"2021-08-12T17:03:51.532692","exception":false,"start_time":"2021-08-12T17:03:38.781578","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:13:49.907339Z","iopub.execute_input":"2021-10-01T17:13:49.907766Z","iopub.status.idle":"2021-10-01T17:13:50.304865Z","shell.execute_reply.started":"2021-10-01T17:13:49.907725Z","shell.execute_reply":"2021-10-01T17:13:50.302969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submt=sample\nsubmt['landmarks']=PRED\nsubmt.to_csv('submission.csv',index=False)\nsubmt","metadata":{"papermill":{"duration":0.10163,"end_time":"2021-08-12T17:03:52.412723","exception":false,"start_time":"2021-08-12T17:03:52.311093","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-01T17:13:50.306201Z","iopub.execute_input":"2021-10-01T17:13:50.306536Z","iopub.status.idle":"2021-10-01T17:13:50.356569Z","shell.execute_reply.started":"2021-10-01T17:13:50.306507Z","shell.execute_reply":"2021-10-01T17:13:50.355591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### ","metadata":{"papermill":{"duration":0.101467,"end_time":"2021-08-12T17:03:52.616455","exception":false,"start_time":"2021-08-12T17:03:52.514988","status":"completed"},"tags":[]}}]}