Kaggle competition: Cassava Leaf Disease Classification
best_degree = pd.DataFrame()
for place in result.place.unique() :
a = result[result['place']==place]
best_degree = best_degree.append(a[a['RMSLE'] == a['RMSLE'].min() ])
 print(best_degree.groupby('degree')['place'].nunique())
 print('Zero polynomial(no fit): ',best_degree[best_degree['RMSLE']<0.00001]['place'].unique() )<count_unique_values>

 class CassavaDataset(Dataset):
def __init__(self, data, targets, dataset, transform=None):

 self.files = data
self.targets = targets
self.classes = list(set(targets))
self.transform = transform
self.dataset = dataset
def __len__(self):
return len(self.files)
 def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
name = self.files[idx]
img_name = os.path.join(name)
 image = Image.open(img_name)

input_size = 384
imagenet_stats =([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 if self.dataset == 'train':
transform = transforms.Compose([transforms.RandomResizedCrop(( input_size, input_size)) ,
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.ToTensor() ,
transforms.Normalize(*imagenet_stats)])
 image = transform(image)
 elif self.dataset == 'test':
transform = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.ToTensor() ,
transforms.Normalize(*imagenet_stats)])
 image = transform(image)

label = self.targets[idx]
if self.transform:
sample = self.transform(sample)
 return image, label
fit_best_degree = best_degree[best_degree['RMSLE']>0.00001]
twodeg_places = fit_best_degree[fit_best_degree['degree']==2]['place'].unique()
threedeg_places = fit_best_degree[fit_best_degree['degree']==3]['place'].unique()
fourdeg_places = fit_best_degree[fit_best_degree['degree']==4]['place'].unique()
fivedeg_places = fit_best_degree[fit_best_degree['degree']==5]['place'].unique()
nofit_places1 = best_degree[best_degree['RMSLE']<0.00001]['place'].unique()
print(fit_best_degree.nunique())
 print(len(twodeg_places), len(threedeg_places),
len(fourdeg_places), len(fivedeg_places), len(nofit_places1))<feature_engineering>

 dfx = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 df_train, df_valid = model_selection.train_test_split(dfx, test_size=0.1, random_state=42, stratify=dfx.label.values)
 _train = df_train.reset_index(drop=True)
 df_valid = df_valid.reset_index(drop=True)
 image_path = ".. /input/cassava-leaf-disease-classification/train_images/"
train_image_paths = [os.path.join(image_path, x)for x in df_train.image_id.values]
valid_image_paths = [os.path.join(image_path, x)for x in df_valid.image_id.values]
train_targets = df_train.label.values
valid_targets = df_valid.label.values
XYtest = XYtest.reset_index(drop=True)
 XYtest['intercept'] = -1<choose_model_class>

 cassava_train = CassavaDataset(train_image_paths, train_targets, 'train')
 cassava_test = CassavaDataset(valid_image_paths, valid_targets, 'test')
 batch_size = 16
train_loader = DataLoader(cassava_train, batch_size=batch_size, shuffle=False, num_workers=2)
 test_loader = DataLoader(cassava_test, batch_size=batch_size, shuffle=False, num_workers=2 )
poly_predicted_confirmedcases = pd.DataFrame()
for place in twodeg_places:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['ConfirmedCases']
Xtest = XYtest[XYtest['place']==place][['label','intercept']]
model = make_pipeline(PolynomialFeatures(2), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(Xtest))
a = pd.DataFrame(zip(XYtrain[XYtrain['place']==place]['place'],
y_pred),columns=['place','ConfirmedCases'])
 poly_predicted_confirmedcases = poly_predicted_confirmedcases.append(a)
 for place in threedeg_places:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['ConfirmedCases']
Xtest = XYtest[XYtest['place']==place][['label','intercept']]
model = make_pipeline(PolynomialFeatures(3), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(Xtest))
b = pd.DataFrame(zip(XYtrain[XYtrain['place']==place]['place'],
y_pred.tolist()),columns=['place','ConfirmedCases'])
 poly_predicted_confirmedcases = poly_predicted_confirmedcases.append(b)
 for place in fourdeg_places:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['ConfirmedCases']
