Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<save_to_csv>
! pip install.. /input/tta-torch
output.to_csv("output.csv", index=False, quoting=3 )<define_variables>
! pip install.. /input/timm-package/timm-0.1.26-py3-none-any.whl
data = pd.read_csv('/kaggle/input/train_labels.csv')
 train_path = '/kaggle/input/train/'
test_path = '/kaggle/input/test/'
data['label'].value_counts()<split>
import torch
import torchvision
import skimage.io as io
import os
import numpy as np
from PIL import Image
import torch.nn as nn
import pandas as pd
import pickle
import pytorch_lightning as pl
import timm
import ttach as tta
def readImage(path):
bgr_img = cv2.imread(path)
 b,g,r = cv2.split(bgr_img)
 rgb_img = cv2.merge([r,g,b])
 return rgb_img<define_variables>
class CassavaNet(nn.Module):
def __init__(self):
super().__init__()
backbone = timm.create_model(TIMM_MODEL, pretrained=True)
 n_features = backbone.fc.in_features
self.backbone = nn.Sequential(*backbone.children())[:-2]
self.classifier = nn.Linear(n_features, 5)
 self.pool = nn.AdaptiveAvgPool2d(( 1, 1))
def forward_features(self, x):
x = self.backbone(x)
 return x
def forward(self, x):
feats = self.forward_features(x)
 x = self.pool(feats ).view(x.size(0), -1)
 x = self.classifier(x)
 return x
def load_checkpoint(filepath):
checkpoint = torch.load(filepath)
 model = checkpoint['model']
model.load_state_dict(checkpoint['state_dict'])
 for parameter in model.parameters() :
parameter.requires_grad = False
model.eval()
return model
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )
ORIGINAL_SIZE = 96
CROP_SIZE = 90
RANDOM_ROTATION = 3
RANDOM_SHIFT = 2
RANDOM_BRIGHTNESS = 7
RANDOM_CONTRAST = 5
RANDOM_90_DEG_TURN = 1
def readCroppedImage(path, augmentations = True):
bgr_img = cv2.imread(path)
 b,g,r = cv2.split(bgr_img)
 rgb_img = cv2.merge([r,g,b])
 if(not augmentations):
return rgb_img / 255
rotation = random.randint(-RANDOM_ROTATION,RANDOM_ROTATION)
 if(RANDOM_90_DEG_TURN == 1):
rotation += random.randint(-1,1)* 90
M = cv2.getRotationMatrix2D(( 48,48),rotation,1)
 rgb_img = cv2.warpAffine(rgb_img,M,(96,96))
x = random.randint(-RANDOM_SHIFT, RANDOM_SHIFT)
 y = random.randint(-RANDOM_SHIFT, RANDOM_SHIFT)
 start_crop =(ORIGINAL_SIZE - CROP_SIZE)// 2
end_crop = start_crop + CROP_SIZE
rgb_img = rgb_img[(start_crop + x):(end_crop + x),(start_crop + y):(end_crop + y)] / 255
flip_hor = bool(random.getrandbits(1))
flip_ver = bool(random.getrandbits(1))
if(flip_hor):
rgb_img = rgb_img[:, ::-1]
if(flip_ver):
rgb_img = rgb_img[::-1, :]
br = random.randint(-RANDOM_BRIGHTNESS, RANDOM_BRIGHTNESS)/ 100.
 rgb_img = rgb_img + br
cr = 1.0 + random.randint(-RANDOM_CONTRAST, RANDOM_CONTRAST)/ 100.
 rgb_img = rgb_img * cr
rgb_img = np.clip(rgb_img, 0, 1.0)
 return rgb_img<drop_column>
model_0 = load_checkpoint('.. /input/cassavamodels2/ckpt_resnet50-512-0.pth')
 model_0 = model_0.cuda()
model_1 = load_checkpoint('.. /input/cassavamodels2/ckpt_resnet50-512-0-snap.pth')
 model_1 = model_1.cuda()
model_2 = load_checkpoint('.. /input/cassavamodels2/ckpt_resnet50-512-0.pth')
 model_2 = model_2.cuda()
train_df = data.set_index('id')
 train_names = train_df.index.values
train_labels = np.asarray(train_df['label'].values)
 tr_n, tr_idx, val_n, val_idx = train_test_split(train_names, range(len(train_names)) , test_size=0.1, stratify=train_labels, random_state=123 )<define_variables>
tta_transforms = tta.Compose(
 [
tta.HorizontalFlip() ,
tta.Rotate90(angles=[0, 180]),
tta.Multiply(factors=[0.95, 1, 1.05])
 ] 
)
 merge_mode = 'mean'
tta_model_0 = tta.ClassificationTTAWrapper(model_0, tta_transforms, merge_mode = merge_mode)
 tta_model_1 = tta.ClassificationTTAWrapper(model_1, tta_transforms, merge_mode = merge_mode)
 tta_model_2 = tta.ClassificationTTAWrapper(model_2, tta_transforms, merge_mode = merge_mode )
arch = densenet169
BATCH_SIZE = 128
sz = CROP_SIZE
MODEL_PATH = str(arch ).split() [1]<create_dataframe>
database_base_path = '.. /input/cassava-leaf-disease-classification/'
n_classes = 5
files_path = f'{database_base_path}test_images/'
test_size = len(os.listdir(files_path))
test_preds = np.zeros(( test_size, n_classes))
loader = torchvision.transforms.Compose([
torchvision.transforms.Resize(512),
torchvision.transforms.CenterCrop(448),
torchvision.transforms.ToTensor() ,
torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 image_names = os.listdir(files_path)
 def image_loader(image_name):
image = Image.open(image_name)
 image = loader(image ).float()
image = image.unsqueeze(0)
 return image.cuda()
def ensembled_prediction(model_0, model_1, model_2, files_path):
labels = []
for img in os.listdir(files_path):
image = image_loader(files_path + img)
 model_0.eval()
model_1.eval()
model_2.eval()
with torch.no_grad() :
pred_0 = model_0(image.cuda())
 pred_1 = model_1(image.cuda())
 pred_2 = model_2(image.cuda())
 pred = pred_0 + pred_1 + pred_2
label = torch.argmax(pred)
 labels.append(label.item())
 return np.array(labels )
train_dict = {'name': train_path + train_names, 'label': train_labels}
df = pd.DataFrame(data=train_dict)
 test_names = []
for f in os.listdir(test_path):
test_names.append(test_path + f)
 df_test = pd.DataFrame(np.asarray(test_names), columns=['name'] )<data_type_conversions>
test_preds = ensembled_prediction(tta_model_0, tta_model_1, tta_model_2, files_path)
 test_preds
<categorify><EOS> 
submission = pd.DataFrame({'image_id': image_names, 'label': test_preds})
 submission.to_csv('submission.csv', index=False)
 display(submission.head() )