Kaggle competition: Google Landmark Retrieval 2020
<SOS> metric: custom_loss Kaggle data source: landmark-retrieval-2020<feature_engineering>
Image(filename='.. /input/landmark-retrieval-2020/train/0/0/0/000014b1f770f640.jpg')
train_df["masks"] = [np.array(load_img(".. /input/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]<categorify>
K.set_image_data_format('channels_last')
 K.set_learning_phase(1)
train_df["coverage"] = train_df.masks.map(np.sum)/ pow(img_size_ori, 2)
 def cov_to_class(val):
for i in range(0, 11):
if val * 10 <= i :
return i
train_df["coverage_class"] = train_df.coverage.map(cov_to_class)
<split>
print(device_lib.list_local_devices())
 tf.debugging.set_log_device_placement(True)
 a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
 b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
 c = tf.matmul(a, b)
 print(c )
ids_train, ids_valid, x_train, x_valid, y_train, y_valid, cov_train, cov_test, depth_train, depth_test = train_test_split(
 train_df.index.values,
np.array(train_df.images.tolist() ).reshape(-1, img_size_target, img_size_target, 1),
np.array(train_df.masks.tolist() ).reshape(-1, img_size_target, img_size_target, 1),
train_df.coverage.values,
train_df.z.values,
test_size=0.2, stratify=train_df.coverage_class, random_state= 1618 )<categorify>
train = pd.read_csv(".. /input/landmark-retrieval-2020/train.csv")
 def get_paths(sub):
index = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
paths = []
for a in index:
for b in index:
for c in index:
try:
paths.extend([f".. /input/landmark-retrieval-2020/{sub}/{a}/{b}/{c}/" + x for x in os.listdir(f".. /input/landmark-retrieval-2020/{sub}/{a}/{b}/{c}")])
 except:
pass
return paths
def build_model() :
def conv(f, k=3, act='relu'):
return Conv2D(f,(k, k), activation=act, kernel_initializer='he_normal', padding='same')
 def _res_conv(inputs, f, k=3):
channels = int(inputs.shape[-1])
 cs = inputs
cs = BatchNormalization()(cs)
 cs = Activation('relu' )(cs)
 cs = conv(f, 3, act=None )(cs)
 cs = BatchNormalization()(cs)
 cs = Activation('relu' )(cs)
 cs = conv(f, 3, act=None )(cs)
 if f!=channels:
t1 = conv(f, 1, None )(inputs)
 else:
t1 = inputs
out = Add()([t1, cs])
 return out
def pool() :
return MaxPooling2D(( 2, 2))
def up(x, shape):
x = Lambda(lambda x: tf.image.resize_bilinear(x, shape, align_corners=True))(x)
 x = Conv2D(K.int_shape(x)[-1]//2,(2, 2), activation=None, kernel_initializer='he_normal', padding='same' )(x)
 return x
inputs = Input(shape=(img_size_ori, img_size_ori, 1))
depths = Input(shape=(1,))
preprocess1 = Lambda(lambda x: x/255.0 )(inputs)
 r = 16
rep = 4
mid_rep = 3
x = preprocess1
skip_connections = []
shapes = []
pad_mode = ['same', 'valid', 'same', 'valid']
for t in range(rep):
x = conv(r*int(2**t), 3, None )(x)
 x = _res_conv(x, r*int(2**t), 3)
 x = _res_conv(x, r*int(2**t), 3)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 skip_connections.append(x)
 shapes.append(K.int_shape(x)[1:3])
 x = pool()(x)
 x = Dropout(0.3 )(x)
 shape = K.int_shape(x)[1:3]
aux = Lambda(lambda x: x/1000.0 )(depths)
 aux = RepeatVector(np.prod(np.asarray(shape)) )(aux)
 aux = Reshape(( *shape,1))(aux)
 x = concatenate([x, aux])
 x = conv(r*int(2**rep), 3, None )(x)
 for t in range(mid_rep):
x = _res_conv(x, r*int(2**rep))
for t, s, p in zip(reversed(range(rep)) , reversed(skip_connections), reversed(shapes)) :
x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 x = up(x, p)
 x = concatenate([s, x])
 x = Dropout(0.3 )(x)
 x = conv(r*int(2**t), 3, None )(x)
 x = _res_conv(x, r*int(2**t), 3)
 x = _res_conv(x, r*int(2**t), 3)
 x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 outputs = Conv2D(1,(1, 1), activation=None, kernel_initializer='he_normal', padding='valid' )(x)
 return Model([inputs, depths], [outputs] )<compute_train_metric>
train_path = train
train_path["id"] = train_path.id.map(lambda path: f".. /input/landmark-retrieval-2020/train/{path[0]}/{path[1]}/{path[2]}/{path}.jpg" )
def get_iou_vector(A, B):
batch_size = A.shape[0]
metric = []
for batch in range(batch_size):
t, p = A[batch]>0, B[batch]>0
intersection = np.logical_and(t, p)
 union = np.logical_or(t, p)
 iou =(np.sum(intersection > 0)+ 1e-10)/(np.sum(union > 0)+ 1e-10)
 thresholds = np.arange(0.5, 1, 0.05)
 s = []
for thresh in thresholds:
s.append(iou > thresh)
 metric.append(np.mean(s))
return np.mean(metric)
 def lb(label, pred):
return tf.py_func(get_iou_vector, [label, pred > 0], tf.float64 )<compute_train_metric>
def lovasz_grad(gt_sorted):

 gts = tf.reduce_sum(gt_sorted)
 intersection = gts - tf.cumsum(gt_sorted)
 union = gts + tf.cumsum(1.- gt_sorted)
 jaccard = 1.- intersection / union
jaccard = tf.concat(( jaccard[0:1], jaccard[1:] - jaccard[:-1]), 0)
 return jaccard
def lovasz_hinge(logits, labels, per_image=True, ignore=None):

 if per_image:
def treat_image(log_lab):
log, lab = log_lab
log, lab = tf.expand_dims(log, 0), tf.expand_dims(lab, 0)
 log, lab = flatten_binary_scores(log, lab, ignore)
 return lovasz_hinge_flat(log, lab)
 losses = tf.map_fn(treat_image,(logits, labels), dtype=tf.float32)
 loss = tf.reduce_mean(losses)
 else:
loss = lovasz_hinge_flat(*flatten_binary_scores(logits, labels, ignore))
return loss
def lovasz_hinge_flat(logits, labels):

 def compute_loss() :
labelsf = tf.cast(labels, logits.dtype)
 signs = 2.* labelsf - 1.
 errors = 1.- logits * tf.stop_gradient(signs)
 errors_sorted, perm = tf.nn.top_k(errors, k=tf.shape(errors)[0], name="descending_sort")
 gt_sorted = tf.gather(labelsf, perm)
 grad = lovasz_grad(gt_sorted)
 loss = tf.tensordot(tf.nn.relu(errors_sorted), tf.stop_gradient(grad), 1, name="loss_non_void")
 return loss
loss = tf.cond(tf.equal(tf.shape(logits)[0], 0),
lambda: tf.reduce_sum(logits)* 0.,
compute_loss,
strict=True,
name="loss" 
)
 return loss
def flatten_binary_scores(scores, labels, ignore=None):

 scores = tf.reshape(scores,(-1,))
labels = tf.reshape(labels,(-1,))
if ignore is None:
return scores, labels
valid = tf.not_equal(labels, ignore)
 vscores = tf.boolean_mask(scores, valid, name='valid_scores')
 vlabels = tf.boolean_mask(labels, valid, name='valid_labels')
 return vscores, vlabels
def lovasz_loss(y_true, y_pred):
y_true, y_pred = K.cast(K.squeeze(y_true, -1), 'int32'), K.cast(K.squeeze(y_pred, -1), 'float32')
 logits = y_pred
loss = lovasz_hinge(logits, y_true, per_image = True, ignore = None)
 return loss<compute_test_metric>
batch_size = 4
seed = 42
IMAGE_SIZE = 128
shape =(IMAGE_SIZE, IMAGE_SIZE, 3)
 val_sample = 0.2
def bce(y_true, y_pred):
return K.mean(K.binary_crossentropy(y_true, K.sigmoid(y_pred)) )<concatenate>
train_labels = pd.read_csv('.. /input/landmark-retrieval-2020/train.csv')
 train_labels.head()
x_train = np.append(x_train, [np.fliplr(x)for x in x_train], axis=0)
 depth_train = np.append(depth_train, depth_train, axis=0)
 y_train = np.append(y_train, [np.fliplr(x)for x in y_train], axis=0)
 print(x_train.shape)
 print(depth_train.shape)
 print(y_valid.shape )<choose_model_class>
k =train[['id','landmark_id']].groupby(['landmark_id'] ).agg({'id':'count'})
 k.rename(columns={'id':'Count_class'}, inplace=True)
 k.reset_index(level=(0), inplace=True)
 freq_ct_df = pd.DataFrame(k)
 freq_ct_df.head()
model = build_model()
c = optimizers.adam(lr = 0.01)
 model.compile(loss=bce, optimizer=c, metrics=[lb])
 model.summary()<train_model>
train_labels = pd.merge(train,freq_ct_df, on = ['landmark_id'], how='left')
 train_labels.head()
model_checkpoint = ModelCheckpoint(save_model_name,monitor='lb',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='lb', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)
 epochs = 50
batch_size = 32
history = model.fit([x_train, depth_train], y_train,
validation_data=[[x_valid, depth_test], y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[ model_checkpoint,reduce_lr],
verbose=2, shuffle=True )<load_pretrained>
freq_ct_df.sort_values(by=['Count_class'],ascending=False,inplace=True)
 freq_ct_df.head()
model.load_weights(save_model_name)
 c = optimizers.adam(lr = 0.01)
 model.compile(loss=lovasz_loss, optimizer=c, metrics=[lb])
<train_model>
TOP = 40000
early_stopping = EarlyStopping(monitor='val_lb', mode = 'max',patience=20, verbose=1)
 model_checkpoint = ModelCheckpoint(save_model_name,monitor='val_lb',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_lb', mode = 'max',factor=0.5, patience=5, min_lr=0.0001, verbose=1)
 epochs = 60
batch_size = 32
history = model.fit([x_train, depth_train], y_train,
validation_data=[[x_valid, depth_test], y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[ model_checkpoint,reduce_lr,early_stopping],
verbose=2, shuffle=True )<load_pretrained>
freq_ct_df_top = freq_ct_df.iloc[:TOP]
top_class = freq_ct_df_top['landmark_id'].tolist()
model.load_weights(save_model_name )<predict_on_test>
def getTrainParams() :
print("Encoding labels")
 data = topclass_train.copy()
le = preprocessing.LabelEncoder()
print("fitting LabelENcoder")
 data['label'] = le.fit_transform(data['landmark_id'])
 print("Success in LabelENcoder")
 lbls = topclass_train['landmark_id'].tolist()
lb = LabelBinarizer(sparse_output=False)
 print("fitting LabelBinarizer")
 labels = lb.fit_transform(lbls)
 print("Success in LabelBinarizer")
 x = np.array(topclass_train['id'].tolist())
 print("id to array")
 y = labels
print("labels to array")
 return x,y,le
def predict_result(model,x_test,depth_test,img_size_target):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test = model.predict([x_test,depth_test] ).reshape(-1, img_size_target, img_size_target)
 preds_test2_refect = model.predict([x_test_reflect,depth_test] ).reshape(-1, img_size_target, img_size_target)
 preds_test += np.array([ np.fliplr(x)for x in preds_test2_refect])
 return preds_test/2<predict_on_test>
class Landmark2020_DataGenerator(tf.keras.utils.Sequence):
def __init__(self, paths, labels, batch_size, shape, shuffle = False, use_cache = False, augment = False):
self.paths, self.labels = paths, labels
self.batch_size = batch_size
self.shape = shape
self.shuffle = shuffle
self.use_cache = use_cache
self.augment = augment
if use_cache == True:
self.cache = np.zeros(( paths.shape[0], shape[0], shape[1], shape[2]), dtype=np.float16)
 self.is_cached = np.zeros(( paths.shape[0]))
self.on_epoch_end()
def __len__(self):
return int(np.ceil(len(self.paths)/ float(self.batch_size)))
 def __getitem__(self, idx):
indexes = self.indexes[idx * self.batch_size :(idx+1)* self.batch_size]
paths = self.paths[indexes]
X = np.zeros(( paths.shape[0], self.shape[0], self.shape[1], self.shape[2]))
if self.use_cache == True:
X = self.cache[indexes]
for i, path in enumerate(paths[np.where(self.is_cached[indexes] == 0)]):
image = self.__load_image(path)
 self.is_cached[indexes[i]] = 1
self.cache[indexes[i]] = image
X[i] = image
else:
for i, path in enumerate(paths):
X[i] = self.__load_image(path)
 y = self.labels[indexes]
if self.augment == True:
seq = iaa.Sequential([
iaa.OneOf([
iaa.Fliplr(0.5),
iaa.ContrastNormalization(( 0.75, 1.5)) ,
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
iaa.Multiply(( 0.8, 1.2), per_channel=0.2),
iaa.Affine(rotate=0),
iaa.Fliplr(0.5),
])], random_order=True)
 X = np.concatenate(( X, seq.augment_images(X), seq.augment_images(X), seq.augment_images(X)) , 0)
 y = np.concatenate(( y, y, y, y), 0)
 return X, y
def on_epoch_end(self):
self.indexes = np.arange(len(self.paths))
if self.shuffle == True:
np.random.shuffle(self.indexes)
 def __iter__(self):

 for item in(self[i] for i in range(len(self))):
yield item
def __load_image(self, path):
image_norm = cv2_imread(path)/255.0
im = cv2_resize(image_norm,(IMAGE_SIZE,IMAGE_SIZE))
return im
preds_valid = predict_result(model,x_valid,depth_test,img_size_target )<compute_train_metric>
nlabls = topclass_train['landmark_id'].nunique()
thresholds = np.linspace(-0.5, 0.5, 100)
 ious = np.array([iou_metric_batch(y_valid, preds_valid > threshold)for threshold in tqdm_notebook(thresholds)])
 print(ious )<categorify>
from tensorflow.keras.applications import InceptionV3
def rle_encode(im):

 pixels = im.flatten(order = 'F')
 pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )<predict_on_test>
NN_branches = 20
DENSE_UNITS = 2048
preds_test = predict_result(model,x_test,x_depth_test,img_size_target )<compute_test_metric>
base_model = InceptionV3(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3),
weights=None, include_top=False)
 base_model.load_weights(".. /input/keraspretrainedmodel/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5")
 input_image = Input(( IMAGE_SIZE, IMAGE_SIZE, 3), dtype = tf.uint8)
 x = tf.cast(input_image, tf.float32)
 x = tf.keras.applications.inception_v3.preprocess_input(x)
 x = base_model(input_image)
 x = GlobalMaxPooling2D()(x)
 x = Flatten()(x)
 x = Dense(DENSE_UNITS,activation='relu' )(x)
 x = tf.keras.layers.LayerNormalization()(x)
 embedding_model = Model(inputs = input_image,outputs = x,name='embedding_model')
 cat_out_layers = []
output = None
for i in range(1,NN_branches+1):
cat = Dense(DENSE_UNITS//NN_branches,activation = 'relu',name = 'cat'+str(i))(embedding_model.output)
 if i != NN_branches:
cat_out = Dense(nlabls//NN_branches, activation='softmax', name = 'cat'+str(i)+'_out', kernel_initializer = glorot_uniform(seed=0))(cat)
 else:
cat_out = Dense(nlabls//NN_branches+nlabls%NN_branches, activation='softmax', name = 'cat'+str(i)+'_out', kernel_initializer = glorot_uniform(seed=0))(cat)
 cat_out_layers.append(cat_out)
 output = Concatenate()(cat_out_layers)
 model = Model(inputs=[input_image], outputs=[output])
 model.summary()
t1 = time.time()
pred_dict = {idx: rle_encode(preds_test[i] > threshold_best)for i, idx in enumerate(tqdm_notebook(test_df.index.values)) }
t2 = time.time()
print(f"Usedtime = {t2-t1} s" )<save_to_csv>
def top_5_accuracy(y_true, y_pred):
return top_k_categorical_accuracy(y_true, y_pred, k=5 )
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv(submission_file )<train_model>
for layer in base_model.layers:
layer.trainable = False
t_finish = time.time()
print(f"Kernel run time = {(t_finish-t_start)/3600} hours" )<import_modules>
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc',top_5_accuracy])
 model.summary()
imread, imshow, concatenate_images 
)
 Input,Dropout,BatchNormalization,
Activation,Add, LeakyReLU,
UpSampling2D, Reshape, GaussianNoise,
Wrapper, Dense 
)
 EarlyStopping, ModelCheckpoint, ReduceLROnPlateau 
)
 ImageDataGenerator, array_to_img, img_to_array,
load_img 
)
<train_model>
gc.collect()
class DropConnect(Wrapper):
def __init__(self, layer, prob=1., **kwargs):
self.prob = prob
self.layer = layer
super(DropConnect, self ).__init__(layer, **kwargs)
 if 0.< self.prob < 1.:
self.uses_learning_phase = True
def build(self, input_shape):
if not self.layer.built:
self.layer.build(input_shape)
 self.layer.built = True
super(DropConnect, self ).build()
def compute_output_shape(self, input_shape):
return self.layer.compute_output_shape(input_shape)
 def call(self, x):
if 0.< self.prob < 1.:
self.layer.kernel = K.in_train_phase(K.dropout(self.layer.kernel, self.prob), self.layer.kernel)
 self.layer.bias = K.in_train_phase(K.dropout(self.layer.bias, self.prob), self.layer.bias)
 return self.layer.call(x )<feature_engineering>
paths, labels,_ = getTrainParams()
def set_random(number):
os.environ['PYTHONHASHSEED'] = '6072019'
np.random.seed(number)
 random.seed(number)
 session_conf = tf.ConfigProto(
 intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1 
)
 tf.set_random_seed(number)
 sess = tf.Session(graph=tf.get_default_graph() , config=session_conf)
 K.set_session(sess )<train_model>
gc.collect()
def upsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_target, img_size_target), mode='constant', preserve_range=True)
 def downsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_ori, img_size_ori), mode='constant', preserve_range=True )<compute_test_metric>
