Kaggle competition: iMet Collection 2019 - FGVC6
FE = []
COLS = ['Sex','SmokingStatus']
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int)
<feature_engineering>
WORKERS = 2
CHANNEL = 3
NUM_CLASSES = 1103
warnings.filterwarnings("ignore")
 SIZE = 224
RESNET_VER = 2
epochs = 27
batch_size = 88
SPLIT_RATIO = 0.1
POST_PROCESS = False
LR = 5e-4
LR_FACTOR = 0.2
PATIENCE=2
LR_WARM = 1e-3
gamma = 0.0
checkpoint_file = '.. /working/resnet50_focal.h5'
<feature_engineering>
path_to_train = '.. /input/imet-2019-fgvc6/train/'
data = pd.read_csv('.. /input/imet-2019-fgvc6/train.csv')
 train_dataset_info = []
for name, labels in zip(data['id'], data['attribute_ids'].str.split(' ')) :
train_dataset_info.append({
'path':os.path.join(path_to_train, name),
'labels':np.array([int(label)for label in labels])})
 train_dataset_info = np.array(train_dataset_info )
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['FVC_BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())


 FE += ['age','percent','week','FVC_BASE'] 
<filter>
DATA_ROOT = '.. /input/imet-2019-fgvc6/'
def make_folds(n_folds: int)-> pd.DataFrame:
df = pd.read_csv(DATA_ROOT+ 'train.csv')
 cls_counts = Counter(cls for classes in df['attribute_ids'].str.split()
for cls in classes)
 fold_cls_counts = defaultdict(int)
 folds = [-1] * len(df)
 for item in tqdm(df.sample(frac=1, random_state=42 ).itertuples() ,
total=len(df)) :
cls = min(item.attribute_ids.split() , key=lambda cls: cls_counts[cls])
 fold_counts = [(f, fold_cls_counts[f, cls])for f in range(n_folds)]
min_count = min([count for _, count in fold_counts])
 random.seed(item.Index)
 fold = random.choice([f for f, count in fold_counts
if count == min_count])
 folds[item.Index] = fold
for cls in item.attribute_ids.split() :
fold_cls_counts[fold, cls] += 1
df['fold'] = folds
return df
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data<feature_engineering>
epsilon = K.epsilon()
def focal_loss(y_true, y_pred):
pt = y_pred * y_true +(1-y_pred)*(1-y_true)
 pt = K.clip(pt, epsilon, 1-epsilon)
 CE = -K.log(pt)
 FL = K.pow(1-pt, gamma)* CE
loss = K.sum(FL, axis=1)
 return loss
k=0
tr['KGroup'] = 0
for pat in tr.Patient.unique() :
k=k+1
tr.loc[tr.Patient == pat, 'KGroup'] = k
k=0
chunk['KGroup'] = 0
for pat in chunk.Patient.unique() :
k=k+1
chunk.loc[chunk.Patient == pat, 'KGroup'] = k
k=0
sub['KGroup'] = 0
for pat in sub.Patient.unique() :
k=k+1
sub.loc[sub.Patient == pat, 'KGroup'] = k
del k, pat<import_modules>
beta_f2=2
def f2(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=1)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=1)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=1)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=1)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f2 =(1+beta_f2**2)*p*r /(p*beta_f2**2 + r + K.epsilon())
 f2 = tf.where(tf.is_nan(f2), tf.zeros_like(f2), f2)
 return K.mean(f2 )
from tensorflow.keras import datasets, layers, models, regularizers, optimizers, callbacks
from tensorflow.keras.layers import Flatten, BatchNormalization
from keras.regularizers import l1_l2,l2,l1
from tensorflow.keras.optimizers import SGD
from tensorflow.keras import activations
from keras.layers import LeakyReLU
from tensorflow.keras.callbacks import History, EarlyStopping<init_hyperparams>
class data_generator(Sequence):
def create_train(dataset_info, batch_size, shape, augument=True):
assert shape[2] == 3
while True:
dataset_info = shuffle(dataset_info)
 for start in range(0, len(dataset_info), batch_size):
