Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<prepare_x_and_y>
train = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')
 test = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv')
 sub = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
labels = train_data[:,0]
train = train_data[:,1:]/255<split>
models = [ "getting-started-with-tfrecords", "melanoma-efficientnetb6-with-attention-mechanism", "triple-stratified-kfold-with-tfrecords"]
models = ["384-E6-with-2018","512-E6","768-E2","512-E5","effb3-fulldata-upsample","effb2-fulldata-upsample"]
models = ["effb0-fulldata-upsample","effb1-fulldata-upsample","effb2-512-fulldata-upsample","effb3-fulldata-upsample","effb4-fulldata-upsample","effb5-fulldata-upsample"]
models = ["effb1-fulldata-upsample",
"melanoma-efficientnetb6-with-attention-mechanism",
"effb2-fulldata-upsample",
"effb4-fulldata-upsample",
"384-E6-with-2018",
"effb0-fulldata-upsample"]
models = ["384-E6-with-2018","512-E6","768-E2","512-E5","effb2-fulldata-upsample",
"effb1-fulldata-upsample"]
for model in models:
dirname = "/kaggle/input/siim3reorg/" + model
_oof = pd.read_csv(os.path.join(dirname, "oof.csv"))
score = metrics.roc_auc_score(_oof['target'], _oof['pred'])
 print(f"{model}: OOF auc:{score:.4}")
 _oof = _oof.rename(columns={"pred":model} ).drop(["target"],axis=1)
 if "fold" in _oof.columns:
_oof = _oof.drop(["fold"],axis=1)
 train = train.merge(_oof, on="image_name")
 _sub = pd.read_csv(os.path.join(dirname, "submission.csv"))
_sub.columns = ["image_name",model]
test = test.merge(_sub, on="image_name")
dummy_y = keras.utils.to_categorical(labels)
 x_train, x_test, y_train, y_test = train_test_split(train, dummy_y, test_size=0.1, random_state=166,stratify=labels )<choose_model_class>
train["pred_rank"] = 0
train["pred_power"] = 0
train["pred_avg"] = 0
for c in models:
train["pred_rank"] += train[c].rank() / train[c].rank().max()
train["pred_power"] += np.power(train[c],2)/np.power(train[c],2 ).max()
train["pred_avg"] += train [c]/train [c].max()
train["pred_rank"] = train["pred_rank"]/len(models)
 train["pred_power"] = train["pred_power"]/len(models)
 train["pred_avg"] = train["pred_avg"]/len(models)
 score = metrics.roc_auc_score(train['target'], train["pred_avg"])
 print(f'OOF avg_auc:{score}')
 score = metrics.roc_auc_score(train['target'], train["pred_rank"])
 print(f'OOF rank_auc:{score}')
 score = metrics.roc_auc_score(train['target'], train["pred_power"])
 print(f'OOF pow_auc:{score}' )
model = Sequential()
callbacks = [keras.callbacks.ModelCheckpoint('minist.h5', monitor='val_acc', verbose=1, save_best_only=True,
mode='auto')]
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',padding='same',
input_shape=(28,28,1)))
 model.add(Conv2D(64,(3, 3),padding='same', activation='relu'))
model.add(Conv2D(128,(3, 3),padding='same', activation='relu'))
model.add(Conv2D(128,(28, 28),activation='relu'))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()
sgd = SGD(lr=0.01, momentum=0.9)
 model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
 model.fit(x_train, y_train,
batch_size=128,
epochs=50,
verbose=1,
validation_data=(x_test, y_test),callbacks=callbacks )<load_pretrained>
test["target"] = 0.0
for c in models:
test["target"] += test[c].rank() / test[c].rank().max()
test["target"] = test["target"]/len(models)
 sub = test[["image_name","target"]]
sub.to_csv("submission_rank.csv",index=False)
 sub.head()
model.load_weights('minist.h5' )<predict_on_test>
test["target"] = 0.0
for c in models:
test["target"] += np.power(test[c],2)/np.power(test[c],2 ).max()
test["target"] = test["target"]/len(models)
 sub = test[["image_name","target"]]
sub.to_csv("submission_pow.csv",index=False)
 sub.head()
predict = model.predict(test )<prepare_output>
test["target"] = 0.0
for c in models:
test["target"] += test[c]/test[c].max()
test["target"] = test["target"]/len(models)
 sub = test[["image_name","target"]]
sub.to_csv("submission_avg.csv",index=False)
 sub.head()
results = np.argmax(predict,axis = 1 )<save_to_csv>
def dim_optimizer(df_oof, features, init_points = 20, n_iter = 30):
pbounds = {'c0':(0.0, 1.0), 'c1':(0.0, 1.0), 'c2':(0.0, 1.0),'c3':(0.0, 1.0),'c4':(0.0, 1.0),'c5':(0.0, 1.0)}
features = features
def dim_opt(df_oof, c0,c1,c2,c3,c4,c5):
x = c0*df_oof[ features[0] ] + c1*df_oof[ features[1]] + c2*df_oof[ features[2]] + c3*df_oof[ features[3]] + c4*df_oof[ features[4]] + c5*df_oof[ features[5]]
return metrics.roc_auc_score(df_oof['target'], x)
 def q(c0, c1,c2,c3,c4,c5):
return dim_opt(df_oof, c0, c1,c2,c3,c4,c5)
 optimizer = BayesianOptimization(
 f=q,
pbounds=pbounds,
random_state=42, 
)
 optimizer.maximize(
 init_points=init_points,
n_iter=n_iter, 
)
 c0 = optimizer.max["params"]["c0"]
c1 = optimizer.max["params"]["c1"]
c2= optimizer.max["params"]["c2"]
c3= optimizer.max["params"]["c3"]
c4= optimizer.max["params"]["c4"]
c5= optimizer.max["params"]["c5"]
t = optimizer.max["target"]
print(f'bo auc:{t}, c0:{c0}, c1:{c1}, c2:{c2},c3:{c3},c4:{c4},c5:{c5}')
 return c0, c1, c2,c3,c4,c5
c0, c1, c2,c3,c4,c5 = dim_optimizer(train, models, init_points = 40, n_iter = 40)
 print(models[0],c0)
 print(models[1],c1)
 print(models[2],c2)
 print(models[3],c3)
 print(models[4],c4)
 print(models[5],c5)
submission = pd.DataFrame({"ImageId":range(1,28001),"Label":results})
 submission.to_csv("cnn_mnist.csv",index=False )<load_from_csv>
def bo_pred(df):
x = c0*df[ models[0] ] + c1*df[ models[1]] + c2*df[ models[2]] + c3*df[ models[3]] + c4*df[ models[4]] + c5*df[ models[5]]
return x
train["pred"] = bo_pred(train)
 score = metrics.roc_auc_score(train['target'], train['pred'])
 print(f"auc bo:{score}")
<train_model><EOS> 
test["target"] = bo_pred(test)
 sub = test[["image_name","target"]]
sub.to_csv("submission_bo.csv",index=False)
 sub.head()