This notebook demonstrates various techniques of effective Neural Network models training using the Callbacks mechanism of FastAI library (v1).
Getting the Data¶
from fastai.vision import *
path = untar_data('https://s3.amazonaws.com/fast-ai-imageclas/imagewoof2')
We will decode the dataset labels to be more meaningful to a human.
lbl_dict = dict(
n02093754='Australian terrier',
n02089973='Border terrier',
n02099601='Samoyed',
n02087394='Beagle',
n02105641='Shih-Tzu',
n02096294='English foxhound',
n02088364='Rhodesian ridgeback',
n02115641='Dingo',
n02111889='Golden retriever',
n02086240='Old English sheepdog'
)
def lbl_trfm(o):
return lbl_dict[str(o).split('/')[-2]] # Use folder name of image object `o`
Data augmentation¶
Instead of feeding the model with the same pictures every time, we do small random transformations (a bit of rotation, zoom, translation, etc...) that don't change what's inside the image (to the human eye) but do change its pixel values. Models trained with data augmentation will then generalize better.
tfms = get_transforms()
tfms
Let's pull together the data into a singe ImageDataBunch
object.
data = (ImageList.from_folder(path)
.split_by_folder(valid='val')
.label_from_func(lbl_trfm)
.transform(tfms, size=160)
.databunch(bs=32)
.normalize(imagenet_stats))
data
data.show_batch(5, figsize=(16,16))
Randomly Initialized CONV Model¶
results = pd.DataFrame(index=range(5))
int(torch.cuda.max_memory_allocated(0)/(1024*1024))
from fastai.callbacks import *
learn = cnn_learner(data, models.resnet50, metrics=[accuracy], pretrained=False, callback_fns=[CSVLogger,ShowGraph])
learn.lr_find()
learn.recorder.plot()
learn.fit_one_cycle(5,max_lr=5e-3)
results['resnet50'] = pd.read_csv(path/'history.csv',usecols=['accuracy'])
results.head()
results.plot()
Pretrained CONV net Model¶
learn = cnn_learner(data, models.resnet50, metrics=[accuracy], pretrained=True, callback_fns=[CSVLogger,ShowGraph])
learn.lr_find()
learn.recorder.plot()
learn.fit_one_cycle(5,max_lr=slice(2e-4,2e-3,2e-2))
results['resnet50 pretrained diff LR 2'] = pd.read_csv(path/'history.csv',usecols=['accuracy'])
results.plot()
Results¶
interp = ClassificationInterpretation.from_learner(learn)
losses,idxs = interp.top_losses()
interp.plot_confusion_matrix(figsize=(8,8))
interp.plot_top_losses(9, figsize=(16,16))