Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<count_values>
pip install --quiet /kaggle/input/kerasapplications
df_train.nom_9.value_counts().head()<load_from_csv>
pip install --quiet /kaggle/input/efficientnet-git
%%time
train_df = pd.read_csv('/kaggle/input/cat-in-the-dat/train.csv', index_col='id')
 sample_submission_df = pd.read_csv('/kaggle/input/cat-in-the-dat/sample_submission.csv')
 test_df = pd.read_csv('/kaggle/input/cat-in-the-dat/test.csv', index_col='id' )<prepare_x_and_y>
import efficientnet.tfkeras as efn
train_df = shuffle(train_df, random_state=random_state)
 train_Y = train_df['target'].copy()
train_X = train_df.drop('target', axis=1 ).copy()<categorify>
warnings.simplefilter("ignore")
def get_pipline(estimator):
return Pipeline(steps=[ 
('preproc', ColumnTransformer([ 
('bin_0_2', 'passthrough', ['bin_0', 'bin_1', 'bin_2']), 
('bin_3_4', FunctionTransformer(func=lambda X: X.replace({'F': 0, 'T': 1, 'N': 0, 'Y': 1}), validate=False), [
'bin_3', 'bin_4'] 
), 
('nom_0_4', OneHotEncoder(sparse=True, handle_unknown='ignore'), [
'nom_0', 'nom_1', 'nom_2', 'nom_3', 'nom_4', 'nom_5', 'nom_6', 'nom_7', 'nom_8', 'nom_9'] 
), 
('ord', Pipeline(steps=[ 
('replace', ColumnTransformer([ 
('encoder', OrdinalEncoder(categories=[
['Novice', 'Contributor', 'Expert', 'Master', 'Grandmaster'],
['Freezing', 'Cold', 'Warm', 'Hot', 'Boiling Hot', 'Lava Hot'],
np.sort(train_df['ord_3'].unique()),
np.sort(train_df['ord_4'].unique()),
np.sort(train_df['ord_5'].unique()),
]), ['ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5']),
], remainder='passthrough')) , 
('mm_scaler', MinMaxScaler())
 ]), ['ord_0', 'ord_1', 'ord_2', 'ord_3', 'ord_4', 'ord_5', 'day', 'month'])
 ])) , 
('est', estimator)
 ])
 xgb_pipline = get_pipline(XGBClassifier(objective='binary:logistic', n_estimators=1100, max_depth=6, gamma=5))
logit_pipline = get_pipline(LogisticRegression(solver='lbfgs', max_iter=225, C=0.12, random_state=random_state))
pipline = VotingClassifier([('xgb', xgb_pipline),('logit', logit_pipline)], voting='soft', n_jobs=-1 )<find_best_params>
print('Train images: %d' %len(os.listdir(os.path.join(WORK_DIR, "train_images"))))
params = {'est__solver': ['lbfgs'], 'est__C': [0.11, 0.12, 0.13], 'est__max_iter': [225, 250, 275]}
gs_cv = GridSearchCV(logit_pipline, params, scoring='roc_auc', cv=cv, n_jobs=-1, return_train_score=True, verbose=1)
<compute_train_metric>
with open(os.path.join(WORK_DIR, "label_num_to_disease_map.json")) as file:
print(json.dumps(json.loads(file.read()), indent=4))
cv_scores = cross_validate(
 pipline, train_X, train_Y, scoring='roc_auc', cv=cv, n_jobs=-1,
return_train_score=True, return_estimator=True, verbose=1
)<save_to_csv>
train_labels = pd.read_csv(os.path.join(WORK_DIR, "train.csv"))
print(train_labels.head() )
%%time
scores = np.array([est.predict_proba(test_df)for est in cv_scores['estimator']])
 mean_scores = scores.mean(axis=0)[:, 0]
submit_df = pd.DataFrame({ 'id': test_df.index, 'target': mean_scores })
 submit_df.to_csv('submission.csv', index=False )<define_variables>
data_normall = pd.read_csv(".. /input/anomaly005/normall_all.csv")
 data_normall
DEBUG = True
GBDT = "catboost"<define_variables>
data_anomaly = pd.read_csv(".. /input/anomaly005/anomaly_all.csv")
 data_anomaly
INPUT_PREFIX = "/kaggle/input/cat-in-the-dat"
TRAIN_PATH = os.path.join(INPUT_PREFIX, "train.csv")
 TEST_PATH = os.path.join(INPUT_PREFIX, "test.csv")
 SAMPLE_SUBMISSION_PATH = os.path.join(INPUT_PREFIX, "sample_submission.csv")
 LGB_PARAMS = {}
