Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<drop_column>
df_tour.drop(['DayNum', 'WScore', 'LScore', 'WLoc', 'NumOT'], inplace=True, axis=1)
 df_tour.tail()
df_subs_tour = df_tour[df_tour['Season'] >= min(massey_seasons)]<feature_engineering>
import os
import glob
import random
import shutil
import warnings
import json
import itertools
import numpy as np
import pandas as pd
from collections import Counter
import plotly.express as px
import matplotlib.pyplot as plt
import seaborn as sns
import keras
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
from PIL import Image
from sklearn.model_selection import train_test_split
WMassey = [0]*len(df_subs_tour)
 LMassey = [0]*len(df_subs_tour)
 for ind, row in df_subs_tour.iterrows() :
season = row['Season']
wid = row['WTeamID']
lid = row['LTeamID']
WMassey[ind] = df_SeasonTeamID[df_SeasonTeamID['Season']==season][wid].values[0]
LMassey[ind] = df_SeasonTeamID[df_SeasonTeamID['Season']==season][lid].values[0]<prepare_output>
work_dir = '.. /input/cassava-leaf-disease-classification/'
train_path = '/kaggle/input/cassava-leaf-disease-classification/train_images'
df_subs_tour['WMassey'] = WMassey
df_subs_tour['LMassey'] = LMassey
df_subs_tour.head(10 )<merge>
def seed_everything(seed=0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
seed = 123
seed_everything(seed)
 warnings.filterwarnings('ignore' )
df_winseeds = df_seeds.rename(columns={'TeamID':'WTeamID', 'int_seed':'WSeed'})
 df_loseseeds = df_seeds.rename(columns={'TeamID':'LTeamID', 'int_seed':'LSeed'})
 df_d = pd.merge(left=df_subs_tour, right=df_winseeds, how='left', on=['Season', 'WTeamID'])
 df_concat = pd.merge(left=df_d, right=df_loseseeds, on=['Season', 'LTeamID'])
 df_concat['SeedDiff'] = df_concat['WSeed'] - df_concat['LSeed']
df_concat['MasseyDiff'] = df_concat['WMassey'] - df_concat['LMassey']
df_concat.head()<prepare_output>
data = pd.read_csv(work_dir + 'train.csv')
 print(data['label'].value_counts() )
df_wins = pd.DataFrame()
df_wins['SeedDiff'] = df_concat['SeedDiff']
df_wins['MasseyDiff'] = df_concat['MasseyDiff']
df_wins['Result'] = 1
df_losses = pd.DataFrame()
df_losses['SeedDiff'] = -df_concat['SeedDiff']
df_losses['MasseyDiff'] = -df_concat['MasseyDiff']
df_losses['Result'] = 0
df_predictions = pd.concat(( df_wins, df_losses))
df_predictions.head()<prepare_x_and_y>
with open(work_dir + 'label_num_to_disease_map.json')as f:
real_labels = json.load(f)
 real_labels = {int(k):v for k,v in real_labels.items() }
data['class_name'] = data['label'].map(real_labels)
 real_labels
X_train = df_predictions[['SeedDiff', 'MasseyDiff']].values
y_train = df_predictions['Result'].values
X_train, y_train = shuffle(X_train, y_train )<import_modules>
train, test = train_test_split(data, test_size = 0.05, random_state = 123, stratify = data['class_name'] )
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV<train_on_grid>
IMG_SIZE = 300
size =(IMG_SIZE,IMG_SIZE)
 n_CLASS = 5
BATCH_SIZE = 15
rf = RandomForestClassifier()
rf_params = {'n_estimators': [100, 200, 300, 400, 500, 1000]}
rf_grid = GridSearchCV(rf, rf_params, scoring='neg_log_loss', refit=True)
 rf_grid.fit(X_train, y_train)
 print('Best log_loss: {:.4}, with best C: {}'.format(rf_grid.best_score_, rf_grid.best_params_['n_estimators']))<train_on_grid>
datagen_train = ImageDataGenerator(
 preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,
rotation_range = 40,
width_shift_range = 0.2,
height_shift_range = 0.2,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest', 
)
 datagen_val = ImageDataGenerator(
 preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,
)
logreg = LogisticRegression(solver='lbfgs')
 params = {'C': [0.001,0.01, 1, 10, 100]}
log_grid = GridSearchCV(logreg, params, scoring='neg_log_loss', refit=True)
 log_grid.fit(X_train, y_train)
 print('Best log_loss: {:.4}, with best C: {}'.format(log_grid.best_score_, log_grid.best_params_['C']))<load_from_csv>
train_set = datagen_train.flow_from_dataframe(
 train,
directory=train_path,
seed=123,
x_col='image_id',
y_col='class_name',
target_size = size,
class_mode='categorical',
interpolation='nearest',
shuffle = True,
batch_size = BATCH_SIZE, 
)
 test_set = datagen_val.flow_from_dataframe(
 test,
directory=train_path,
seed=123,
x_col='image_id',
y_col='class_name',
target_size = size,
class_mode='categorical',
interpolation='nearest',
shuffle=True,
batch_size=BATCH_SIZE,
)
df_sub = pd.read_csv('.. /input/SampleSubmissionStage2.csv')
 df_massey_2019 = pd.read_csv('.. /input/prelim2019_masseyordinals/Prelim2019_MasseyOrdinals.csv')
 len_sub = len(df_sub )<groupby>
def create_model() :
model = Sequential()
model.add(
 EfficientNetB5(
 input_shape =(IMG_SIZE, IMG_SIZE, 3),
include_top = False,
weights='imagenet',
drop_connect_rate=0.6, 
)
)
 model.add(GlobalAveragePooling2D())
 model.add(Flatten())
 model.add(Dense(
 256,
activation='relu',
bias_regularizer=tf.keras.regularizers.L1L2(l1=0.01, l2=0.001)
))
model.add(Dropout(0.5))
model.add(Dense(n_CLASS, activation = 'softmax'))
return model
leaf_model = create_model()
leaf_model.summary()
df_massey_2019 = df_massey_2019[df_massey_2019['Season']==2019]
df_m = df_massey_2019.groupby('TeamID' ).mean()
df_m.reset_index(drop=False, inplace=True)
 df_m.head()<string_transform>
EPOCHS = 50
STEP_SIZE_TRAIN = train_set.n // train_set.batch_size
STEP_SIZE_TEST = test_set.n // test_set.batch_size
def getYearTeams(ID):
return(int(x)for x in ID.split('_'))<prepare_x_and_y>
def model_fit() :
leaf_model = create_model()
loss = tf.keras.losses.CategoricalCrossentropy(
 from_logits = False,
label_smoothing=0.0001,
name='categorical_crossentropy' 
)
 leaf_model.compile(
 optimizer = Adam(learning_rate = 1e-3),
loss = loss,
metrics = ['categorical_accuracy'] 
)
 es = EarlyStopping(
 monitor='val_loss',
mode='min',
patience=5,
restore_best_weights=True,
verbose=1, 
)
 checkpoint_cb = ModelCheckpoint(
 "Cassava_best_model_b5.h5",
save_best_only=True,
monitor='val_loss',
mode='min', 
)
 reduce_lr = ReduceLROnPlateau(
 monitor='val_loss',
factor=0.2,
patience=2,
min_lr=1e-6,
mode='min',
verbose=1, 
)
 history = leaf_model.fit(
 train_set,
validation_data=test_set,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_steps=STEP_SIZE_TEST,
callbacks=[es, checkpoint_cb, reduce_lr], 
)
 leaf_model.save('Cassava_best_model'+'.h5')
 return history
X_test = np.zeros(shape=(len_sub, 2))
for ii, row in df_sub.iterrows() :
year, t1, t2 = getYearTeams(row['ID'])
 t1_seed = df_seeds[(df_seeds['TeamID'] == t1)&(df_seeds['Season'] == year)]['int_seed'].values[0]
t2_seed = df_seeds[(df_seeds['TeamID'] == t2)&(df_seeds['Season'] == year)]['int_seed'].values[0]
t1_mass = df_m[(df_m['TeamID'] == t1)]['OrdinalRank'].values[0]
t2_mass = df_m[(df_m['TeamID'] == t2)]['OrdinalRank'].values[0]
diff_seed = t1_seed - t2_seed
diff_mass = t1_mass - t2_mass
X_test[ii, 0] = diff_seed
X_test[ii, 1] = diff_mass<predict_on_test>
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
K.set_session(sess )
<save_to_csv><EOS> 
try:
final_model = keras.models.load_model('Cassava_best_model.h5')
 except Exception as e:
with tf.device('/GPU:0'):
results = model_fit()
print('Train Categorical Accuracy: ', max(results.history['categorical_accuracy']))
print('Test Categorical Accuracy: ', max(results.history['val_categorical_accuracy']))