Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<split>
! pip install '.. /input/efficientnet-pytorch/EfficientNet-PyTorch/EfficientNet-PyTorch-master/'
X_train, X_test, y_train, y_test = train_test_split(train['review'], train['sentiment'], test_size=0.22, random_state=101)
 len(X_train), len(X_test), len(X_train)+ len(X_test )<compute_test_metric>
import os
import gc
import cv2
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import albumentations as A
from efficientnet_pytorch import EfficientNet
def pred(predicted,compare):
cm = pd.crosstab(compare,predicted)
 TN = cm.iloc[0,0]
FN = cm.iloc[1,0]
TP = cm.iloc[1,1]
FP = cm.iloc[0,1]
print("CONFUSION MATRIX ------->> ")
 print(cm)
 print()
print('Classification paradox :------->>')
 print('Accuracy :- ', round(((TP+TN)*100)/(TP+TN+FP+FN),2))
print()
print('False Negative Rate :- ',round(( FN*100)/(FN+TP),2))
print()
print('False Postive Rate :- ',round(( FP*100)/(FP+TN),2))
print()
print(classification_report(compare,predicted))<predict_on_test>
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
 effnet_b1=EfficientNet.from_pretrained('efficientnet-b1',
weights_path='.. /input/efficientnet-pytorch/efficientnet-b1-dbc7070a.pth',
include_top=True )
pipeline = Pipeline([ 
('bow', CountVectorizer(analyzer=clean_text)) , 
('tfidf', TfidfTransformer()), 
('classifier', LogisticRegression(random_state=101)) ,
])
 pipeline.fit(X_train,y_train)
 predictions = pipeline.predict(X_train)
 pred(predictions,y_train )<predict_on_test>
CFG = {
"IMG_SIZE": 384,
"BATCH_SIZE": 16,
"IMG_FOLDER": ".. /input/cassava-leaf-disease-classification/train_images",
"EPOCHS": 10,
"NUM_FOLDS": 5,
"device": device
}
print(device )
predictions = pipeline.predict(X_test)
 pred(predictions,y_test )<predict_on_test>
class CassavaModel(nn.Module):
def __init__(self, _backbone):
super(CassavaModel, self ).__init__()
self._backbone=_backbone
self._backbone._fc=nn.Linear(in_features=_backbone._fc.in_features,
out_features=5,
bias=True)
 def forward(self, x):
x=self._backbone(x)
 return x
pipeline = Pipeline([ 
('bow', CountVectorizer(analyzer=clean_text)) , 
('tfidf', TfidfTransformer()), 
('classifier', MultinomialNB()),
])
 pipeline.fit(X_train,y_train)
 predictions = pipeline.predict(X_train)
 pred(predictions,y_train )<predict_on_test>
models=[]
for model_name in os.listdir('.. /input/enesemble'):
model_path=os.path.join('.. /input/enesemble', model_name)
 model=CassavaModel(effnet_b1)
 model.load_state_dict(torch.load(model_path))
model=model.to(device)
 models.append(model )
predictions = pipeline.predict(X_test)
 pred(predictions,y_test )<train_model>
test_folder=".. /input/cassava-leaf-disease-classification/test_images"
class TestDataset(torch.utils.data.Dataset):
def __init__(self, augmentation):
self.test_images=os.listdir(test_folder)
 self.augmentation=augmentation
def __len__(self):
return len(self.test_images)
 def __getitem__(self, idx):
image_id=self.test_images[idx]
image_path=os.path.join(test_folder, image_id)
 img=read_image(image_path)
 if self.augmentation:
img=self.augmentation(image=img)['image']
img=torch.tensor(img ).permute(2, 1, 0)
 return(image_id, img )
pipeline = Pipeline([ 
('bow', CountVectorizer(analyzer=clean_text)) , 
('tfidf', TfidfTransformer()), 
('classifier', RandomForestClassifier(n_estimators = 500)) ,
])
 pipeline.fit(X_train,y_train)
 predictions = pipeline.predict(X_train)
 pred(predictions,y_train )<predict_on_test>
val_transform=val_augmentation()
pred=nn.Softmax(dim=1)
 test_dataset=TestDataset(val_transform)
 test_dataloader=torch.utils.data.DataLoader(test_dataset,
shuffle=False,
pin_memory=True,
batch_size=16,
num_workers=4)
 submission_data=[]
with torch.no_grad() :
for(image_id,img)in test_dataloader:
img=img.to(device)
 yout=torch.zeros(img.shape[0], 5 ).to(device)
 for model in models:
yout+=model(img)
 yout/=5
yout=pred(yout)
 ypred=torch.argmax(yout, dim=1 ).cpu().numpy()
for i in range(img.shape[0]):
submission_data.append({
'image_id': image_id[i],
'label': ypred[i]
} )
<predict_on_test><EOS> 
submission_df=pd.DataFrame.from_dict(submission_data)
 submission_df.to_csv('submission.csv', index=False )