In [ ]:
# automatic reloading and inline plotting
%reload_ext autoreload
%autoreload 2
%matplotlib inline

In [ ]:
# imports
# This file contains all the main external libs we'll use
from fastai.imports import *

from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *

In [ ]:
PATH = "data/dog_breed_identification/"
sz = 128
bs = 128

In [ ]:
# check images
files = !ls {PATH}train/ | head
img = plt.imread(f'{PATH}train/{files[0]}')
plt.imshow(img)

In [ ]:
# check format that labels are given in, choose validation labels
labels = pd.read_csv(PATH+'labels.csv')

labels.pivot_table(columns='breed', aggfunc=len).T.sort_values(by='id')

In [ ]:
# make val indices
val_idxs = get_cv_idxs(len(labels))

In [ ]:
def get_data(sz, bs, arch):
    # specify transforms I want
    # can play with max zoom after basic train
    tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
    data = ImageClassifierData.from_csv(path=PATH, folder='train', csv_fname=PATH +
                                        'labels.csv', bs=bs, tfms=tfms, val_idxs=val_idxs, suffix='.jpg', test_name='test')
    return data if sz > 300 else data.resize(340, 'tmp')

In [ ]:
tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
data = ImageClassifierData.from_csv(path=PATH, folder='train', csv_fname=PATH +'labels.csv', bs=bs, tfms=tfms, val_idxs=val_idxs, suffix='.jpg', test_name='test')

In [ ]:
arch = resnext101_64
data = get_data(sz, bs, arch)
learn = ConvLearner.pretrained(arch, data, precompute=True)

In [ ]:
# this block was for checking original size
# size_d = {k: Image.open(PATH+k).size for k in data.trn_ds.fnames}
# widths, heights = list(zip(*size_d.values()))
# widths = np.array(widths)
# heights = np.array(heights)
# plt.hist(widths[widths < 1000])
# plt.hist(heights[heights < 1000])

In [ ]:
learn.lr_find()

In [ ]:
learn.sched.plot_lr()

In [ ]:
learn.sched.plot()

In [ ]:
lr=0.12

In [ ]:
learn.fit(lr, n_cycle=3)

In [ ]:
# now disable precompute because data augmentation
learn.precompute=False

In [ ]:
learn.fit(lr, n_cycle=3, cycle_len=1)

In [ ]:
# unfreeze layers
learn.unfreeze()
lr_rates = np.array([lr/100, lr/10, lr])

In [ ]:
learn.fit(lr_rates, n_cycle=3, cycle_len=1)

In [ ]: