Table Of Contents
Table Of Contents

08. Finetune a pretrained detection model

Fine-tuning is commonly used approach to transfer previously trained model to a new dataset. It is especially useful if the targeting new dataset is relatively small.

Finetuning from pre-trained models can help reduce the risk of overfitting. Finetuned model may also generalizes better if the previously used dataset is in the similar domain of the new dataset.

This tutorial opens up a good approach for fine-tuning object detection models provided by GluonCV. More Specifically, we show how to use a customized Pikachu dataset and illustrate the finetuning fundamentals step by step. You will be familiarize the steps and modify it to fit your own object detection projects.

import time
from matplotlib import pyplot as plt
import numpy as np
import mxnet as mx
from mxnet import autograd, gluon
import gluoncv as gcv
from gluoncv.utils import download, viz

Pikachu Dataset

First we will start with a nice Pikachu dataset generated by rendering 3D models on random real-world scenes. You can refer to Prepare custom datasets for object detection for tutorial of how to create your own datasets.

url = ''
idx_url = ''
download(url, path='pikachu_train.rec', overwrite=False)
download(idx_url, path='pikachu_train.idx', overwrite=False)


Downloading pikachu_train.rec from
Downloading pikachu_train.idx from

We can load dataset using RecordFileDetection

dataset ='pikachu_train.rec')
classes = ['pikachu']  # only one foreground class here
image, label = dataset[0]
print('label:', label)
# display image and label
ax = viz.plot_bbox(image, bboxes=label[:, :4], labels=label[:, 4:5], class_names=classes)


label: [[309.6292  205.79944 355.75494 274.14044   0.     ]]

Pre-trained models

Now we can grab a pre-trained model to finetune from. Here we have so many choices from Detection Model Zoo. Again for demo purpose, we choose a fast SSD network with MobileNet1.0 backbone.

net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_voc', pretrained=True)

reset network to predict pikachus!

# now the output layers that used to map to VOC classes are now reset to distinguish pikachu (and background).

There is a convenient API for creating custom network with pre-trained weights. This is equivalent to loading pre-trained model and call net.reset_class.

net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_custom', classes=classes,
    pretrained_base=False, transfer='voc')

By loading from fully pre-trained models, you are not only loading base network weights (mobilenet for example), but also some additional blocks for object detection specifically.

Pretrained model from detection task is more relevant and adaptive than pretrained_base network which is usually trained on ImageNet for image classification task.

Therefore finetuning may converge significantly faster and better in some situations.

Finetuning is a new round of training


You will find a more detailed training implementation of SSD here: Download

def get_dataloader(net, train_dataset, data_shape, batch_size, num_workers):
    from import Tuple, Stack, Pad
    from import SSDDefaultTrainTransform
    width, height = data_shape, data_shape
    # use fake data to generate fixed anchors for target generation
    with autograd.train_mode():
        _, _, anchors = net(mx.nd.zeros((1, 3, height, width)))
    batchify_fn = Tuple(Stack(), Stack(), Stack())  # stack image, cls_targets, box_targets
    train_loader =
        train_dataset.transform(SSDDefaultTrainTransform(width, height, anchors)),
        batch_size, True, batchify_fn=batchify_fn, last_batch='rollover', num_workers=num_workers)
    return train_loader

train_data = get_dataloader(net, dataset, 512, 16, 0)

Try use GPU for training

    a = mx.nd.zeros((1,), ctx=mx.gpu(0))
    ctx = [mx.gpu(0)]
    ctx = [mx.cpu()]

Start training(finetuning)

trainer = gluon.Trainer(
    net.collect_params(), 'sgd',
    {'learning_rate': 0.001, 'wd': 0.0005, 'momentum': 0.9})

mbox_loss = gcv.loss.SSDMultiBoxLoss()
ce_metric = mx.metric.Loss('CrossEntropy')
smoothl1_metric = mx.metric.Loss('SmoothL1')

for epoch in range(0, 2):
    tic = time.time()
    btic = time.time()
    net.hybridize(static_alloc=True, static_shape=True)
    for i, batch in enumerate(train_data):
        batch_size = batch[0].shape[0]
        data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0)
        cls_targets = gluon.utils.split_and_load(batch[1], ctx_list=ctx, batch_axis=0)
        box_targets = gluon.utils.split_and_load(batch[2], ctx_list=ctx, batch_axis=0)
        with autograd.record():
            cls_preds = []
            box_preds = []
            for x in data:
                cls_pred, box_pred, _ = net(x)
            sum_loss, cls_loss, box_loss = mbox_loss(
                cls_preds, box_preds, cls_targets, box_targets)
        # since we have already normalized the loss, we don't want to normalize
        # by batch-size anymore
        ce_metric.update(0, [l * batch_size for l in cls_loss])
        smoothl1_metric.update(0, [l * batch_size for l in box_loss])
        name1, loss1 = ce_metric.get()
        name2, loss2 = smoothl1_metric.get()
        if i % 20 == 0:
            print('[Epoch {}][Batch {}], Speed: {:.3f} samples/sec, {}={:.3f}, {}={:.3f}'.format(
                epoch, i, batch_size/(time.time()-btic), name1, loss1, name2, loss2))
        btic = time.time()


[Epoch 0][Batch 0], Speed: 2.225 samples/sec, CrossEntropy=11.958, SmoothL1=1.986
[Epoch 0][Batch 20], Speed: 12.737 samples/sec, CrossEntropy=4.358, SmoothL1=1.231
[Epoch 0][Batch 40], Speed: 10.232 samples/sec, CrossEntropy=3.299, SmoothL1=0.966
[Epoch 1][Batch 0], Speed: 9.863 samples/sec, CrossEntropy=1.530, SmoothL1=0.312
[Epoch 1][Batch 20], Speed: 11.802 samples/sec, CrossEntropy=1.615, SmoothL1=0.463
[Epoch 1][Batch 40], Speed: 13.789 samples/sec, CrossEntropy=1.580, SmoothL1=0.463

Save finetuned weights to disk


Predict with finetuned model

We can test the performance using finetuned weights

test_url = ''
download(test_url, 'pikachu_test.jpg')
net = gcv.model_zoo.get_model('ssd_512_mobilenet1.0_custom', classes=classes, pretrained_base=False)
x, image ='pikachu_test.jpg', 512)
cid, score, bbox = net(x)
ax = viz.plot_bbox(image, bbox[0], score[0], cid[0], class_names=classes)


Downloading pikachu_test.jpg from

In two epochs and less than 5 min, we are able to detect pikachus perfectly!


This finetune tutorial is not limited to SSD, you can extend it to Faster-RCNN, YOLO training by adapting a training blocks in the following examples:

Download Download

Total running time of the script: ( 2 minutes 40.680 seconds)

Gallery generated by Sphinx-Gallery