Kaggle competition: Mechanisms of Action (MoA) Prediction
if VALIDATE:
histories = []
oof_pred = []; oof_labels = []
kfold = KFold(FOLDS, shuffle = True, random_state = SEED)
 for f,(train_index, val_index)in enumerate(kfold.split(TRAINING_FILENAMES)) :
print('
print('Getting datasets...'); print('')
 val_ds = get_val_dataset(list(pd.DataFrame({'TRAINING_FILENAMES': TRAINING_FILENAMES} ).loc[val_index]['TRAINING_FILENAMES']),
one_hot=True,labeled=True, return_image_names=False, repeat=False, shuffle=False)
 print('Getting model...'); print(''); print('Training model...'); print('')
 model = tf.keras.models.load_model(f'.. /input/cassava-tensorflow-starter-training/EFFNET_{f}_34_CUTMIXUP_512_full.h5')
 oof_labels.append([target.numpy() for img, target in iter(val_ds.unbatch())])
 x_oof = val_ds.map(lambda image, image_name: image)
 oof_pred.append(np.argmax(model.predict(x_oof), axis=-1))
del model; z = gc.collect()<compute_test_metric>
df = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 non_ctl_idx = df.loc[df['cp_type']!='ctl_vehicle'].index.to_list()
df = df.drop(['sig_id','cp_type','cp_dose','cp_time'],axis=1)
 data_X_test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 data_X_test = data_X_test.drop(['sig_id','cp_dose','cp_time'],axis=1)
 dada = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 dada = dada.drop('sig_id',axis=1)
 df_label = dada.values
df = df.iloc[non_ctl_idx]
df_label = df_label[non_ctl_idx]
if VALIDATE:
y_true = np.concatenate(oof_labels)
 y_preds = np.concatenate(oof_pred)
 print(classification_report(np.argmax(y_true, axis=1)if AUG_TYPE is 'CUTMIXUP' else y_true, y_preds))
print(f"OOF accuracy score: {accuracy_score(np.argmax(y_true, axis=1)if AUG_TYPE is 'CUTMIXUP' else y_true, y_preds)}" )<define_variables>
json_file_path = '.. /input/mainfile/main_predictors.json'
with open(json_file_path, 'r')as j:
predictors = json.loads(j.read())
 predictors = predictors['start_predictors']
JPEG_PATH = ".. /input/cassava-leaf-disease-classification/test_images"
JPEG_PATH_TR = ".. /input/cassava-leaf-disease-classification/train_images"
def load_image(jpeg_path, image_id):
img = cv2.imread(os.path.join(jpeg_path, image_id)) /255.0
img = cv2.resize(img,(512, 512)) [:, :, ::-1]
return img
def generator(filepath, paths, batch_size=32):
i=0
print(len(paths))
while i <= len(paths):
batch = []
for cpt in range(batch_size):
if i + cpt >= len(paths):
i += batch_size
break
batch.append(load_image(filepath, paths[i+cpt]))
i += batch_size
yield np.stack(batch )<load_from_csv>
cs = df.columns.str.contains('c-')
 gs = df.columns.str.contains('g-')
 def preprocessor(train,test):
n_gs = 2
n_cs = 100
pca_cs = PCA(n_components = n_cs)
 pca_gs = PCA(n_components = n_gs)
 train_pca_gs = pca_gs.fit_transform(train[:,gs])
 train_pca_cs = pca_cs.fit_transform(train[:,cs])
 test_pca_gs = pca_gs.transform(test[:,gs])
 test_pca_cs = pca_cs.transform(test[:,cs])
 train_c_mean = train[:,cs].mean(axis=1)
 test_c_mean = test[:,cs].mean(axis=1)
 train_g_mean = train[:,gs].mean(axis=1)
 test_g_mean = test[:,gs].mean(axis=1)
 train = np.concatenate(( train,train_pca_gs,train_pca_cs,train_c_mean[:,np.newaxis]
,train_g_mean[:,np.newaxis]),axis=1)
 test = np.concatenate(( test,test_pca_gs,test_pca_cs,test_c_mean[:,np.newaxis],
test_g_mean[:,np.newaxis]),axis=1)
 scaler = preprocessing.StandardScaler()
train = scaler.fit_transform(train)
 test = scaler.transform(test)
 return train, test
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 tr = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv' )<predict_on_test>
p_min = 0.0005
p_max = 0.9995
def logloss(y_true, y_pred):
y_pred = tf.clip_by_value(y_pred,p_min,p_max)
 return -backend.mean(y_true*backend.log(y_pred)+(1-y_true)*backend.log(1-y_pred))
preds_all = []
preds_model = []
for fold in range(FOLDS):
print(f"
ds_test = generator(JPEG_PATH,submission.image_id.values)
 K.clear_session()
print('Loading and inferring...')
 model = tf.keras.models.load_model(f'.. /input/cassava-tensorflow-starter-training/EFFNET_{fold}_34_CUTMIXUP_512_full.h5')
 preds = model.predict(ds_test, verbose=True)
 preds_model.append(preds)
 preds_model = np.stack(preds_model ).mean(0)
 preds_all.append(preds_model)
 preds_all = np.stack(preds_all )<save_to_csv>
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_logloss', factor=0.1, verbose=1,mode='min',
patience=2, min_lr=1E-5)
 early_st = tf.keras.callbacks.EarlyStopping(monitor='val_logloss', min_delta=1E-5, patience=6, verbose=1, mode='min',
baseline=None, restore_best_weights=True )
submission["label"] = preds_all.mean(0 ).argmax(1)
 submission.to_csv("submission.csv", index=False )<define_variables>
def create_model(n_features, n_features_2, n_labels, label_smoothing = 0.0005):
input_pri = tf.keras.layers.Input(shape =(n_features,), name = 'Input1')
 input_sec = tf.keras.layers.Input(shape =(n_features_2,), name = 'Input2')
 mouth_1 = Sequential([
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(512, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(256, activation="relu")) ,
],name='Head1')
 input_tir = mouth_1(input_pri)
 input_tir_concat = tf.keras.layers.Concatenate()([input_sec, input_tir])
 num_columns = 703
stomach = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(512, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(512, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(256, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(256, activation="relu")) ,
],name='Head2')
 input_qua = stomach(input_tir_concat)
 input_qua_avg = tf.keras.layers.Average()([input_tir, input_qua])
 stomach2 = Sequential([
tf.keras.layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(256, kernel_initializer='lecun_normal', activation='selu')) ,
tf.keras.layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, kernel_initializer='lecun_normal', activation='selu')) ,
tf.keras.layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
],name='Head3')
 output = stomach2(input_qua_avg)
 model = Model(inputs = [input_pri, input_sec], outputs = output)
 model.compile(optimizer=tf.optimizers.Adam() ,
loss=losses.BinaryCrossentropy(label_smoothing=0.0005),metrics=logloss 
)
 return model
