Kaggle competition: Google Landmark Recognition 2020
<SOS> metric: GoogleGlobalAP Kaggle data source: landmark-recognition-2020<categorify>
!pip install -U.. /input/kerasapplications/Keras_Applications-1.0.8-py3-none-any.whl
!pip install -U.. /input/efficientnetwhl/efficientnet-1.1.0-py3-none-any.whl
credits_types = pd.get_dummies(bureau.CREDIT_TYPE)
 credits_types["SK_ID_CURR"] = bureau.SK_ID_CURR
credits_types = credits_types.groupby("SK_ID_CURR" ).sum(axis=0)
 credits_types.columns = ["credits_types_" + "_".join(c.lower().split())for c in credits_types]
credits_types<concatenate>
print(tf.__version__)
 tf.get_logger().setLevel(logging.ERROR)
 warnings.filterwarnings("ignore")
 mixed_precision = False
DEVICE = "TPU"
if DEVICE == "TPU":
print("connecting to TPU...")
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print("Could not connect to TPU")
 tpu = None
if tpu:
try:
print("initializing TPU...")
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("TPU initialized")
 except _:
print("failed to initialize TPU")
 else:
DEVICE = "GPU"
if DEVICE != "TPU":
print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
if DEVICE == "GPU":
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
 strategy_num = strategy.num_replicas_in_sync
print(f'strategy.num: {strategy_num}')
 config = {
'learning_rate': 5e-3,
'momentum': 0.9,
'scale': 30,
'margin': 0.3,
'clip_grad': 10.0,
'n_epochs': 3,
'input_size':(800,800, 3),
'n_classes': 81313,
}
BATCH_SIZE=32*strategy_num
AUTO = tf.data.experimental.AUTOTUNE
extended_data.append(credits_types)
 del credits_types<data_type_conversions>
model_b7_1024 = create_model_b7_1024()
model_b7_1024.load_weights('.. /input/glr2-sub-model/Acc-0.994-efnb7-model.h5')
 model_b7_1024.trainable=False
model_b7_1024 = tf.keras.Model(
 inputs=model_b7_1024.get_layer('input/image' ).input,
outputs=model_b7_1024.get_layer('head/bn_0' ).output)
 model_b7_1024.summary()
model_b6_512 = create_model_b6_512()
model_b6_512.load_weights('.. /input/glr2-sub-model/Acc-0.997-effb6-512-model.h5')
 model_b6_512.trainable=False
model_b6_512 = tf.keras.Model(
 inputs=model_b6_512.get_layer('input/image' ).input,
outputs=model_b6_512.get_layer('head/bn_1' ).output)
 model_b6_512.summary()
model_b7_512 = create_model_b7_512()
model_b7_512.load_weights('.. /input/glr2-sub-model/Acc-0.985-efnb7-512-model.h5')
 model_b7_512.trainable=False
model_b7_512 = tf.keras.Model(
 inputs=model_b7_512.get_layer('input/image' ).input,
outputs=model_b7_512.get_layer('head/bn_1' ).output)
 model_b7_512.summary()
model_b7_2560 = create_model_b7_2560()
model_b7_2560.load_weights('.. /input/glr2-sub-model/Acc-0.996-efnb7-2560-model.h5')
 model_b7_2560.trainable=False
model_b7_2560 = tf.keras.Model(
 inputs=model_b7_2560.get_layer('input/image' ).input,
outputs=model_b7_2560.get_layer('head/pooling' ).output)
 model_b7_2560.summary()
bureau.DAYS_ENDDATE_FACT.fillna(bureau.DAYS_CREDIT_ENDDATE, inplace=True)
 bureau.DAYS_CREDIT_ENDDATE.fillna(bureau.DAYS_ENDDATE_FACT, inplace=True)
 bureau.DAYS_ENDDATE_FACT.fillna(0, inplace=True)
 bureau.DAYS_CREDIT_ENDDATE.fillna(0, inplace=True)
 bureau["days_end_credit_advancement"] = bureau.DAYS_CREDIT_ENDDATE - bureau.DAYS_ENDDATE_FACT