train_generator = Landmark2020_DataGenerator(pathsTrain, labelsTrain, batch_size, shape, use_cache=False, augment = False, shuffle = True)
 val_generator = Landmark2020_DataGenerator(pathsVal, labelsVal, batch_size, shape, use_cache=False, shuffle = False )
def swish(x):
return(K.sigmoid(x)* x )<drop_column>
epochs = 1
use_multiprocessing = True
def dropconnect(input_value, prob):
return tf.nn.dropout(input_value, keep_prob=prob)* prob<categorify>
model.fit_generator(
 train_generator,
steps_per_epoch=labelsTrain.shape[0]/batch_size,
validation_data=val_generator,
validation_steps=labelsVal.shape[0]/batch_size,
epochs=epochs,
use_multiprocessing=use_multiprocessing,
verbose=1 )
def rle_encode(im):

 pixels = im.flatten(order = 'F')
 pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )<predict_on_test>
gc.collect()
def predict_result(model,x_test,img_size_target):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test1 = model.predict(x_test ).reshape(-1, img_size_target, img_size_target)
 preds_test2_refect = model.predict(x_test_reflect ).reshape(-1, img_size_target, img_size_target)
 preds_test2 = np.array([ np.fliplr(x)for x in preds_test2_refect])
 preds_avg =(preds_test1 +preds_test2)/2