end = min(start + batch_size, len(dataset_info))
batch_images = []
X_train_batch = dataset_info[start:end]
batch_labels = np.zeros(( len(X_train_batch), NUM_CLASSES))
for i in range(len(X_train_batch)) :
image = data_generator.load_image(
 X_train_batch[i]['path'], shape)
 if augument:
image = data_generator.augment(image)
 batch_images.append(preprocess_input(image))
batch_labels[i][X_train_batch[i]['labels']] = 1
yield np.array(batch_images, np.float32), batch_labels
def create_valid(dataset_info, batch_size, shape, augument=False):
assert shape[2] == 3
while True:
for start in range(0, len(dataset_info), batch_size):
end = min(start + batch_size, len(dataset_info))
batch_images = []
X_train_batch = dataset_info[start:end]
batch_labels = np.zeros(( len(X_train_batch), NUM_CLASSES))
for i in range(len(X_train_batch)) :
image = data_generator.load_image(
 X_train_batch[i]['path'], shape)
 if augument:
image = data_generator.augment(image)
 batch_images.append(preprocess_input(image))
batch_labels[i][X_train_batch[i]['labels']] = 1
yield np.array(batch_images, np.float32), batch_labels
def load_image(path, shape):
image = cv2.imread(path+'.png')
 image = cv2.resize(image,(SIZE, SIZE))
return image
def augment(image):
augment_img = iaa.Sequential([
iaa.SomeOf(( 0,4),[
iaa.Crop(percent=(0, 0.25)) ,
iaa.ContrastNormalization(( 0.8, 1.2)) ,
iaa.Multiply(( 0.9, 1.1), per_channel=0.2),
iaa.Fliplr(0.5),
])], random_order=True)
 image_aug = augment_img.augment_image(image)
 return image_aug
ALPHA= 0.01
L2 = 0.001
MOMENTUM = 0.9
DPO = 0.3
use_metric = 1.0
BATCH_SIZE = 16
HDO = 144
EPOCHS = 2000
OPTIMIZER= "adamax"
qmin = 0.2
qmed = 0.5
qmax = 0.8
CORRECTION = tf.constant(1, dtype='float32')
 GROUPS = True
NFOLD = 5
TESTSIZE = 0.0
VAL_SPLIT = 0.1
ERR = 0.0<choose_model_class>
BatchNormalization, Input, Conv2D, Concatenate)
tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False,name='Adam' )<choose_model_class>
def ResNet(stack_fn,
preact,
use_bias,
model_name='resnet',
include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):

 global backend, layers, models, keras_utils
backend, layers, models, keras_utils = keras.backend, keras.layers, keras.models, keras.utils
if not(weights in {'imagenet', None} or os.path.exists(weights)) :
raise ValueError('The `weights` argument should be either '
'`None`(random initialization), `imagenet` '
'(pre-training on ImageNet), '
'or the path to the weights file to be loaded.')
 if weights == 'imagenet' and include_top and classes != 1000:
raise ValueError('If using `weights` as `"imagenet"` with `include_top`'
' as true, `classes` should be 1000')
 input_shape = utils._obtain_input_shape(input_shape,
default_size=224,
min_size=32,
data_format=backend.image_data_format() ,
require_flatten=include_top,
weights=weights)
 if input_tensor is None:
img_input = layers.Input(shape=input_shape)
 else:
if not backend.is_keras_tensor(input_tensor):
img_input = layers.Input(tensor=input_tensor, shape=input_shape)
 else:
img_input = input_tensor
bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
x = layers.ZeroPadding2D(padding=(( 3, 3),(3, 3)) , name='conv1_pad' )(img_input)
 x = layers.Conv2D(64, 7, strides=2, use_bias=use_bias, name='conv1_conv' )(x)
 if preact is False:
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name='conv1_bn' )(x)
 x = layers.Activation('relu', name='conv1_relu' )(x)
 x = layers.ZeroPadding2D(padding=(( 1, 1),(1, 1)) , name='pool1_pad' )(x)
 x = layers.MaxPooling2D(3, strides=2, name='pool1_pool' )(x)
 x = stack_fn(x)
 if preact is True:
x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name='post_bn' )(x)
 x = layers.Activation('relu', name='post_relu' )(x)
 if include_top:
