Lesson 6: pets revisited

In [1]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline
In [2]:
from fastai.vision import *
In [3]:
bs = 64
In [5]:
path = untar_data(URLs.PETS) / 'images'

Data augmentation

In [6]:
tfms = get_transforms(max_rotate=20, max_zoom=1.3, max_lighting=0.4, max_warp=0.4,
                      p_affine=1., p_lighting=1.)
In [7]:
doc(get_transforms)
In [8]:
src = ImageItemList.from_folder(path).random_split_by_pct(0.2, seed=2)
In [9]:
def get_data(size, bs, padding_mode='reflection'):
    return (src.label_from_re(r'([^/]+)_\d+.jpg$')
            .transform(tfms, size=size, padding_mode=padding_mode)
            .databunch(bs=bs).normalize(imagenet_stats))
In [10]:
data = get_data(224, bs, 'zeros')
In [11]:
def _plot(i, j, ax):
    x, y = data.train_ds[3]
    x.show(ax, y=y)

plot_multi(_plot, 3, 3, figsize=(8,8))
In [12]:
data = get_data(224, bs)
In [13]:
plot_multi(_plot, 3, 3, figsize=(8,8))

Train a model

In [14]:
gc.collect()
Out[14]:
14593
In [15]:
learn = create_cnn(data, models.resnet34, metrics=error_rate, bn_final=True)
In [16]:
learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [20]:
learn.recorder.plot()
In [21]:
learn.fit_one_cycle(3, slice(1e-2), pct_start=0.8)
Total time: 03:37

epoch train_loss valid_loss error_rate
1 2.431377 1.230201 0.290934
2 1.439160 0.372741 0.102842
3 0.878912 0.300877 0.087957
In [22]:
learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [24]:
learn.recorder.plot(skip_start=0)
In [25]:
learn.unfreeze()
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-3), pct_start=0.8)
Total time: 02:31

epoch train_loss valid_loss error_rate
1 0.694553 0.305744 0.079161
2 0.650280 0.301226 0.070365
In [26]:
data = get_data(352, bs)
learn.data = data
In [27]:
learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [29]:
learn.recorder.plot(skip_start=0)
In [30]:
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))
Total time: 04:58

epoch train_loss valid_loss error_rate
1 0.599940 0.277238 0.057510
2 0.561108 0.279816 0.056157
In [31]:
learn.save('352')
In [ ]:
learn.save('352')

Convolution kernel

In [32]:
data = get_data(352,16)
In [33]:
learn = create_cnn(data, models.resnet34, metrics=error_rate, bn_final=True).load('352')
In [66]:
idx = 33
x, y = data.valid_ds[idx]
x.show()
data.valid_ds.y[idx]
Out[66]:
Category Ragdoll