Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<load_pretrained>
Path.ls = lambda x: list(x.iterdir())
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 effnet_path = '.. /input/efficientnet-pytorch/'
sys.path.append(effnet_path)
 test_path = Path(".. /input/cassava-leaf-disease-classification/test_images")
 test_fnames = test_path.ls()
def get_base() :
layers = cut_model(f(True), cut)
 return nn.Sequential(*layers)
 def load_pretrained(model, path):
weights = torch.load(PRETRAINED, map_location=lambda storage, loc: storage)
 model.load_state_dict(weights, strict=False)
 return model<categorify>
test_df = pd.read_csv(".. /input/cassava-leaf-disease-classification/sample_submission.csv")
 train_df = pd.read_csv(".. /input/cassava-leaf-disease-classification/train.csv")
 num_classes = train_df['label'].nunique()
class SaveFeatures() :
features=None
def __init__(self, m): self.hook = m.register_forward_hook(self.hook_fn)
 def hook_fn(self, module, input, output): self.features = output
def remove(self): self.hook.remove()<concatenate>
mean = [0.4589, 0.5314, 0.3236]
std = [0.2272, 0.2297, 0.2200]
test_tfms = albumentations.Compose([
albumentations.RandomResizedCrop(256, 256),
albumentations.HorizontalFlip(p=0.5),
albumentations.HueSaturationValue(
 hue_shift_limit=0.2,
sat_shift_limit=0.2,
val_shift_limit=0.2,
p=0.5 
),
albumentations.RandomBrightnessContrast(
 brightness_limit=(-0.1,0.1),
contrast_limit=(-0.1, 0.1),
p=0.5 
),
albumentations.Normalize(
 mean=mean,
std=std,
max_pixel_value=255.0,
p=1.0 
),
ToTensorV2() ], p=1.)
class LeafDataTest(Dataset):
def __init__(self, df, split="test"):
self.transforms = test_tfms
self.paths = [test_path/id_ for id_ in df['image_id'].values]
def __getitem__(self, idx):
img = cv2.imread(str(self.paths[idx])) [..., ::-1]
img = self.transforms(image=img)['image']
return img
def __len__(self):
return len(self.paths)
 def make_dataloaders(batch_size=32, num_workers=4, pin_memory=True, **kwargs):
dataset = LeafDataTest(**kwargs)
 dataloader = DataLoader(dataset, batch_size=batch_size, num_workers=num_workers,
pin_memory=pin_memory, shuffle=True if kwargs['split'] == "train" else False)
 return dataloader
class UnetBlock(nn.Module):
def __init__(self, up_in, x_in, n_out):
super().__init__()
up_out = x_out = n_out//2
self.x_conv = nn.Conv2d(x_in, x_out, 1)
 self.tr_conv = nn.ConvTranspose2d(up_in, up_out, 2, stride=2)
 self.bn = nn.BatchNorm2d(n_out)
 def forward(self, up_p, x_p):
up_p = self.tr_conv(up_p)
 x_p = self.x_conv(x_p)
 cat_p = torch.cat([up_p,x_p], dim=1)
 return self.bn(F.relu(cat_p))<choose_model_class>
test_dl = make_dataloaders(df=test_df, split="test" )
class UnetModel() :
def __init__(self,model,name='unet'):
self.model,self.name = model,name
def get_layer_groups(self, precompute):
lgs = list(split_by_idxs(children(self.model.rn), [lr_cut]))
return lgs + [children(self.model)[1:]]<prepare_x_and_y>
class EfficientNetModel(nn.Module):
def __init__(self, arch="b4", dropout=0.2, n_out=5,
pretrained=True, freeze=True):
super().__init__()
if pretrained:
self.model = EfficientNet.from_pretrained(f"efficientnet-{arch}")
 if freeze:
for p in self.model.parameters() :
p.requires_grad = False
else:
self.model = EfficientNet.from_name(f"efficientnet-{arch}")
 self.lin1 = nn.Linear(1792 * 2, 512)
 self.lin2 = nn.Linear(512, n_out)
 self.bn1 = nn.BatchNorm1d(1792 * 2)
 self.bn2 = nn.BatchNorm1d(512)
 self.dropout = dropout
