Establishing a Baseline

First, we’ll create a baseline, using plain SGD, and compare it to fastai’s default optimizer. We’ll start by grabbing Imagenette with the same get_data we used in <>:

In [ ]:

  1. #hide_input
  2. def get_data(url, presize, resize):
  3. path = untar_data(url)
  4. return DataBlock(
  5. blocks=(ImageBlock, CategoryBlock), get_items=get_image_files,
  6. splitter=GrandparentSplitter(valid_name='val'),
  7. get_y=parent_label, item_tfms=Resize(presize),
  8. batch_tfms=[*aug_transforms(min_scale=0.5, size=resize),
  9. Normalize.from_stats(*imagenet_stats)],
  10. ).dataloaders(path, bs=128)

In [ ]:

  1. dls = get_data(URLs.IMAGENETTE_160, 160, 128)

We’ll create a ResNet-34 without pretraining, and pass along any arguments received:

In [ ]:

  1. def get_learner(**kwargs):
  2. return cnn_learner(dls, resnet34, pretrained=False,
  3. metrics=accuracy, **kwargs).to_fp16()

Here’s the default fastai optimizer, with the usual 3e-3 learning rate:

In [ ]:

  1. learn = get_learner()
  2. learn.fit_one_cycle(3, 0.003)
epochtrain_lossvalid_lossaccuracytime
02.5719322.6850400.32254800:11
11.9046741.8525890.43745200:11
21.5869091.3749080.59490400:11

Now let’s try plain SGD. We can pass opt_func (optimization function) to cnn_learner to get fastai to use any optimizer:

In [ ]:

  1. learn = get_learner(opt_func=SGD)

The first thing to look at is lr_find:

In [ ]:

  1. learn.lr_find()

Out[ ]:

  1. (0.017378008365631102, 3.019951861915615e-07)

Establishing a Baseline - 图1

It looks like we’ll need to use a higher learning rate than we normally use:

In [ ]:

  1. learn.fit_one_cycle(3, 0.03, moms=(0,0,0))
epochtrain_lossvalid_lossaccuracytime
02.9694122.2145960.24203800:09
12.4427301.8459500.36254800:09
22.1571591.7411430.40891700:09

Because accelerating SGD with momentum is such a good idea, fastai does this by default in fit_one_cycle, so we turn it off with moms=(0,0,0). We’ll be discussing momentum shortly.)

Clearly, plain SGD isn’t training as fast as we’d like. So let’s learn some tricks to get accelerated training!