Kaggle competition: Plant Pathology 2020 - FGVC7
if tf.config.list_physical_devices('GPU')is not None:
print('Training on GPU' )<define_search_model>
sub1 = pd.read_csv('.. /input/tpu-incepresnetv2-enb7/submission.csv')
 sub1.head()
lr_callback = tf.keras.callbacks.ReduceLROnPlateau()<train_model>
sub2 = pd.read_csv('.. /input/average-efficientnet/submission.csv')
 sub2.head()
gru_histories = []
gru_private_preds = np.zeros(( private_df.shape[0], 130, 5))
gru_public_preds = np.zeros(( public_df.shape[0], 107, 5))
rskf = RepeatedStratifiedKFold(FOLDS, n_repeats = REPEATS, random_state = SEED)
 for f,(train_index, val_index)in enumerate(rskf.split(train_inputs, train['SN_filter'])) :
sv_gru = tf.keras.callbacks.ModelCheckpoint(f'gru-{f}.h5')
 train_ = train_inputs[train_index]
train_labs = train_labels[train_index]
val_ = train_inputs[val_index]
val_labs = train_labels[val_index]
gru = build_model(gru=True)
 history = gru.fit(train_, train_labs,
validation_data=(val_,val_labs),
batch_size=BATCH_SIZE,
epochs=EPOCHS,
callbacks=[lr_callback,sv_gru],
verbose = VERBOSE)
 gru_histories.append(history)
 gru_short = build_model(gru=True, seq_len=107, pred_len=107)
 gru_short.load_weights(f'gru-{f}.h5')
 gru_public_pred = gru_short.predict(public_inputs)/ FOLDS
gru_long = build_model(gru=True, seq_len=130, pred_len=130)
 gru_long.load_weights(f'gru-{f}.h5')
 gru_private_pred = gru_long.predict(private_inputs)/ FOLDS * REPEATS
gru_public_preds += gru_public_pred
gru_private_preds += gru_private_pred
del gru_short, gru_long<compute_test_metric>
sub3 = pd.read_csv('.. /input/classification-densenet201-efficientnetb7/submission.csv')
 sub3.head()
print(f" GRU mean fold MCRMSE: {np.mean([min(history.history['val_loss'])for history in gru_histories])}" )<find_best_model_class>
sub4 = pd.read_csv('.. /input/plant-pathology-pytorch-gpu-model/v6_efficientnet_b4_5_fold_submission1_fold_0_39ep_best_fold_1_30ep_best_fold_2_33ep_best_fold_3_25ep_best_fold_4_44ep_best.csv')
 sub4.head()
lstm_histories = []
lstm_private_preds = np.zeros(( private_df.shape[0], 130, 5))
lstm_public_preds = np.zeros(( public_df.shape[0], 107, 5))
rskf = RepeatedStratifiedKFold(FOLDS, n_repeats = REPEATS, random_state = SEED)
 for f,(train_index, val_index)in enumerate(rskf.split(train_inputs, train['SN_filter'])) :
sv_gru = tf.keras.callbacks.ModelCheckpoint(f'lstm-{f}.h5')
 train_ = train_inputs[train_index]
train_labs = train_labels[train_index]
val_ = train_inputs[val_index]
val_labs = train_labels[val_index]
lstm = build_model(lstm=True)
 history = lstm.fit(
 train_, train_labs,
validation_data=(val_,val_labs),
batch_size=BATCH_SIZE,
epochs=EPOCHS,
callbacks=[lr_callback,sv_gru],
verbose = VERBOSE)
 lstm_histories.append(history)
 lstm_short = build_model(lstm=True, seq_len=107, pred_len=107)
 lstm_short.load_weights(f'lstm-{f}.h5')
 lstm_public_pred = lstm_short.predict(public_inputs)/ FOLDS
lstm_long = build_model(lstm=True, seq_len=130, pred_len=130)
 lstm_long.load_weights(f'lstm-{f}.h5')
 lstm_private_pred = lstm_long.predict(private_inputs)/ FOLDS * REPEATS
lstm_public_preds += lstm_public_pred
lstm_private_preds += lstm_private_pred
del lstm_short, lstm_long<compute_test_metric>
sub5 = pd.read_csv('.. /input/tf-zoo-models-on-tpu/submission.csv')
 sub5.head()
print(f" LSTM mean fold validation MCRMSE: {np.mean([min(history.history['val_loss'])for history in lstm_histories])}" )<split>
sub6 = pd.read_csv('.. /input/fork-of-plant-2020-tpu-915e9c/submission.csv')
 sub6.head()
public_df = test.query("seq_length == 107" ).copy()
private_df = test.query("seq_length == 130" ).copy()
public_inputs = preprocess_inputs(public_df)
 private_inputs = preprocess_inputs(private_df )<prepare_output>
sub = pd.read_csv('.. /input/plant-pathology-2020-fgvc7/sample_submission.csv')
 sub.head()
preds_gru = []
for df, preds in [(public_df, gru_public_preds),(private_df, gru_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_gru.append(single_df)
 preds_gru_df = pd.concat(preds_gru)
 preds_gru_df.head()<prepare_output>
sub.healthy =(sub1.healthy + sub2.healthy + sub3.healthy + sub4.healthy + sub5.healthy + sub6.healthy)/6
preds_lstm = []
for df, preds in [(public_df, lstm_public_preds),(private_df, lstm_private_preds)]:
for i, uid in enumerate(df.id):
single_pred = preds[i]
single_df = pd.DataFrame(single_pred, columns=target_cols)
 single_df['id_seqpos'] = [f'{uid}_{x}' for x in range(single_df.shape[0])]
preds_lstm.append(single_df)
 preds_lstm_df = pd.concat(preds_lstm)
 preds_lstm_df.head()<prepare_output>
sub.multiple_diseases =(sub1.multiple_diseases + sub2.multiple_diseases + sub3.multiple_diseases + sub4.multiple_diseases + sub5.multiple_diseases + sub6.multiple_diseases)/6
blend_preds_df = pd.DataFrame()
blend_preds_df['id_seqpos'] = preds_gru_df['id_seqpos']
blend_preds_df['reactivity'] =.5*preds_gru_df['reactivity'] +.5*preds_lstm_df['reactivity']
blend_preds_df['deg_Mg_pH10'] =.5*preds_gru_df['deg_Mg_pH10'] +.5*preds_lstm_df['deg_Mg_pH10']
blend_preds_df['deg_pH10'] =.5*preds_gru_df['deg_pH10'] +.5*preds_lstm_df['deg_pH10']
blend_preds_df['deg_Mg_50C'] =.5*preds_gru_df['deg_Mg_50C'] +.5*preds_lstm_df['deg_Mg_50C']
blend_preds_df['deg_50C'] =.5*preds_gru_df['deg_50C'] +.5*preds_lstm_df['deg_50C']<merge>
sub.rust =(sub1.rust + sub2.rust + sub3.rust + sub4.rust + sub5.rust + sub6.rust)/6
submission = sample_sub[['id_seqpos']].merge(blend_preds_df, on=['id_seqpos'])
 submission.head()<save_to_csv>
sub.to_csv('submission.csv', index=False )
<set_options><EOS> 
sub.to_csv('submission.csv', index=False )