%reload_ext autoreload
%autoreload 2
%matplotlib inline
from fastai.vision import *
from fastai.metrics import error_rate
bs = 64
# bs = 16 # uncomment this line if you run out of memory even after clicking Kernel->Restart
path = untar_data(URLs.PETS); path
path.ls()
PosixPath('/home/sgugger/.fastai/data/oxford-iiit-pet')
path_img = path/'images'
fnames = get_image_files(path_img)
fnames[:5]
[PosixPath('/home/ubuntu/.fastai/data/oxford-iiit-pet/images/saint_bernard_188.jpg'), PosixPath('/home/ubuntu/.fastai/data/oxford-iiit-pet/images/staffordshire_bull_terrier_114.jpg'), PosixPath('/home/ubuntu/.fastai/data/oxford-iiit-pet/images/Persian_144.jpg'), PosixPath('/home/ubuntu/.fastai/data/oxford-iiit-pet/images/Maine_Coon_268.jpg'), PosixPath('/home/ubuntu/.fastai/data/oxford-iiit-pet/images/newfoundland_95.jpg')]
np.random.seed(2)
pat = r'/([^/]+)_\d+.jpg$'
data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=bs
).normalize(imagenet_stats)
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
learn.fit_one_cycle(4)
epoch | train_loss | valid_loss | error_rate |
---|---|---|---|
1 | 1.409939 | 0.357608 | 0.102165 |
2 | 0.539408 | 0.242496 | 0.073072 |
3 | 0.340212 | 0.221338 | 0.066306 |
4 | 0.261859 | 0.216619 | 0.071042 |
learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
learn.recorder.plot()
learn.unfreeze()
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))
epoch | train_loss | valid_loss | error_rate |
---|---|---|---|
1 | 0.242544 | 0.208489 | 0.067659 |
2 | 0.206940 | 0.204482 | 0.062246 |