Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<train_model>
import numpy as np
import pandas as pd
import os
from matplotlib import pyplot as plt
def fit_the_model_with_data(_model, _epochs, X_train, Y_train, X_val, Y_val, _cp):
original_hist = _model.fit(np.array(X_train), [np.array(Y_train),np.array(Y_train),np.array(Y_train)], epochs=_epochs, batch_size=batch_size,
verbose=0,
callbacks=[lr_decay, _cp],
validation_data=(np.array(X_val), [np.array(Y_val),np.array(Y_val),np.array(Y_val)]))
return original_hist<import_modules>
WEIGHT = 1
from sklearn.model_selection import KFold
import numpy as np<prepare_x_and_y>
submission = pd.read_csv('.. /input/siim-isic-melanoma-classification/sample_submission.csv')
 test = pd.read_csv('.. /input/siim-isic-melanoma-classification/test.csv')
 sub_best = pd.read_csv('.. /input/eda-modelling-of-the-external-data-inc-ensemble/external_meta_ensembled.csv' )
X = np.copy(train_np_n)
 y = np.copy(train_label_np)
 kf = KFold(n_splits=4,shuffle=True)
 kf.get_n_splits(X)
 print(kf )<train_on_grid>
files_sub = [
'.. /input/minmax-ensemble-0-9526-lb/submission.csv',
'.. /input/new-basline-np-log2-ensemble-top-10/submission.csv',
'.. /input/stacking-ensemble-on-my-submissions/submission_mean.csv',
'.. /input/analysis-of-melanoma-metadata-and-effnet-ensemble/ensembled.csv',
'.. /input/eda-modelling-of-the-external-data-inc-ensemble/external_meta_ensembled.csv',
'.. /input/submission-exploration/submission.csv',
'.. /input/rc-fork-siim-isic-melanoma-384x384/sub_EfficientNetB2_384.csv',
'.. /input/train-cv/submission.csv',
'.. /input/triple-stratified-kfold-with-tfrecords/submission.csv',
'.. /input/rank-then-blend/blend_sub.csv',
'.. /input/siim-isic-melanoma-classification-ensemble/submission.csv'
]
files_sub = sorted(files_sub)
 print(len(files_sub))