max_cols = ["CREDIT_DAY_OVERDUE", "AMT_CREDIT_MAX_OVERDUE", "CNT_CREDIT_PROLONG", "days_end_credit_advancement"]
bureau_agg_data = bureau[max_cols + ["SK_ID_CURR"]].fillna(0 ).groupby("SK_ID_CURR" ).max()
bureau_agg_data.columns = ["max_" + "_".join(c.lower().split())for c in bureau_agg_data]
display(bureau_agg_data)
<concatenate>
def read_test_file() :
files_paths = glob.glob('.. /input/landmark-recognition-2020' + '/test/*/*/*/*')
 mapping = {}
for path in files_paths:
mapping[path.split('/')[-1].split('.')[0]] = path
df = pd.read_csv('.. /input/landmark-recognition-2020/sample_submission.csv')
 df['path'] = df['id'].map(mapping)
 return df
df=read_test_file()
print(len(df))
def load_labelmap() :
with open('.. /input/landmark-recognition-2020/train.csv', mode='r')as csv_file:
csv_reader = csv.DictReader(csv_file)
 labelmap = {row['id']: row['landmark_id'] for row in csv_reader}
return labelmap
def read_train_file() :
labelmap=load_labelmap()
files_paths = glob.glob('.. /input/landmark-recognition-2020' + '/train/*/*/*/*')
 mapping = {}
for path in files_paths:
image_id=path.split('/')[-1].split('.')[0]
mapping[path] = image_id
return mapping
df_train=read_train_file()
print(len(df_train))
extended_data.append(bureau_agg_data)
 del bureau_agg_data<feature_engineering>
AUTO = tf.data.experimental.AUTOTUNE
def read_image(img_id,image_path):
image = tf.io.read_file(image_path)
 image = tf.image.decode_jpeg(image, channels=3)
 image = tf.image.resize(image, config['input_size'][0:2], method='bilinear')
 image = tf.cast(image, tf.float32)
 image /= 255.
 return image,img_id
def create_dataset(df,batch_size):
image_paths= df.path
img_id=df.id
dataset = tf.data.Dataset.from_tensor_slices(( img_id,image_paths))
dataset = dataset.map(read_image,tf.data.experimental.AUTOTUNE)
 dataset = dataset.batch(batch_size)
 dataset = dataset.prefetch(AUTO)
 return dataset
def create_train_dataset(df,batch_size):
image_paths= list(df_train.keys())
 img_id=list(df_train.values())
 dataset = tf.data.Dataset.from_tensor_slices(( img_id,image_paths))
dataset = dataset.map(read_image,tf.data.experimental.AUTOTUNE)
 dataset = dataset.batch(batch_size)
 dataset = dataset.prefetch(AUTO)
 return dataset
sum_cols = ["CREDIT_DAY_OVERDUE", "AMT_CREDIT_MAX_OVERDUE", "CNT_CREDIT_PROLONG", "days_end_credit_advancement", "AMT_CREDIT_SUM", "AMT_CREDIT_SUM_DEBT", "AMT_CREDIT_SUM_LIMIT", "AMT_CREDIT_SUM_OVERDUE"]
bureau_agg_data = bureau[sum_cols + ["SK_ID_CURR"]].fillna(0 ).groupby("SK_ID_CURR" ).sum(axis=0)
 bureau_agg_data["total_outstanding_debt_ratio"] = bureau_agg_data.AMT_CREDIT_SUM_DEBT / bureau_agg_data.AMT_CREDIT_SUM
bureau_agg_data.columns = ["sum_" + "_".join(c.lower().split())for c in bureau_agg_data]
display(bureau_agg_data)
<concatenate>
INPUT_DIR = os.path.join('.. ', 'input')
 DATASET_DIR = os.path.join(INPUT_DIR, 'landmark-recognition-2020')
 TEST_IMAGE_DIR = os.path.join(DATASET_DIR, 'test')
 TRAIN_IMAGE_DIR = os.path.join(DATASET_DIR, 'train')
 TRAIN_LABELMAP_PATH = os.path.join(DATASET_DIR, 'train.csv')
 NUM_PUBLIC_TRAIN_IMAGES = 1580470
