Kaggle competition: Cassava Leaf Disease Classification
num_pipeline = Pipeline([ 
('imputer', SimpleImputer(strategy="median")) , 
('robust_scaler', RobustScaler())
 ] )<train_model>
warnings.filterwarnings("ignore")
 warnings.filterwarnings("ignore", category=DeprecationWarning )
n_fold = 5
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42)
 def train_model(X, X_test, y, params=None, folds=folds, model_type='lgb', plot_feature_importance=False, model=None):
prediction = np.zeros(X_test.shape[0])
 scores = []
feature_importance = pd.DataFrame()
for fold_n,(train_index, valid_index)in enumerate(folds.split(X)) :
print('Fold', fold_n, 'started at', time.ctime())
 if model_type == 'sklearn':
X_train, X_valid = X[train_index], X[valid_index]
else:
X_train, X_valid = X.values[train_index], X.values[valid_index]
y_train, y_valid = y[train_index], y[valid_index]
if model_type == 'lgb':
model = lgb.LGBMRegressor(**params, n_estimators = 20000, nthread = 4, n_jobs = -1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='rmse',
verbose=1000, early_stopping_rounds=200)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration_)
 if model_type == 'xgb':
train_data = xgb.DMatrix(data=X_train, label=y_train)
 valid_data = xgb.DMatrix(data=X_valid, label=y_valid)
 watchlist = [(train_data, 'train'),(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=20000, evals=watchlist, early_stopping_rounds=200, verbose_eval=500, params=params)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid), ntree_limit=model.best_ntree_limit)
 y_pred = model.predict(xgb.DMatrix(X_test.values), ntree_limit=model.best_ntree_limit)
 if model_type == 'sklearn':
model = model
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid ).reshape(-1,)
 score = mean_squared_error(y_valid, y_pred_valid)
 y_pred = model.predict(X_test)
 if model_type == 'cat':
model = CatBoostRegressor(iterations=20000, eval_metric='RMSE', **params)
 model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test)
 scores.append(mean_squared_error(y_valid, y_pred_valid)** 0.5)
 prediction += y_pred
if model_type == 'lgb':
fold_importance = pd.DataFrame()
fold_importance["feature"] = X.columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
 prediction /= n_fold
print('CV mean score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores)))
 gc.enable()
del model, y_pred_valid, X_test,X_train,X_valid, y_pred, y_train
gc.collect()
if model_type == 'lgb':
feature_importance["importance"] /= n_fold
if plot_feature_importance:
cols = feature_importance[["feature", "importance"]].groupby("feature" ).mean().sort_values(
 by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12)) ;
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False)) ;
plt.title('LGB Features(avg over folds)');
return scores, prediction, feature_importance
return scores, prediction
else:
return scores, prediction 
<categorify>
img_train = '.. /input/cassava-leaf-disease-classification/train_images'
img_test = '.. /input/cassava-leaf-disease-classification/test_images'
base_weight = '.. /input/cassava-baseline/'
train_df = pd.read_csv('.. /input/cassava-train-folds/train_folds.csv')
 BATCH_SIZE = 64
train_dummies = pd.get_dummies(X[:X_train.shape[0]])
 test_dummies = pd.get_dummies(X[X_train.shape[0]:])
 train_dummies, test_dummies = train_dummies.align(test_dummies, axis=1, join='inner' )<train_model>
class TestDataset(Dataset):
def __init__(self,df,im_path,transforms=None):
self.df = df
self.im_path = im_path
self.transforms = transforms
def __getitem__(self,idx):
img_path = self.df.iloc[idx]['image_id']
img = Image.open(self.im_path+"/"+img_path)
 if self.transforms:
img = self.transforms(**{"image": np.array(img)})["image"]
return img
def __len__(self):
return self.df.shape[0]
params = {
'num_leaves': 30,
'min_data_in_leaf': 20,
'objective': 'regression',
'max_depth': 6,
'learning_rate': 0.01,
"boosting": "gbdt",
"feature_fraction": 0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9,
"bagging_seed": 11,
"metric": 'rmse',
"lambda_l1": 0.2,
}
score_lgb, prediction_lgb, _ = train_model(train_dummies, test_dummies, train_labels, params=params, model_type='lgb', plot_feature_importance=True )<save_to_csv>
class Net(nn.Module):
def __init__(self,model_name='efficientnet-b3',pool_type=F.adaptive_avg_pool2d):
super().__init__()
self.pool_type = pool_type
self.backbone = EfficientNet.from_name(model_name)
 in_features = getattr(self.backbone,'_fc' ).in_features
self.classifier = nn.Linear(in_features,5)
 def forward(self,x):
features = self.pool_type(self.backbone.extract_features(x),1)
 features = features.view(x.size(0),-1)
 return self.classifier(features)
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['revenue'] = np.expm1(prediction_lgb)
 sub.to_csv("lgb_model.csv", index=False )<sort_values>
imagenet_stats = {'mean':[0.485, 0.456, 0.406], 'std':[0.229, 0.224, 0.225]}
test_tfms = A.Compose([
A.Resize(384,384,always_apply=1,p=1),
ToTensor(normalize=imagenet_stats)
 ] )
score_xgb.sort(reverse=True)
 dictvalues.update({'RMSE_XGB': score_xgb} )<save_to_csv>
val_preds = []
val_targets = []
for fold in range(5):
df_t = train_df[train_df.kfold==fold].reset_index(drop=True)
 dataset_val = TestDataset(df_t,img_train,test_tfms)
 dataloader_val = DataLoader(dataset_val, batch_size=BATCH_SIZE, num_workers=4, shuffle=False)
 temp = pred(base_weight+f"fold{fold}.pth",dataloader_val)
 temp = torch.argmax(temp, dim=1)
 val_preds.extend(temp)
 val_targets.extend(df_t.label.values )
sub['revenue'] = np.expm1(prediction_xgb)
 sub.to_csv("XGB_model.csv", index=False )<save_to_csv>
print(f"accuracy : {accuracy_score(val_targets,val_preds)}" )
sub['revenue'] = np.expm1(prediction_cat)
 sub.to_csv("cat_model.csv", index=False )<save_to_csv>
submission_df = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission_df.iloc[:, 1] = 0
submission_df.head()
sub['revenue'] = np.expm1(( prediction_lgb + prediction_xgb + prediction_cat)/ 3)
 sub.to_csv("combined.csv", index=False )<import_modules>
if submission_df.shape[0] == 1:
submission_df = pd.DataFrame([{'image_id': '2216849948.jpg', 'label': 0},{'image_id': '2216849948.jpg', 'label': 0}])
 submission_df.reset_index(drop=True, inplace=True)
 submission_df.head()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
from collections import Counter
from sklearn.model_selection import train_test_split
import lightgbm as lgb<load_from_csv>
submissions=None
for fold in range(5):
dataset_test = TestDataset(submission_df,img_test,test_tfms)
 dataloader_test = DataLoader(dataset_test, batch_size=BATCH_SIZE, num_workers=4, shuffle=False)
 test_preds = pred(base_weight+f"fold{fold}.pth",dataloader_test)
 if submissions is None:
submissions = test_preds /5
else:
submissions+= test_preds /5
train = pd.read_csv('.. /input/tmdb-box-office-prediction/train.csv')
 test = pd.read_csv('.. /input/tmdb-box-office-prediction/test.csv')
 sample_submission = pd.read_csv('.. /input/tmdb-box-office-prediction/sample_submission.csv' )<feature_engineering>
submission_df['label'] = torch.argmax(submissions, dim=1)
 submission_df.to_csv('submission.csv', index=False)
 submission_df