Kaggle competition: Google Landmark Retrieval 2020
params = {
"eta": 0.1,
"booster": "gblinear",
"objective": "multi:softprob",
"alpha": 4,
"lambda": 0,
"silent": 1,
"seed": 1233,
"num_class": 12,
"eval_metric": "mlogloss"
}<compute_train_metric>
import numpy as np
import os
import cv2
import glob
import tensorflow as tf
import keras
from keras.models import load_model, save_model
from keras.layers import Input, GlobalAveragePooling2D, GlobalMaxPooling2D
import keras.backend as K
from keras.models import Model, load_model
from keras.applications import VGG16
from keras.applications.vgg16 import preprocess_input
xgb.cv(params, dtrain,
num_boost_round=50,
maximize = False )<choose_model_class>
files = glob.glob(".. /input/landmark-retrieval-2020/train/a/b/c/*.jpg")
 for i in range(10):
im = cv2.imread(files[i])
 print(im.shape )
model = RandomForestClassifier() 
<train_model>
vgg = VGG16(input_shape=(224,224,3), weights=None, include_top=False)
 vgg.load_weights(".. /input/keras-pretrained-models/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5")
 input_image = Input(( 224,224,3))
x = vgg(input_image)
 output = GlobalMaxPooling2D()(x)
 model = Model(inputs=[input_image], outputs=[output])
 model.summary()
model = xgb.train(params, dtrain, num_boost_round=25 )<prepare_x_and_y>
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self ).__init__()
self.model = model
@tf.function(input_signature=[
tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image')
 ])
 def call(self, input_image):
output_tensors = {}
im = tf.image.resize(input_image,(224,224))
im = preprocess_input(im)
 extracted_features = self.model(tf.convert_to_tensor([im], dtype=tf.uint8)) [0]
output_tensors['global_descriptor'] = tf.identity(extracted_features, name='global_descriptor')
 return output_tensors
dtest = xgb.DMatrix(Xtest )<predict_on_test>
m = MyModel()
served_function = m.call
tf.saved_model.save(
 m, export_dir="./my_model", signatures={'serving_default': served_function} )
<save_to_csv><EOS> 
with ZipFile('submission.zip','w')as zip:
zip.write('./my_model/saved_model.pb', arcname='saved_model.pb')
 zip.write('./my_model/variables/variables.data-00000-of-00002', arcname='variables/variables.data-00000-of-00002')
 zip.write('./my_model/variables/variables.data-00001-of-00002', arcname='variables/variables.data-00001-of-00002')
 zip.write('./my_model/variables/variables.index', arcname='variables/variables.index' )