def forward(self, x):
x = self.model.extract_features(x)
 avg = F.adaptive_avg_pool2d(x, 1)
 max_ = F.adaptive_max_pool2d(x, 1)
 cat = torch.cat(( avg.view(x.size(0), -1), max_.view(x.size(0), -1)) , dim=1)
 x = self.bn1(cat)
 x = F.dropout(x, self.dropout)
 x = F.relu(self.bn2(self.lin1(x)))
 x = self.lin2(x)
 return x
x_names = [f'{x}.png' for x in train_names]
x_names_path = np.array([str(TRAIN_DN/x)for x in x_names])
 y_names = [x for x in x_names]
y_names_path = np.array([str(MASKS_DN/x)for x in x_names] )<set_options>
model = model = EfficientNetModel(pretrained=False, freeze=False ).to(device)
 model.load_state_dict(torch.load(".. /input/pytorch-better-normalization-onecycle-lr-train/effnet.pt", map_location=device))
model.eval() ;
aug_tfms = [RandomRotate(4, tfm_y=TfmType.CLASS),
RandomFlip(tfm_y=TfmType.CLASS),
RandomLighting(0.05, 0.05, tfm_y=TfmType.CLASS)] 
<split>
def inference_one_pass(model, test_dl):
model.eval()
all_preds = []
with torch.no_grad() :
for batch in tqdm(test_dl):
preds = model(batch.to(device))
all_preds.append(preds)
 return torch.cat(all_preds, dim=0 )
lr=3e-3
wd=1e-7
lrs = np.array([lr/100,lr/10,lr])
 n_folds = 8
out=np.zeros(( 18000,sz,sz))
alpha = 0
for i in range(n_folds):
val_size = 4000//n_folds
val_idxs=list(range(i*val_size,(i+1)*val_size)) 
(( val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs, x_names_path, y_names_path)
 test_x = np.array(test_names_png)
 tfms = tfms_from_model(resnet34, sz=sz, pad=0, crop_type=CropType.NO, tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)
 datasets = ImageData.get_ds(CustomDataset,(trn_x,trn_y),(val_x,val_y), tfms,(test_x, test_x), path=PATH)
 md = ImageData(PATH, datasets, bs=64, num_workers=nw, classes=None)
 denorm = md.trn_ds.denorm
f = resnet34
cut,lr_cut = model_meta[f]
m_base = load_pretrained(get_base() ,PRETRAINED)
 m = to_gpu(Unet34(m_base))
models = UnetModel(m)
 learn = ConvLearner(md, models, tmp_name=TMP, models_name=MODEL)
 learn.opt_fn=optim.Adam
learn.crit = lovasz_hinge
learn.metrics=[accuracy_thresh(0.5),dice, IoU]
learn.freeze_to(2)
 learn.fit(lr,2,wds=wd,cycle_len=10,use_clr_beta=(10,10, 0.85, 0.9))
learn.unfreeze()
learn.fit(lrs, 3, wds=wd, cycle_len=10,use_clr_beta=(10,10, 0.85, 0.9))
print(f'computing test set: {i}')
 out+=learn.predict(is_test=True)
 print('Computing optimal threshold')
 preds, targs = learn.predict_with_targs()
IoUs=[]
for a in np.arange(0, 1, 0.1):
IoUs.append(IoU_np(preds, targs, a))
IoU_max = np.array(IoUs ).argmax()
print(f'optimal Threshold: {IoU_max/10.0}')
 alpha+=IoU_max/10.0<feature_engineering>
num_passes = 5
tta = None
for _ in range(num_passes):
all_preds = inference_one_pass(model, test_dl)
 if tta is None:
tta = all_preds
else:
tta += all_preds
tta /= float(num_passes)
 label_preds = tta.argmax(dim=1 )
out = out/n_folds
alpha = alpha/n_folds<categorify>
test_df['label'] = label_preds.cpu().numpy()
<create_dataframe><EOS> 
test_df.to_csv("submission.csv", index=False )