Kaggle competition: Cassava Leaf Disease Classification
covid_train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/train.csv",
parse_dates=['Date'] )<data_type_conversions>
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
sys.path.append(package_path )
covid_train['Date'].astype(int)/ 10**15<install_modules>
warnings.filterwarnings("ignore")
!pip install seaborn==0.11.0<import_modules>
CFG = {
'fold_num': 5,
'seed': 719,
'model_arch': 'tf_efficientnet_b4_ns',
'img_size': 512,
'epochs': 10,
'train_bs': 32,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 3,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}
pd.options.display.max_rows=200
pd.set_option('mode.chained_assignment', None)
 simplefilter("ignore", category=ConvergenceWarning)
 simplefilter("ignore", category=RuntimeWarning)
 sns.__version__<load_from_csv>
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head(10 )
train = pd.read_csv('/kaggle/input/titanic/train.csv', index_col='PassengerId')
 test = pd.read_csv('/kaggle/input/titanic/test.csv', index_col='PassengerId' )<count_missing_values>
train.label.value_counts()
train.isna().sum()<count_missing_values>
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()
test.isna().sum()<categorify>
def seeder(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def get_img(path):
im_bgr = cv2.imread(path)
 im_rgb = im_bgr[:, :, ::-1]
return im_rgb
def imputer(df):
age_impute_series = df.groupby(['Pclass', 'Sex'] ).Age.transform('mean')
 df.Age.fillna(age_impute_series, inplace=True)
 df.Cabin = df.Cabin.str.extract(pat='([A-Z])')
 df.Cabin.fillna('M', inplace=True)
 df['Deck'] = df.Cabin.replace({'A':'ABC', 'B':'ABC', 'C':'ABC', 'D':'DE', 'E':'DE', 'F':'FG',
'G':'FG', 'T':'ABC'})
 df.drop('Cabin', axis=1, inplace=True)
 for feature in df.columns:
df[feature].fillna(df[feature].mode() [0], inplace=True)
 return df<categorify>
img = get_img('.. /input/cassava-leaf-disease-classification/train_images/1000015157.jpg')
 plt.figure(figsize=(15,15))
plt.imshow(img)
 plt.show()
train_imputed = imputer(train.copy())
 test_imputed = imputer(test.copy() )<categorify>
class CassavaDataset(Dataset):
def __init__(self,df,data_root,transforms=None,output_label=True):
super(CassavaDataset ).__init__()
self.df=df.reset_index().copy()
self.data_root=data_root
self.transforms=transforms
self.output_label=output_label
def __len__(self):
return self.df.shape[0]
def __getitem__(self,index:int):
if self.output_label == True:
label=self.iloc[index]["label"]
path = "{}/{}".format(self.data_root, self.df.iloc[index]['image_id'])
 img=get_img(path)
 if self.transforms:
img=self.transforms(image=img)["image"]
if self.output_label == True:
return img,torch.tensor(label,dtype=torch.double)
 else:
return img
def ticket_extractor(ticket):
alpha = re.sub('\d', '', ticket)
 if alpha:
return alpha
else:
num = re.search('\d{1,9}', ticket)
 return ticket
temp = train_imputed.copy()
temp['Ticket_extracted'] = temp.Ticket.apply(ticket_extractor)
 for i in range(len(temp.Ticket)) :
try:
int(temp.Ticket_extracted.iloc[i])
 temp.Ticket_extracted.iloc[i] = f'Num_{len(temp.Ticket_extracted.iloc[i])}'
except:
continue
for label, pattern in [('_ca', 'C[.]?A[.]?'),('_PC', 'PC'),('_SOTON', 'SOTON'),('_STON', 'STON'), 
('_WC', 'W[.]?[/]?C'),('_SC', 'S[.]?C[.]?'),('_A', 'A[.]?'),('_SOC', 'S[.]?O[.]?[/]?C'), 
('_PP', 'PP'),('_FC', '(F.C.|F.C.C.) '),('_LS_number', 'Num_(6|7)'),('_SS_number', 'Num_(3|4|5)'), 
('rare', '^[^_]')]:
temp.Ticket_extracted[temp.Ticket_extracted.str.contains(pattern)] = label
temp['Ticket_extracted'].value_counts(dropna=False)
 fig, ax = plt.subplots(2, 1, figsize=(15, 15))
sns.countplot(data=temp, x='Ticket_extracted', hue='Survived', ax=ax[0])
 ax[0].set_title('Extracted Ticket - Survival "count" plot', size=20, loc='Left', y=1.03)
 sns.barplot(data=temp, x='Ticket_extracted', y='Survived', ax=ax[1])
 ax[1].set_title('Extracted Ticket - Survival "chance" plot', size=20, loc='Left', y=1.03)
 plt.show()<feature_engineering>
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_train_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
ToTensorV2(p=1.0),
], p=1.)
def get_valid_transforms() :
return Compose([
CenterCrop(CFG['img_size'], CFG['img_size'], p=1.) ,
Resize(CFG['img_size'], CFG['img_size']),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)
def get_inference_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)
temp = train_imputed.copy()
temp['Title'] = temp.Name.str.extract(pat='([a-zA-Z]+\.) ')
 temp.Title[~temp.Title.isin(['Mr.', 'Miss.', 'Mrs.', 'Master.'])] = 'rare'<categorify>
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x
def feature_creator(df_train, df_test):
df_train['Fare_cat'] = pd.qcut(df_train['Fare'], 7)
 df_test['Fare_cat'] = pd.qcut(df_test['Fare'], 7)
 df_train['Fare_cat'] = LabelEncoder().fit_transform(df_train['Fare_cat'])
 df_test['Fare_cat'] = LabelEncoder().fit_transform(df_test['Fare_cat'])
 df_train['Age_cat'] = pd.cut(df_train.Age, bins=[0, 5, 24, 30, 36, np.inf])
 df_test['Age_cat'] = pd.cut(df_test.Age, bins=[0, 5, 24, 30, 36, np.inf])
 df_train['Age_cat'] = LabelEncoder().fit_transform(df_train['Age_cat'])
 df_test['Age_cat'] = LabelEncoder().fit_transform(df_test['Age_cat'])
 for df in [df_train, df_test]:
df['Title'] = df.Name.str.extract(pat='([a-zA-Z]+\.) ')
 df.Title[~df.Title.isin(['Mr.', 'Miss.', 'Mrs.', 'Master.'])] = 'rare'
df_full = pd.concat([df_train, df_test], axis=0)
 df_full['Ticket_frequency'] = df_full.Ticket.map(df_full.Ticket.value_counts())
 df_train = df_full.loc[df_train.index]
df_test = df_full.loc[df_test.index].loc[df_test.index].drop('Survived', axis=1)
 for df in [df_train, df_test]:
df['Ticket_extracted'] = df.Ticket.apply(ticket_extractor)
 for i in range(len(df.Ticket)) :
try:
int(df.Ticket_extracted.iloc[i])
 df.Ticket_extracted.iloc[i] = f'Num_{len(df.Ticket_extracted.iloc[i])}'
except:
continue
for label, pattern in [('_ca', 'C[.]?A[.]?'),('_PC', 'PC'),('_SOTON', 'SOTON'),('_STON', 'STON'), 
('_WC', 'W[.]?[/]?C'),('_SC', 'S[.]?C[.]?'),('_A', 'A[.]?'),('_SOC', 'S[.]?O[.]?[/]?C'), 
('_PP', 'PP'),('_FC', '(F.C.|F.C.C.) '),('_LS_number', 'Num_(6|7)'), 
('_SS_number', 'Num_(3|4|5)'),('rare', '^[^_]')]:
df.Ticket_extracted[df.Ticket_extracted.str.contains(pattern)] = label
df_train['Family_size'] = df_train['SibSp'] + df_train['Parch'] + 1
df_test['Family_size'] = df_test['SibSp'] + df_test['Parch'] + 1
df_train['Family_size_cat'] = df_train.Family_size.replace({1:'alone', 2:'small_family', 3:'small_family', 4:'small_family'
,5:'large_family', 6:'large_family', 7:'large_family'
,8:'large_family', 9:'large_family', 10:'large_family'
,11:'large_family'})
 df_test['Family_size_cat'] = df_test.Family_size.replace({1:'alone', 2:'small_family', 3:'small_family', 4:'small_family'
,5:'large_family', 6:'large_family', 7:'large_family'
,8:'large_family', 9:'large_family', 10:'large_family'
,11:'large_family'})
 df_train['Name_length'] = df_train.Name.str.replace(pat='[^a-zA-Z]', repl='' ).str.len()
df_test['Name_length'] = df_test.Name.str.replace(pat='[^a-zA-Z]', repl='' ).str.len()
df_full = pd.concat([df_train, df_test], axis=0 ).reset_index()
df_full['Family_name'] = df_full.Name.str.split(',', n=1, expand=True ).iloc[:, 0]
df_full['Family_Survival'] = 0.5
for grp, grp_df in df_full.groupby(['Family_name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df_full.loc[df_full['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df_full.loc[df_full['PassengerId'] == passID, 'Family_Survival'] = 0
for _, grp_df in df_full.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
df_full.loc[df_full['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
df_full.loc[df_full['PassengerId'] == passID, 'Family_Survival'] = 0
df_full.set_index('PassengerId', inplace=True)
 df_train = df_full[:891]
df_test = df_full[891:]
df_train.drop(['Family_name'], axis=1, inplace=True)
 df_test.drop(['Family_name', 'Survived'], axis=1, inplace=True)
 y_train =df_train.Survived
df_train.drop('Survived', axis=1, inplace=True)
 return df_train, y_train, df_test<data_type_conversions>
if __name__ == "__main__":
seeder(CFG["seed"])
 folds = StratifiedKFold(n_splits=CFG['fold_num'] ).split(np.arange(train.shape[0]), train.label.values)
 for fold,(trn_idx, val_idx)in enumerate(folds):
if fold > 0:
break
print('Inference fold {} started'.format(fold))
test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms() , output_label=False)
 valid_ = train.loc[val_idx,:].reset_index(drop=True)
 valid_ds = CassavaDataset(valid_, '.. /input/cassava-leaf-disease-classification/train_images/', transforms=get_inference_transforms() , output_label=False)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 device = torch.device(CFG['device'])
 model = CassvaImgClassifier(CFG['model_arch'], train.label.nunique() ).to(device)
 val_preds = []
tst_preds = []
for i, epoch in enumerate([1]):
model.load_state_dict(torch.load(".. /input/aashika-cassava/tf_efficientnet_b4_ns_fold_0_9"))
with torch.no_grad() :
for _ in range(CFG["tta"]):
val_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, val_loader, device)]
tst_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
val_preds = np.mean(val_preds, axis=0)
 tst_preds = np.mean(tst_preds, axis=0)
 print('fold {} validation loss = {:.5f}'.format(fold, log_loss(valid_.label.values, val_preds)))
 print('fold {} validation accuracy = {:.5f}'.format(fold,(valid_.label.values==np.argmax(val_preds, axis=1)).mean()))
del model
torch.cuda.empty_cache()
X_train, y_train, X_test = feature_creator(train_imputed.copy() , test_imputed.copy())
 y_train = y_train.astype(int )<choose_model_class>
test['label'] = np.argmax(tst_preds, axis=1)
 test.head()
<define_search_space><EOS> 
test.to_csv('submission.csv', index=False )