Lesson 6: pets revisited

In [ ]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline

from fastai.vision import *
In [ ]:
bs = 64
In [ ]:
path = untar_data(URLs.PETS)/'images'

Data augmentation

In [ ]:
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 [ ]:
doc(get_transforms)
In [ ]:
src = ImageList.from_folder(path).split_by_rand_pct(0.2, seed=2)
In [ ]:
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 [ ]:
data = get_data(224, bs, 'zeros')
In [ ]:
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 [ ]:
data = get_data(224,bs)
In [ ]:
plot_multi(_plot, 3, 3, figsize=(8,8))

Train a model

In [ ]:
gc.collect()
learn = cnn_learner(data, models.resnet34, metrics=error_rate, bn_final=True)
Out[ ]:
29237
In [ ]:
learn.fit_one_cycle(3, slice(1e-2), pct_start=0.8)
Total time: 01:08

epoch train_loss valid_loss error_rate time
0 2.458192 1.221388 0.276049 00:23
1 1.467777 0.333426 0.078484 00:22
2 0.906456 0.259250 0.066982 00:22
In [ ]:
learn.unfreeze()
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-3), pct_start=0.8)
Total time: 00:59

epoch train_loss valid_loss error_rate time
0 0.708001 0.269306 0.058187 00:29
1 0.666836 0.267181 0.060217 00:29
In [ ]:
data = get_data(352,bs)
learn.data = data
In [ ]:
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))
Total time: 02:15

epoch train_loss valid_loss error_rate time
0 0.633500 0.261447 0.054804 01:08
1 0.595398 0.259402 0.051421 01:06
In [ ]:
learn.save('352')

Convolution kernel

In [ ]:
data = get_data(352,16)
In [ ]:
learn = cnn_learner(data, models.resnet34, metrics=error_rate, bn_final=True).load('352')
In [ ]:
idx=0
x,y = data.valid_ds[idx]
x.show()
data.valid_ds.y[idx]
Out[ ]:
Category newfoundland
In [ ]:
k = tensor([
    [0.  ,-5/3,1],
    [-5/3,-5/3,1],
    [1.  ,1   ,1],
]).expand(1,3,3,3)/6
In [ ]:
k
Out[ ]:
tensor([[[[ 0.0000, -0.2778,  0.1667],
          [-0.2778, -0.2778,  0.1667],
          [ 0.1667,  0.1667,  0.1667]],

         [[ 0.0000, -0.2778,  0.1667],
          [-0.2778, -0.2778,  0.1667],
          [ 0.1667,  0.1667,  0.1667]],

         [[ 0.0000, -0.2778,  0.1667],
          [-0.2778, -0.2778,  0.1667],
          [ 0.1667,  0.1667,  0.1667]]]])
In [ ]:
k.shape
Out[ ]:
torch.Size([1, 3, 3, 3])
In [ ]:
t = data.valid_ds[0][0].data; t.shape
Out[ ]:
torch.Size([3, 352, 352])
In [ ]:
t[None].shape
Out[ ]:
torch.Size([1, 3, 352, 352])
In [ ]:
edge = F.conv2d(t[None], k)
In [ ]:
show_image(edge[0], figsize=(5,5));