x = layers.GlobalAveragePooling2D(name='avg_pool' )(x)
 x = layers.Dense(classes, activation='softmax', name='probs' )(x)
 else:
if pooling == 'avg':
x = layers.GlobalAveragePooling2D(name='avg_pool' )(x)
 elif pooling == 'max':
x = layers.GlobalMaxPooling2D(name='max_pool' )(x)
 if input_tensor is not None:
inputs = keras_utils.get_source_inputs(input_tensor)
 else:
inputs = img_input
model = models.Model(inputs, x, name=model_name)
 if(weights == 'imagenet')and(model_name in WEIGHTS_HASHES):
if include_top:
file_name = model_name + '_weights_tf_dim_ordering_tf_kernels.h5'
file_hash = WEIGHTS_HASHES[model_name][0]
else:
file_name = model_name + '_weights_tf_dim_ordering_tf_kernels_notop.h5'
file_hash = WEIGHTS_HASHES[model_name][1]
weights_path = keras_utils.get_file(file_name,
BASE_WEIGHTS_PATH + file_name,
cache_subdir='models',
file_hash=file_hash)
 model.load_weights(weights_path)
 elif weights is not None:
model.load_weights(weights)
 return model
tf.keras.optimizers.Adamax(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, name='Adamax' )<compute_test_metric>
def block2(x, filters, kernel_size=3, stride=1,
conv_shortcut=False, name=None):

 bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
preact = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_preact_bn' )(x)
 preact = layers.Activation('relu', name=name + '_preact_relu' )(preact)
 if conv_shortcut is True:
shortcut = layers.Conv2D(4 * filters, 1, strides=stride,
name=name + '_0_conv' )(preact)
 else:
shortcut = layers.MaxPooling2D(1, strides=stride )(x)if stride > 1 else x
x = layers.Conv2D(filters, 1, strides=1, use_bias=False,
name=name + '_1_conv' )(preact)
 x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_1_bn' )(x)
 x = layers.Activation('relu', name=name + '_1_relu' )(x)
 x = layers.ZeroPadding2D(padding=(( 1, 1),(1, 1)) , name=name + '_2_pad' )(x)
 x = layers.Conv2D(filters, kernel_size, strides=stride,
use_bias=False, name=name + '_2_conv' )(x)
 x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5,
name=name + '_2_bn' )(x)
 x = layers.Activation('relu', name=name + '_2_relu' )(x)
 x = layers.Conv2D(4 * filters, 1, name=name + '_3_conv' )(x)
 x = layers.Add(name=name + '_out' )([shortcut, x])
 return x
def stack2(x, filters, blocks, stride1=2, name=None):

 x = block2(x, filters, conv_shortcut=True, name=name + '_block1')
 for i in range(2, blocks):
x = block2(x, filters, name=name + '_block' + str(i))
x = block2(x, filters, stride=stride1, name=name + '_block' + str(blocks))
return x
def ResNet50V2(include_top=True,
weights='imagenet',
input_tensor=None,
input_shape=None,
pooling=None,
classes=1000,
**kwargs):
def stack_fn(x):
x = stack2(x, 64, 3, name='conv2')
 x = stack2(x, 128, 4, name='conv3')
 x = stack2(x, 256, 6, name='conv4')
 x = stack2(x, 512, 3, stride1=1, name='conv5')
 return x
return ResNet(stack_fn, True, True, 'resnet50v2',
include_top, weights,
input_tensor, input_shape,
pooling, classes,
**kwargs )
def myLoss(y_true,y_pred):
sigma =(y_pred[:, 2] - y_pred[:, 0])
 delta = tf.abs(y_true[:, 0] - y_pred[:, 1])
 errore = tf.abs(sigma)- tf.abs(delta)
 return errore<compute_test_metric>
def create_model_resnet50(n_out=NUM_CLASSES):
print(RESNET_VER)
 if RESNET_VER == 1:
