text_plain_output_1.png
from sklearn.metrics import cohen_kappa_score

import os

import pandas as pd

import os
os.listdir('../input')

def seed_everything(seed):
    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 = 999
seed_everything(SEED)

base_image_dir = os.path.join('..', 'input/aptos2019-blindness-detection/')
train_dir = os.path.join(base_image_dir, 'train_images/')
df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir, '{}.png'.format(x)))
df = df.drop(columns=['id_code'])
df = df.sample(frac=1).reset_index(drop=True)

bs = 64
sz = 224

tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=360, max_warp=0, max_zoom=1.1, max_lighting=0.1, p_lighting=0.5)
src = ImageList.from_df(df=df, path='./', cols='path').split_by_rand_pct(0.2).label_from_df(cols='diagnosis')
data = src.transform(tfms, size=sz, resize_method=ResizeMethod.SQUISH, padding_mode='zeros').databunch(bs=bs, num_workers=4).normalize(imagenet_stats)

from sklearn.metrics import cohen_kappa_score
def quadratic_kappa(y_hat, y):
    return torch.tensor(cohen_kappa_score(y_hat.argmax(dim=-1), y, weights='quadratic'), device='cuda:0')

learn = cnn_learner(data, base_arch=models.resnet50, metrics=[quadratic_kappa])

learn.fit_one_cycle(4, max_lr=0.01)

learn.unfreeze()
learn.fit_one_cycle(6, max_lr=slice(1e-06, 0.001))

interp = ClassificationInterpretation.from_learner(learn)
losses, idxs = interp.top_losses()
len(data.valid_ds) == len(losses) == len(idxs)

idx = 1
im, cl = learn.data.dl(DatasetType.Valid).dataset[idx]
cl = int(cl)
im.show(title=f'pred. class: {interp.pred_class[idx]}, actual class: {learn.data.classes[cl]}')