finetune.md 3.2 KB
Newer Older
L
linjintao 已提交
1 2
# Tutorial 1: Finetuning Models

L
linjintao 已提交
3
This tutorial provides instructions for users to use the pre-trained models
L
linjintao 已提交
4 5 6 7
to finetune them on other datasets, so that better performance can be get.

There are two steps to finetune a model on a new dataset.

L
linjintao 已提交
8
1. Add support for the new dataset. See [Tutorial 2: Adding New Dataset](new_dataset.md).
L
linjintao 已提交
9 10 11
1. Modify the configs. This will be discussed in this tutorial.

For example, if the user want to finetune models pre-trained on Kinetics-400 Dataset to another dataset, say UCF101,
L
linjintao 已提交
12
then four parts in the config (see [here](../config_recognition.md)) needs attention.
L
linjintao 已提交
13

14
## Modify Head
L
linjintao 已提交
15 16 17 18 19

The `num_classes` in the `cls_head` need to be changed to the class number of the new dataset.
The weights of the pre-trained models are reused except for the final prediction layer.
So it is safe to change the class number.
In our case, UCF101 has 101 classes.
20
So we change it from 400 (class number of Kinetics-400) to 101.
L
linjintao 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

```python
model = dict(
    type='Recognizer2D',
    backbone=dict(
        type='ResNet',
        pretrained='torchvision://resnet50',
        depth=50,
        norm_eval=False),
    cls_head=dict(
        type='TSNHead',
        num_classes=101,   # change from 400 to 101
        in_channels=2048,
        spatial_type='avg',
        consensus=dict(type='AvgConsensus', dim=1),
        dropout_ratio=0.4,
        init_std=0.01))
```

L
linjintao 已提交
40
Note that the `pretrained='torchvision://resnet50'` setting is used for initializing backbone.
L
linjintao 已提交
41 42 43 44
If you are training a new model from ImageNet-pretrained weights, this is for you.
However, this setting is not related to our task at hand.
What we need is `load_from`, which will be discussed later.

45
## Modify Dataset
L
linjintao 已提交
46

J
JoannaLXY 已提交
47
MMAction2 supports UCF101, Kinetics-400, Moments in Time, Multi-Moments in Time, THUMOS14,
L
linjintao 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
Something-Something V1&V2, ActivityNet Dataset.
The users may need to adapt one of the above dataset to fit for their special datasets.
In our case, UCF101 is already supported by various dataset types, like `RawframeDataset`,
so we change the config as follows.

```python
# dataset settings
dataset_type = 'RawframeDataset'
data_root = 'data/ucf101/rawframes_train/'
data_root_val = 'data/ucf101/rawframes_val/'
ann_file_train = 'data/ucf101/ucf101_train_list.txt'
ann_file_val = 'data/ucf101/ucf101_val_list.txt'
ann_file_test = 'data/ucf101/ucf101_val_list.txt'

```

64
## Modify Training Schedule
L
linjintao 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77

Finetuning usually requires smaller learning rate and less training epochs.

```python
# optimizer
optimizer = dict(type='SGD', lr=0.005, momentum=0.9, weight_decay=0.0001)  # change from 0.01 to 0.005
optimizer_config = dict(grad_clip=dict(max_norm=40, norm_type=2))
# learning policy
lr_config = dict(policy='step', step=[40, 80])
total_epochs = 50 # change from 100 to 50
checkpoint_config = dict(interval=5)
```

78
## Use Pre-Trained Model
L
linjintao 已提交
79 80 81 82
To use the pre-trained model for the whole network, the new config adds the link of pre-trained models in the `load_from`.

```python
# use the pre-trained model for the whole TSN network
L
linjintao 已提交
83
load_from = 'https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmaction/mmaction-v1/recognition/tsn_r50_1x1x3_100e_kinetics400_rgb/tsn_r50_1x1x3_100e_kinetics400_rgb_20200614-e508be42.pth'  # model path can be found in model zoo
L
linjintao 已提交
84
```