package_path = '.. /input/vision-transformer-pytorch/VisionTransformer-Pytorch'
sys.path.append(package_path )<import_modules>
n_seeds = 6
np.random.seed(1)
 seeds = np.random.randint(0,100,size=n_seeds)
 n_labels = df_label.shape[1]
n_train = df.shape[0]
n_test = data_X_test.shape[0]
n_folds = 10
y_pred = np.zeros(( n_test,n_labels))
oof = tf.constant(0.0)
 hists = []
for seed in seeds:
fold = 0
print(fold)
 mlkf = KFold(n_splits=n_folds,shuffle=True,random_state=seed)
 for n,(train, test)in enumerate(mlkf.split(df,df_label)) :
tf.print('OOF score is ',oof)
 print(f'Fold {n}')
 train_pri, validation_pri = preprocessor(df.iloc[train].values,
df.iloc[test].values)
 _,data_test_pri = preprocessor(df.iloc[train].values,
data_X_test.drop('cp_type',axis=1 ).values)
 train_sec = df.iloc[train][predictors].values
validation_sec = df.iloc[test][predictors].values
data_test_sec = data_X_test[predictors].values
train_y = df_label[train]
validation_y = df_label[test]
n_features = train_pri.shape[1]
n_features_2 = train_sec.shape[1]
model = create_model(n_features, n_features_2, n_labels)
 hist = model.fit([train_pri,train_sec],train_y, batch_size=128, epochs=200,verbose=1,validation_data =([validation_pri,validation_sec],validation_y),callbacks =[reduce_lr, early_st])
 hists.append(hist)
 model.save('_seed_'+str(seed)+'_fold_'+str(fold))
y_val = model.predict([validation_pri,validation_sec])
 oof += logloss(tf.constant(validation_y,dtype=tf.float32),tf.constant(y_val,dtype=tf.float32)) /(n_folds*n_seeds)
 y_pred += model.predict([data_test_pri,data_test_sec])/(n_folds*n_seeds)
 fold += 1
gc.collect()
tf.keras.backend.clear_session()
del train_pri
del train_sec
del validation_pri
del validation_sec
del train_y
del validation_y
del data_test_pri
del data_test_sec
import os
import pandas as pd
import albumentations as albu
import matplotlib.pyplot as plt
import json
import seaborn as sns
import cv2
import albumentations as albu
import numpy as np<load_from_csv>
final = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 final.iloc[:,1:] = np.clip(y_pred,p_min,p_max)
 final.iloc[data_X_test['cp_type'] == 'ctl_vehicle',1:] = 0
final.to_csv('submission.csv', index=False)
 final.head()