base_model =ResNet50(weights=None, include_top=False)
 base_model.load_weights('.. /input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
 else:
base_model =ResNet50V2(weights=None, include_top=False)
 base_model.load_weights('.. /input/keras-pretrain-model-weights/resnet50v2_weights_tf_dim_ordering_tf_kernels_notop.h5')
 x0 = base_model.output
x1 = GlobalAveragePooling2D()(x0)
 x2 = GlobalMaxPooling2D()(x0)
 x = Concatenate()([x1,x2])
 x = BatchNormalization()(x)
 x = Dense(1024, activation='relu' )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.5 )(x)
 x = Dense(512, activation='relu' )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.5 )(x)
 predictions = Dense(n_out, activation='sigmoid' )(x)
 model = Model(inputs=base_model.input, outputs=predictions)
 return model
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 BEST = 0
def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma =(y_pred[:, 2] - y_pred[:, 0])
 fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric = -(( delta / sigma_clip)* sq2 + tf.math.log(sigma_clip * sq2)) /BEST
return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [qmin,qmed,qmax]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred

 v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda, err):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)+ err * myLoss(y_true, y_pred)
 return loss
def make_model() :
z = L.Input(( len(FE),), name="Patient")
 x = L.LeakyReLU(alpha=ALPHA )(z)
 x = L.Dense(HDO, kernel_regularizer=l2(L2))(x)
 x = L.LeakyReLU(alpha=ALPHA )(x)
 x = L.Dense(HDO, kernel_regularizer=l2(L2))(x)
 p1 = L.Dense(3, activation="swish", name="p1" )(x)
 model = M.Model(z, p1, name="CNN")
 model.compile(loss=mloss(use_metric, ERR), optimizer= OPTIMIZER, metrics=[score])
 return model 
<count_unique_values>
model = create_model_resnet50(
 n_out=NUM_CLASSES)
 for layer in model.layers:
layer.trainable = False
for i in range(-10,0):
model.layers[i].trainable = True
model.compile(
 loss='binary_crossentropy',
optimizer=Adam(LR_WARM),metrics=['acc',f2])
 model.summary()
print(len(tr['KGroup'].unique()))<count_unique_values>
EarlyStopping, ReduceLROnPlateau,CSVLogger)
 checkpoint = ModelCheckpoint(checkpoint_file, monitor='val_loss', verbose=1,
save_best_only=True, mode='min', save_weights_only = False)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=LR_FACTOR, patience=PATIENCE,
verbose=1, mode='auto', min_delta=0.0001, cooldown=0, min_lr=1e-5)
 csv_logger = CSVLogger(filename='.. /working/training_log.csv',
separator=',',
append=True)
 indexes = np.arange(train_dataset_info.shape[0])
 train_indexes, valid_indexes = train_test_split(indexes, test_size=SPLIT_RATIO, random_state=8)
 train_generator = data_generator.create_train(
 train_dataset_info[train_indexes], batch_size,(SIZE,SIZE,3), augument=True)
 train_generator_warmup = data_generator.create_train(
 train_dataset_info[train_indexes], batch_size,(SIZE,SIZE,3), augument=False)
 validation_generator = data_generator.create_valid(
 train_dataset_info[valid_indexes], batch_size,(SIZE,SIZE,3), augument=False)
 callbacks_list = [checkpoint, csv_logger, reduceLROnPlat]
a=tr['KGroup'].unique()
b=int(len(tr['KGroup'].unique())*(1-TESTSIZE))<define_variables>
print(np.ceil(float(len(train_indexes)) / float(batch_size)))
 print(np.ceil(float(len(valid_indexes)) / float(batch_size)))
 print(len(train_indexes), batch_size )
train_group = np.random.choice(a, b, replace=False)
 train_group = list(train_group)
 test_group = []
for k in range(1,len(tr['KGroup'].unique())+1):
if k not in train_group:
test_group.append(int(k))
print(len(train_group),len(test_group))
del a,b,k<prepare_x_and_y>
model.fit_generator(
 train_generator_warmup,
steps_per_epoch=np.ceil(float(len(train_indexes)) / float(batch_size)) ,
epochs=1,
max_queue_size=16, workers=WORKERS, use_multiprocessing=True,
verbose=1 )
X_train = pd.DataFrame()
Y_train = pd.DataFrame()
for k in train_group:
X_train = X_train.append(tr.loc[tr.KGroup == k])
 Y_train = X_train['FVC']