MAX_NUM_EMBEDDINGS = -1
NUM_TO_RERANK = 3
TOP_K = 3
MAX_INLIER_SCORE = 35
MAX_REPROJECTION_ERROR = 7.0
MAX_RANSAC_ITERATIONS = 70000
HOMOGRAPHY_CONFIDENCE = 0.99
extended_data.append(bureau_agg_data)
 del bureau_agg_data<groupby>
sum_cols = ["AMT_CREDIT_SUM", "AMT_CREDIT_SUM_DEBT"]
active_bureau = bureau[bureau.CREDIT_ACTIVE == "Active"][sum_cols + ["SK_ID_CURR"]].groupby("SK_ID_CURR" ).sum(axis=0)
 active_bureau.columns = ["total_active_debts", "outstanding_active_debts"]
display(active_bureau)
 extended_data.append(active_bureau)
 del active_bureau<concatenate>
@tf.function(input_signature=[tf.TensorSpec(shape=[None, None, None, 3],dtype=tf.float32,name='input_image')])
 def serving_train(input_image):
outputs0 = model_b7_1024(input_image)
 outputs0 = tf.math.l2_normalize(outputs0,axis=-1)
 outputs1 = model_b6_512(input_image)
 outputs1 = tf.math.l2_normalize(outputs1,axis=-1)
 outputs2 = model_b7_512(input_image)
 outputs2 = tf.math.l2_normalize(outputs2,axis=-1)
 outputs3 = model_b7_2560(input_image)
 outputs3 = tf.math.l2_normalize(outputs3,axis=-1)
 outputs=tf.concat([outputs0,outputs1,outputs2,outputs3],axis=-1)
 return outputs
def get_train_ids_labels_and_scores() :
train_dataset=create_train_dataset(df_train,8)
 features_2048=[]
ID_marks=[]
train_dataset = tqdm.tqdm(train_dataset)
 for k, inputs in enumerate(train_dataset):
ID_marks+=list(inputs[1].numpy())
 out=serving_train(inputs[0])
 features_2048.append(out)
 del inputs
del out
gc.collect()
del train_dataset
gc.collect()
features_2048=tf.concat(features_2048,axis=0)
 ID_marks=np.array(ID_marks)
 features_2048=tf.concat(features_2048,axis=0)
 features_2048=tf.transpose(features_2048)
 print(features_2048.shape)
 @tf.function(input_signature=[tf.TensorSpec(shape=[None, None, None, 3],dtype=tf.float32,name='input_image')])
 def serving(input_image):
outputs0 = model_b7_1024(input_image)
 outputs0 = tf.math.l2_normalize(outputs0,axis=-1)
 outputs1 = model_b6_512(input_image)
 outputs1 = 0.7*tf.math.l2_normalize(outputs1,axis=-1)
 outputs2 = model_b7_512(input_image)
 outputs2 = tf.math.l2_normalize(outputs2,axis=-1)
 outputs3 = model_b7_2560(input_image)
 outputs3 = tf.math.l2_normalize(outputs3,axis=-1)
 outputs=tf.concat([outputs0,outputs1,outputs2,outputs3],axis=-1)
 outputs=tf.matmul(outputs,features_2048)
 return outputs
num_k=NUM_TO_RERANK
labelmap=load_labelmap()
test_dataset=create_dataset(df,8)
 ID_test=list(df.id)
 test_dataset = tqdm.tqdm(test_dataset)
 train_ids_labels_and_scores=[]
for k, inputs in enumerate(test_dataset):
out=serving(inputs[0] ).numpy()
for j in range(inputs[1].shape[0]):
top_k = np.argpartition(out[j], -num_k)[-num_k:]
top_k_score=out[j][top_k]/4.0
top_k_id=[str(x)[2:-1] for x in ID_marks[top_k]]
top_k_label=[labelmap[x] for x in top_k_id]
train_ids_labels_and_scores.append(list(zip(top_k_id,top_k_label,top_k_score)))
 del inputs
