Kaggle competition: Plant Pathology 2020 - FGVC7
<SOS> metric: MCAUC Kaggle data source: plant-pathology-2020-fgvc7<set_options>
warnings.filterwarnings('ignore')
 __print__ = print
def print(string):
os.system(f'echo "{string}"')
 __print__(string )
del sales_train_validation, calendar, sell_prices
gc.collect()<filter>
DIR_INPUT = '/kaggle/input/plant-pathology-2020-fgvc7'
SEED = 42
N_FOLDS = 5
N_EPOCHS = 10
BATCH_SIZE = 64
SIZE = 512
MIN_D = 1400
sales_data = sales_data[sales_data['d'] > MIN_D]<define_variables>
class PlantModel(nn.Module):
def __init__(self, num_classes=4):
super().__init__()
self.backbone = torchvision.models.resnet18(pretrained=True)
 in_features = self.backbone.fc.in_features
self.logit = nn.Linear(in_features, num_classes)
 def forward(self, x):
batch_size, C, H, W = x.shape
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x = self.backbone.layer1(x)
 x = self.backbone.layer2(x)
 x = self.backbone.layer3(x)
 x = self.backbone.layer4(x)
 x = F.adaptive_avg_pool2d(x,1 ).reshape(batch_size,-1)
 x = F.dropout(x, 0.25, self.training)
 x = self.logit(x)
 return x
d_lgb_train = [i for i in range(1914-28)]
d_lgb_eval = [i for i in range(1914-28, 1914)]
d_test_1w = [i for i in range(1914, 1914+7)]
d_test_2w = [i for i in range(1914+7, 1914+7+7)]
d_test_3w = [i for i in range(1914+7+7, 1914+7+7+7)]
d_test_4w = [i for i in range(1914+7+7+7, 1914+7+7+7+7)]<prepare_x_and_y>
transforms_train = A.Compose([
A.RandomResizedCrop(height=SIZE, width=SIZE, p=1.0),
A.Flip() ,
A.ShiftScaleRotate(rotate_limit=1.0, p=0.8),
A.OneOf([
A.IAAEmboss(p=1.0),
A.IAASharpen(p=1.0),
A.Blur(p=1.0),
], p=0.5),
A.OneOf([
A.ElasticTransform(p=1.0),
A.IAAPiecewiseAffine(p=1.0)
 ], p=0.5),
A.Normalize(p=1.0),
ToTensorV2(p=1.0),
])
 transforms_valid = A.Compose([
A.Resize(height=SIZE, width=SIZE, p=1.0),
A.Normalize(p=1.0),
ToTensorV2(p=1.0),
] )
lgb_train_x = sales_data.loc[sales_data['d'].isin(d_lgb_train)].drop(['sales'], axis=1)
 lgb_train_y = sales_data.loc[sales_data['d'].isin(d_lgb_train)]['sales']
lgb_eval_x = sales_data.loc[sales_data['d'].isin(d_lgb_eval)].drop(['sales'], axis=1)
 lgb_eval_y = sales_data.loc[sales_data['d'].isin(d_lgb_eval)]['sales']
test_x_1w = sales_data.loc[sales_data['d'].isin(d_test_1w)].drop(['sales'], axis=1)
 test_x_2w = sales_data.loc[sales_data['d'].isin(d_test_2w)].drop(['sales'], axis=1)
 test_x_3w = sales_data.loc[sales_data['d'].isin(d_test_3w)].drop(['sales'], axis=1)
 test_x_4w = sales_data.loc[sales_data['d'].isin(d_test_4w)].drop(['sales'], axis=1)
 del sales_data
gc.collect()<init_hyperparams>
submission_df = pd.read_csv(DIR_INPUT + '/sample_submission.csv')
 submission_df.iloc[:, 1:] = 0
submission_df.head()
lgb_params = {
'objective': 'poisson',
'metric': 'rmse',
'seed': 101,
'learning_rate': 0.01,
}<train_model>
dataset_test = PlantDataset(df=submission_df, transforms=transforms_valid)
 dataloader_test = DataLoader(dataset_test, batch_size=BATCH_SIZE, num_workers=4, shuffle=False )
lgb_train = lgb.Dataset(lgb_train_x, lgb_train_y)
 lgb_eval = lgb.Dataset(lgb_eval_x, lgb_eval_y, reference=lgb_train)
 model = lgb.train(lgb_params, lgb_train, num_boost_round=10000, valid_sets=lgb_eval, early_stopping_rounds=200 )<save_model>
train_df = pd.read_csv(DIR_INPUT + '/train.csv')
 train_labels = train_df.iloc[:, 1:].values
train_y = train_labels[:, 2] + train_labels[:, 3] * 2 + train_labels[:, 1] * 3
train_df.head()
model.save_model('model.txt' )<predict_on_test>
folds = StratifiedKFold(n_splits=N_FOLDS, shuffle=True, random_state=SEED)
 oof_preds = np.zeros(( train_df.shape[0], 4))
pred_y_1w = model.predict(test_x_1w, num_iteration=model.best_iteration)
 result_1w = pd.DataFrame({'d': test_x_1w['d'], 'sales': pred_y_1w})
 test_x_2w['lag_7'] = pred_y_1w
pred_y_2w = model.predict(test_x_2w, num_iteration=model.best_iteration)
 result_2w = pd.DataFrame({'d': test_x_2w['d'], 'sales': pred_y_2w})
 test_x_3w['lag_7'] = pred_y_2w
pred_y_3w = model.predict(test_x_3w, num_iteration=model.best_iteration)
 result_3w = pd.DataFrame({'d': test_x_3w['d'], 'sales': pred_y_3w})
 test_x_4w['lag_7'] = pred_y_3w
