Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<save_to_csv>
package_path = '.. /input/vision-transformer-pytorch/VisionTransformer-Pytorch'
sys.path.append(package_path)
sample_df = pd.read_csv(SAMPLE_SUB)
 sample_list = list(sample_df.id)
 pred_list = [p for p in preds_t]
pred_dic = dict(( key, value)for(key, value)in zip(learn.data.test_ds.fnames,pred_list))
pred_list_cor = [pred_dic[id] for id in sample_list]
df = pd.DataFrame({'id':sample_list,'label':pred_list_cor})
 df.to_csv('submission.csv'.format(MODEL_PATH), header=True, index=False)
<set_options>
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
sys.path.append(package_path )
%matplotlib inline 
<categorify>
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
class CancerDataset(Dataset):
def __init__(self, datafolder, datatype='train', transform = transforms.Compose([transforms.ToTensor() ]), labels_dict={}):
self.datafolder = datafolder
self.datatype = datatype
self.image_files_list = [s for s in os.listdir(datafolder)]
self.transform = transform
self.labels_dict = labels_dict
if self.datatype == 'train':
self.labels = [labels_dict[i.split('.')[0]] for i in self.image_files_list]
else:
self.labels = [0 for _ in range(len(self.image_files_list)) ]
def __len__(self):
return len(self.image_files_list)
 def __getitem__(self, idx):
img_name = os.path.join(self.datafolder, self.image_files_list[idx])
 image = Image.open(img_name)
 image = self.transform(image)
 img_name_short = self.image_files_list[idx].split('.')[0]
if self.datatype == 'train':
label = self.labels_dict[img_name_short]
else:
label = 0
return image, label<load_from_csv>
CFG = {
'fold_num': 10,
'seed': 719,
'model_arch': 'tf_efficientnet_b3_ns',
'img_size': 384,
'epochs': 100,
'train_bs': 28,
'valid_bs': 32,
'lr': 1e-2,
'num_workers': 10,
'accum_iter': 1,
'verbose_step': 2,
'device': 'cuda:0',
'tta': 10,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}
IMAGE_NOT_FOUND_COUNTER = 0
labels = pd.read_csv('.. /input/train_labels.csv')
 data_transforms = transforms.Compose([
transforms.RandomHorizontalFlip() ,
transforms.RandomVerticalFlip() ,
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 data_transforms_test = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5))
])
 tr, val = train_test_split(labels.label, stratify=labels.label, test_size=0.1)
 print("number of training data: ",len(tr))
print("number of testing data: ",len(val))
img_class_dict = {k:v for k, v in zip(labels.id, labels.label)}
train_sampler = SubsetRandomSampler(list(tr.index))
valid_sampler = SubsetRandomSampler(list(val.index))
batch_size = 256
num_workers = 0
dataset = CancerDataset(datafolder='.. /input/train/', datatype='train', transform=data_transforms, labels_dict=img_class_dict)
 test_set = CancerDataset(datafolder='.. /input/test/', datatype='test', transform=data_transforms_test)
 train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=train_sampler, num_workers=num_workers)
 valid_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, sampler=valid_sampler, num_workers=num_workers)
 test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, num_workers=num_workers )<concatenate>
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head()
class Flatten(nn.Module):
def forward(self, x):
N, C, H, W = x.size()
return x.view(N, -1 )<train_model>
train.label.value_counts()
avg_loss_list = []
acc_list = []
def train(model, train_loader ,loss_fn, optimizer, num_epochs = 1):
total_loss =0
for epoch in range(num_epochs):
print('Starting epoch %d / %d' %(epoch + 1, num_epochs))
model.train()
for t,(x, y)in enumerate(train_loader):
x_var = Variable(x.type(gpu_dtype))
y_var = Variable(y.type(gpu_dtype ).long())
 scores = model(x_var)
 loss = loss_fn(scores, y_var)
 total_loss += loss.data
if(t + 1)% print_every == 0:
avg_loss = total_loss/print_every
print('t = %d, avg_loss = %.4f' %(t + 1, avg_loss))
avg_loss_list.append(avg_loss)
 total_loss = 0
optimizer.zero_grad()
loss.backward()
optimizer.step()
acc = check_accuracy(fixed_model_gpu, valid_loader)
 print('acc = %f' %(acc))
def check_accuracy(model, loader):
print('Checking accuracy on test set')
 num_correct = 0
num_samples = 0
model.eval()
for x, y in loader:
x_var = Variable(x.type(gpu_dtype))
scores = model(x_var)
 _, preds = scores.data.cpu().max(1)
 num_correct +=(preds == y ).sum()
