{"cells":[{"metadata":{},"cell_type":"markdown","source":"Import Libraries","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"The extracted features dataset: https://www.kaggle.com/cemsina/google-landmark-features","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nfrom keras.optimizers import Adam\nfrom keras.layers import *\nfrom keras.models import *\nimport keras.preprocessing.image as image\nfrom keras.utils import to_categorical\nimport math\nfrom tqdm.notebook import tqdm\nfrom keras.applications.densenet import DenseNet121\nfrom keras import backend as K\nimport threading\nimport gc\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"CONSTANTS","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_WIDTH  = 224\nIMG_HEIGHT = 224\nREAD_SIZE = 100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/landmark-recognition-2020/train.csv')\ntest = pd.read_csv('../input/landmark-recognition-2020/sample_submission.csv')\nw = open(\"google_landmark_features.csv\",\"w\")\ntrain_ids = train.id.values\ntest_ids = test.id.values\ndef get_data_X(image_name,folder):\n    img = image.load_img(f'../input/landmark-recognition-2020/{folder}/{image_name[0]}/{image_name[1]}/{image_name[2]}/{image_name}.jpg',target_size=(IMG_WIDTH, IMG_HEIGHT))\n    X = image.img_to_array(img) / 255\n    img.close()\n    return np.array(X)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"inp = Input((IMG_WIDTH,IMG_HEIGHT,3))\nx = DenseNet121(input_tensor=inp, include_top=False)\nx = x.output\nx = GlobalAveragePooling2D()(x)\nx = Lambda(lambda x: K.expand_dims(x,axis=-1))(x)\nx = AveragePooling1D(4)(x) # feature count reduction\nout = Lambda(lambda x: x[:,:,0])(x)\nmodel = Model(inp,out)    \n\ndef append_to_file(ids,folder):\n    X = np.array([get_data_X(idx,folder) for idx in ids])\n    y = model.predict(X)\n    for i,idx in enumerate(ids):\n        features = idx+\",\"+\",\".join([str(e) for e in y[i]])\n        w.write(features+\"\\n\")\n    w.flush()\n    gc.collect()\n    return\n\ndef chunks(arr, n):\n    n = max(1, n)\n    return [arr[i:i+n] for i in range(0, len(arr), n)]\n\nw.write(',')\nw.write(','.join([str(i) for i in range(0,256)])+\"\\n\")\n\ntrain_chunks = chunks(train_ids, READ_SIZE)\ntest_chunks = chunks(test_ids, READ_SIZE)\n\nfor chunk in tqdm(train_chunks,desc=\"Train\"):\n    p = threading.Thread(target=append_to_file, args=(chunk,\"train\"))\n    p.start()\n    p.join()\n\nfor chunk in tqdm(test_chunks,desc=\"Test\"):\n    p = threading.Thread(target=append_to_file, args=(chunk,\"test\"))\n    p.start()\n    p.join()\n\nw.close()\nprint(\"OK\")","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}