Kaggle competition: Google Landmark Recognition 2020
<SOS> metric: GoogleGlobalAP Kaggle data source: landmark-recognition-2020<create_dataframe>
plt.rcdefaults()
plt.style.use('dark_background')
 warnings.filterwarnings("ignore" )
train1 = pd.DataFrame(scaled_train, index=train1.index, columns=train1.columns)
 test1 = pd.DataFrame(scaled_test, index=test1.index, columns=test1.columns )<predict_on_test>
INPUT_PATH = os.path.join('.. ', 'input')
 DATASET_PATH = os.path.join(INPUT_PATH, 'landmark-recognition-2020')
 TRAIN_IMAGE_PATH = os.path.join(DATASET_PATH, 'train')
 TEST_IMAGE_PATH = os.path.join(DATASET_PATH, 'test')
 TRAIN_CSV_PATH = os.path.join(DATASET_PATH, 'train.csv')
 SUBMISSION_CSV_PATH = os.path.join(DATASET_PATH, 'sample_submission.csv' )
GLM = LogisticRegression(C = 0.0001)
 GLM.fit(train1, TARGET)
 GLM_pred = GLM.predict_proba(test1)[:, 1]
submission = test[['SK_ID_CURR']]
submission['TARGET'] = GLM_pred<save_to_csv>
train = pd.read_csv(TRAIN_CSV_PATH)
 print("training dataset has {} rows and {} columns".format(train.shape[0],train.shape[1]))