XGB_PARAMS = {}
if DEBUG is True:
CB_PARAMS = {
"loss_function": "Logloss",
"eval_metric": "AUC",
"n_estimators": 10,
"learning_rate": 0.02,
"random_state": 42,
"use_best_model": True,
"depth": 7,
}
else:
CB_PARAMS = {
"loss_function": "Logloss",
"eval_metric": "AUC",
"n_estimators": 4000,
"learning_rate": 0.02,
"random_state": 42,
"use_best_model": True,
"depth": 7,
}
ES_ROUNDS = 1000
VERBOSE = 500
N_FOLDS = 5
EVAL_RATE = 0.05
CAT_FEATURES = [
"bin_0",
"bin_1",
"bin_2",
"bin_3",
"bin_4",
"nom_0",
"nom_1",
"nom_2",
"nom_3",
"nom_4",
"nom_5",
"nom_6",
"nom_7",
"nom_8",
"nom_9",
"ord_0",
"ord_1",
"ord_2",
"ord_3",
"ord_4",
"ord_5",
"day",
"month",
]
TARGET_COL = "target"
REMOVE_COLS = []
pd.set_option("display.max_columns", 25)
 warnings.filterwarnings("ignore" )<load_from_csv>
train,val25 = train_test_split(data_normall, test_size=0.25, random_state=0 , shuffle=True)
 train.to_csv('train.csv',index=False)
 val25.to_csv('val25.csv',index=False )
class Loader:
def __init__(self):
self
def _load_csv(self, path: str, nrows: int)-> pd.DataFrame:
return pd.read_csv(path, nrows=nrows ).set_index("id")
 def load_train(self, path: str = TRAIN_PATH, nrows: int = None)-> pd.DataFrame:
return self._load_csv(path, nrows=nrows)
 def load_test(self, path: str = TEST_PATH, nrows: int = None)-> pd.DataFrame:
return self._load_csv(path, nrows=nrows)
 def load_submission(
 self, path: str = SAMPLE_SUBMISSION_PATH, nrows: int = None 
)-> pd.DataFrame:
return self._load_csv(path, nrows=nrows )<prepare_x_and_y>
a = pd.read_csv(".. /input/anomaly005/anomaly_all.csv")
 b = pd.read_csv("./val25.csv")
 merged =pd.concat([a,b])
 merged.to_csv('./val30.csv', index=False )
class Preprocesser:
def __init__(self):
self
def run(
 self, train: pd.DataFrame, test: pd.DataFrame 
)-> Tuple[pd.DataFrame, np.array, pd.DataFrame]:
X_train, y_train = train.drop(TARGET_COL, axis=1), train[TARGET_COL].values
X_test = test.copy()
return(X_train, y_train, X_test )<set_options>
data_test =pd.read_csv("./train.csv")
 data_test
class Estimator:
def __init__(self):
self.lgb_params = LGB_PARAMS
self.xgb_params = XGB_PARAMS
self.cb_params = CB_PARAMS
self.es_rounds = ES_ROUNDS
self.verbose = VERBOSE
self.cat_features = CAT_FEATURES
self.gbdt = GBDT
self.model = None
self.models = []
self.kfolds = []
self.tr_idxs = []
self.val_idxs = []
self.pred_eval = None
self.pred_valid = None
self.pred_test = None
def fit_(
 self,
X_train: pd.DataFrame,
y_train: np.array,
X_eval: pd.DataFrame = None,
y_eval: np.array = None, 
)-> None:
self.model = cb.CatBoostClassifier(**self.cb_params)
 if X_eval is not None:
self.model.fit(
 X_train,
y_train,
eval_set=[(X_train, y_train),(X_eval, y_eval)],
early_stopping_rounds=self.es_rounds,
verbose=self.verbose,
cat_features=self.cat_features, 
)
 else:
self.model.fit(X_train, y_train, cat_features=self.cat_features)
 self.models.append(self.model)
 def predict_(self, model: Any, X: np.array)-> np.array:
return model.predict(X, prediction_type="Probability")[:, 1]
def evaluate_(self, y_true: np.array, y_pred: np.array)-> float:
return roc_auc_score(y_true, y_pred)
 def kfold_fit(
 self,
X: pd.DataFrame,
y: np.array,
n_splits: int = N_FOLDS,
shuffle: bool = True, 
)-> None:
self.kfolds = StratifiedKFold(
 n_splits=n_splits, random_state=42, shuffle=shuffle 
)
 train_length = len(X)
 eval_length = int(train_length * EVAL_RATE)
 self.pred_eval = np.zeros(( eval_length))