del k<prepare_x_and_y>
print(K.eval(model.optimizer.lr))
X_test = pd.DataFrame()
Y_test = pd.DataFrame()
if TESTSIZE > 0:
for k in test_group:
X_test = X_test.append(tr.loc[tr.KGroup == k])
 Y_test = X_test['FVC'] 
<prepare_x_and_y>
for layer in model.layers:
layer.trainable = True
model.compile(
 loss=focal_loss,
optimizer=Adam(lr=LR),
metrics=['acc',f2])
 hist = model.fit_generator(
 train_generator,
steps_per_epoch=np.ceil(float(len(train_indexes)) / float(batch_size)) ,
validation_data=validation_generator,
validation_steps=np.ceil(float(len(valid_indexes)) / float(batch_size)) ,
epochs=epochs,
verbose=1,
max_queue_size=16, workers=WORKERS, use_multiprocessing=True,
callbacks=callbacks_list )
z = X_train[FE].values
z = np.reshape(z,(z.shape[0],z.shape[1],1))
y = Y_train.values
ze = sub[FE].values
ze = np.reshape(ze,(ze.shape[0],z.shape[1],1))
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))
pred_act = np.zeros(( z.shape[0], 3))
if TESTSIZE > 0:
z_test = X_test[FE].values
z_test = np.reshape(z_test,(z_test.shape[0],z_test.shape[1],1))
y_test = Y_test.values
pred_test = np.zeros(( z_test.shape[0], 3)) 
<train_model>
print(K.eval(model.optimizer.lr))
def analyze_model(trax, tray, testx, testy):
a = net.evaluate(trax,tray, verbose=0, batch_size=BATCH_SIZE)
 b = net.evaluate(testx,testy, verbose=0, batch_size=BATCH_SIZE)
 c = tf.constant(BEST, dtype='float32')
 print("train:", a*c,'
')
 print("test:", b*c,'
' )<train_model>
print(os.listdir('.. /working/'))
model.load_weights(checkpoint_file )
def analyze_GROUP_model(a,b,c):
print("train:", a[0] / NFOLD ,a[1] * BEST / NFOLD,'
')
 print("val:", b[0] / NFOLD ,b[1] * BEST / NFOLD,'
')
 print("test:", c[0] / NFOLD ,c[1] * BEST / NFOLD,'
' )<compute_train_metric>

 BATCH = 512
fullValGen = data_generator.create_valid(
 train_dataset_info[valid_indexes], BATCH,(SIZE,SIZE,3))
n_val = round(train_dataset_info.shape[0]*0.2)//BATCH
print(n_val)
 lastFullValPred = np.empty(( 0, NUM_CLASSES))
lastFullValLabels = np.empty(( 0, NUM_CLASSES))
for i in tqdm(range(n_val+1)) :
im, lbl = next(fullValGen)
 scores = model.predict(im)
 lastFullValPred = np.append(lastFullValPred, scores, axis=0)
 lastFullValLabels = np.append(lastFullValLabels, lbl, axis=0)
 print(lastFullValPred.shape, lastFullValLabels.shape )
def laplace_Group_model(scope,base_x,base_y, predicted, bias):
actualFVC = base_y
predictedFVC = predicted
sigma_opt_actual = mean_absolute_error(actualFVC, predictedFVC[:, 1])
 sigmaSTD = mean_squared_error(actualFVC, predictedFVC[:, 1])
 unc_actual =(predictedFVC[:,2] - predictedFVC[:, 0])* bias
sigma_mean_actual = np.mean(unc_actual)
 best = laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = actualFVC,\
confidence= 0)
 print('laplace_log_likelihood su '+ scope +':
')
 print("opt_actual", laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = predictedFVC[:,1],\
confidence= sigma_opt_actual))
print("mean_squared", laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = predictedFVC[:,1],\
confidence= sigmaSTD))
print("unc_actual", laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = predictedFVC[:,1],\
confidence= unc_actual))
print("mean_actual", laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = predictedFVC[:,1],\
confidence= sigma_mean_actual))
return(best )<compute_train_metric>
def my_f2(y_true, y_pred):
assert y_true.shape[0] == y_pred.shape[0]
tp = np.sum(( y_true == 1)&(y_pred == 1),axis=1)
 tn = np.sum(( y_true == 0)&(y_pred == 0),axis=1)
 fp = np.sum(( y_true == 0)&(y_pred == 1),axis=1)
 fn = np.sum(( y_true == 1)&(y_pred == 0),axis=1)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f2 =(1+beta_f2**2)*p*r /(p*beta_f2**2 + r + 1e-15)
 return np.mean(f2)
 def find_best_fixed_threshold(preds, targs, do_plot=True):
score = []
thrs = np.arange(0, 0.5, 0.01)
 for thr in tqdm(thrs):
score.append(my_f2(targs,(preds > thr ).astype(int)))
 score = np.array(score)
 pm = score.argmax()
best_thr, best_score = thrs[pm], score[pm].item()
print(f'thr={best_thr:.3f}', f'F2={best_score:.3f}')
 if do_plot:
plt.plot(thrs, score)
 plt.vlines(x=best_thr, ymin=score.min() , ymax=score.max())
 plt.text(best_thr+0.03, best_score-0.01, f'$F_{2}=${best_score:.3f}', fontsize=14);
plt.show()
return best_thr, best_score
def laplace_model(scope,base_x,base_y,base_pred):
actualFVC = base_y
predictedFVC = base_pred
sigma_opt_actual = mean_absolute_error(actualFVC, predictedFVC[:, 1])
 sigmaSTD = mean_squared_error(actualFVC, predictedFVC[:, 1])
 unc_actual = predictedFVC[:,2] - predictedFVC[:, 0]
sigma_mean_actual = np.mean(unc_actual)
 best = laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = actualFVC,\
confidence= 0)
 print('laplace_log_likelihood su '+ scope +':
')
 print("opt_actual", laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = predictedFVC[:,1],\
confidence= sigma_opt_actual))
print("mean_squared", laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = predictedFVC[:,1],\
confidence= sigmaSTD))
print("unc_actual", laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = predictedFVC[:,1],\
confidence= unc_actual))
print("mean_actual", laplace_log_likelihood(actual_fvc = actualFVC, predicted_fvc = predictedFVC[:,1],\
confidence= sigma_mean_actual))
return(best )<compute_test_metric>
best_thr, best_score = find_best_fixed_threshold(lastFullValPred, lastFullValLabels, do_plot=True )
BEST = laplace_model('BEST',z,y,pred)
 start_at = time()<train_on_grid>
