Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<import_modules>
from sklearn.ensemble import RandomForestRegressor<choose_model_class>
!pip install.. /input/ramki-cassava-weights/vision_transformer_pytorch-1.0.2-py2.py3-none-any.whl
rf = RandomForestRegressor(n_estimators=100 )<train_model>
TRAINING = False
WEIGHT_FILE = '.. /input/ramki-cassava-weights/fold-4-weight-at-epoch-4-acc-0.95957.pth'
rf.fit(train,y_train )<predict_on_test>
import numpy as np
import pandas as pd
import os
from PIL import Image, ImageFilter
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from torch.optim import *
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split, StratifiedKFold
from torchvision import models
import time
from tqdm.auto import tqdm
import random
from torch.optim.lr_scheduler import StepLR
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import seaborn as sns
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data.sampler import WeightedRandomSampler
from sklearn.metrics import classification_report
import torchvision.models as models
from vision_transformer_pytorch import VisionTransformer
preds = rf.predict(test )<load_from_csv>
from albumentations.pytorch import ToTensorV2
from albumentations import Rotate
import albumentations as A
submission=pd.read_csv(".. /input/sampleSubmission.csv" )<prepare_output>
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 device
submission["count"] = np.expm1(preds )<save_to_csv>
if torch.cuda.is_available() :
print(torch.cuda.device_count() )
submission.to_csv("allrf.csv", index=False )<import_modules>
logs='logs/all-vti-tta-fold-'
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<load_from_csv>
SEED = 2286
N_FOLDS = 5
N_EPOCHS = 10
BATCH_SIZE = 16
IMG_SIZE = 384
LR = 1e-4
NUM_CLASSES = 5
OPTM_STEP = 1
TTA=5
EVAL_TTA_EVERY=5
sample=pd.read_csv('.. /input/sampleSubmission.csv')
 train_df=pd.read_csv('.. /input/train.csv')
 test_df=pd.read_csv('.. /input/test.csv' )<count_unique_values>
def seed_everything(seed):

 random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed_everything(SEED )
train_df.nunique()<count_unique_values>
base_path = '.. /input/cassava-leaf-disease-classification/'
test_df.nunique()<count_missing_values>
train_path =base_path + 'train_images/'
test_path = base_path + 'test_images/'
train_csv = pd.read_csv(base_path + 'train.csv')
 sample = pd.read_csv(base_path + 'sample_submission.csv' )
train_df.isnull().sum()<groupby>
train_csv_disease = train_csv.label.map({0:"Cassava Bacterial Blight(CBB)",
1:"Cassava Brown Streak Disease(CBSD)",
2:"Cassava Green Mottle(CGM)",
3:"Cassava Mosaic Disease(CMD)",
4:"Healthy"})
 diseases = train_csv_disease.value_counts()
season_df=train_df.groupby('season' )<define_variables>
class CasavaDataset(Dataset):
def __init__(self, dataframe, transforms=None, test=False):
self.df = dataframe
self.transforms = transforms
self.test = test
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
label = self.df.iloc[idx].label
p = self.df.iloc[idx].image_id
if self.test == False:
p_path = train_path + p
else:
p_path = test_path + p
image = Image.open(p_path ).convert('RGB')
 image = np.array(image)
 if self.transforms:
transformed = self.transforms(image=image)
 image = transformed['image']
return image, label
cat_feat=['season','weather','holiday','workingday']
cont_feat=['temp','atemp','humidity','windspeed']
count_feats=['casual','registered','count']<feature_engineering>
folds = StratifiedKFold(n_splits=N_FOLDS, shuffle=True, random_state=SEED)
hour=[]
day=[]
month=[]
year=[]
for row in train_df['datetime']:
date_hour=row.split()
date=date_hour[0]
hour_row=date_hour[1]
hour.append(hour_row.split(':')[0])
 date=date.split('-')
 day.append(date[2])
 month.append(date[1])
 year.append(date[0] )<feature_engineering>
