Important: This notebook will only work with fastai-0.7.x. Do not try to run any fastai-1.x code from this path in the repository because it will load fastai-0.7.x

Image classification with Convolutional Neural Networks


In [ ]:
# Put these at the top of every notebook, to get automatic reloading and inline plotting
%reload_ext autoreload
%autoreload 2
%matplotlib inline

In [ ]:
# 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/dogscats/"
sz=224
arch=vgg16
bs=64

In [ ]:
# Uncomment the below if you need to reset your precomputed activations
# !rm -rf {PATH}tmp

In [ ]:
data = ImageClassifierData.from_paths(PATH, bs=bs, tfms=tfms_from_model(arch, sz))

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


100%|██████████| 360/360 [01:21<00:00,  4.44it/s]
100%|██████████| 32/32 [00:12<00:00,  2.64it/s]

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


[ 0.       0.05266  0.02898  0.99072]                         
[ 1.       0.04534  0.02512  0.99023]                         
[ 2.       0.04645  0.02377  0.99121]                         


In [ ]:


In [ ]:
tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)

In [ ]:
data = ImageClassifierData.from_paths(PATH, tfms=tfms, bs=bs, num_workers=4)
learn = ConvLearner.pretrained(arch, data, precompute=True)

In [ ]:
learn.fit(1e-2, 2)


[ 0.       0.05622  0.0268   0.98926]                         
[ 1.       0.04222  0.02508  0.99072]                          


In [ ]:
learn.precompute=False

In [ ]:
learn.fit(1e-2, 1, cycle_len=1)


[ 0.       0.05511  0.02654  0.98909]                         


In [ ]:
learn.unfreeze()

In [ ]:
lr=np.array([1e-4,1e-3,1e-2])

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


[ 0.       0.05664  0.0245   0.99023]                         


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


[ 0.       0.05955  0.02217  0.9917 ]                         
[ 1.       0.03902  0.0181   0.99414]                         
[ 2.       0.03676  0.0168   0.99414]                         
[ 3.       0.03097  0.01607  0.99316]                         
[ 4.       0.02823  0.01574  0.99316]                         
[ 5.       0.02663  0.01448  0.99316]                         
[ 6.       0.03113  0.01642  0.99414]                         


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


[ 0.       0.02769  0.01896  0.99268]                         
[ 1.       0.01939  0.01417  0.99365]                         
[ 2.       0.01935  0.0142   0.99414]                         
[ 3.       0.02465  0.01641  0.99316]                         
[ 4.       0.02753  0.01376  0.99365]                         
[ 5.       0.01721  0.01413  0.99414]                         
[ 6.       0.0221   0.01317  0.99365]                         
[ 7.       0.01789  0.01379  0.99268]                         
[ 8.       0.01861  0.0139   0.99316]                         


In [ ]:
log_preds,y = learn.TTA()
probs = np.mean(np.exp(log_preds),0);accuracy_np(probs,y)


Out[ ]:
0.995

In [ ]: