Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<define_variables>
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
sys.path.append(package_path)
 DATA_DIR = '.. /input/cassava-leaf-disease-classification'
MODEL_DIR = '.. /input/efficientnet-baseline-train-amp-aug'
list_of_cast_members = list(train['cast'].apply(lambda x: [i['name'] for i in x] if str(x)!= 'nan' else [] ).values )<define_variables>
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 
)
top_cast_members = [m[0] for m in Counter([i for j in list_of_cast_members for i in j] ).most_common(50)]<feature_engineering>
CFG = {
'fold_num': 5,
'seed': 719,
'model_arch': 'tf_efficientnet_b4_ns',
'img_size': 512,
'epochs': 10,
'train_bs': 32,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 3,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}
for g in top_cast_members:
train['cast_member_'+g] = train['cast'].apply(lambda x: 1 if g in str(x)else 0)
 for g in top_cast_members:
test['cast_member_'+g] = test['cast'].apply(lambda x: 1 if g in str(x)else 0 )<drop_column>
train = pd.read_csv(f'{DATA_DIR}/train.csv' )
train = train.drop(['cast'], axis=1)
 test = test.drop(['cast'], axis=1 )<feature_engineering>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def get_img(path):
im_bgr = cv2.imread(path)
 im_rgb = im_bgr[:, :, ::-1]
return im_rgb
train['crew'] = train['crew'].apply(lambda x: eval(x)if str(x)!= 'nan' else [])
 test['crew'] = test['crew'].apply(lambda x: eval(x)if str(x)!= 'nan' else [] )<define_variables>
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']
img = get_img(f"{self.data_root}/{self.df.loc[index]['image_id']}")
 if self.transforms:
img = self.transforms(image=img)['image']
if self.output_label:
return img, target
else:
return img
list_of_crew_members = list(train['crew'].apply(lambda x: [i['name'] for i in x] if str(x)!= 'nan' else [] ).values )<define_variables>
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
top_crew_members = [m[0] for m in Counter([i for j in list_of_crew_members for i in j] ).most_common(50)]<feature_engineering>
def inference_one_epoch(model, data_loader, device):
model.eval()
image_preds_all = []
pbar = tqdm(enumerate(data_loader), total=len(data_loader))
for step,(imgs)in pbar:
imgs = imgs.to(device ).float()
image_preds = model(imgs)
 image_preds_all += [torch.softmax(image_preds, 1 ).detach().cpu().numpy() ]
image_preds_all = np.concatenate(image_preds_all, axis=0)
 return image_preds_all
for g in top_crew_members:
train['crew_member_'+g] = train['crew'].apply(lambda x: 1 if g in str(x)else 0)
 for g in top_crew_members:
test['crew_member_'+g] = test['crew'].apply(lambda x: 1 if g in str(x)else 0 )<drop_column>
%%time
if __name__ == '__main__':
seed_everything(CFG['seed'])
 stratifiedKFold = StratifiedKFold(n_splits=CFG['fold_num'])
 folds = stratifiedKFold.split(np.arange(train.shape[0]), train.label.values)
 for fold,(trn_idx, val_idx)in enumerate(folds):
if fold > 0:
break
print(f'Inference fold {fold} started')
 valid_ = train.loc[val_idx,:].reset_index(drop=True)
 valid_ds = CassavaDataset(
 valid_, f'{DATA_DIR}/train_images/',
transforms=get_inference_transforms() , output_label=False)
 test = pd.DataFrame()
test['image_id'] = list(os.listdir(f'{DATA_DIR}/test_images/'))
test_ds = CassavaDataset(
 test, f'{DATA_DIR}/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 = CassvaImgClassifier(CFG['model_arch'], train.label.nunique() ).to(device)
 val_preds = []
tst_preds = []
for i, epoch in enumerate(CFG['used_epochs']):
model.load_state_dict(
 torch.load(f'{MODEL_DIR}/{CFG["model_arch"]}_fold_{fold}_{epoch}'))
with torch.no_grad() :
sum_weights = sum(CFG['weights'])
 for _ in range(CFG['tta']):
val_image_preds = inference_one_epoch(model, val_loader, device)
 val_preds += [CFG['weights'][i] / sum_weights / CFG['tta']*val_image_preds]
tst_image_preds = inference_one_epoch(model, tst_loader, device)
 tst_preds += [CFG['weights'][i] / sum_weights / CFG['tta']*tst_image_preds]
val_preds = np.mean(val_preds, axis=0)
 tst_preds = np.mean(tst_preds, axis=0)
 print(f'fold {fold} validation loss = {log_loss(valid_.label.values, val_preds):.5f}')
 print(f'fold {fold} validation accuracy = {(valid_.label.values==np.argmax(val_preds, axis=1)).mean() :.5f}')
 del model
torch.cuda.empty_cache()
<feature_engineering><EOS> 
test['label'] = np.argmax(tst_preds, axis=1)
 test.to_csv('submission.csv', index=False )