num_samples += preds.size(0)
 acc = float(num_correct)/ num_samples
acc_list.append(acc)
 return acc
print('Got %d / %d correct(%.2f)' %(num_correct, num_samples, 100 * acc))<define_search_model>
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()
print_every = 100
gpu_dtype = torch.cuda.FloatTensor
out_1 = 32
out_2 = 64
out_3 = 128
out_4 = 256
k_size_1 = 3
padding_1 = 1
num_epochs = 10
fixed_model_base = nn.Sequential(
 nn.Conv2d(3, out_1, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_1),
nn.Conv2d(out_1 , out_1, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_1),
nn.Conv2d(out_1 , out_1, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_1),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(out_1 , out_2, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_2),
nn.Conv2d(out_2 , out_2, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_2),
nn.Conv2d(out_2 , out_2, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_2),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(out_2 , out_3, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_3),
nn.Conv2d(out_3 , out_3, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_3),
nn.Conv2d(out_3 , out_3, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_3),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(out_3 , out_4, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_4),
nn.Conv2d(out_4 , out_4, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_4),
nn.Conv2d(out_4 , out_4, padding= padding_1, kernel_size=k_size_1, stride=1),
nn.ReLU(inplace=True),
nn.BatchNorm2d(out_4),
nn.MaxPool2d(2, stride=2),
Flatten() ,
nn.Linear(9216,512),
nn.ReLU(inplace=True),
nn.Linear(512,10),
nn.ReLU(inplace=True),
nn.Linear(10,2), 
)
 fixed_model_gpu = fixed_model_base.type(gpu_dtype)
 print(fixed_model_gpu)
 loss_fn = nn.modules.loss.CrossEntropyLoss()
optimizer = optim.Adamax(fixed_model_gpu.parameters() , lr = 1e-3)
 train(fixed_model_gpu, train_loader ,loss_fn, optimizer, num_epochs=num_epochs)
 check_accuracy(fixed_model_gpu, valid_loader )<compute_test_metric>
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
print(avg_loss_list,acc_list )<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 
)
 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.)
fixed_model_gpu.eval()
preds = []
for batch_i,(data, target)in enumerate(test_loader):
data, target = data.cuda() , target.cuda()
output = fixed_model_gpu(data)
 pr = output[:,1].detach().cpu().numpy()
for i in pr:
preds.append(i)
 test_preds = pd.DataFrame({'imgs': test_set.image_files_list, 'preds': preds})
 test_preds['imgs'] = test_preds['imgs'].apply(lambda x: x.split('.')[0])
 data_to_submit = pd.read_csv('.. /input/sample_submission.csv')
 data_to_submit = pd.merge(data_to_submit, test_preds, left_on='id', right_on='imgs')
 data_to_submit = data_to_submit[['id', 'preds']]
data_to_submit.columns = ['id', 'label']
data_to_submit.head()<save_to_csv>
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
class EnsembleClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model1 = VisionTransformer.from_name('ViT-B_16', num_classes=5)
 self.model1.load_state_dict(torch.load('.. /input/vit-model-1/ViT-B_16.pt'))
self.model2 = CassvaImgClassifier(model_arch, n_class, pretrained)
 def forward(self, x):
x1 = self.model1(x)
 x2 = self.model2(x)
 return 0.6 * x1 + 0.4 * x2
def load(self, state_dict):
self.model2.load_state_dict(state_dict )
data_to_submit.to_csv('csv_to_submit.csv', index = False )<import_modules>
if __name__ == '__main__':
seed_everything(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))
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)
 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)
 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 = EnsembleClassifier(CFG['model_arch'], train.label.nunique() ).to(device)
 val_preds = []
tst_preds = []
for i, epoch in enumerate(CFG['used_epochs']):
model.load(torch.load('.. /input/fork-pytorch-efficientnet-baseline-train-amp-a/{}_fold_{}_{}'.format(CFG['model_arch'], fold, epoch)))
 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()
Conv2D, SeparableConv1D, Add, BatchNormalization, Activation, GlobalAveragePooling2D, LeakyReLU, Flatten
Lambda, Multiply, LSTM, Bidirectional, PReLU, MaxPooling1D
print(os.listdir(".. /input")) 
<load_from_csv>
test['label'] = np.argmax(tst_preds, axis=1)
 test.head()
<define_variables><EOS> 
test.to_csv('submission.csv', index=False )