del out
gc.collect()
del features_2048
del test_dataset
gc.collect()
return ID_test[0:len(train_ids_labels_and_scores)],train_ids_labels_and_scores
data = data.join(pd.concat(extended_data, axis=1))
display(data)
 del extended_data<feature_engineering>
SAVED_MODEL_DIR = '.. /input/delg-saved-models/local_and_global'
DELG_MODEL = tf.saved_model.load(SAVED_MODEL_DIR)
 DELG_IMAGE_SCALES_TENSOR = tf.convert_to_tensor([0.70710677, 1.0, 1.4142135])
 DELG_SCORE_THRESHOLD_TENSOR = tf.constant(175.)
DELG_INPUT_TENSOR_NAMES = [
'input_image:0', 'input_scales:0', 'input_abs_thres:0'
]
GLOBAL_FEATURE_EXTRACTION_FN = DELG_MODEL.prune(DELG_INPUT_TENSOR_NAMES,['global_descriptors:0'])
 LOCAL_FEATURE_NUM_TENSOR = tf.constant(1000)
 LOCAL_FEATURE_EXTRACTION_FN = DELG_MODEL.prune(
 DELG_INPUT_TENSOR_NAMES + ['input_max_feature_num:0'],
['boxes:0', 'features:0'] )
data["credit_goodsprice_ratio"] = data.AMT_CREDIT / data.AMT_GOODS_PRICE
data["annuity_income_ratio"] = data.AMT_ANNUITY / data.AMT_INCOME_TOTAL
data["annuity_credit_ratio"] = data.AMT_ANNUITY / data.AMT_CREDIT
data["total_new_credit_increase"] = data.AMT_CREDIT + data.outstanding_active_debts
data["credit_increase"] = data.AMT_CREDIT / data.total_new_credit_increase
data["total_new_credit_income_ratio"] = data.total_new_credit_increase / data.AMT_INCOME_TOTAL<train_model>
def get_image_path(subset, image_id):
name =image_id
return os.path.join(DATASET_DIR, subset, name[0], name[1], name[2],'{}.jpg'.format(name))
def load_image_tensor(image_path):
return tf.convert_to_tensor(
 np.array(PIL.Image.open(image_path ).convert('RGB')))
 def extract_local_features(image_path):

 image_tensor = load_image_tensor(image_path)
 features = LOCAL_FEATURE_EXTRACTION_FN(image_tensor, DELG_IMAGE_SCALES_TENSOR,
DELG_SCORE_THRESHOLD_TENSOR,
LOCAL_FEATURE_NUM_TENSOR)
 keypoints = tf.divide(
 tf.add(
 tf.gather(features[0], [0, 1], axis=1),
tf.gather(features[0], [2, 3], axis=1)) , 2.0 ).numpy()
descriptors = tf.nn.l2_normalize(
 features[1], axis=1, name='l2_normalization' ).numpy()
return keypoints, descriptors
def get_putative_matching_keypoints(test_keypoints,
test_descriptors,
train_keypoints,
train_descriptors,
max_distance=0.99):

 train_descriptor_tree = spatial.cKDTree(train_descriptors)
 _, matches = train_descriptor_tree.query(
 test_descriptors, distance_upper_bound=max_distance)
 test_kp_count = test_keypoints.shape[0]
train_kp_count = train_keypoints.shape[0]
test_matching_keypoints = np.array([
test_keypoints[i,]
for i in range(test_kp_count)
 if matches[i] != train_kp_count
])
 train_matching_keypoints = np.array([
train_keypoints[matches[i],]
for i in range(test_kp_count)
 if matches[i] != train_kp_count
])
 return test_matching_keypoints, train_matching_keypoints
