From: https://www.kaggle.com/rftexas/basic-fast-ai-size-320-resnet152
Author: PAB97
Score: 0.246
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory
import os
print(os.listdir("../input"))
# Any results you write to the current directory are saved as output.
from fastai.vision import *
from fastai.metrics import fbeta
from torch.utils import model_zoo
Path('models').mkdir(exist_ok=True)
!cp '../input/resnet152/resnet152.pth' 'models/'
def load_url(*args, **kwargs):
model_dir = Path('models')
filename = 'resnet152.pth'
if not (model_dir/filename).is_file(): raise FileNotFoundError
return torch.load(model_dir/filename)
model_zoo.load_url = load_url
path = Path("../input/imet-2019-fgvc6")
len((path/"test").ls())
bs = 32
train_df = pd.read_csv(path/"train.csv", nrows=4000)
test_df = pd.read_csv(path/"sample_submission.csv")
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=90.0, max_zoom=1.5, max_lighting=0.2, xtra_tfms=[(symmetric_warp(magnitude=(-0,0), p=0)),])
train, test = [ImageList.from_df(df, path=path, cols='id', folder=folder, suffix='.png') for df, folder in zip([train_df, test_df], ['train', 'test'])]
data = (train.split_by_rand_pct(0.1, seed=42)
.label_from_df(cols='attribute_ids', label_delim=' ')
.add_test(test)
.transform(tfms, size=250)
.databunch(bs=bs).normalize(imagenet_stats))
data.show_batch()
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, input, target):
if not (target.size() == input.size()):
raise ValueError("Target size ({}) must be the same as input size ({})"
.format(target.size(), input.size()))
max_val = (-input).clamp(min=0)
loss = input - input * target + max_val + \
((-max_val).exp() + (-input - max_val).exp()).log()
invprobs = F.logsigmoid(-input * (target * 2.0 - 1.0))
loss = (invprobs * self.gamma).exp() * loss
return loss.sum(dim=1).mean()
learn = cnn_learner(data, models.resnet152, metrics=[fbeta], loss_func=FocalLoss(), model_dir="/tmp/models/")
learn.freeze()
learn.lr_find()
learn.recorder.plot()
learn.fit_one_cycle(10, slice(1e-2, 1e-1), wd=0.1)
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()
learn.fit_one_cycle(10, slice(1e-6), wd=0.1)
learn.recorder.plot_losses()
def find_best_fixed_threshold(preds, targs, do_plot=True):
score = []
thrs = np.arange(0, 0.5, 0.01)
for thr in progress_bar(thrs):
score.append(fbeta(valid_preds[0],valid_preds[1], thresh=thr))
score = np.array(score)
pm = score.argmax()
best_thr, best_score = thrs[pm], score[pm].item()
print(f'thr={best_thr:.3f}', f'F2={best_score:.3f}')
if do_plot:
plt.plot(thrs, score)
plt.vlines(x=best_thr, ymin=score.min(), ymax=score.max())
plt.text(best_thr+0.03, best_score-0.01, f'$F_{2}=${best_score:.3f}', fontsize=14);
plt.show()
return best_thr
i2c = np.array([[i, c] for c, i in learn.data.train_ds.y.c2i.items()]).astype(int) # indices to class number correspondence
def join_preds(preds, thr):
return [' '.join(i2c[np.where(t==1)[0],1].astype(str)) for t in (preds[0].sigmoid()>thr).long()]
valid_preds = learn.get_preds(DatasetType.Valid)
best_thr = find_best_fixed_threshold(*valid_preds)
test_preds = learn.get_preds(ds_type=DatasetType.Test)
test_df.attribute_ids = join_preds(test_preds, best_thr)
test_df.head()
test_df.to_csv('submission.csv', index=False)