Xtest = XYtest[XYtest['place']==place][['label','intercept']]
model = make_pipeline(PolynomialFeatures(4), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(Xtest))
c = pd.DataFrame(zip(XYtrain[XYtrain['place']==place]['place'],
y_pred.tolist()),columns=['place','ConfirmedCases'])
 poly_predicted_confirmedcases = poly_predicted_confirmedcases.append(c)
 for place in fivedeg_places:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['ConfirmedCases']
Xtest = XYtest[XYtest['place']==place][['label','intercept']]
model = make_pipeline(PolynomialFeatures(5), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(Xtest))
d = pd.DataFrame(zip(XYtrain[XYtrain['place']==place]['place'],
y_pred.tolist()),columns=['place','ConfirmedCases'])
 poly_predicted_confirmedcases = poly_predicted_confirmedcases.append(d)
<create_dataframe>

 class AverageMeter:

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output, target, topk=(1,)) :

 maxk = max(topk)
 batch_size = target.size(0)
 _, pred = output.topk(maxk, 1, True, True)
 pred = pred.t()
correct = pred.eq(target.reshape(1, -1 ).expand_as(pred))
return [correct[:k].reshape(-1 ).float().sum(0)* 100./ batch_size for k in topk]
fatalities_result=pd.DataFrame()
for place in poly_data.place.unique() :
for degree in [2,3,4,5]:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['Fatalities']
model = make_pipeline(PolynomialFeatures(degree), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(features))
rmsle = RMSLE(y_pred, target)
 fatalities_result = fatalities_result.append(pd.DataFrame({'place':[place],
'degree':[degree],'RMSLE': [rmsle]}))<count_unique_values>

 def train_epoch(model, loader, device, loss_func, optimizer, scheduler):
model.train()
summary_loss = AverageMeter()
summary_acc = AverageMeter()
start = time.time()
n = len(loader)
 for batch in tqdm(loader):
images, labels = batch
images = images.to(device)
 labels = labels.to(device)
 out = model(images)
 loss = loss_func(out, labels)
 loss.backward()
optimizer.step()
optimizer.zero_grad()
with torch.no_grad() :
acc = accuracy(out, labels)[0]
summary_loss.update(loss.detach().item() , batch_size)
 summary_acc.update(acc.detach().item() , batch_size)
 train_time = str(datetime.timedelta(seconds=time.time() - start))
print('Train loss: {:.5f} - Train acc: {:.2f}% - time: {}'.format(summary_loss.avg,
summary_acc.avg,
train_time))
return summary_loss, summary_acc
fat_best_degree = pd.DataFrame()
for place in fatalities_result.place.unique() :
a = fatalities_result[fatalities_result['place']==place]
fat_best_degree = fat_best_degree.append(a[a['RMSLE'] == a['RMSLE'].min() ])
 print(fat_best_degree.groupby('degree')['place'].nunique())
 print('Zero polynomial(no fit): ',
fat_best_degree[fat_best_degree['RMSLE']<0.000001]['place'].unique() )<count_unique_values>

 resnet = timm.create_model('resnext50_32x4d', pretrained=True)
 num_ftrs = resnet.fc.in_features
resnet.fc = nn.Linear(num_ftrs, 5)
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 resnet.to(device )
fit_best_degree = fat_best_degree[fat_best_degree['RMSLE']>0.000001]
twodeg_places = fit_best_degree[fit_best_degree['degree']==2]['place'].unique()
threedeg_places = fit_best_degree[fit_best_degree['degree']==3]['place'].unique()
fourdeg_places = fit_best_degree[fit_best_degree['degree']==4]['place'].unique()
fivedeg_places = fit_best_degree[fit_best_degree['degree']==5]['place'].unique()
nofit_places2 = fat_best_degree[fat_best_degree['RMSLE']<0.000001]['place'].unique()
print(fit_best_degree.nunique())
 print(len(twodeg_places), len(threedeg_places),
len(fourdeg_places), len(fivedeg_places), len(nofit_places2))<choose_model_class>

 num_epochs = 1
best_acc = 0
best_epoch = 0
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(resnet.parameters() , lr=0.01, momentum=0.9)
 scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.2, patience=2, verbose=True, eps=1e-6)
 for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch + 1, num_epochs))
train_loss, train_acc = train_epoch(resnet, train_loader, device, criterion, optimizer, scheduler)
 val_loss, val_acc = validate_epoch(resnet, test_loader, device, criterion)
 scheduler.step(val_loss.avg)
 if val_acc.avg > best_acc:
best_acc = val_acc.avg
best_epoch = epoch
if epoch == 9:
print('Saving model...')
 PATH = './timm_resnext_epoch{}_384.pth'.format(epoch + 1)
 torch.save(resnet.state_dict() ,PATH )
poly_predicted_fatalities = pd.DataFrame()
for place in twodeg_places:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['Fatalities']
Xtest = XYtest[XYtest['place']==place][['label','intercept']]
model = make_pipeline(PolynomialFeatures(2), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(Xtest))
a = pd.DataFrame(zip(XYtrain[XYtrain['place']==place]['place'],
y_pred.tolist()),columns=['place','Fatalities'])
 poly_predicted_fatalities = poly_predicted_fatalities.append(a)
 for place in threedeg_places:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['Fatalities']
Xtest = XYtest[XYtest['place']==place][['label','intercept']]
model = make_pipeline(PolynomialFeatures(3), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(Xtest))
b = pd.DataFrame(zip(XYtrain[XYtrain['place']==place]['place'],
y_pred.tolist()),columns=['place','Fatalities'])
 poly_predicted_fatalities = poly_predicted_fatalities.append(b)
 for place in fourdeg_places:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['Fatalities']
Xtest = XYtest[XYtest['place']==place][['label','intercept']]
model = make_pipeline(PolynomialFeatures(4), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(Xtest))
c = pd.DataFrame(zip(XYtrain[XYtrain['place']==place]['place'],
y_pred.tolist()),columns=['place','Fatalities'])
 poly_predicted_fatalities = poly_predicted_fatalities.append(c)
 for place in fivedeg_places:
features = XYtrain[XYtrain['place']==place][['label','intercept']]
target = XYtrain[XYtrain['place']==place]['Fatalities']
Xtest = XYtest[XYtest['place']==place][['label','intercept']]
model = make_pipeline(PolynomialFeatures(5), Ridge())
 model.fit(np.array(features), target)
 y_pred = model.predict(np.array(Xtest))
d = pd.DataFrame(zip(XYtrain[XYtrain['place']==place]['place'],
y_pred.tolist()),columns=['place','Fatalities'])
 poly_predicted_fatalities = poly_predicted_fatalities.append(d)
<create_dataframe>

 num_epochs = 10
best_acc = 0
best_epoch = 0
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(xception.parameters() , lr=0.01, momentum=0.9)
 scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.2, patience=2, verbose=True, eps=1e-6)
 for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch + 1, num_epochs))
train_loss, train_acc = train_epoch(xception, train_loader, device, criterion, optimizer, scheduler)
 val_loss, val_acc = validate_epoch(xception, test_loader, device, criterion)
 scheduler.step(val_loss.avg)
 if val_acc.avg > best_acc:
best_acc = val_acc.avg
best_epoch = epoch
if epoch == 9:
print('Saving model...')
 PATH = './timm_xception_epoch{}_384.pth'.format(epoch + 1)
 torch.save(xception.state_dict() ,PATH )
for place in nofit_places1:
e = poly_data[(poly_data['place']==place)&(poly_data['date']>'2020-03-11')]
f = e['ConfirmedCases'].fillna(method = 'ffill')
 g = pd.DataFrame(zip(e['place'], f),columns=['place','ConfirmedCases'])
 poly_predicted_confirmedcases = poly_predicted_confirmedcases.append(g)
 for place in nofit_places2:
h = poly_data[(poly_data['place']==place)&(poly_data['date']>'2020-03-11')]
i = h['Fatalities'].fillna(method = 'ffill')
 j = pd.DataFrame(zip(h['place'], i),columns=['place','Fatalities'])
 poly_predicted_fatalities = poly_predicted_fatalities.append(j )<prepare_output>

 PATH = './timm_resnext_epoch10_384.pth'
resnet = timm.create_model('resnext50_32x4d', pretrained=False)
 num_ftrs = resnet.fc.in_features
resnet.fc = nn.Linear(num_ftrs, 5)
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 resnet.to(device)
 resnet.load_state_dict(torch.load(PATH))
resnet.eval()
poly_predicted_confirmedcases2= pd.DataFrame({'date':XYtest.date,
'place':poly_predicted_confirmedcases['place'].tolist() ,
'ConfirmedCases':poly_predicted_confirmedcases['ConfirmedCases'].tolist() })
 poly_predicted_confirmedcases2.head()<prepare_output>

 submission_df = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission_df.head()
poly_predicted_fatalities2= pd.DataFrame({'date':XYtest.date,
'place':poly_predicted_fatalities['place'].tolist() ,
'Fatalities':poly_predicted_fatalities['Fatalities'].tolist() })
 poly_predicted_fatalities2.head()<merge>

 input_size = 384
stats =([0.4914, 0.4822, 0.4465], [0.247, 0.243, 0.261])
 trans1 = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.Pad(8, padding_mode='reflect'),
transforms.ToTensor() ,
transforms.Normalize(*stats)])
 trans2 = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.RandomHorizontalFlip(p=0.3),
transforms.RandomResizedCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize(*stats)])
 trans3 = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.RandomVerticalFlip(p=0.3),
transforms.RandomResizedCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize(*stats)])
 trans4 = transforms.Compose([transforms.Resize(( input_size, input_size)) ,
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.RandomResizedCrop(input_size),
transforms.ToTensor() ,
transforms.Normalize(*stats)])
 transs = [trans1, trans2, trans3, trans4]
poly_compiled = poly_predicted_confirmedcases2.merge(poly_predicted_fatalities2, how='inner', on=['place','date'] )<merge>

 test_path = '/kaggle/input/cassava-leaf-disease-classification/test_images/'
test_images = os.listdir(test_path)
 train_image_paths = [os.path.join(test_path, x)for x in test_images]
y_preds = []
y2_preds = []
p = 0
for i in test_images:
res = []
image = Image.open(f'/kaggle/input/cassava-leaf-disease-classification/test_images/{i}')
 input_size = 384
outs = torch.Tensor(np.zeros(( len(transs), 5)))
 outs2 = torch.Tensor(np.zeros(( len(transs), 5)))
 k = 0
for trans in transs:
img = trans(image)
 img = img.reshape(1, img.shape[0], img.shape[1], img.shape[2])
 img = Variable(img.to(device))
out = resnet(img)
 out2= xception(img)
 outs[k,:] = 4*out
outs2[k,:] = 5*out2
k += 1
out = outs.mean(axis=0)
 out2 = outs2.mean(axis=0)
 res.append(out2)
 mean = torch.mean(torch.stack(res), dim = 0)
 _, predicted = torch.max(out2.data, 0)
 y_preds.append(predicted.item() )
test_poly_compiled= test.merge(poly_compiled, how='inner', on=['place','date'])
 test_poly_compiled<load_from_csv>
df_sub = pd.DataFrame({'image_id': test_images, 'label': y_preds})
 display(df_sub )
<merge><EOS> 
df_sub.to_csv('submission.csv', index=False )