def get_num_inliers(test_keypoints, test_descriptors, train_keypoints,
train_descriptors):

 test_match_kp, train_match_kp = get_putative_matching_keypoints(
 test_keypoints, test_descriptors, train_keypoints, train_descriptors)
 if test_match_kp.shape[0] <= 4:
return 0
try:
_, mask = pydegensac.findHomography(test_match_kp, train_match_kp,
MAX_REPROJECTION_ERROR,
HOMOGRAPHY_CONFIDENCE,
MAX_RANSAC_ITERATIONS)
 except np.linalg.LinAlgError:
return 0
return int(copy.deepcopy(mask ).astype(np.float32 ).sum())
 def get_total_score(num_inliers, global_score):
local_score = min(num_inliers, MAX_INLIER_SCORE)/ MAX_INLIER_SCORE
return local_score + 0.8*global_score
def rescore_and_rerank_by_num_inliers(test_image_id,
train_ids_labels_and_scores):

 test_image_path = get_image_path('test', test_image_id)
 test_keypoints, test_descriptors = extract_local_features(test_image_path)
 for i in range(len(train_ids_labels_and_scores)) :
train_image_id, label, global_score = train_ids_labels_and_scores[i]
train_image_path = get_image_path('train', train_image_id)
 train_keypoints, train_descriptors = extract_local_features(
 train_image_path)
 num_inliers = get_num_inliers(test_keypoints, test_descriptors,
train_keypoints, train_descriptors)
 total_score = get_total_score(num_inliers, global_score)
 train_ids_labels_and_scores[i] =(train_image_id, label, total_score)
 train_ids_labels_and_scores.sort(key=lambda x: x[2], reverse=True)
 return train_ids_labels_and_scores
def get_prediction_map(test_ids, train_ids_labels_and_scores):

 prediction_map = dict()
for test_index, test_id in enumerate(test_ids):
hex_test_id = test_id
aggregate_scores = {}
for _, label, score in train_ids_labels_and_scores[test_index][:TOP_K]:
if label not in aggregate_scores:
aggregate_scores[label] = 0
aggregate_scores[label] += score
label, score = max(aggregate_scores.items() , key=operator.itemgetter(1))
prediction_map[hex_test_id] = {'score': score, 'class': label}
return prediction_map
def get_predictions(labelmap):
test_ids, train_ids_labels_and_scores=get_train_ids_labels_and_scores()
pre_verification_predictions = get_prediction_map(test_ids, train_ids_labels_and_scores)
 test_ids = tqdm.tqdm(test_ids)
 for test_index, test_id in enumerate(test_ids):
train_ids_labels_and_scores[test_index] = rescore_and_rerank_by_num_inliers(
 test_id, train_ids_labels_and_scores[test_index])
 post_verification_predictions = get_prediction_map(
 test_ids, train_ids_labels_and_scores)
 return pre_verification_predictions, post_verification_predictions
<train_on_grid><EOS> 
def save_submission_csv(predictions=None):
if predictions is None:
shutil.copyfile(
 os.path.join(DATASET_DIR, 'sample_submission.csv'), 'submission.csv')
 return
with open('submission.csv', 'w')as submission_csv:
csv_writer = csv.DictWriter(submission_csv, fieldnames=['id', 'landmarks'])
 csv_writer.writeheader()
for image_id, prediction in predictions.items() :
label = prediction['class']
score = prediction['score']
if score>0.3:
csv_writer.writerow({'id': image_id, 'landmarks': f'{label} {score}'})
 else:
csv_writer.writerow({'id': image_id, 'landmarks': ''})
 def main() :
pass
labelmap = load_labelmap()
num_training_images = len(labelmap.keys())
 print(f'Found {num_training_images} training images.')
 if num_training_images == NUM_PUBLIC_TRAIN_IMAGES:
print(
 f'Found {NUM_PUBLIC_TRAIN_IMAGES} training images.Copying sample submission.' 
)
 save_submission_csv()
return
_, post_verification_predictions = get_predictions(labelmap)
 save_submission_csv(post_verification_predictions)
 if __name__ == '__main__':
main()