text_plain_output_1.png image_output_2.png image_output_1.png
from albumentations import Compose, Normalize, HorizontalFlip, VerticalFlip,RandomGamma, RandomRotate90,GaussNoise,Cutout

from albumentations.pytorch import ToTensor

from sklearn.model_selection import StratifiedKFold

from sklearn.model_selection import train_test_split

from torch.optim import Adam, SGD

from torch.utils.data import DataLoader, Dataset

from tqdm import tqdm

import cv2

import cv2

import datetime

import numpy as np

import numpy as np # linear algebra

import os

import os

import pandas as pd

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import random

import time

import timm

import torch

import torch.nn as nn

import numpy as np
import pandas as pd
import os

import datetime
dt_now = datetime.datetime.now()
dt_now_ = str(dt_now).replace(' ', '_')

import pandas as pd
df = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')

class CFG:
    debug = True
    n_classes = 5
    lr = 0.0001
    batch_size = 8
    epochs = 1
    seed = 777
    n_fold = 4
    warmup = -1
    device = 0
    amp = True
    amp_inf = False
    smooth = False
    smooth_alpha = 0.1
    efnet_num = 10
    drop_rate = 0.25
    crop = False
    psuedo_label = False
    pseudo_predict = '2020-11-06_14:50:43.385109_predict.csv'
    TTA = False
    Attention = False
    white = False
    model_name = 'tf_efficientnet_b0_ns'
    zoom = True
SIZE = 512
model_names = ['tf_efficientnet_b0_ns', 'vit_base_resnet26d_224', 'resnest50d', 'vit_base_patch32_384']

def seed_torch(seed=42):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic = True
seed_torch(seed=42)
torch.cuda.set_device(CFG.device)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

from sklearn.model_selection import train_test_split
train, test = train_test_split(df, test_size=0.9, stratify=df['label'], random_state=2020)
train = train.reset_index(drop=True)
test = test.reset_index(drop=True)

class efnet_model(nn.Module):
    def __init__(self):
        super().__init__()
        if CFG.efnet_num == 0:
            self.model = geffnet.efficientnet_b0(pretrained=True, drop_rate=CFG.drop_rate)
        elif CFG.efnet_num == 1:
            self.model = geffnet.efficientnet_b1(pretrained=True, drop_rate=CFG.drop_rate)
        elif CFG.efnet_num == 2:
            self.model = geffnet.efficientnet_b2(pretrained=True, drop_rate=CFG.drop_rate)
        elif CFG.efnet_num == 10:
            self.model = timm.create_model(CFG.model_name, pretrained=True, num_classes=CFG.n_classes)
        elif CFG.efnet_num < 3:
            self.model.classifier = nn.Linear(self.model.classifier.in_features, 2)
    def forward(self, x):
        if CFG.efnet_num == 10:
            x = self.model(x)
        else:
            x = self.model(x)
        return x

if CFG.debug:
    folds = train.sample(n=200, random_state=CFG.seed).reset_index(drop=True).copy()
else:
    folds = train.copy()
train_labels = folds['label'].values
kf = StratifiedKFold(n_splits=CFG.n_fold, shuffle=True, random_state=CFG.seed)
for fold, (train_index, val_index) in enumerate(kf.split(folds.values, train_labels)):
    folds.loc[val_index, 'fold'] = int(fold)

class TrainDataset(Dataset):
    def __init__(self, df, transform1=None, transform2=None):
        self.df = df
        self.transform = transform1
        self.transform_ = transform2
    def __len__(self):
        return len(self.df)
    def __getitem__(self, idx):
        path = self.df['image_id'].values[idx]
        file_path = '/kaggle/input/cassava-leaf-disease-classification/train_images/{}'.format(path)
        image = cv2.imread(file_path)
        if CFG.crop:
            image = crop_object(image)
        try:
            image = cv2.resize(image, (SIZE, SIZE))
        except Exception as e:
        label_ = self.df['label'].values[idx]
        if self.transform:
            image = self.transform(image=image)['image']
        if self.transform_:
            image = self.transform_(image=image)['image']
        label = torch.tensor(label_)
        return (image, label)

def get_transforms1(*, data):
    if data == 'train':
        return Compose([HorizontalFlip(p=0.5), VerticalFlip(p=0.5), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
    elif data == 'valid':
        return Compose([Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
def to_tensor(*args):
    return Compose([ToTensor()])

def train_fn(fold):
    trn_idx = folds[folds['fold'] != fold].index
    val_idx = folds[folds['fold'] == fold].index
    train_df = folds.loc[trn_idx].reset_index(drop=True)
    valid_df = folds.loc[val_idx].reset_index(drop=True)
    train_dataset = TrainDataset(folds.loc[trn_idx].reset_index(drop=True), transform1=get_transforms1(data='train'), transform2=to_tensor())
    valid_dataset = TrainDataset(folds.loc[val_idx].reset_index(drop=True), transform1=get_transforms1(data='valid'), transform2=to_tensor())
    train_loader = DataLoader(train_dataset, batch_size=CFG.batch_size, shuffle=True, num_workers=4)
    valid_loader = DataLoader(valid_dataset, batch_size=CFG.batch_size, shuffle=False, num_workers=4)
    model = efnet_model()
    model.to(device)
    optimizer = Adam(model.parameters(), lr=CFG.lr, amsgrad=False)
    criterion = nn.CrossEntropyLoss()
    softmax = nn.Softmax(dim=1)
    for epoch in range(CFG.epochs):
        start_time = time.time()
        model.train()
        avg_loss = 0.0
        tk0 = tqdm(enumerate(train_loader), total=len(train_loader))
        for i, (images, labels) in tk0:
            images = images.to(device)
            labels = labels.to(device)
            optimizer.zero_grad()
            y_preds = model(images.float())
            loss = criterion(y_preds, labels.long())
            loss.backward()
            optimizer.step()
            avg_loss += loss.item() / len(train_loader)
        model.eval()
        avg_val_loss = 0.0
        valid_labels = []
        preds = []
        tk1 = tqdm(enumerate(valid_loader), total=len(valid_loader))
        for i, (images, labels) in tk1:
            images = images.to(device)
            labels = labels.to(device)
            with torch.no_grad():
                y_preds = model(images.float())
                loss = criterion(y_preds, labels.long())
            valid_labels.append(labels.to('cpu').detach().numpy().copy())
            y_preds = softmax(y_preds)
            preds.append(y_preds.to('cpu').detach().numpy().copy())
            avg_val_loss += loss.item() / len(valid_loader)
        preds = np.concatenate(preds)
        valid_labels = np.concatenate(valid_labels)
        torch.save(model.state_dict(), f'fold{fold}_{dt_now_}_baseline.pth')
        return (preds, valid_labels)

def auc(predict, labels):
    pass

predict = []
labels = []
for fold in range(CFG.n_fold):
    _pred, _label = train_fn(fold)
    predict.append(_pred)
    labels.append(_label)
predict = np.concatenate(predict)
labels = np.concatenate(labels)
score = auc(predict, labels)
print(predict)
print(labels)
print(len(labels))
print(score)