Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<save_to_csv>
!pip install.. /input/timm034/timm-0.3.4-py3-none-any.whl
imgDataBunch.export(fname='./export.pkl' )<categorify>
import time
import os
import timm
import numpy as np
import pandas as pd
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.sampler import SubsetRandomSampler, RandomSampler, SequentialSampler
import albumentations
from albumentations.pytorch import ToTensorV2
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm
sz = 68
def imageToTensorImage(path):
bgr_img = cv2.imread(path)
 b,g,r = cv2.split(bgr_img)
 rgb_img = cv2.merge([r,g,b])
 H,W,C = rgb_img.shape
rgb_img = rgb_img[(H-sz)//2:(sz +(H-sz)//2),(H-sz)//2:(sz +(H-sz)//2),:] / 256
return vision.Image(px=pil2tensor(rgb_img, np.float32))
img = imageToTensorImage('/kaggle/input/test/0eb051700fb6b1bf96188f36c8e4889598c6a157.tif')
 img.show(y=learner.predict(img)[0])
 classes = ['negative', 'tumor']
print('This is a ' + classes[int(learner.predict(img)[0])] + ' tissue image.' )<install_modules>
image_folder = '.. /input/cassava-leaf-disease-classification/test_images/'
enet_type = 'cspresnext50'
image_size = 512
batch_size = 4
num_workers = 4
out_dim = 5
device = torch.device('cuda' )
!pip install fastai==0.7.0 --no-deps
!pip install torch==0.4.1 torchvision==0.2.1<import_modules>
ls.. /input/cdl-cspresnext50-512/
from fastai.conv_learner import *
from fastai.dataset import *
import pandas as pd
import numpy as np
import os
from sklearn.model_selection import train_test_split 
<define_variables>
model_pths = [
'.. /input/cdl-cspresnext50-512/light_best_model_fold0.pth',
'.. /input/cdl-cspresnext50-512/light_best_model_fold1.pth',
'.. /input/cdl-cspresnext50-512/light_best_model_fold2.pth',
'.. /input/cdl-cspresnext50-512/light_best_model_fold3.pth',
'.. /input/cdl-cspresnext50-512/light_best_model_fold4.pth',
]
MODEL_PATH = 'Dn121_v1'
TRAIN = '.. /input/train/'
TEST = '.. /input/test/'
LABELS = '.. /input/train_labels.csv'
SAMPLE_SUB = '.. /input/sample_submission.csv'
ORG_SIZE=96
BATCH_SIZE = 128<choose_model_class>
class net(nn.Module):
def __init__(self, model_name=enet_type, pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=pretrained)
 n_features = self.model.head.fc.in_features
self.model.head.fc = nn.Linear(n_features, 5)
 def forward(self, x):
output = self.model(x)
 return output
arch = dn121
nw = 4<split>
class LEAFDataset(Dataset):
def __init__(self, folder, transforms=None):
self.file_names = os.listdir(folder)
 self.transforms = transforms
def __len__(self):
return len(self.file_names)
 def __getitem__(self, index):
image_id = self.file_names[index]
image_file = os.path.join(image_folder, image_id)
 image = cv2.imread(image_file)
 image = image[:, :, ::-1]
if self.transforms is not None:
image = self.transforms(image=image)['image']
return image, image_id
train_df = pd.read_csv(LABELS ).set_index('id')
 train_names = train_df.index.values
train_labels = np.asarray(train_df['label'].values)
 print("Number of positive samples = {:.4f}%".format(np.count_nonzero(train_labels)*100/len(train_labels)))
 test_names = [f.replace(".tif","")for f in os.listdir(TEST)]
tr_n, val_n = train_test_split(train_names, test_size=0.15, random_state=42069)
 print(len(tr_n), len(val_n))<prepare_x_and_y>
transform = albumentations.Compose([
albumentations.Resize(image_size, image_size),
albumentations.Normalize() ,
ToTensorV2()
] )
class HCDDataset(FilesDataset):
def __init__(self, fnames, path, transform):
self.train_df = train_df
super().__init__(fnames, transform, path)
 def get_x(self, i):
img = open_image(os.path.join(self.path, self.fnames[i]+".tif"))
img = img[(ORG_SIZE-self.sz)//2:(ORG_SIZE+self.sz)//2,(ORG_SIZE-self.sz)//2:(ORG_SIZE+self.sz)//2,:]
return img
def get_y(self, i):
if(self.path == TEST): return 0
return self.train_df.loc[self.fnames[i]]['label']
def get_c(self):
return 2 
<create_dataframe>
res = []
for model_pth in model_pths:
model = net(enet_type)
 model.load_state_dict(torch.load(model_pth))
model.eval()
model.to(device)
 test_dataset = LEAFDataset(image_folder, transforms=transform)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, num_workers=num_workers)
 single_model_probs = []
image_ids_list = []
with torch.no_grad() :
for idx,(images, image_ids)in enumerate(tqdm(test_loader)) :
outputs = model(images.to(device))
pred = outputs.detach().softmax(1 ).cpu().numpy()
single_model_probs.append(pred)
 image_ids_list.append(image_ids)
 single_model_probs = np.concatenate(single_model_probs)
 image_ids_list = np.concatenate(image_ids_list)
 res.append(single_model_probs)
 del model
torch.cuda.empty_cache()
res = sum(res)/ len(model_pths)
 probs = res.argmax(1 )
def get_data(sz, bs):
aug_tfms = [RandomRotate(20, tfm_y=TfmType.NO),
RandomDihedral(tfm_y=TfmType.NO)]
tfms = tfms_from_model(arch, sz, crop_type=CropType.NO, tfm_y=TfmType.NO,
aug_tfms=aug_tfms)
 ds = ImageData.get_ds(HCDDataset,(tr_n[:-(len(tr_n)% bs)], TRAIN), 
(val_n, TRAIN), tfms, test=(test_names, TEST))
md = ImageData("./", ds, bs, num_workers=nw, classes=None)
 return md 
<load_pretrained>
sub = pd.DataFrame({'image_id': image_ids_list, 'label': probs});sub.head()
md = get_data(96, BATCH_SIZE)
 learn = ConvLearner.pretrained(arch, md)
 learn.opt_fn = optim.Adam<train_model>
sub.to_csv('submission.csv', index=False )
<predict_on_test><EOS> 
sub.to_csv('submission.csv', index=False )