Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<feature_engineering>
!nvidia-smi
test["datetime-year"] = test["datetime"].dt.year
test["datetime-month"] = test["datetime"].dt.month
test["datetime-day"] = test["datetime"].dt.day
test["datetime-hour"] = test["datetime"].dt.hour
test["datetime-minute"] = test["datetime"].dt.minute
test["datetime-second"] = test["datetime"].dt.second
test["datetime-dayofweek"] = test["datetime"].dt.dayofweek
print(test.shape)
 test[["datetime", "datetime-year", "datetime-month", "datetime-day", "datetime-hour", "datetime-minute", "datetime-second", "datetime-dayofweek"]].head()<feature_engineering>
!pip install.. /input/timmmodels/dist/timm-0.3.4.tar
test.loc[test["datetime-dayofweek"] == 0, "datetime-dayofweek(humanized)"] = "Monday"
test.loc[test["datetime-dayofweek"] == 1, "datetime-dayofweek(humanized)"] = "Tuesday"
test.loc[test["datetime-dayofweek"] == 2, "datetime-dayofweek(humanized)"] = "Wednesday"
test.loc[test["datetime-dayofweek"] == 3, "datetime-dayofweek(humanized)"] = "Thursday"
test.loc[test["datetime-dayofweek"] == 4, "datetime-dayofweek(humanized)"] = "Friday"
test.loc[test["datetime-dayofweek"] == 5, "datetime-dayofweek(humanized)"] = "Saturday"
test.loc[test["datetime-dayofweek"] == 6, "datetime-dayofweek(humanized)"] = "Sunday"
print(test.shape)
 test[["datetime", "datetime-dayofweek", "datetime-dayofweek(humanized)"]].head()<set_options>
import os
import cv2
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import models
from torch.utils.data import DataLoader, Dataset
from torch.cuda import amp
import albumentations as A
from albumentations.pytorch import ToTensorV2
import timm
%matplotlib inline 
<data_type_conversions>
ROOT_DIR = ".. /input/cassava-leaf-disease-classification"
TEST_DIR = ".. /input/cassava-leaf-disease-classification/test_images"
train["datetime-year(str)"] = train["datetime-year"].astype('str')
 train["datetime-month(str)"] = train["datetime-month"].astype('str')
 train["datetime-year_month"] = train["datetime-year(str)"] + "-" + train["datetime-month(str)"]
print(train.shape)
 train[["datetime", "datetime-year_month"]].head()<define_variables>
class CFG:
model_name = 'tf_efficientnet_b4_ns'
img_size = 512
loadmodelpath = '/kaggle/input/cassava-bitempered-logistic-loss/bitemp-01.pth'
num_classes = 5
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )
feature_names = ["datetime-year", "season", "datetime-hour", "datetime-dayofweek", "workingday", "holiday",
"weather", "humidity", "temp", "atemp", "windspeed"]
feature_names<define_variables>
model = timm.create_model(CFG.model_name, pretrained=False)
 num_features = model.classifier.in_features
model.classifier = nn.Linear(num_features, CFG.num_classes)
 model.to(CFG.device);
label_name = "count"
label_name<choose_model_class>
model = torch.load(CFG.loadmodelpath)
 model.eval()
model = RandomForestRegressor(n_jobs=-1,
random_state=37)
 model<compute_test_metric>
T_DIR = TEST_DIR
t_df = pd.read_csv(f"{ROOT_DIR}/sample_submission.csv" )
def rmsle(predict, actual):
predict = np.array(predict)
 actual = np.array(actual)
 log_predict = np.log(predict + 1)
 log_actual = np.log(actual + 1)
 distance = log_predict - log_actual
square_distance = distance ** 2
mean_square_distance = square_distance.mean()
score = np.sqrt(mean_square_distance)
 return score
rmsle_score = make_scorer(rmsle)
 rmsle_score<compute_train_metric>
t_data = CassavaLeafDataset(T_DIR, t_df, transforms=data_transforms["valid"])
 t_loader = DataLoader(dataset=t_data, batch_size=1, num_workers=4, pin_memory=True, shuffle=False )
score = cross_val_score(model, X_train, y_train,
cv=20, scoring=rmsle_score ).mean()
print("Score = {0:.5f}".format(score))<train_model>
submit_df = pd.DataFrame(t_df ).copy(deep=True)
 for i,(inputs, _)in enumerate(t_loader):
inputs = inputs.to(CFG.device)
 outputs = model(inputs ).detach().cpu().numpy()
pred_label = np.argmax(outputs)
 submit_df.iloc[i] = [t_df.iloc[i]['image_id'], pred_label]
<train_model><EOS> 
submit_df.to_csv("/kaggle/working/submission.csv", index=False )