Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<normalization>
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
def load_image(path,pad=True, mask = False):

 flip = False
if "_gael" in path:
path = path.replace("_gael","")
 flip = True
img = cv2.imread(str(path))
if flip:
img = cv2.flip(img, 0)
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 height, width, _ = img.shape
if pad:
if height % 32 == 0:
y_min_pad = 0
y_max_pad = 0
else:
y_pad = 32 - height % 32
y_min_pad = int(y_pad / 2)
 y_max_pad = y_pad - y_min_pad
if width % 32 == 0:
x_min_pad = 0
x_max_pad = 0
else:
x_pad = 32 - width % 32
x_min_pad = int(x_pad / 2)
 x_max_pad = x_pad - x_min_pad
img = cv2.copyMakeBorder(img, y_min_pad, y_max_pad, x_min_pad, x_max_pad, cv2.BORDER_REFLECT_101)
 if mask:
img = img[:, :, 0:1] // 255
return torch.from_numpy(np.transpose(img,(2, 0, 1)).astype('float32'))
else:
img = img / 255.0
return torch.from_numpy(np.transpose(img,(2, 0, 1)).astype('float32')) 
<train_model>
from glob import glob
from sklearn.model_selection import GroupKFold, StratifiedKFold
import cv2
from skimage import io
import torch
from torch import nn
import os
from datetime import datetime
import time
import random
import cv2
import torchvision
from torchvision import transforms
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torch.utils.data.sampler import SequentialSampler, RandomSampler
from torch.cuda.amp import autocast, GradScaler
import sklearn
import warnings
import joblib
from sklearn.metrics import roc_auc_score, log_loss
from sklearn import metrics
import warnings
import cv2
import pydicom
import timm
from scipy.ndimage.interpolation import zoom
from sklearn.metrics import log_loss
def pred_resize(predictions,height,width):

 if height % 32 == 0:
y_min_pad = 0
y_max_pad = 0
else:
y_pad = 32 - height % 32
y_min_pad = int(y_pad / 2)
 y_max_pad = y_pad - y_min_pad
if width % 32 == 0:
x_min_pad = 0
x_max_pad = 0
else:
x_pad = 32 - width % 32
x_min_pad = int(x_pad / 2)
 x_max_pad = x_pad - x_min_pad
predictions = predictions[:, y_min_pad:128 - y_max_pad, x_min_pad:128 - x_max_pad]
print("Prediction Shape: ",predictions.shape)
 return predictions<concatenate>
CFG = {
'fold_num': 5,
'seed': 719,
'model_arch': 'vit_base_patch16_384',
'img_size': 384,
'epochs': 10,
'train_bs': 32,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'used_folds':[0,2,3],
'used_epochs': [7,8,9],
'tta': 3
}
def stack_and_resize(predictions,height,width):

 preds_stacked = np.vstack(predictions)[:, 0, :, :]
return pred_resize(preds_stacked,height,width )<set_options>
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head()
warnings.filterwarnings("ignore" )<load_from_csv>
train.label.value_counts()
directory = '.. /input/tgs-salt-identification-challenge'
depths_df = pd.read_csv(os.path.join(directory, 'train.csv'))
train_path = os.path.join(directory, 'train')
 test_path = os.path.join(directory, 'test')
 ids_val = _pickle.load(open(".. /input/intermediatetgs/val_index.obj","rb"))
ids_train = _pickle.load(open(".. /input/intermediatetgs/train_index.obj","rb"))
file_list = list(depths_df['id'].values)
 file_list_val = list(ids_val)
 file_list_train = list(ids_train)
 file_list_flip = [s + "_gael" for s in file_list_train]
file_list_train = file_list_train + file_list_flip
test_file_list = glob.glob(os.path.join(test_path, 'images', '*.png'))
test_file_list = [f.split('/')[-1].split('.')[0] for f in test_file_list]
print("Training Data:")
 print("Train: ",len(file_list_train))
print("Validation: ",len(file_list_val))
print("Total: ",len(file_list))
print("Testing Data:")
 print("Images: ",len(test_file_list))