class CassvaModel(nn.Module):
def __init__(self, model_arch='tf_efficientnet_b7_ns', n_class=5, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 in_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(in_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x
train_df['hour']=hour
train_df['day']=day
train_df['month']=month
train_df['year']=year<feature_engineering>
def build_model() :
if TRAINING:
model = VisionTransformer.from_pretrained('ViT-B_16', num_classes=5)
 print('model created with imagenet weights')
 else:
model = VisionTransformer.from_name('ViT-B_16', num_classes=5)
 print('model loaded from random weight')
 return model.to(device )
hour=[]
day=[]
month=[]
year=[]
for row in test_df['datetime']:
date_hour=row.split()
date=date_hour[0]
hour_row=date_hour[1]
hour.append(hour_row.split(':')[0])
 date=date.split('-')
 day.append(date[2])
 month.append(date[1])
 year.append(date[0])
 test_df['hour']=hour
test_df['day']=day
test_df['month']=month
test_df['year']=year<data_type_conversions>
testset = CasavaDataset(sample, transforms=valid_transforms, test=True)
 test_loader = DataLoader(testset, batch_size=BATCH_SIZE, shuffle=False, num_workers=1 )
datetime=['hour','day','month','year']
for time in datetime:
train_df[time]=train_df[time].astype(int)
 test_df[time]=test_df[time].astype(int )<create_dataframe>
class AverageMeter:

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
train_df=pd.DataFrame(train_df )<rename_columns>
def train_model(model, epoch, dataloader_train, criterion, optimizer):
model.train()
losses = AverageMeter()
accs = AverageMeter()
tk = tqdm(dataloader_train, total=len(dataloader_train), position=0, leave=True)
 optimizer.zero_grad()
for idx,(img, labels)in enumerate(tk):
images, labels = img.to(device), labels.to(device ).long()
predicted = model(images)
 loss = criterion(predicted, labels)
 loss.backward()
if(idx+1)% OPTM_STEP == 0:
optimizer.step()
optimizer.zero_grad()
predicted_classes = predicted.argmax(1)
 correctly_identified_sum =(predicted_classes==labels ).sum().item()
number_of_images = images.size(0)
 accs.update(correctly_identified_sum/number_of_images, number_of_images)
 losses.update(loss.item() , number_of_images)
 tk.set_postfix(loss=losses.avg,acc=accs.avg)
 return losses.avg, accs.avg
def test_model(model, dataloader_valid, criterion):
model.eval()
losses = AverageMeter()
accs = AverageMeter()
with torch.no_grad() :
tk = tqdm(dataloader_valid, total=len(dataloader_valid), position=0, leave=True)
 for idx,(images, labels)in enumerate(tk):
images, labels = images.to(device), labels.to(device ).long()
output_valid = model(images)
 loss = criterion(output_valid, labels)
 losses.update(loss.item() , images.size(0))
accs.update(( output_valid.argmax(1)==labels ).sum().item() /images.size(0),images.size(0))
tk.set_postfix(loss=losses.avg,acc=accs.avg)
 return losses.avg,accs.avg , loss
train_df.set_index('datetime',inplace=True )<rename_columns>
def tta_model(model, dataloader_tta):
model.eval()
losses = AverageMeter()
accs = AverageMeter()
pred = []
all_labels = []
with torch.no_grad() :
for tta_idx in tqdm(range(TTA)) :
avg_preds = []
for idx,(images, labels)in enumerate(dataloader_tta):
if tta_idx==0:
all_labels.extend(labels.tolist())
 images, labels = images.to(device), labels.to(device ).long()
output = model(images)
 avg_preds.append(output.softmax(1 ).to('cpu' ).detach().numpy())
 all_pred = np.concatenate(avg_preds)
 pred.append(all_pred)
 pred = np.mean(pred, axis=0)
 acc =(pred.argmax(1)==all_labels ).sum().item() /pred.shape[0]
return acc, pred
test_df.set_index('datetime',inplace=True )<drop_column>
X = train_csv.iloc[:,:-1]
y = train_csv.iloc[:,-1:]
train_df.drop(columns=['casual','registered'],axis=1,inplace=True )<categorify>
if TRAINING:
for i_fold,(train_idx, valid_idx)in enumerate(folds.split(X,y)) :
print("Fold {}/{}".format(i_fold + 1, N_FOLDS))
train = train_csv.iloc[train_idx]
train.reset_index(drop=True, inplace=True)
 valid = train_csv.iloc[valid_idx]
valid.reset_index(drop=True, inplace=True)
 unique_labels, nSamples = np.unique(train.iloc[:,-1:], return_counts =True)
 classWeights = [1 -(x / sum(nSamples)) for x in nSamples]
classWeights = torch.FloatTensor(classWeights ).to(device)
 dataset_train = CasavaDataset(train, transforms=train_transforms)
 dataset_valid = CasavaDataset(valid, transforms=valid_transforms)
 dataset_tta = CasavaDataset(valid, transforms=tta_inference_transforms)
 dataloader_train = DataLoader(dataset_train, batch_size=BATCH_SIZE, num_workers=1, shuffle=True)
 dataloader_valid = DataLoader(dataset_valid, batch_size=BATCH_SIZE, num_workers=1, shuffle=False)
 dataloader_tta = DataLoader(dataset_tta, batch_size=BATCH_SIZE, num_workers=1, shuffle=False)
 optimizer = torch.optim.AdamW(model.parameters() , lr=LR)
 criterion = nn.CrossEntropyLoss(weight=classWeights)
 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.1, \
patience=5, verbose=True, min_lr=1e-5)
 best_acc = 0
best_loss = 100
for epoch in range(1,N_EPOCHS+1):
train_loss, train_acc = train_model(model, epoch, dataloader_train, criterion, optimizer)
 val_loss, val_acc, loss = test_model(model, dataloader_valid, criterion)
 writer.add_scalar('training acc', train_acc, i_fold*epoch+epoch)
 writer.add_scalar('training loss', train_loss, i_fold*epoch+epoch)
 writer.add_scalar('validation loss', val_loss, i_fold*epoch+epoch)
 writer.add_scalar('validation Acc', val_acc, i_fold*epoch+epoch)
 writer.flush()
scheduler.step(val_acc)
 if epoch % EVAL_TTA_EVERY ==0:
tta_acc = tta_model(model, dataloader_tta)
 print("Acc -----> TTA:{} ==> Normal:{}", tta_acc,val_acc)
 if val_loss < best_loss:
best_loss = val_loss
torch.save(model.state_dict() , 'vti_weigthts/fold-{}-loss-weight-at-epoch-{}-loss-{:.5}.pth'.format(i_fold, epoch, best_loss))
if val_acc > best_acc:
best_acc = val_acc
torch.save(model.state_dict() , 'vti_weigthts/fold-{}-weight-at-epoch-{}-acc-{:.5}.pth'.format(i_fold, epoch, best_acc))
print('current_val_acc:', val_acc, 'best_val_acc:', best_acc)
 writer.close()
show_metrics(model, dataloader_train, criterion)
weather_df=pd.get_dummies(train_df['weather'],prefix='weather')
 yr_df=pd.get_dummies(train_df['year'],prefix='year')
 month_df=pd.get_dummies(train_df['month'],prefix='month')
 hour_df=pd.get_dummies(train_df['hour'],prefix='hour')
 season_df=pd.get_dummies(train_df['season'],prefix='season')
 train_df=train_df.join(weather_df)
 train_df=train_df.join(yr_df)
 train_df=train_df.join(month_df)
 train_df=train_df.join(hour_df)
 train_df=train_df.join(season_df)
 weather_df=pd.get_dummies(test_df['weather'],prefix='weather')
 yr_df=pd.get_dummies(test_df['year'],prefix='year')
 month_df=pd.get_dummies(test_df['month'],prefix='month')
 hour_df=pd.get_dummies(test_df['hour'],prefix='hour')
 season_df=pd.get_dummies(test_df['season'],prefix='season')
 test_df=test_df.join(weather_df)
 test_df=test_df.join(yr_df)
 test_df=test_df.join(month_df)
 test_df=test_df.join(hour_df)
 test_df=test_df.join(season_df )<drop_column>
model = build_model()
model.load_state_dict(torch.load(WEIGHT_FILE, map_location='cuda:0'))
model = model.to(device )
train_df.drop(columns=['season','hour','month','year','weather'],axis=1,inplace=True)
 test_df.drop(columns=['season','hour','month','year','weather'],axis=1,inplace=True )<compute_test_metric>
test_pred = []
model.eval()
tta_acc, pred = tta_model(model, test_loader)
 pred = pred.argmax(1 ).astype('int')
 test_pred.extend(pred)
 sample.label = test_pred
sample.to_csv('submission.csv',index=False )
<prepare_x_and_y><EOS> 
!cat submission.csv