pred_y_4w = model.predict(test_x_4w, num_iteration=model.best_iteration)
 result_4w = pd.DataFrame({'d': test_x_4w['d'], 'sales': pred_y_4w} )<concatenate>
model = PlantModel(num_classes=4 )
result = pd.concat([result_1w, result_2w, result_3w, result_4w])
 result['d'] = result['d'].map(lambda x: 'F' + str(x - 1913))
result = result.reset_index().pivot(index='id', columns='d', values='sales')
 result<save_to_csv>
class DenseCrossEntropy(nn.Module):
def __init__(self):
super(DenseCrossEntropy, self ).__init__()
def forward(self, logits, labels):
logits = logits.float()
labels = labels.float()
logprobs = F.log_softmax(logits, dim=-1)
 loss = -labels * logprobs
loss = loss.sum(-1)
 return loss.mean()
result_e = pd.DataFrame(0, index=result.index, columns=result.columns)
 result_e.index = pd.Series(result_e.index ).apply(lambda s: s.replace('validation', 'evaluation'))
pd.concat([result, result_e] ).to_csv('submission.csv' )<set_options>
def train_one_fold(i_fold, model, criterion, optimizer, dataloader_train, dataloader_valid):
train_fold_results = []
for epoch in range(N_EPOCHS):
os.system(f'echo " Epoch {epoch}"')
 model.train()
tr_loss = 0
for step, batch in enumerate(dataloader_train):
images = batch[0]
labels = batch[1]
images = images.to(device, dtype=torch.float)
 labels = labels.to(device, dtype=torch.float)
 outputs = model(images)
 loss = criterion(outputs, labels.squeeze(-1))
loss.backward()
tr_loss += loss.item()
optimizer.step()
optimizer.zero_grad()
model.eval()
val_loss = 0
val_preds = None
val_labels = None
for step, batch in enumerate(dataloader_valid):
images = batch[0]
labels = batch[1]
if val_labels is None:
val_labels = labels.clone().squeeze(-1)
 else:
val_labels = torch.cat(( val_labels, labels.squeeze(-1)) , dim=0)
 images = images.to(device, dtype=torch.float)
 labels = labels.to(device, dtype=torch.float)
 with torch.no_grad() :
outputs = model(images)
 loss = criterion(outputs, labels.squeeze(-1))
val_loss += loss.item()
preds = torch.softmax(outputs, dim=1 ).data.cpu()
if val_preds is None:
val_preds = preds
else:
val_preds = torch.cat(( val_preds, preds), dim=0)
 train_fold_results.append({
'fold': i_fold,
'epoch': epoch,
'train_loss': tr_loss / len(dataloader_train),
'valid_loss': val_loss / len(dataloader_valid),
'valid_score': roc_auc_score(val_labels, val_preds, average='macro'),
})
 return val_preds, train_fold_results
def seed_everything(seed=0):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 set_random_seed(seed)
 seed = 0
seed_everything(seed)
 %matplotlib inline
sns.set(style="whitegrid")
 warnings.filterwarnings("ignore" )<load_from_csv>
submissions = None
train_results = []
for i_fold,(train_idx, valid_idx)in enumerate(folds.split(train_df, train_y)) :
print("Fold {}/{}".format(i_fold + 1, N_FOLDS))
valid = train_df.iloc[valid_idx]
valid.reset_index(drop=True, inplace=True)
 train = train_df.iloc[train_idx]
train.reset_index(drop=True, inplace=True)
 dataset_train = PlantDataset(df=train, transforms=transforms_train)
 dataset_valid = PlantDataset(df=valid, transforms=transforms_valid)
 dataloader_train = DataLoader(dataset_train, batch_size=BATCH_SIZE, num_workers=4, shuffle=True)
 dataloader_valid = DataLoader(dataset_valid, batch_size=BATCH_SIZE, num_workers=4, shuffle=False)
 device = torch.device("cuda:0")
 model = PlantModel(num_classes=4)
 model.to(device)
 criterion = DenseCrossEntropy()
plist = [{'params': model.parameters() , 'lr': 5e-5}]
optimizer = optim.Adam(plist, lr=5e-5)
 val_preds, train_fold_results = train_one_fold(i_fold, model, criterion, optimizer, dataloader_train, dataloader_valid)
 oof_preds[valid_idx, :] = val_preds.numpy()
train_results = train_results + train_fold_results
model.eval()
test_preds = None
for step, batch in enumerate(dataloader_test):
images = batch[0]
images = images.to(device, dtype=torch.float)
 with torch.no_grad() :
outputs = model(images)
 if test_preds is None:
test_preds = outputs.data.cpu()
else:
test_preds = torch.cat(( test_preds, outputs.data.cpu()), dim=0)
 submission_df[['healthy', 'multiple_diseases', 'rust', 'scab']] = torch.softmax(test_preds, dim=1)
 submission_df.to_csv('submission_fold_{}.csv'.format(i_fold), index=False)
 if submissions is None:
submissions = test_preds / N_FOLDS
else:
submissions += test_preds / N_FOLDS
print("5-Folds CV score: {:.4f}".format(roc_auc_score(train_labels, oof_preds, average='macro')) )
train = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 test = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 print('Number of train samples: ', train.shape[0])
 print('Number of test samples: ', test.shape[0])
 train["id_code"] = train["id_code"].apply(lambda x: x + ".png")
 test["id_code"] = test["id_code"].apply(lambda x: x + ".png")
 train['diagnosis'] = train['diagnosis'].astype('str')
 display(train.head() )<init_hyperparams>
train_results = pd.DataFrame(train_results)
 train_results.head(10 )
<split><EOS> 
submission_df[['healthy', 'multiple_diseases', 'rust', 'scab']] = torch.softmax(submissions, dim=1)
 submission_df.to_csv('submission.csv', index=False )