{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport time, os, glob\nimport cv2\n\nfrom keras.applications.vgg16 import VGG16\nfrom keras.optimizers import SGD, Adam\nfrom keras.layers import GlobalAveragePooling2D\nfrom keras import Model\nfrom keras.applications.imagenet_utils import preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_model(num_class, input_size, feature_layer):\n    base_model = VGG16(weights='imagenet', include_top=False,\n                       input_shape=[input_size,input_size,3], classes=num_class) # include_top=False\n    x = base_model.get_layer(feature_layer).output\n    x = GlobalAveragePooling2D()(x)\n\n    model = Model(inputs=base_model.input, outputs=x)\n    optimizer = Adam(lr=0.0001)\n    model.compile(loss='categorical_crossentropy',\n                  optimizer=optimizer,\n                  metrics=['accuracy'])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def test_generator(x_train, batch_size, input_size, shuffle=False):\n    batch_index = 0\n    n = x_train.shape[0]\n    while 1:\n        if batch_index == 0:\n            index_array = np.arange(n)\n            if shuffle:\n                index_array = np.random.permutation(n)\n\n        current_index = (batch_index * batch_size) % n\n        if n >= current_index + batch_size:\n            current_batch_size = batch_size\n            batch_index += 1\n        else:\n            current_batch_size = n - current_index\n            batch_index = 0\n\n        batch_x = []\n        batch_id = index_array[current_index: current_index + current_batch_size]\n        for id in batch_id:\n            img = cv2.imread(x_train['path'][id]).astype(np.float32)\n            img = img[:,:,::-1]\n            img = preprocess_input(img)\n            batch_x.append(img)\n        batch_x = np.array(batch_x, np.float32)\n\n        yield batch_x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index_path = \"../input/landmark-retrieval-2020/index/*/*/*/*\"\nindex_list = sorted(glob.glob(index_path + \"*\")) # 76176\nlen_index = len(index_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"query_path = \"../input/landmark-retrieval-2020/test/*/*/*/*\"\nindex_list += sorted(glob.glob(query_path + \"*\")) # 114943\nindex_list = pd.DataFrame(index_list, columns=['path'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_size = 224\n# feature_layer = \"block3_conv3\"\n# feature_layer = \"block4_conv3\"\nfeature_layer = \"block5_conv3\"\nmodel = get_model(1,input_size, feature_layer)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_size","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 128\ngen_test = test_generator(index_list, batch_size, input_size)\nfeature = model.predict_generator(generator=gen_test,\n                                     steps=np.ceil(index_list.shape[0] / batch_size),\n                                     verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index_list","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}