submission = pd.read_csv(SUBMISSION_CSV_PATH)
 print("submission dataset has {} rows and {} columns
".format(submission.shape[0],submission.shape[1]))
submission.to_csv('GLM_baseline.csv', index = False )<train_model>
print(Fore.BLUE + f"{'---'*20}
Mapping for Training Data
{'---'*20}")
 data_label_dict = {'image': [], 'target': []}
for i in tqdm(range(train.shape[0])) :
data_label_dict['image'].append(
 TRAIN_IMAGE_PATH + '/' +
train['id'][i][0] + '/' +
train['id'][i][1]+ '/' +
train['id'][i][2]+ '/' +
train['id'][i] + ".jpg")
 data_label_dict['target'].append(
 train['landmark_id'][i])
 train_pathlabel = pd.DataFrame(data_label_dict)
 print(train_pathlabel.head())
 print(Fore.BLUE + f"{'---'*20}
Mapping for Test Data
{'---'*20}",Style.RESET_ALL)
 data_label_dict = {'image': []}
for i in tqdm(range(submission.shape[0])) :
data_label_dict['image'].append(
 TEST_IMAGE_PATH + '/' +
submission['id'][i][0] + '/' +
submission['id'][i][1]+ '/' +
submission['id'][i][2]+ '/' +
submission['id'][i] + ".jpg")
 test_pathlabel = pd.DataFrame(data_label_dict)
 print(test_pathlabel.head() )
X_train = train1
y_train = TARGET
X_test = test1
dtrain = xgb.DMatrix(X_train, y_train)
 dtest = xgb.DMatrix(X_test)
 params = {
'min_child_weight': 19.0,
'objective': 'binary:logistic',
'max_depth': 7,
'eta': 0.025,
'eval_metric': 'auc',
'max_delta_step': 1.8,
'colsample_bytree': 0.4,
'subsample': 0.8,
'gamma': 0.65
}
watchlist = [(dtrain, 'train')]
XGB_model = xgb.train(params, dtrain,
300,
watchlist,
verbose_eval=50)
 XGB_pred = XGB_model.predict(dtest )<prepare_output>
print("There are", train.landmark_id.nunique() , "landmarks in the training dataset" )
submission_2 = test[['SK_ID_CURR']]
submission_2['TARGET'] = XGB_pred<save_to_csv>
train.landmark_id.value_counts()
submission_2.to_csv('XGB_pred2.csv', index = False )<load_from_csv>
import copy
import csv
import gc
import operator
import os
import pathlib
import shutil
import numpy as np
import PIL
import pydegensac
from scipy import spatial
import tensorflow as tf
print('CONVERTING ALL INPUT FILES.... ')
 print('-' * 80)
 print('train')
 app_train = import_data('.. /input/application_train.csv')
 print('-' * 80)
 print('test')
 app_test = import_data('.. /input/application_test.csv')
 print('-' * 80)
 print('bureau_balance')
 bur_bal = import_data('.. /input/bureau_balance.csv')
 print('-' * 80)
 print('bureau')
 bur = import_data('.. /input/bureau.csv')
 print('-' * 80)
 print('credit_card_balance')
 credit = import_data('.. /input/credit_card_balance.csv')
 print('-' * 80)
 print('installments_payments')
 install = import_data('.. /input/installments_payments.csv')
 print('-' * 80)
 print('pos_cash_balance')
 pos = import_data('.. /input/POS_CASH_balance.csv')
 print('-' * 80)
 print('previous_application')
 prev = import_data('.. /input/previous_application.csv')
 print('CONVERSION FOR ALL FILES COMPLETE.... ')
<train_model>
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' )
print('SAVING A COPY OF TEST FILE FOR LATER USE.... ')
 app_test1 = app_test
print('SAVING COMPLETE.... ' )<categorify>
NUM_PUBLIC_TRAIN_IMAGES = 1580470
MAX_NUM_EMBEDDINGS = -1
print('WRITING A PROGRAM TO LABEL CODE CATEGORY COL.... ')
 def encode(df, nan_as_category = True):
original_columns = list(df.columns)
 categorical_columns = [col for col in df.columns if df[col].dtype == 'object']
df = pd.get_dummies(df, columns= categorical_columns, dummy_na= nan_as_category)
 new_columns = [c for c in df.columns if c not in original_columns]
return new_columns,df
print('PROGRAM COMPLETE.... ' )<filter>
NUM_TO_RERANK = 3
TOP_K = 3
print('IMPUTING OCCUPATION TYPE BASED ON SALARY SLAB IN TRAIN & TEST DATA.... ')
 miss = app_train['OCCUPATION_TYPE'].isnull()
miss1 = app_test['OCCUPATION_TYPE'].isnull()
test = []
test1 = []
df = app_train.loc[miss,'AMT_INCOME_TOTAL']
df_test = app_test.loc[miss1, 'AMT_INCOME_TOTAL']
for x in df:
if x < 130791:
test.append('Cleaning staff')
 elif(x>130791)&(x< 133228):
test.append('Low-skill Laborers')
 elif(x>133228)&(x< 138397):
test.append('Cooking staff')
 elif(x>138397)&(x <144273):
test.append('Waiters/barmen staff')
 elif(x>144273)&(x < 149663):
test.append('Security staff')
 elif(x>149663)&(x < 149710):
test.append('Medicine staff')
 elif(x>149710)&(x <152303):
test.append('Sales staff')
 elif(x>152303)&(x <160542):
test.append('Secretaries')
 elif(x>160542)&(x <166357):
test.append('Laborers')
 elif(x>166357)&(x <172657):
test.append('Core staff')
 elif(x>172657)&(x <182335):
test.append('Private service staff')
 elif(x>182335)&(x <182842):
test.append('High skill tech staff')
 elif(x>182842)&(x <187012):
test.append('Drivers')
 elif(x>187012)&(x <188916):
test.append('HR staff')
 elif(x>188916)&(x <194578):
test.append('Accountants')
 elif(x>194578)&(x <195004):
test.append('Realty agents')
 elif(x>195004)&(x <213446):
test.append('IT staff')
 elif(x>213446):
test.append('Managers')
<feature_engineering>
MAX_INLIER_SCORE = 35
MAX_REPROJECTION_ERROR = 7.0
MAX_RANSAC_ITERATIONS = 8500000
HOMOGRAPHY_CONFIDENCE = 0.99
print('BINARIZING Y/N COLUMNS FOR TRAIN AND TEST DATA.... ')
 for df in(app_train,app_test):
for bin_feature in ['CODE_GENDER', 'FLAG_OWN_CAR', 'FLAG_OWN_REALTY']:
df[bin_feature], uniques = pd.factorize(df[bin_feature])
 print('BINARUZING COMPLETE.... ')
 print('FEATURE ENGINEERING FOR TRAIN & TEST.... ')
 for df in(app_train,app_test):
df['DAYS_EMPLOYED'].replace(365243, np.nan, inplace= True)
 df['DAYS_EMPLOYED_PERC'] = df['DAYS_EMPLOYED'] / df['DAYS_BIRTH']
df['DAYS_REG_PERC'] = df['DAYS_REGISTRATION'] / df['DAYS_BIRTH']
df['DAYS_ID_PERC'] = df['DAYS_ID_PUBLISH'] / df['DAYS_REGISTRATION']
df['INCOME_CREDIT_PERC'] = df['AMT_INCOME_TOTAL'] / df['AMT_CREDIT']
df['INCOME_PER_PERSON'] = df['AMT_INCOME_TOTAL'] / df['CNT_FAM_MEMBERS']
df['ANNUITY_INCOME_PERC'] = df['AMT_ANNUITY'] / df['AMT_INCOME_TOTAL']
df['LOAN_INCOME_RATIO'] = df['AMT_CREDIT'] / df['AMT_INCOME_TOTAL']
df['CONSUMER_GOODS_RATIO'] = df['AMT_CREDIT'] / df['AMT_GOODS_PRICE']
df['ANNUITY LENGTH'] = df['AMT_CREDIT'] / df['AMT_ANNUITY']
df['ANN_LENGTH_EMPLOYED_RATIO'] = df['ANNUITY LENGTH'] / df['DAYS_EMPLOYED']
df['WORKING_LIFE_RATIO'] = df['DAYS_EMPLOYED'] / df['DAYS_BIRTH']
df['TOTAL_DOCS_SUBMITTED'] = df.loc[:, df.columns.str.contains('FLAG_DOCUMENT')].sum(axis=1)
 print('FEATURE ENGINEERING COMPLETE.... ')
<data_type_conversions>
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'
]
print('REPLACE ODD VALUES WITH NAN.... ')
 prev['DAYS_FIRST_DRAWING'].replace(365243, np.nan, inplace= True)
 prev['DAYS_FIRST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE_1ST_VERSION'].replace(365243, np.nan, inplace= True)
 prev['DAYS_LAST_DUE'].replace(365243, np.nan, inplace= True)
 prev['DAYS_TERMINATION'].replace(365243, np.nan, inplace= True)
 print('FEATURE ENGINEERING FOR PREVIOUS APP.... ')
 prev['ASK_AMT_RATIO'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
prev['APPROVED'] =(prev['NAME_CONTRACT_STATUS'] == 'Approved' ).astype(int)
 prev['REFUSED'] =(prev['NAME_CONTRACT_STATUS'] == 'Refused' ).astype(int)
 prev['APP_CREDIT_PERC'] = prev['AMT_APPLICATION'] / prev['AMT_CREDIT']
agg = {
'AMT_ANNUITY': ['mean','max','sum','var','median'],
'AMT_APPLICATION': ['mean','max','sum','var','median'],
'AMT_CREDIT': ['mean','max','sum','var','median'],
'APP_CREDIT_PERC': ['mean','max','sum','var','median'],
'AMT_DOWN_PAYMENT': ['mean','max','sum','var','median'],
'AMT_GOODS_PRICE': ['mean','max','sum','var','median'],
'HOUR_APPR_PROCESS_START': ['mean','max','sum','var','median'],
'RATE_DOWN_PAYMENT': ['mean','max','sum','var','median'],
'DAYS_DECISION': ['mean','max','sum','var','median'],
'CNT_PAYMENT': ['mean', 'sum','max','var','median'],
}
prev=prev.groupby('SK_ID_CURR' ).agg(agg)
 prev.reset_index(inplace=True)
 prev.columns = pd.Index([e[0] + "_" + e[1].upper() for e in prev.columns.tolist() ])
 prev.rename(columns={'SK_ID_CURR_':'SK_ID_CURR'}, inplace=True)
 print('FEATURE ENGINEERING FOR PREV COMPLETE' )<feature_engineering>
NUM_EMBEDDING_DIMENSIONS = 2048
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'] )
print('FEATURE ENGINEERING FOR CREDIT.....')
 credit['AMT_DRAW'] = credit['AMT_DRAWINGS_ATM_CURRENT'] + credit['AMT_DRAWINGS_CURRENT'] +credit['AMT_DRAWINGS_OTHER_CURRENT'] + credit['AMT_DRAWINGS_POS_CURRENT']
credit['CNT_DRAW'] = credit['CNT_DRAWINGS_ATM_CURRENT'] + credit['CNT_DRAWINGS_CURRENT'] + credit['CNT_DRAWINGS_OTHER_CURRENT'] + credit['CNT_DRAWINGS_POS_CURRENT']
credit['cr_util'] =(credit['AMT_BALANCE']*100)/ credit['AMT_CREDIT_LIMIT_ACTUAL']
credit['PAID%'] =(credit['AMT_PAYMENT_TOTAL_CURRENT']*100)/ credit['AMT_TOTAL_RECEIVABLE']
credit=credit.replace(np.inf, np.nan)
 credit=credit.replace(-np.inf, np.nan)
 credit=credit.groupby('SK_ID_CURR' ).agg(['mean','sum','max','min','var','median'])
 credit.reset_index(inplace=True)
 credit.columns = pd.Index([e[0] + "_" + e[1].upper() for e in credit.columns.tolist() ])
 del credit['SK_ID_PREV_MEAN']
del credit['SK_ID_PREV_MAX']
del credit['SK_ID_PREV_MIN']
del credit['SK_ID_PREV_SUM']
del credit['SK_ID_PREV_VAR']
del credit['SK_ID_PREV_MEDIAN']
print('FEATURE ENGINEERING FOR CREDIT COMPLETE' )<rename_columns>
def to_hex(image_id)-> str:
return '{0:0{1}x}'.format(image_id, 16)
 def get_image_path(subset, image_id):
name = to_hex(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_global_features(image_root_dir):

 image_paths = [x for x in pathlib.Path(image_root_dir ).rglob('*.jpg')]
num_embeddings = len(image_paths)
 if MAX_NUM_EMBEDDINGS > 0:
num_embeddings = min(MAX_NUM_EMBEDDINGS, num_embeddings)
 ids = num_embeddings * [None]
embeddings = np.empty(( num_embeddings, NUM_EMBEDDING_DIMENSIONS))
for i, image_path in enumerate(image_paths):
if i >= num_embeddings:
break
ids[i] = int(image_path.name.split('.')[0], 16)
 image_tensor = load_image_tensor(image_path)
 features = GLOBAL_FEATURE_EXTRACTION_FN(image_tensor,
DELG_IMAGE_SCALES_TENSOR,
DELG_SCORE_THRESHOLD_TENSOR)
 embeddings[i, :] = tf.nn.l2_normalize(
 tf.reduce_sum(features[0], axis=0, name='sum_pooling'),
axis=0,
name='final_l2_normalization' ).numpy()
return ids, embeddings
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.9):

 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 + 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 load_labelmap() :
with open(TRAIN_LABELMAP_PATH, 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 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 = to_hex(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, test_embeddings = extract_global_features(TEST_IMAGE_DIR)
 train_ids, train_embeddings = extract_global_features(TRAIN_IMAGE_DIR)
 train_ids_labels_and_scores = [None] * test_embeddings.shape[0]
for test_index in range(test_embeddings.shape[0]):
distances = spatial.distance.cdist(
 test_embeddings[np.newaxis, test_index, :], train_embeddings,
'cosine')[0]
partition = np.argpartition(distances, NUM_TO_RERANK)[:NUM_TO_RERANK]
nearest = sorted([(train_ids[p], distances[p])for p in partition],
key=lambda x: x[1])
 train_ids_labels_and_scores[test_index] = [ 
(train_id, labelmap[to_hex(train_id)], 1.- cosine_distance)
 for train_id, cosine_distance in nearest
]
del test_embeddings
del train_embeddings
del labelmap
gc.collect()
pre_verification_predictions = get_prediction_map(
 test_ids, train_ids_labels_and_scores)
 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
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']
csv_writer.writerow({'id': image_id, 'landmarks': f'{label} {score}'} )
<feature_engineering><EOS> 
def main() :
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)
 main()