{"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 pandas as pd\nimport numpy as np\nimport cv2\nimport os\nimport numpy\nfrom matplotlib import pyplot, cm\nfrom IPython.display import Image\nimport glob","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = []\nimg_list = []\ni = 1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/landmark-recognition-2020/train.csv')\ntrain.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train_1, x_train_2, y_train_1, y_train_2 = train_test_split(train.id, train.landmark_id, test_size=0.10, random_state=42, stratify=train.landmark_id)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_3, x_train_4, y_train_3, y_train_4 = train_test_split(x_train_2, y_train_2, test_size=0.20, random_state=42)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_3.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_3.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_4.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_4.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_4 = x_train_4.reset_index()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_4","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_4.id[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_4","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for filepath in glob.iglob('../input/landmark-recognition-2020/train/*/*/*/*.jpg', recursive=True):\n#    head, tail = os.path.split(filepath)\n#    x = tail.replace(\".jpg\", '')\n#    for jj in range(len(x_train_4)):\n#        if x in x_train_4.id[jj]:\n#            print (\"Found\", jj)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lst_x4 = x_train_4.id.tolist()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for filepath in glob.iglob('../input/landmark-recognition-2020/train/*/*/*/*.jpg', recursive=True):\n    head, tail = os.path.split(filepath)\n    x = tail.replace(\".jpg\", '')\n    #for jj in range(len(x_train_4)):\n    if x in lst_x4:\n        src = cv2.imread(filepath, cv2.IMREAD_UNCHANGED)\n\n        #percent by which the image is resized\n        scale_percent = 5\n\n        width = 28\n        height = 28\n\n        # dsize\n        dsize = (width, height)\n\n        # resize image\n        output = cv2.resize(src, dsize)\n\n        #cv2.imwrite('temp_img.png',output)\n\n        #temp_im = cv2.imread('temp_img.png')\n        temp_im = cv2.cvtColor(output, cv2.COLOR_BGR2HSV)\n        v = numpy.vstack(temp_im)\n        b = v.flatten()\n        filename.append(os.path.basename(filepath))\n        img_list.append(b)\n        print (i, os.path.basename(filepath))\n        i = i + 1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(list(zip(filename, img_list)), \n               columns =['FileNames', \"Img_List\"])\ndf.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3 = pd.DataFrame(df['Img_List'].to_list())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df4 = pd.concat([df, df3], axis=1)\nprint (df4.head())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df5 = df4.drop(\"Img_List\", axis = 1)\ndf5.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df6 = df5.drop(\"FileNames\", axis = 1)\ndf6.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df7 = df6 / 255\ndf7.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df\ndel df3\ndel df4\ndel df5\ndel df6\n#del df7","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(df7, y_train_4, test_size=0.2, random_state= 0)\nprint (X_train.shape, y_train.shape)\nprint (X_val.shape, y_val.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import GradientBoostingClassifier  #GBM algorithm\nfrom sklearn.model_selection import GridSearchCV   #Perforing grid search\nfrom sklearn.metrics import precision_score\nfrom sklearn.metrics import recall_score\nfrom sklearn.metrics import f1_score\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import accuracy_score\nfrom xgboost.sklearn import XGBClassifier","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gbm0 = GradientBoostingClassifier()\ngbm0.fit(X_train, y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = gbm0.predict(X_val)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}