eval_aucs = []
for fold_idx,(tr_idx, val_idx)in enumerate(self.kfolds.split(X, y)) :
self.tr_idxs.append(tr_idx)
 self.val_idxs.append(val_idx)
 X_tr, y_tr = X.iloc[tr_idx, :], y[tr_idx]
X_train, X_eval =(
 X_tr.head(train_length - eval_length),
X_tr.tail(eval_length), 
)
 y_train, y_eval = y_tr[:(train_length - eval_length)], y_tr[-eval_length:]
del X_tr, y_tr
self.fit_(X_train, y_train, X_eval, y_eval)
 eval_auc = self.evaluate_(y_eval, self.predict_(self.model, X_eval))
eval_aucs.append(eval_auc)
 print(f"fold {fold_idx+1} eval_auc: {eval_auc}")
 del X_train, y_train, X_eval, y_eval
print(f"
mean eval_auc: {np.mean(eval_aucs)}")
 def kfold_predict(self, X_train: pd.DataFrame, X_test: pd.DataFrame)-> None:
self.pred_valid = np.zeros(( len(X_train)))
 self.pred_test = np.zeros(( len(X_test)))
 for fold_idx in range(self.kfolds.n_splits):
val_idx = self.val_idxs[fold_idx]
model = self.models[fold_idx]
valid_pred = self.predict_(model, X_train.iloc[val_idx, :].values)
 test_pred = self.predict_(model, X_test.values)
 self.pred_valid[val_idx] = valid_pred
self.pred_test += test_pred / self.kfolds.n_splits
def kfold_feature_importance(self)-> pd.DataFrame:
df_fi = pd.DataFrame()
for i, model in enumerate(self.models):
features = model.feature_names_
importances = model.feature_importances_
df_tmp = pd.DataFrame(
 {"feature": features, f"importance_{i}": importances}
).set_index("feature")
 if i == 0:
df_fi = df_tmp.copy()
else:
df_fi = df_fi.join(df_tmp, how="left", on="feature")
 del df_tmp
df_fi["importance"] = df_fi.values.mean(axis=1)
 df_fi.sort_values("importance", ascending=False, inplace=True)
 df_fi.reset_index(inplace=True)
 return df_fi
def plot_feature_importance(self):
df_fi = self.kfold_feature_importance()
sns.set()
plt.figure(figsize=(6,10))
sns.barplot(y=df_fi["feature"], x=df_fi["importance"])
 plt.tight_layout()
plt.show()<save_to_csv>
data_val = pd.read_csv("./val30.csv")
 data_val
class Submitter:
def __init__(self, pred: np.array, filename: str = "submission.csv")-> None:
loader = Loader()
self.df = loader.load_submission()
self.df["target"] = pred
self.df.to_csv(f"{filename}")
 print("file saved" )<prepare_x_and_y>
BATCH_SIZE = 4
STEPS_PER_EPOCH = len(data_test)/ BATCH_SIZE
VALIDATION_STEPS = len(data_val)/ BATCH_SIZE
EPOCHS = 10
TARGET_SIZE = 512
%%time
loader = Loader()
preprocesser = Preprocesser()
estimator = Estimator()
train = loader.load_train()
test = loader.load_test()
X_train, y_train, X_test = preprocesser.run(train, test)
 del train, test<train_model>
data_test.label = data_test.label.astype('str')
 train_datagen = ImageDataGenerator(
 preprocessing_function = None,
rotation_range = 45,
zoom_range = 0.2,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest',
shear_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
featurewise_center = True,
featurewise_std_normalization = True)
 train_generator = train_datagen.flow_from_dataframe(data_test,
directory = os.path.join(WORK_DIR, "train_images"),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,shuffle=True,
class_mode = "sparse" )
%%time
estimator.kfold_fit(X_train, y_train)
 estimator.kfold_predict(X_train, X_test)
 valid_auc = estimator.evaluate_(y_train, estimator.pred_valid)
 print(f"
valid_auc: {valid_auc}" )<load_from_csv>
data_val.label = data_val.label.astype('str')
 validation_datagen = ImageDataGenerator(rescale=1./255)
 validation_generator = train_datagen.flow_from_dataframe(data_val,
directory = os.path.join(WORK_DIR, "train_images"),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,shuffle=True,
class_mode = "sparse" )
%%time
train = pd.read_csv('.. /input/cat-in-the-dat/train.csv')
 test = pd.read_csv('.. /input/cat-in-the-dat/test.csv')
 print(train.shape)
 print(test.shape )<drop_column>
def create_model() :
model = models.Sequential()
model.add(efn.EfficientNetB4(include_top = False, weights = ".. /input/efficientnet-keras-noisystudent-weights-b0b7/efficientnet-b4_noisy-student_notop.h5",
input_shape =(TARGET_SIZE, TARGET_SIZE, 3)))
 model.add(layers.GlobalAveragePooling2D())
 model.add(Dense(1024, activation="relu"))
model.add(Dense(512, activation="relu"))
model.add(Dropout(0.25))
model.add(Dense(5, activation="softmax"))
model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["sparse_categorical_accuracy"])
 return model
model = create_model()
model.summary()
%%time
target = train['target']
train_id = train['id']
test_id = test['id']
train.drop(['target', 'id'], axis=1, inplace=True)
 test.drop('id', axis=1, inplace=True)
 print(train.shape)
 print(test.shape )<categorify>
print('Our EfficientNet CNN has %d layers' %len(model.layers))
%%time
traintest = pd.concat([train, test])
 dummies = pd.get_dummies(traintest, columns=traintest.columns, drop_first=True, sparse=True)
 train_ohe = dummies.iloc[:train.shape[0], :]
test_ohe = dummies.iloc[train.shape[0]:, :]
print(train_ohe.shape)
 print(test_ohe.shape )<split>
model_save = ModelCheckpoint('./EffNetB_best_weights.h5',
save_best_only = True,
save_weights_only = True,
monitor = 'val_loss',
mode = 'min', verbose = 1)
 early_stop = EarlyStopping(monitor = 'val_loss', min_delta = 0.001,
patience = 5, mode = 'min', verbose = 1,
restore_best_weights = True)
 reduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.3,
patience = 2, min_delta = 0.001,
mode = 'min', verbose = 1 )
%%time
def run_cv_model(train, test, target, model_fn, params={}, eval_fn=None, label='model'):
kf = KFold(n_splits=5)
 fold_splits = kf.split(train, target)
 cv_scores = []
