{"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 tensorflow as tf\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\nimport glob\nimport cv2\nimport numpy as np\nfrom tensorflow.keras import models, layers\nfrom IPython.display import clear_output\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-02T19:58:31.094398Z","iopub.execute_input":"2022-08-02T19:58:31.095031Z","iopub.status.idle":"2022-08-02T19:58:38.246400Z","shell.execute_reply.started":"2022-08-02T19:58:31.094894Z","shell.execute_reply":"2022-08-02T19:58:38.245196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(r\"../input/landmark-retrieval-2021/train.csv\")\nsample_df =  pd.read_csv(r\"../input/landmark-retrieval-2021/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:55.861844Z","iopub.execute_input":"2022-08-02T13:28:55.862577Z","iopub.status.idle":"2022-08-02T13:28:57.290463Z","shell.execute_reply.started":"2022-08-02T13:28:55.862534Z","shell.execute_reply":"2022-08-02T13:28:57.289488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import ResNet50","metadata":{"execution":{"iopub.status.busy":"2022-08-02T13:28:57.291964Z","iopub.execute_input":"2022-08-02T13:28:57.292615Z","iopub.status.idle":"2022-08-02T13:28:57.299520Z","shell.execute_reply.started":"2022-08-02T13:28:57.292563Z","shell.execute_reply":"2022-08-02T13:28:57.298182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet = ResNet50(weights=\"imagenet\",include_top=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:56:33.970813Z","iopub.execute_input":"2022-08-01T12:56:33.971862Z","iopub.status.idle":"2022-08-01T12:56:39.198746Z","shell.execute_reply.started":"2022-08-01T12:56:33.971822Z","shell.execute_reply":"2022-08-01T12:56:39.197710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = ResNet50(weights=\"imagenet\", include_top=False,input_shape=(224,224,3))\nx = base_model.output\nfeatures = tf.keras.layers.GlobalAveragePooling2D()(x)\nfeature_extraction_model = tf.keras.Model(inputs=base_model.input, outputs=features)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:56:39.202707Z","iopub.execute_input":"2022-08-01T12:56:39.203020Z","iopub.status.idle":"2022-08-01T12:56:40.598098Z","shell.execute_reply.started":"2022-08-01T12:56:39.202992Z","shell.execute_reply":"2022-08-01T12:56:40.597135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#BURASI MANUEL DENEMEK İÇİN\n\nimg = '899f66ffe9ba3559'\nimg1 = cv2.imread('../input/landmark-retrieval-2021/test/' + img[0] + '/' + img[1] + '/'+ img[2] + '/' + img + '.jpg', cv2.IMREAD_COLOR)\nimg1 = cv2.resize(img1, (224,224))\nimg1 = np.expand_dims(img1,axis=0)\nimg = 'dd7e7efdace99087'\nimg2 = cv2.imread('../input/landmark-retrieval-2021/test/' + img[0] + '/' + img[1] + '/'+ img[2] + '/' + img + '.jpg', cv2.IMREAD_COLOR)\nimg2 = cv2.resize(img2, (224,224))\nimg2 = np.expand_dims(img2,axis=0)\n\nA = feature_extraction_model.predict(img1)\nB = feature_extraction_model.predict(img2)\nres = tf.keras.losses.cosine_similarity(A,B)\nprint(res)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T07:53:46.269324Z","iopub.execute_input":"2022-07-29T07:53:46.269825Z","iopub.status.idle":"2022-07-29T07:53:55.538272Z","shell.execute_reply.started":"2022-07-29T07:53:46.269778Z","shell.execute_reply":"2022-07-29T07:53:55.536976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-07-29T11:22:02.417977Z","iopub.execute_input":"2022-07-29T11:22:02.418322Z","iopub.status.idle":"2022-07-29T11:22:02.430413Z","shell.execute_reply.started":"2022-07-29T11:22:02.418291Z","shell.execute_reply":"2022-07-29T11:22:02.429329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#EVALUATION BAŞLANGIÇ\nembed_to_label = {}\nspace = []\nfor row in range(300):\n    print((row / 300) * 100 )\n    row_id = df.iloc[row][\"id\"]\n    row_landmark_id = df.iloc[row][\"landmark_id\"]\n    \n    \n    img1 = cv2.imread('../input/landmark-retrieval-2021/train/' + row_id[0] + '/' + row_id[1] + '/'+ row_id[2] + '/' + row_id + '.jpg', cv2.IMREAD_COLOR)\n    img1 = cv2.resize(img1, (224,224))\n    img1 = np.expand_dims(img1,axis=0)\n    \n    \n    embed = feature_extraction_model.predict(img1)\n    \n    space.append(embed)\n    \n    embed_key = ''.join(str(num) for num in embed)\n    embed_to_label[embed_key] = row_landmark_id\n    clear_output(wait=True)\n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:56:51.931903Z","iopub.execute_input":"2022-08-01T12:56:51.932282Z","iopub.status.idle":"2022-08-01T12:57:15.210519Z","shell.execute_reply.started":"2022-08-01T12:56:51.932251Z","shell.execute_reply":"2022-08-01T12:57:15.209161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nembed_keys = []\narray_l2norm2d = []\n\nfor compare1 in range(100):\n    arrayL2Norm = []# store distance of a test image from all train images\n    vectors = []\n    print(compare1)\n    for compare2 in range(100):  \n            \n        if compare1 == compare2:\n            continue\n        \n        #l2norm = np.sum(((space[compare1]-space[compare2]))**2)**(0.5) # distance between two images; 255 is max. pixel value ==> normalization   \n        l2norm = tf.keras.losses.cosine_similarity(space[compare1],space[compare2])\n        \n        arrayL2Norm.append(l2norm)\n        vectors.append(space[compare2])\n        \n    pack = sorted(zip(arrayL2Norm,vectors))\n    \n    arrayL2Norm = [x for x,y in pack]\n    \n    vectors = [y for x,y in pack]\n    \n    embed_keys.append(vectors)\n    \n    clear_output(wait=True)\n    \n\n\n\n\n    #EVALUATION BİTİŞ","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:59:11.742273Z","iopub.execute_input":"2022-08-01T12:59:11.742645Z","iopub.status.idle":"2022-08-01T12:59:32.997029Z","shell.execute_reply.started":"2022-08-01T12:59:11.742613Z","shell.execute_reply":"2022-08-01T12:59:32.996019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(arrayL2Norm[2])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T12:58:12.512667Z","iopub.status.idle":"2022-08-01T12:58:12.513034Z","shell.execute_reply.started":"2022-08-01T12:58:12.512854Z","shell.execute_reply":"2022-08-01T12:58:12.512872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for key in range(len(embed_keys)):\n    print(len(embed_keys[key]))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-29T08:04:12.825867Z","iopub.execute_input":"2022-07-29T08:04:12.826325Z","iopub.status.idle":"2022-07-29T08:04:12.834651Z","shell.execute_reply.started":"2022-07-29T08:04:12.826292Z","shell.execute_reply":"2022-07-29T08:04:12.833001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}