files_sub
_i = 0
for train_index, val_index in kf.split(X):
filepath=".. /work/kfold_cp"+str(_i)+".hdf5"
_cp = ModelCheckpoint(filepath, monitor='val_last_fc_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='max', period=1)
 _m = create_model2() ;
X_t, X_v = X[train_index], X[val_index]
y_t, y_v = y[train_index], y[val_index]
X_t = X_t.reshape(X_t.shape[0],28,28,1)
 X_v = X_v.reshape(X_v.shape[0],28,28,1)
 Y_t = to_categorical(y_t)
 Y_v = to_categorical(y_v)
 print(X_t.shape, y_t.shape, Y_t.shape, X_v.shape, y_v.shape, Y_v.shape)
 epochs=100
history = fit_the_model_with_data(_m, epochs, X_t, Y_t, X_v, Y_v, _cp)
 check_result(history.history);
_m.save(".. /work/"+"kfold"+str(_i)+'.h5')
 _i += 1<load_pretrained>
for file in files_sub:
test[file.replace(".csv", "")] = pd.read_csv(file ).sort_values('image_name')["target"]
test['id'] = test.index
model_cp0 = load_model(".. /work/kfold_cp0.hdf5")
 model_cp1 = load_model(".. /work/kfold_cp1.hdf5")
 model_cp2 = load_model(".. /work/kfold_cp2.hdf5")
 model_cp3 = load_model(".. /work/kfold_cp3.hdf5" )<predict_on_test>
test["diff_good1"] = test['.. /input/rank-then-blend/blend_sub'] - test['.. /input/triple-stratified-kfold-with-tfrecords/submission']
test["diff_good1"] = test['.. /input/train-cv/submission'] - test['.. /input/siim-isic-melanoma-classification-ensemble/submission']
test["diff_good2"] = test['.. /input/rc-fork-siim-isic-melanoma-384x384/sub_EfficientNetB2_384'] - test['.. /input/submission-exploration/submission']
test["diff_good3"] = test['.. /input/analysis-of-melanoma-metadata-and-effnet-ensemble/ensembled'] - test['.. /input/new-basline-np-log2-ensemble-top-10/submission']
test["diff_bad1"] = test['.. /input/stacking-ensemble-on-my-submissions/submission_mean'] - test['.. /input/minmax-ensemble-0-9526-lb/submission']
def pred_argmax(_model, _data, _label=np.array([0])) :
c_prob, c_prob1, c_prob2 = _model.predict(_data)
 pred= np.argmax(c_prob, axis=1)
 if len(_label)!= 1:
correct = np.argmax(_label,axis=1)
 else:
correct = 0
return c_prob, pred, correct
def pred_argmax2(_model,_gen, _data, _label=np.array([0])) :
_batch_size=128
_s = len(_data)/ _batch_size
c_prob = _model.predict_generator(_gen.flow(_data,batch_size=_batch_size,shuffle=None), steps=_s)
 pred= np.argmax(c_prob, axis=1)
 if len(_label)!= 1:
correct = np.argmax(_label,axis=1)
 else:
correct = 0
return c_prob, pred, correct
def disp_image(_data, _pred, _correct, _list, _isdisplay="off"):
pair_list = []
wrong_list = []
for _i in _list:
if _isdisplay == "on":
plt.imshow(_data[_i].reshape(28, 28), cmap=plt.get_cmap('gray'))
plt.show()
pair_list.append(str(_correct[_i])+" as "+str(_pred[_i]))
wrong_list.append(_pred[_i])
 return pair_list, wrong_list<categorify>
test["sub_best"] = test['.. /input/eda-modelling-of-the-external-data-inc-ensemble/external_meta_ensembled']
col_comment = ["id", "image_name", "patient_id", "sub_best"]
col_diff = [column for column in test.columns if "diff" in column]
test_diff = test[col_comment + col_diff].reset_index(drop=True)
 test_diff["diff_avg"] = test_diff[col_diff].mean(axis=1 )
X_all = train_np_n.reshape(train_np_n.shape[0], 28, 28, 1)
 Y_all = to_categorical(train_label_np)
 print(X_all.shape)
 print(Y_all.shape)
<prepare_output>
test_diff["sub_new"] = test_diff.apply(lambda x:(1+WEIGHT*x["diff_avg"])*x["sub_best"] if x["diff_avg"]<0 else(1-WEIGHT*x["diff_avg"])*x["sub_best"] + WEIGHT*x["diff_avg"] , axis=1 )
X0_cp_all_prob, X0_cp_all_pred, X0_cp_all_correct = pred_argmax(model_cp0, X_all,Y_all)
 X1_cp_all_prob, X1_cp_all_pred, X1_cp_all_correct = pred_argmax(model_cp1, X_all,Y_all)
 X2_cp_all_prob, X2_cp_all_pred, X2_cp_all_correct = pred_argmax(model_cp2, X_all,Y_all)
 X3_cp_all_prob, X3_cp_all_pred, X3_cp_all_correct = pred_argmax(model_cp3, X_all,Y_all)
 _d0_cp = np.not_equal(X0_cp_all_pred, X0_cp_all_correct)
 _d1_cp = np.not_equal(X1_cp_all_pred, X1_cp_all_correct)
 _d2_cp = np.not_equal(X2_cp_all_pred, X2_cp_all_correct)
 _d3_cp = np.not_equal(X3_cp_all_pred, X3_cp_all_correct)
 print(sum(_d0_cp))
print(sum(_d1_cp))
print(sum(_d2_cp))
print(sum(_d3_cp))
DF_cp = np.argmin([sum(_d0_cp),sum(_d1_cp),sum(_d2_cp),sum(_d3_cp)], axis=0)
 print(DF_cp )<prepare_output>
submission["target"] = sub_best["target"]
submission.head()
X0_test_prob, X0_test_pred, X0_test_correct = pred_argmax(model_cp0, X_test)
 X1_test_prob, X1_test_pred, X1_test_correct = pred_argmax(model_cp1, X_test)
 X2_test_prob, X2_test_pred, X2_test_correct = pred_argmax(model_cp2, X_test)
 X3_test_prob, X3_test_pred, X3_test_correct = pred_argmax(model_cp3, X_test)
<concatenate>
submission.loc[test["id"], "target"] = test_diff["sub_new"].values
<count_values><EOS> 
submission.to_csv("submission.csv", index=False)
 submission.head()