pred_full_test = 0
pred_train = np.zeros(( train.shape[0]))
i = 1
for dev_index, val_index in fold_splits:
print('Started ' + label + ' fold ' + str(i)+ '/5')
 dev_X, val_X = train[dev_index], train[val_index]
dev_y, val_y = target[dev_index], target[val_index]
params2 = params.copy()
pred_val_y, pred_test_y = model_fn(dev_X, dev_y, val_X, val_y, test, params2)
 pred_full_test = pred_full_test + pred_test_y
pred_train[val_index] = pred_val_y
if eval_fn is not None:
cv_score = eval_fn(val_y, pred_val_y)
 cv_scores.append(cv_score)
 print(label + ' cv score {}: {}'.format(i, cv_score))
i += 1
print('{} cv scores : {}'.format(label, cv_scores))
print('{} cv mean score : {}'.format(label, np.mean(cv_scores)))
 print('{} cv std score : {}'.format(label, np.std(cv_scores)))
 pred_full_test = pred_full_test / 5.0
results = {'label': label,
'train': pred_train, 'test': pred_full_test,
'cv': cv_scores}
return results
def runLR(train_X, train_y, test_X, test_y, test_X2, params):
print('Train LR')
 model = LogisticRegression(**params)
 model.fit(train_X, train_y)
 print('Predict 1/2')
 pred_test_y = model.predict_proba(test_X)[:, 1]
print('Predict 2/2')
 pred_test_y2 = model.predict_proba(test_X2)[:, 1]
return pred_test_y, pred_test_y2
lr_params = {'solver': 'lbfgs', 'C': 0.1}
results = run_cv_model(train_ohe, test_ohe, target, runLR, lr_params, auc, 'lr' )<save_to_csv>
history = model.fit(
 train_generator,
steps_per_epoch = STEPS_PER_EPOCH,
epochs = EPOCHS,
validation_data = validation_generator,
validation_steps = VALIDATION_STEPS,
)
%%time
submission = pd.DataFrame({'id': test_id, 'target': results['test']})
 submission.to_csv('submission.csv', index=False )<install_modules>
ss = pd.read_csv(os.path.join(WORK_DIR, "sample_submission.csv"))
ss
preds = []
for image_id in ss.image_id:
image = Image.open(os.path.join(WORK_DIR, "test_images", image_id))
image = image.resize(( TARGET_SIZE, TARGET_SIZE))
image = np.expand_dims(image, axis = 0)
 preds.append(np.argmax(model.predict(image)))
 ss['label'] = preds
ss
ss.to_csv('submission.csv', index = False )
!pip install the1owl<set_options>
model.save('outliersModel4_12.h5' )
warnings.filterwarnings('ignore')
 PATH = '/kaggle/input/cat-in-the-dat/'<load_from_csv>
model.save_weights('outliersWeight4_12.h5' )