Teach you step by step how to use the MMSelfSup framework [1]

introduce

Task introduction

Self-supervised learning (Self-supervised learning, SSL) is a potential learning paradigm, which aims to use massive unlabeled data for representation learning. In SSL, we train the model by constructing a reasonable pre-training task (which can automatically generate labels, that is, self-supervision), and learn a pre-training model with powerful modeling capabilities. Based on the training model obtained by self-supervised learning, we can improve the performance of various downstream vision tasks (image classification, object detection, semantic segmentation, etc.).

MMSelfSup

MMSelfSup is an open source self-supervised representation learning toolbox based on PyTorch.
The framework is as follows:
insert image description here

Its features are:

  • Multi-method integration
    MMSelfSup provides a variety of cutting-edge self-supervised learning algorithms, and most of the self-supervised pre-training learning settings are the same to obtain a fairer comparison in the benchmark.
  • Modular design
    MMSelfSup complies with the consistent design philosophy of the OpenMMLab project and carries out a modular design, which is convenient for users to customize and implement their own algorithms.
  • Standardized performance evaluation
    MMSelfSup has rich benchmarks for evaluation and testing, including linear evaluation, SVM / Low-shot SVM of linear features, semi-supervised classification, object detection and semantic segmentation.
  • Compatibility
    Compatible with OpenMMLab's major algorithm libraries, with a wealth of downstream evaluation tasks and applications of pre-trained models.

step

First make sure pytorch is installed.

1. Install the mmcv library

pip install openmim
mim install mmcv

### 2. Download the mmselfsup project

git clone https://github.com/open-mmlab/mmselfsup.git

You can use git, or you can download the compressed package on github and unzip it

3. Enter the project main folder and install the required third-party packages

cd mmselfsup-main
pip install -e .

4. Find the configuration file of the selected model, the file path is in: configs/selfsup

The files under configs/selfsup are shown in the figure below: the name
insert image description here
of the folder is the name of the model , and beit stands for the BEIT model.
Here I choose simsiam.

5. Enter this folder, there are some configuration files under this folder for the subsequent training process

The situation under this folder is as follows:
insert image description here
the naming rules of the configuration files are:

{模型信息}_{模块信息}_{训练信息}_{数据集信息}

simsiam_resnet50_8xb32-coslr-100e_in1k.py indicates that the model name is simsiam, the modules used in it include resnet50, the training is 8 cards, the batch size is 32, the learning rate change function of cos type is used, and the data set is imagenet1k.
The inside of the configuration file is:
insert image description here
the inherited configuration file address is: configs/selfsup/ base , as shown below:
insert image description here

6. View the inherited configuration file of the dataset

Introduction to the document

Because the path of the dataset we use is different from the path set in the inherited configuration file, it needs to be modified.
To do this, you need to first look at what is in the inherited configuration file.
First, you need to modify the root directory of the data set:
insert image description here
then change the relative path of the label file and data file to your own.
insert image description here

Construction of label files

It should be noted that, except for the VOC type, the general dataset folder structure is:

data
│   ├── datasetname
│   │   ├── meta
│   │   ├── train
│   │   ├── val

Among them, there are label files in the meta folder, and data files , such as pictures, are stored in the train and val folders . (train and val can also be placed in a folder)
For supervised training, the basic composition of the label file is:
insert image description here
the left side is the relative path of the data file, and the right side is the corresponding label.
The construction of the label file mmselfsup provides us with a sample, the file path is: tools/dataset_converters/convert_imagenet_subsets.py , just modify it according to its ideas.

7. Build your own configuration file

Create a new file in the folder where the configuration file is located. The naming method is the same as that mentioned before, which is easy to understand.
Then make corresponding modifications according to your actual situation. Note that the defined variable names are not defined casually when modifying, and must be consistent with the names in the inherited files . The general process is described below.
First choose which file to inherit, here I choose: simsiam_resnet50_8xb32-coslr-200e_in1k.py

_base_ = 'simsiam_resnet50_8xb32-coslr-200e_in1k.py'

Note that _base_ is used, and the one on the right is equivalent to the path where the configuration file is located.

Then change the learning rate, because it does not use the same settings as the inherited configuration file, so it needs to be modified. The inherited config file is using 8*32 and I am using 1*32, so the learning rate is divided by 8.

lr = 0.05 / 8
optimizer = dict(lr=lr)

Then modify the relevant settings of the data set, such as: data_root

data_root = '/root/dataset/food-101/'
train_dataloader = dict(
    dataset=dict(
        ann_file='meta/food101_train.txt',
        data_prefix=dict(img_path='images/')
    )
)

Finally, modify the number of training times, how many times the model is saved and the interval between printing logs.

default_hooks = dict(
    logger=dict(interval=10),
    checkpoint=dict(interval=20, max_keep_ckpts=5)
)
train_cfg = dict(max_epochs=200)

8. Conduct training

The training code is in tools/train.py, you can also use the dist_train.sh file. Among them, train.py is suitable for a single card, and dist_train.sh can perform distributed training.

The code for training using dist_train.sh is as follows:

bash dist_train.sh ${
    
    CONFIG} ${
    
    GPUS} --cfg-options model.pretrained=${
    
    PRETRAIN}

The code for training using train.py is as follows:

python train.py ${
    
    CONFIG_FILE} [optional arguments]

Such as: python train.py /root/Project/mmselfsup-main/configs/selfsup/simsiam/simsiam_resnet50_1xb32-coslr-200e_food101.py

If you want to run in the background, change it to: nohup python train.py /root/Project/mmselfsup-main/configs/selfsup/simsiam/simsiam_resnet50_1xb32-coslr-200e_food101.py &

epilogue

The openmm series is a very good open source framework for deep learning, which integrates many excellent algorithms, which are worth learning from and can save a lot of time for writing code by yourself.

Guess you like

Origin blog.csdn.net/qq_41234663/article/details/131297707