dataset = TGSSaltDataset(train_path, file_list_train)
 dataset_val = TGSSaltDataset(train_path, file_list_val)
 test_dataset = TGSSaltDataset(test_path, test_file_list, is_test = True )<load_pretrained>
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()
epoch =100
learning_rate = 0.0001
loss_fn = torch.nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters() , lr=learning_rate, eps=1e-7)
 load_checkpoint(".. /input/80pytorchgamma/high_val_iou_tgs.pth", model, optimizer)
 model = train_model(model,epoch,learning_rate,
loss_fn,optimizer, dataset,
dataset_val,32
)<predict_on_test>
class CassavaDataset(Dataset):
def __init__(
 self, df, data_root, transforms=None, output_label=True 
):
super().__init__()
self.df = df.reset_index(drop=True ).copy()
self.transforms = transforms
self.data_root = data_root
self.output_label = output_label
def __len__(self):
return self.df.shape[0]
def __getitem__(self, index: int):
if self.output_label:
target = self.df.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, target
else:
return img
val_predictions = []
val_masks = []
for image, mask in tqdm_notebook(data.DataLoader(dataset_val, batch_size = 32)) :
image = Variable(image.type(torch.FloatTensor ).cuda())
 y_pred = model(image ).cpu().data.numpy()
val_predictions.append(y_pred)
 val_masks.append(mask)
 val_predictions_stacked = stack_and_resize(val_predictions,101,101)
 val_masks_stacked = stack_and_resize(val_masks,101,101 )<compute_train_metric>
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 
)
 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_inference_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),
ToTensorV2(p=1.0),
], p=1.)
metric_by_threshold = []
for threshold in np.linspace(0.3, 0.7, 50):
val_binary_prediction =(val_predictions_stacked > threshold ).astype(int)
 iou_values = []
for y_mask, p_mask in zip(val_masks_stacked, val_binary_prediction):
iou = jaccard_similarity_score(y_mask.flatten() , p_mask.flatten())
 iou_values.append(iou)
 iou_values = np.array(iou_values)
 accuracies = [
np.mean(iou_values > iou_threshold)
 for iou_threshold in np.linspace(0.5, 0.95, 10)
 ]
print('Threshold: %.2f, Metric: %.3f' %(threshold, np.mean(accuracies)))
 metric_by_threshold.append(( np.mean(accuracies), threshold))
best_metric, best_threshold = max(metric_by_threshold )<categorify>
class ViTBase16(nn.Module):
def __init__(self, n_classes, pretrained=False):
super(ViTBase16, self ).__init__()
self.model = timm.create_model(CFG['model_arch'], pretrained=pretrained)
 self.model.head = nn.Linear(self.model.head.in_features, n_classes)
 def forward(self, x):
x = self.model(x)
 return x
threshold = best_threshold
binary_prediction =(all_predictions_stacked > threshold ).astype(int)
 all_masks = []
for p_mask in list(binary_prediction):
p_mask = rle_encoding(p_mask)
 all_masks.append(' '.join(map(str, p_mask)) )<save_to_csv>
if __name__ == '__main__':
tst_preds_avg = []
seed_everything(CFG['seed'])
 for fold in CFG['used_folds']:
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)
 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 = ViTBase16(n_classes=5 ).to(device)
 tst_preds = []
for i, epoch in enumerate(CFG['used_epochs']):
model.load_state_dict(torch.load('.. /input/vit384-pytorch-weights-exp-1/{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch)))
 with torch.no_grad() :
for _ in range(CFG['tta']):
tst_preds += [1/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
tst_preds_avg.append(np.mean(tst_preds, axis=0))
del model
torch.cuda.empty_cache()
submit = pd.DataFrame([test_file_list, all_masks] ).T
submit.columns = ['id', 'rle_mask']
submit.to_csv('submit_baseline_torch.csv', index = False )<load_from_csv>
test['label'] = np.argmax(np.mean(tst_preds_avg, axis=0), axis=-1)
 test.head()
<count_missing_values><EOS> 
test.to_csv('submission.csv', index=False )