Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<import_modules>
import numpy as np
import pandas as pd
import os
from sklearn.model_selection import train_test_split
import random
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV<import_modules>
!pip install timm --no-index --find-links=file:///kaggle/input/timm-package/
import datetime<choose_model_class>
!pip install albumentations --no-index --find-links=file:///kaggle/input/albumentationspackage/
def model() :
rf = RandomForestRegressor(random_state = 42,bootstrap = False, max_depth= 80, max_features = 2,
min_samples_leaf = 5, min_samples_split = 8, n_estimators = 100)
 return rf<train_model>
import sys
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.nn import Parameter
import os
import cv2
import timm
def train_and_predict(X, y, X_test):
rf_classifier = model()
rf_classifier_model = Pipeline(steps=[ 
('model', rf_classifier)
 ])
 rf_classifier_model.fit(X, y)
 y_pred = rf_classifier_model.predict(X_test)
 y_pred = np.around(y_pred)
 y_pred = y_pred.astype(int)
 return y_pred<load_from_csv>
import albumentations as A
if __name__ == '__main__':
seed = 123
random.seed(seed)
 print('Loading Training Data')
 covid_train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/train.csv",
parse_dates=['Date'])
 covid_train = covid_train.drop(['Province/State'], axis=1)
 covid_train = covid_train.drop(['Country/Region'], axis=1)
 covid_train['Date'] =(pd.to_datetime(covid_train['Date'], unit='s' ).astype(int)/10**15 ).astype(int)
 cols = [col for col in covid_train.columns if col not in ['Id','ConfirmedCases','Fatalities']]
X = covid_train[cols]
y1 = covid_train['ConfirmedCases']
y2 = covid_train['Fatalities']
print('Loading Testing Data')
 covid_test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-1/test.csv",
parse_dates=['Date'])
 covid_test = covid_test.drop(['Province/State'], axis=1)
 covid_test = covid_test.drop(['Country/Region'], axis=1)
 covid_test['Date'] =(pd.to_datetime(covid_test['Date'], unit='s' ).astype(int)/10**15 ).astype(int)
 X_test = covid_test.iloc[:,1:]
confirmer_y_pred = train_and_predict(X, y1, X_test)
 fatality_y_pred = train_and_predict(X, y2, X_test)
 pred=pd.DataFrame()
pred['ForecastId']=covid_test['ForecastId']
pred['ConfirmedCases']=confirmer_y_pred
pred['Fatalities']=fatality_y_pred
pred.to_csv('submission.csv',index=False)
<data_type_conversions>
def gem(x, p=3, eps=1e-5):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-5):
super(GeM, self ).__init__()
self.p = Parameter(torch.ones(1)* p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(
 self.eps)+ ')'
covid_train['Date'] =(pd.to_datetime(covid_train['Date'], unit='s' ).astype(int)/10**9 ).astype(int )<feature_engineering>
class Net(nn.Module):
def __init__(self, num_classes=5):
super().__init__()
self.model = timm.create_model('seresnext50_32x4d', pretrained=False)
 self._avg_pooling = nn.AdaptiveAvgPool2d(1)
 self.dropout=nn.Dropout(0.5)
 self._fc = nn.Linear(2048 , num_classes, bias=True)
 def forward(self, inputs):
input_iid = inputs
input_iid=input_iid/255.
 bs = input_iid.size(0)
 x = self.model.forward_features(input_iid)
 fm = self._avg_pooling(x)
 fm = fm.view(bs, -1)
 feature=self.dropout(fm)
 x = self._fc(feature)
 return x
for i in range(len(covid_train['Date'])) :
covid_train['Date'][i] = covid_train['Date'][i].strftime("%d %B, %Y" )<feature_engineering>
class DatasetTest() :
def __init__(self, test_data_dir):
self.ds = self.get_list(test_data_dir)
 self.root_dir = test_data_dir
self.val_trans=A.Compose([A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.ColorJitter(brightness=0.1, contrast=0.2, saturation=0.2, hue=0.00, always_apply=False, p=1.0),
A.RandomCrop(height= 600, width = 600,always_apply=True, p=1.0)] 
)
 def get_list(self, dir):
pic_list = os.listdir(dir)
 return pic_list
def __len__(self):
return len(self.ds)
 def preprocess_func(self, image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image1=self.val_trans(image=image)['image']
image2=self.val_trans(image=image)['image']
image3=self.val_trans(image=image)['image']
image4=self.val_trans(image=image)['image']
image5=self.val_trans(image=image)['image']
image6=self.val_trans(image=image)['image']
image7=self.val_trans(image=image)['image']
image8=self.val_trans(image=image)['image']
image_batch = np.stack([image1,
image2,
image3,
image4,
image5,
image6,
image7,
image8
])
 image_batch = np.transpose(image_batch, axes=[0, 3, 1, 2])
 return image, image_batch
def __getitem__(self, item):
fname = self.ds[item]
image_path = os.path.join(self.root_dir, fname)
 image = cv2.imread(image_path, -1)
 image,float_image = self.preprocess_func(image)
 return fname, image,float_image
covid_train['Date'] = str(covid_train['Date'] )<load_from_csv>
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 kaggle_root = '/kaggle/input'
model_dir = os.path.join(kaggle_root, 'cassva-models-se50-640')
 weights = [os.path.join(model_dir, f)for f in os.listdir(model_dir)]
test_datadir= os.path.join(kaggle_root, 'cassava-leaf-disease-classification/test_images')
 dataiter=DatasetTest(test_datadir)
 def predict_with_model(model,weights):
merge_res_dict = {}
for j, weight in enumerate(weights):
model.load_state_dict(torch.load(weight, map_location=device), strict=False)
 model.eval()
cur_result=pd.DataFrame(columns=['image_id','label'])
 len_data = len(dataiter)
 image_ids=[]
precictions=[]
for i in range(len(dataiter)) :
print('weight {}: data {}/{}'.format(j, i+1, len_data))
fname,_,float_image=dataiter.__getitem__(i)
 input=torch.from_numpy(float_image ).to(device ).float()
with torch.no_grad() :
output=model(input)
 output=torch.nn.functional.softmax(output,dim=-1)
 output=output.cpu().numpy()
output=np.mean(output,axis=0)
 image_ids.append(fname)
 label=np.argmax(output)
 precictions.append(label)
 if fname not in merge_res_dict:
merge_res_dict[fname] = output
else:
merge_res_dict[fname] += output
merge_result = pd.DataFrame(columns=['image_id','label'])
 precictions = []
for fname in merge_res_dict:
label = np.argmax(merge_res_dict[fname])
 precictions.append(label)
 merge_result['image_id'] = image_ids
merge_result['label'] = precictions
merge_result.to_csv('submission.csv',index=False)
 model=Net().to(device)
 predict_with_model(model,weights )