Lensa ML

Transfer learning: freeze 99.9% of a model, train a new last layer

Transfer learning reuses a model trained on one task as the starting point for another. ResNet-50 learned from 1.2 million labeled ImageNet photos. A new image task often has a few hundred photos per class, too few to train such a network from scratch, but often enough to train one new layer on top of it.

2 min read · Lensa ML
ResNet-50 from ImageNet, new 10-class headstem9.5Kstage 1216Kstage 21.22Mstage 37.10Mstage 415.0Mhead20,490frozen: 23,508,032trainedAll 23,528,522 weights, to scale99.91% frozen0.09% trainedthe sliver at the right end
frozen pretrained layersnew head, trained (outlined so it shows)

Block heights are schematic. The old 1,000-class head (2,049,000 weights) is removed.

  1. ResNet-50 is a 50-layer convolutional network, a standard image design that slides small learned filters across a photo, trained to sort ImageNet photos into 1,000 classes. Here a weight means any learned number, biases included. Its body, a stem and four stages of convolution blocks, turns a photo into 2,048 numbers that describe it: edges and textures in early layers, object parts in later ones. The head, its last layer, maps those 2,048 numbers to 1,000 class scores with 2,049,000 weights.
  2. For a task with 10 classes, such as 10 crop diseases, the 1,000-class head is removed. A new 2,048 to 10 layer takes its place: 2,048 × 10 weights plus 10 biases, 20,490 in all. The body is frozen, meaning its 23,508,032 weights keep their ImageNet values and training never updates them. Counted from the layer shapes, body and old head add up to 25,557,032, the count torchvision lists.
  3. The new model holds 23,528,522 weights. Training changes 20,490 of them, 0.09%, while 99.91% are reused as they are. On the bar drawn to scale, the head is a sliver at the right end. Gradients, the error signals training sends backward through the network, stop at the head, so frozen layers need none. Each step costs less, though every photo still runs forward through the frozen body.

Takeaway

Training only the head, often called feature extraction, suits small datasets. With more data, especially images unlike ImageNet such as medical scans, fine-tune instead: unfreeze the later stages too and train them with a smaller learning rate, the size of each update step, often ten times smaller. Unfreezing stage 4, the deepest group of blocks, raises the trained share from 0.09% to 64%.

Go deeper · interactive

Transfer Learning

On step 4, drag the Freeze layers slider and watch the lock icons move up the network.

Open the explainer

More bites

References