This is a subset of lesson 1 of https://course.fast.ai
%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
We are going to use the Oxford-IIIT Pet Dataset by O. M. Parkhi et al., 2012 which features 12 cat breeds and 25 dogs breeds. Our model will need to learn to differentiate between these 37 distinct categories. According to their paper, the best accuracy they could get in 2012 was 59.21%.
path = untar_data(URLs.PETS)
path_anno = path/'annotations'
path_img = path/'images'
np.random.seed(2)
fnames = get_image_files(path_img)
fnames[0]
PosixPath('/home/jhoward/.fastai/data/oxford-iiit-pet/images/great_pyrenees_173.jpg')
pat = r'/([^/]+)_\d+.jpg$'
data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=bs
).normalize(imagenet_stats)
data.show_batch(rows=3, figsize=(7,6))
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
learn.fit_one_cycle(4)
| epoch | train_loss | valid_loss | error_rate | time |
|---|---|---|---|---|
| 0 | 1.411342 | 0.298996 | 0.086604 | 00:15 |
| 1 | 0.543773 | 0.225010 | 0.078484 | 00:15 |
| 2 | 0.332864 | 0.194363 | 0.070365 | 00:14 |
| 3 | 0.242706 | 0.188766 | 0.067659 | 00:14 |
learn.save('stage-1')
learn.unfreeze()
learn.fit_one_cycle(2, max_lr=slice(1e-6,1e-4))
| epoch | train_loss | valid_loss | error_rate | time |
|---|---|---|---|---|
| 0 | 0.231939 | 0.180866 | 0.062246 | 00:18 |
| 1 | 0.207379 | 0.175878 | 0.062246 | 00:17 |
interp = ClassificationInterpretation.from_learner(learn)
interp.most_confused(min_val=2)
[('Egyptian_Mau', 'Bengal', 8),
('Ragdoll', 'Birman', 6),
('american_pit_bull_terrier', 'staffordshire_bull_terrier', 6),
('beagle', 'basset_hound', 5),
('american_bulldog', 'staffordshire_bull_terrier', 4),
('american_bulldog', 'american_pit_bull_terrier', 3),
('chihuahua', 'miniature_pinscher', 3),
('english_setter', 'english_cocker_spaniel', 3),
('samoyed', 'great_pyrenees', 3),
('Russian_Blue', 'British_Shorthair', 2),
('Siamese', 'Birman', 2),
('american_pit_bull_terrier', 'american_bulldog', 2),
('chihuahua', 'staffordshire_bull_terrier', 2),
('saint_bernard', 'american_bulldog', 2),
('yorkshire_terrier', 'havanese', 2)]