submit = pd.read_csv('.. /input/imet-2019-fgvc6/sample_submission.csv')
 predicted = []
for i, name in tqdm(enumerate(submit['id'])) :
path = os.path.join('.. /input/imet-2019-fgvc6/test/', name)
 image = data_generator.load_image(path,(SIZE,SIZE,3))
score_predict = model.predict(preprocess_input(image[np.newaxis]))
label_predict = np.arange(NUM_CLASSES)[score_predict[0]>=best_thr]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label )
%%time
if GROUPS:
history=History()
groups = X_train.KGroup
gkf = GroupKFold(n_splits= NFOLD)
 cnt=0
net_train = np.zeros(2,)
 net_val = np.zeros(2,)
 net_test = np.zeros(2,)
 start_at = time()
for tr_idx, val_idx in gkf.split(z, y, groups=groups):
cnt += 1
print(f"GROUP {cnt}")
 net = make_model()
start_Fold = time()
net.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_data=(z[val_idx], y[val_idx]), verbose=0, callbacks=[history])
 exec_time = time() - start_Fold
a = net.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE)
 b = net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE)
 print("train su fold:", a[0],a[1]*BEST)
 print("val su fold:", b[0],b[1]*BEST)
 net_train += a
net_val += b
if TESTSIZE > 0:
c = net.evaluate(z_test, y_test, verbose=0, batch_size=BATCH_SIZE)
 print("test su fold:", c[0],c[1]*BEST,)
 net_test += c
pred_test += net.predict(z_test, batch_size=BATCH_SIZE, verbose=0)/ NFOLD
pred[val_idx] += net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD
print("Tempo di addestramento: %d minuti e %d secondi" %(exec_time/60, exec_time%60),'
')
 exec_time = time() - start_at
print("
Tempo totale di addestramento: %d minuti e %d secondi" %(exec_time/60, exec_time%60),'
')
 del a,b<train_model>
submit['attribute_ids'] = predicted
if POST_PROCESS:
submit.to_csv('submission_not_process.csv', index=False)
 else:
submit.to_csv('submission.csv', index=False )
if GROUPS == False:
print("Inizio addestramento:...
 ")
 history = History()
net = make_model()
net.fit(z, y, batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_split=VAL_SPLIT, verbose=0, callbacks=[history])
 exec_time = time() - start_at
print("Tempo totale di addestramento: %d minuti e %d secondi" %(exec_time/60, exec_time%60),'
')
<train_model>
id_rare = set([199,81,221,230,366,11,104,396,262,805,112,146,281,
293,328,987,71,271,201,240,108,142,389,904,312,187])
 id_bool =(lastFullValPred[0] > 2)
 print(sum(id_bool))
for ii in id_rare:
id_bool[ii] = True
print(sum(id_bool))
lastFullValPred2 = lastFullValPred.copy()
lastFullValPred2[0][id_bool] = 0
thresh = np.percentile(lastFullValPred2[0],99.2)
 print(sum(lastFullValPred2[0] < thresh))
lastFullValPred2[0][lastFullValPred2[0] < thresh] = 0
print(sum(lastFullValPred2[0] == 0))
if GROUPS:
analyze_GROUP_model(net_train, net_val, net_test)
 else:
if TESTSIZE > 0:
analyze_model(z,y,z_test, y_test)
 else:
analyze_model(z,y,z, y )<train_on_grid>
print(lastFullValPred2[0].shape,(lastFullValPred2[i] < 0.1 ).shape)
 lastFullValPred2 = lastFullValPred.copy()
for i in tqdm(range(len(lastFullValPred2))):
lastFullValPred2[i][id_bool] = 0
thresh = np.percentile(lastFullValPred2[i],99.2)
 lastFullValPred2[i][lastFullValPred2[i] < thresh] = 0
if GROUPS:
for BIAS in(1,0,0.2,0.4,0.6,0.8,1.0,1.2,1.4,1.6,1.8,2):
print('
BIAS:',BIAS,'
')
 laplace_Group_model('training',z,y,pred, BIAS)
 print('
x--------x
')
 if TESTSIZE > 0: laplace_Group_model('test',z_test,y_test,pred_test, BIAS)
 else:
laplace_model('training',z,y,pred)
 print('
x--------x
')
 if TESTSIZE > 0: laplace_model('test',z_test,y_test,pred_test )<define_variables>
best_thr, best_score = find_best_fixed_threshold(lastFullValPred2, lastFullValLabels, do_plot=True )
predxs = pred
idxs = np.random.randint(0, y.shape[0], 100)
 idxs = np.sort(idxs )<compute_test_metric>
%%time
submit = pd.read_csv('.. /input/imet-2019-fgvc6/sample_submission.csv')
 predicted_post = []
for i, name in tqdm(enumerate(submit['id'])) :
path = os.path.join('.. /input/imet-2019-fgvc6/test/', name)
 image = data_generator.load_image(path,(SIZE,SIZE,3))
score_predict = model.predict(preprocess_input(image[np.newaxis]))
thresh = np.percentile(score_predict,99.2)
 score_predict[score_predict < thresh] = 0
label_predict = np.arange(NUM_CLASSES)[score_predict[0]>=best_thr]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted_post.append(str_predict_label )
<data_type_conversions><EOS> 
submit['attribute_ids'] = predicted_post
if POST_PROCESS:
submit.to_csv('submission.csv', index=False)
 else:
submit.to_csv('submission_post_process.csv', index=False)
 submit2 = submit.copy()
submit2.n_cate = submit.attribute_ids.apply(lambda x: len(x.split(" ")))
 _ = submit2.n_cate.value_counts().sort_index().plot.bar()