|
1 | 1 | # Fine-tune with A Custom Dataset
|
2 | 2 |
|
3 |
| -This document introduces the process for fine-tuning a custom dataset in MindCV and the implementation of fine-tuning techniques such as reading the dataset online, setting the learning rate for specific layers, freezing part of the parameters, etc. The main code is in./example/finetune.py, you can make changes to it based on this tutorial as needed. |
| 3 | +This document introduces the process for fine-tuning a pre-trained model from MindCV on a custom dataset and the implementation of fine-tuning techniques such as reading the dataset online, setting the learning rate for specific layers, freezing part of the parameters, etc. The main code is in./example/finetune.py, you can make changes to it based on this tutorial as needed. |
4 | 4 |
|
5 | 5 | Next, we will use the FGVC-Aircraft dataset as an example to show how to fine-tune the pre-trained model mobilenet v3-small. [Fine-Grained Visual Classification of Aircraft](https://www.robots.ox.ac.uk/~vgg/data/fgvc-aircraft/) is a commonly used fine-grained image Classification benchmark dataset, which contains 10,000 aircraft images from 100 different types of aircraft (a.k.a variants), that is, 100 images for each aircraft type.
|
6 | 6 |
|
@@ -194,10 +194,10 @@ For small-size custom datasets, it is suggested that data augmentation can be us
|
194 | 194 |
|
195 | 195 | Referring to [Stanford University CS231n](https://cs231n.github.io/transfer-learning/#tf), **fine-tuning all the parameters**, **freezing feature network**, and **setting learning rates for specific layers** are commonly used fine-tuning skills. The first one uses pre-trained weights to initialize the parameters of the target model, and then updates all parameters based on the new dataset, so it's usually time-consuming but will get a high precision. Freezing feature networks are divided into freezing all feature networks(linear probe) and freezing partial feature networks. The former uses the pre-trained model as a feature extractor and only updates the parameters of the full connection layer, which takes a short time but has low accuracy; The latter generally freezes the parameters of shallow layers, which only learn the basic features of images, and only updates the parameters of the deep network and the full connection layer. Setting learning rate for specific layers is similar but more elaborate, it specifies the learning rates used by certain layers during training.
|
196 | 196 |
|
197 |
| -For hyper-parameters used in fine-tuning training, you can refer to the configuration file used when pre-training on the ImageNet-1k dataset in ./configs. Note that for fine-tuning, <font color=DarkRed>the hyper-parameter `pretrained` should be set to be `True` </font>to load the pre-training weight, <font color=DarkRed> `num_classes` should be set to be the number of labels </font>of the custom dataset (e.g. 100 for the Aircraft dataset here), moreover, don't forget to <font color=DarkRed>reduce batch_size and epoch_size </font>based on the size of the custom dataset. In addition, since the pre-trained weight already contains a lot of information for identifying images, in order not to destroy this information too much, it is also necessary to<font color=DarkRed> reduce the learning rate `lr` </font>, and it is also recommended to start training and adjust from at most one-tenth of the pre-trained learning rate or 0.0001. These parameters can be modified in the configuration file or added in the shell command as shown below. The training results can be viewed in the file ./ckpt/results.txt. |
| 197 | +For hyper-parameters used in fine-tuning training, you can refer to the configuration file used when pre-trained on the ImageNet-1k dataset in ./configs. Note that for fine-tuning, <font color=DarkRed>the hyper-parameter `pretrained` should be set to be `True` </font>to automatically downloaded and load the pre-trained weight, the parameters of the classification layer will be automatically removed during the process because the `num_classes` is not the default 1000 (but if you want to load a checkpoint file from local directory, do remember to set `pretrained` to be `False`, set the `ckpt_path` and manually delete the parameters of classifier before everything), <font color=DarkRed> `num_classes` should be set to be the number of labels </font>of the custom dataset (e.g. 100 for the Aircraft dataset here), moreover, don't forget to <font color=DarkRed>reduce batch_size and epoch_size </font>based on the size of the custom dataset. In addition, since the pre-trained weight already contains a lot of information for identifying images, in order not to destroy this information too much, it is also necessary to<font color=DarkRed> reduce the learning rate `lr` </font>, and it is also recommended to start training and adjust from at most one-tenth of the pre-trained learning rate or 0.0001. These parameters can be modified in the configuration file or added in the shell command as shown below. The training results can be viewed in the file ./ckpt/results.txt. |
198 | 198 |
|
199 | 199 | ```bash
|
200 |
| -python .examples/finetune/finetune.py --config=./configs/mobilenetv3/mobilnet_v3_small_ascend.yaml --data_dir=./aircraft/data --pretrained=True |
| 200 | +python .examples/finetune/finetune.py --config=./configs/mobilenetv3/mobilnet_v3_small_ascend.yaml --data_dir=./aircraft/data --num_classes=100 --pretrained=True ... |
201 | 201 | ```
|
202 | 202 |
|
203 | 203 | When fine-tuning mobilenet v3-small based on Aircraft dataset, this tutorial mainly made the following changes to the hyper-parameters:
|
|
0 commit comments