text_html_output_1.png
from sklearn.model_selection import StratifiedKFold

from sklearn.model_selection import train_test_split

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 timm

import torch

import torch.nn as nn

import numpy as np
import pandas as pd
import os

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)

folds