Kaggle competition: Human Protein Atlas Image Classification
knn_grid= grid.best_estimator_
knn_grid.score(X,Y )<train_on_grid>
%matplotlib inline
k_range = range(1,31)
 weights_options=['uniform','distance']
param = {'n_neighbors':k_range, 'weights':weights_options}
cv = StratifiedShuffleSplit(n_splits=10, test_size=.30)
 grid = RandomizedSearchCV(KNeighborsClassifier() , param,cv=cv,verbose = False, n_jobs=-1, n_iter=40)
 grid.fit(X,Y )<find_best_params>
os.listdir('.. /input')
 PATH_BASE = '.. /input/'
TRAIN_BASE = 'human-protein-atlas-image-classification/'
MODEL_BASE = '4-channel-v2-with-rare-case-extension-t2/'
PATH_TRAIN = PATH_BASE+TRAIN_BASE+'train/'
print(grid.best_score_)
 print(grid.best_params_)
 print(grid.best_estimator_ )<find_best_params>
raw_labels = pd.read_csv(PATH_BASE+TRAIN_BASE+'train.csv')
 data_names = os.listdir(PATH_TRAIN)
 labels = []
for name, label in zip(raw_labels['Id'],raw_labels['Target'].str.split(" ")) :
labels.append({
'name':name,
'label':label
})
 train_idx, test_idx = train_test_split(labels, test_size=0.2)
 print('train: ' + str(len(train_idx)) + '
'+ 'validation: ' + str(len(test_idx)) )
knn_ran_grid = grid.best_estimator_
knn_ran_grid.score(X,Y )<compute_train_metric>
class data_generator:
def __init__(self):
pass
def batch_train(self, idx, batch_size, shape, augment=True):
while True:
name_list = []
label_list = []
for n in np.random.choice(idx, batch_size):
name_list.append(n['name'])
 int_label = list(map(int, n['label']))
label_list.append(int_label)
 batch_images = np.zeros(( batch_size, shape[0], shape[1], shape[2]))
i = 0
for name in name_list:
image = self.load_img(name, shape)
 if augment:
image = self.augment(image)
 batch_images[i] = image
i+=1
batch_labels = np.zeros(( batch_size, 28))
j = 0
for label in label_list:
batch_labels[j][label] = 1
j+=1
yield batch_images, batch_labels
def load_img(self, name, shape):
R = np.array(Image.open(PATH_TRAIN+name+'_red.png'))
G = np.array(Image.open(PATH_TRAIN+name+'_green.png'))
B = np.array(Image.open(PATH_TRAIN+name+'_blue.png'))
Y = np.array(Image.open(PATH_TRAIN+name+'_yellow.png'))
image = np.stack(( R, G, B, Y),axis=-1)
 image = cv2.resize(image,(shape[0], shape[1]))
image = np.divide(image, 255)
 return image
def augment(self, image):
aug = iaa.OneOf([
iaa.Affine(rotate=90),
iaa.Affine(rotate=180),
iaa.Affine(rotate=270),
iaa.Fliplr(0.5),
iaa.Flipud(0.5)
 ])
 image = aug.augment_image(image)
 return image
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
 Y_pred = gaussian.predict(X_test)
 acc_gaussian = round(accuracy_score(Y_pred, Y_test)* 100, 2)
 acc_gaussian<compute_train_metric>
K.clear_session()
GaussianProcessClassifier = GaussianProcessClassifier()
GaussianProcessClassifier.fit(X_train, Y_train)
 Y_pred = GaussianProcessClassifier.predict(X_test)
 gau_pro_accy = round(accuracy_score(Y_pred, Y_test), 3)
 print(gau_pro_accy )<compute_train_metric>
SHAPE =(299,299,4)
 BATCH_SIZE = 24
def f1(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f1 = 2*p*r /(p+r+K.epsilon())
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return K.mean(f1)
 def show_history(history):
fig, ax = plt.subplots(1, 3, figsize=(15,5))
ax[0].set_title('loss')
 ax[0].plot(history.epoch, history.history["loss"], label="Train loss")
 ax[0].plot(history.epoch, history.history["val_loss"], label="Validation loss")
 ax[1].set_title('f1')
 ax[1].plot(history.epoch, history.history["f1"], label="Train f1")
 ax[1].plot(history.epoch, history.history["val_f1"], label="Validation f1")
 ax[2].set_title('acc')
 ax[2].plot(history.epoch, history.history["acc"], label="Train acc")
 ax[2].plot(history.epoch, history.history["val_acc"], label="Validation acc")
 ax[0].legend()
ax[1].legend()
ax[2].legend()
def f1_loss(y_true, y_pred):
K_epsilon = K.epsilon()
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K_epsilon)
 r = tp /(tp + fn + K_epsilon)
 f1 = 2*p*r /(p+r+K_epsilon)
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return 1-K.mean(f1)
 def binary_focal_loss(y_true, y_pred, gamma=2.0, alpha=0.25):

 epsilon = K.epsilon()
y_pred = K.clip(y_pred, epsilon, 1.0-epsilon)
 p_t = tf.where(K.equal(y_true, 1), y_pred, 1-y_pred)
 alpha_factor = K.ones_like(y_true)*alpha
alpha_t = tf.where(K.equal(y_true, 1), alpha_factor, 1-alpha_factor)
 cross_entropy = -K.log(p_t)
 weight = alpha_t * K.pow(( 1-p_t), gamma)
 loss = weight * cross_entropy
loss = K.sum(loss, axis=1)
 return loss
def SortedDict(adict):
new_dict = {}
ks = adict.keys()
ks = sorted(ks)
 for key in ks:
new_dict[key] = adict[key]
return new_dict
perceptron = Perceptron()
perceptron.fit(X_train, Y_train)
 Y_pred = perceptron.predict(X_test)
 acc_perceptron = round(accuracy_score(Y_pred, Y_test)* 100, 2)
 acc_perceptron<compute_train_metric>
y_true = Input(shape=(None,))
y_pred = Input(shape=(None,))
loss_function = K.Function(inputs=[y_true,y_pred], outputs=[binary_focal_loss(y_true, y_pred)])
 math_loss = -0.75*math.pow(( 1-0.6), 2)*math.log(0.6)- 0.25*math.pow(( 1-0.1), 2)*math.log(0.1)
 print('By manual calculate focal_loss: ', math_loss)
 tensor_loss = loss_function([[[1,0,0,0,1]],[[1,0,0,0.4,0.1]]])
 print('By tensor input via binary_focal loss', tensor_loss[0][0] )
sgd = SGDClassifier()
sgd.fit(X_train, Y_train)
 Y_pred = sgd.predict(X_test)
 acc_sgd = round(accuracy_score(Y_pred, Y_test)* 100, 2)
 acc_sgd<define_variables>
all_models = [logreg,
logreg_grid,
svc,
svc_grid,
linear_svc,
dectree,
dectree_grid,
rf,
rf_grid,
bagging_grid,
adaBoost_grid,
gradient_boost,
XGBClassifier,
ExtraTreesClassifier,
knn,
knn_grid,
knn_ran_grid,
gaussian,
GaussianProcessClassifier,
perceptron,
sgd]
c = []
for i in all_models:
a = i.predict(X_test)
 b = accuracy_score(a, Y_test)
 c.append(b )<create_dataframe>
model = load_model(PATH_BASE+MODEL_BASE+'fine_tune_weights.hdf5', custom_objects={'f1':f1, 'binary_focal_loss': binary_focal_loss} )
models = pd.DataFrame({
'Model': ['logreg',
'logreg_grid',
'svc',
'svc_grid',
'linear_svc',
'dectree',
'dectree_grid',
'rf',
'rf_grid',
'bagging_grid',
'adaBoost_grid',
'gradient_boost',
'XGBClassifier',
'ExtraTreesClassifier',
'knn',
'knn_grid',
'knn_ran_grid',
'gaussian',
'GaussianProcessClassifier',
'perceptron',
'sgd'],
'Score': c })
 models.sort_values(by="Score",ascending=False )<predict_on_test>
for layer in model.layers[:]:
layer.trainable =True
model.compile(
 loss=[binary_focal_loss],
optimizer=Adam(1e-4),
metrics=['acc', f1])
 checkpointer = ModelCheckpoint('fine_tune_weights.hdf5', verbose=2, monitor='val_loss', save_best_only=True, mode='max')
 generator = data_generator()
train_generator = generator.batch_train(train_idx, BATCH_SIZE, SHAPE, augment=True)
 validation_generator = generator.batch_train(test_idx, 620, SHAPE, augment=False )
final_pred_Y = bagging_grid.predict(test)
 final_pred_Y<create_dataframe>
history = model.fit_generator(
 train_generator,
steps_per_epoch=100,
validation_data=next(validation_generator),
epochs=80,
verbose=1,
callbacks=[checkpointer] )
submission = pd.DataFrame({
"PassengerId": passengerid,
"Survived": final_pred_Y
} )<data_type_conversions>
n_list = np.arange(0.1,0.5,0.02)
 for idx in test_idx[:3]:
name0 = idx['name']
print(idx)
 print(idx['name'])
 print(name0 )
submission.PassengerId = submission.PassengerId.astype(int)
 submission.Survived = submission.Survived.astype(int )<save_to_csv>
%%time
TP_data = {}
FP_data = {}
FN_data = {}
F1_best = 0
F1_ther = 0
for threshold in tqdm(n_list):
F1_sum = 0
TP_datai = {}
FP_datai = {}
FN_datai = {}
for i in range(28):
TP_datai[i] = 0
FP_datai[i] = 0
FN_datai[i] = 0
for idx in test_idx[:500]:
name0 = idx['name']
generator = data_generator()
image = generator.load_img(name0, SHAPE)
 score_predict = model.predict(image[np.newaxis,:])
 score_predict = np.array(score_predict)[0]
label_predict = np.arange(28)[score_predict>=threshold]
true_label = idx['label']
true_label = np.array(true_label ).astype(int)
 label_predict = set(label_predict)
 true_label = set(true_label)
 TP = sum(1 for num in label_predict if num in true_label)
 FP = sum(1 for num in label_predict if not num in true_label)
 FN = sum(1 for num in true_label if not num in label_predict)
 TN = 28 -(TP+FP+FN)
 F1_sum += 2*TP/(2*TP+FN+FP)
 for num in label_predict:
if num in true_label:
TP_datai[num] += 1
if num not in true_label:
FP_datai[num] += 1
for num in true_label:
if num not in label_predict:
FN_datai[num] += 1
if F1_sum>F1_best:
F1_best = F1_sum
F1_thre = threshold
TP_data = TP_datai
FP_data = FP_datai
FN_data = FN_datai
print('F1_score_sum: ', F1_sum, 'at threshold: ', threshold)
 TP_data = SortedDict(TP_data)
 FP_data = SortedDict(FP_data)
 FN_data = SortedDict(FN_data)
 print('F1_best ', F1_best, ' F1_thre ', F1_thre)
 print('TP_data ', TP_data)
 print('FP_data ', FP_data)
 print('FN_data ', FN_data )
submission.to_csv("titanic_submission.csv", index=False )<load_from_csv>
submit = pd.read_csv(PATH_BASE+TRAIN_BASE+'sample_submission.csv' )
data_set = pd.read_csv("/kaggle/input/titanic/train.csv")
 data_set_test = pd.read_csv("/kaggle/input/titanic/test.csv" )<data_type_conversions>
%%time
PATH_TRAIN = PATH_BASE+TRAIN_BASE+'test/'
generator = data_generator()
predicted = []
for name in tqdm(submit['Id']):
image = generator.load_img(name, SHAPE)
 score_predict = model.predict(image[np.newaxis,:])[0]
label_predict = np.arange(28)[score_predict>=F1_thre]
str_predict_label = ' '.join(str(l)for l in label_predict)
 predicted.append(str_predict_label )
<prepare_x_and_y><EOS> 
submit['Predicted'] = predicted
submit.to_csv('4 channel V2 with rare T2 plus threshold.csv', index=False )