return preds_avg<concatenate>
gc.collect()
def convolution_block(x, filters, size, strides=(1,1), padding='same', activation=True):
x = Conv2D(filters, size, strides=strides, padding=padding )(x)
 x = BatchNormalization()(x)
 if activation == True:
x = Activation('swish' )(x)
 return x
def residual_block(blockInput, num_filters=16):
x = Activation('swish' )(blockInput)
 x = BatchNormalization()(x)
 x = convolution_block(x, num_filters,(3,3))
x = convolution_block(x, num_filters,(3,3), activation=False)
 x = Add()([x, blockInput])
 return x<choose_model_class>
test_img = skimage.io.imread('.. /input/landmark-retrieval-2020/train/0/0/0/000014b1f770f640.jpg')
 print(test_img.shape)
 print(type(test_img))
y = model.predict(test_img.reshape(1,test_img.shape[0],test_img.shape[1],3))
y
def build_model(input_layer, start_neurons, DropoutRatio = 0.5):
prob_dc = 1 -(DropoutRatio/4)
 conv1 = DropConnect(Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same"), prob =prob_dc )(input_layer)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = Activation('swish' )(conv1)
 pool1 = MaxPooling2D(( 2, 2))(conv1)
 conv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(pool1)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = Activation('swish' )(conv2)
 pool2 = MaxPooling2D(( 2, 2))(conv2)
 pool2 = Dropout(DropoutRatio/2 )(pool2)
 conv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(pool2)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = Activation('swish' )(conv3)
 pool3 = MaxPooling2D(( 2, 2))(conv3)
 pool3 = Dropout(DropoutRatio/2 )(pool3)
 conv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(pool3)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = Activation('swish' )(conv4)
 pool4 = MaxPooling2D(( 2, 2))(conv4)
 pool4 = Dropout(DropoutRatio/2 )(pool4)
 convm = Conv2D(start_neurons * 16,(3, 3), activation=None, padding="same" )(pool4)
 convm = residual_block(convm,start_neurons * 16)
 convm = residual_block(convm,start_neurons * 16)
 convm = Activation('swish' )(convm)
 deconv4 = Conv2DTranspose(start_neurons * 8,(3, 3), strides=(2, 2), padding="same" )(convm)
 uconv4 = concatenate([deconv4, conv4])
 uconv4 = Dropout(DropoutRatio/2 )(uconv4)
 uconv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(uconv4)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = Activation('swish' )(uconv4)
 deconv3 = Conv2DTranspose(start_neurons * 4,(3, 3), strides=(2, 2), padding="valid" )(uconv4)
 uconv3 = concatenate([deconv3, conv3])
 uconv3 = Dropout(DropoutRatio/2 )(uconv3)
 uconv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(uconv3)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = Activation('swish' )(uconv3)
 deconv2 = Conv2DTranspose(start_neurons * 2,(3, 3), strides=(2, 2), padding="same" )(uconv3)
 uconv2 = concatenate([deconv2, conv2])
 uconv2 = Dropout(DropoutRatio/2 )(uconv2)
 uconv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(uconv2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = Activation('swish' )(uconv2)
 deconv1 = Conv2DTranspose(start_neurons * 1,(3, 3), strides=(2, 2), padding="valid" )(uconv2)
 uconv1 = concatenate([deconv1, conv1])
 uconv1 = Dropout(DropoutRatio/2 )(uconv1)
 uconv1 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(uconv1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = Activation('swish' )(uconv1)
 prob = 1 -(DropoutRatio/4)
 uconv1 = Lambda(dropconnect,arguments={"prob":prob} )(uconv1)
 output_layer = Conv2D(1,(1,1), padding="same", activation="sigmoid" )(uconv1)
 return output_layer<load_pretrained>
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self ).__init__()
self.model = embedding_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,(IMAGE_SIZE, IMAGE_SIZE))
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
set_random(1537315160)
 img_size_ori = 101
img_size_target = 101
DATA_DIR = ".. /input/tgsdata/"
x_train2 = np.load(".. /input/intermediatetgs/x_train2.npy")
 x_valid = np.load(".. /input/intermediatetgs/x_valid.npy")
 y_train2 = np.load(".. /input/intermediatetgs/y_train2.npy")
 y_valid = np.load(".. /input/intermediatetgs/y_valid.npy")
 print("Train Data:")
 print("X: ",x_train2.shape,"Y: ",y_train2.shape)
 print("Validation Data:")
 print("X: ",x_valid.shape,"Y: ",y_valid.shape)
 get_custom_objects().update({"swish": swish} )<choose_model_class>
m = MyModel()
served_function = m.call
tf.saved_model.save(
 m,
export_dir="./model",
signatures={'serving_default': served_function}
)
input_layer = Input(( img_size_target, img_size_target, 2))
output_layer = build_model(input_layer, 16,0.6)
 model = Model(input_layer, output_layer)
 adam_opt = optimizers.Adam(lr=0.001,
beta_1=0.9,beta_2=0.999,
epsilon=None,
amsgrad=False,clipnorm=1.0 
)
 model.compile(loss="binary_crossentropy", optimizer=adam_opt, metrics=[my_iou_metric])
<choose_model_class>
with ZipFile('submission.zip','w')as output_zip_file:
for filename in os.listdir('./model'):
if os.path.isfile('./model/'+filename):
output_zip_file.write('./model/'+filename, arcname=filename)
 for filename in os.listdir('./model/variables'):
if os.path.isfile('./model/variables/'+filename):
output_zip_file.write('./model/variables/'+filename, arcname='variables/'+filename)
 for filename in os.listdir('./model/assets'):
if os.path.isfile('./model/assets/'+filename):
output_zip_file.write('./model/assets/'+filename, arcname='assets/'+filename )
early_stopping = EarlyStopping(monitor='val_my_iou_metric', mode = 'max',
patience=35, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric', mode = 'max',factor=0.97,
patience=4, min_lr=0, verbose=1)
 model_checkpoint = ModelCheckpoint("./unet_best1.model",monitor='val_my_iou_metric',
mode = 'max', save_best_only=True, verbose=1)
 epochs = 200
batch_size = 32
history = model.fit(x_train2,
y_train2,
validation_data=[x_valid, y_valid],
epochs=epochs,
shuffle=True,
batch_size=batch_size,
callbacks=[early_stopping, model_checkpoint, reduce_lr],
verbose=1
)<predict_on_test>
image_ids = pd.read_csv(
 '.. /input/landmark-retrieval-2020/train.csv',
nrows=100
)
preds_valid = predict_result(model,x_valid,img_size_target)
 preds_valid2 = np.array([downsample(x)for x in preds_valid])
 y_valid2 = np.array([downsample(x)for x in y_valid] )<find_best_params>
def get_image(img_id):
chars = [char for char in img_id]
dir_1, dir_2, dir_3 = chars[0], chars[1], chars[2]
image = Image.open('.. /input/landmark-retrieval-2020/train/' + dir_1 + '/' + dir_2 + '/' + dir_3 + '/' + img_id + '.jpg')
 image = image.resize(( IMAGE_SIZE, IMAGE_SIZE))
image = np.asarray(image)/ 255.0
return image
thresholds = np.linspace(0.3, 0.7, 31)
 ious = np.array([iou_metric_batch(y_valid2, np.int32(preds_valid2 > threshold)) for threshold in tqdm_notebook(thresholds)])
 threshold_best_index = np.argmax(ious)
 iou_best = ious[threshold_best_index]
threshold_best = thresholds[threshold_best_index]<drop_column>
images = [get_image(img_id)for img_id in image_ids.id]
images = np.array(images )
<load_pretrained>
embeddings = embedding_model.predict(images )
x_test = np.load(".. /input/intermediatetgs/x_test.npy" )<predict_on_test>
distances = distance.cdist(embeddings, embeddings, 'euclidean')
 print(distances.shape )
preds_test = predict_result(model,x_test,img_size_target )<load_pretrained>