Note 5 of the second phase of AI combat camp - MMPretrain code class

Summary

MMPretrain is a newly upgraded pre-training open source algorithm framework, which aims to provide a variety of powerful pre-training backbone networks and supports different pre-training strategies. MMPretrain is derived from the well-known open source projects MMClassification and MMSelfSup and has developed many exciting new features. Currently, the pre-training stage is crucial for visual recognition, and with rich and powerful pre-trained models, we are able to improve various downstream vision tasks.
insert image description here

【Course Link】https://www.bilibili.com/video/BV1Ju4y1Z7ZE
【Lecturer Introduction】Ma Zerun OpenMMLab Algorithm Engineer
insert image description here

MMPreTrain in action

Install

git clone https://github.com/open-mmlab/mmpretrain.git
cd mmpretrain
pip install -U openmim
mim install -e ".[multimodal]"

insert image description here
multimodal: Represents the installation of additional multimodal models
insert image description here

reasoning

Call the script to implement reasoning, the code is as follows:

python demo/image_demo.py ${IMAGE_FILE} ${CONFIG_FILE} ${CHECKPOINT_FILE}

OR using the API

get_model Obtain the model list_models through the model name or model configuration file
List all available model names in MMPretrain
inference_model Use the reasoner corresponding to the task of the model for inference
Call the ResNet example code:

import mmpretrain
print(mmpretrain.__version__)

from mmpretrain import get_model, list_models, inference_model
print(list_models(task='Image Classification', pattern  = 'resnet18'))

print(list_models(task='Image Caption', pattern='blip'))
model = get_model('resnet18_8xb16_cifar10')
print(type(model))

model  =  get_model( 'resnet18_8xb32_in1k')
print(type(model.backbone))

insert image description here
Load pre-trained model inference

inference_model('blip-base_3rdparty_caption','demo/cat-dog.png',show=True)

insert image description here

data set

CustomDataset OR ImageNet

Subfolder method
Label file method
#Configuration file
Example: resnet18_8xb32_in1k.py

_base_ = [
    '../_base_/models/resnet18.py',           # 模型配置
    '../_base_/datasets/imagenet_bs32.py',    # 数据配置
    '../_base_/schedules/imagenet_bs256.py',  # 训练策略配置
    '../_base_/default_runtime.py'            # 默认运行设置
]

training and testing

train

python tools/train.py ${
    
    CONFIG_FILE} [ARGS]

test

python tools/test.py ${
    
    CONFIG_FILE} ${
    
    CHECKPOINT_FILE} [ARGS]

fine-tuning

Dataset address: https://download.openmmlab.com/mmclassification/dataset/cats_dogs_dataset.tar
Create a new data folder under MMpretrain, then decompress the dataset and put it in.
To view the model, enter the command:

ls configs

insert image description here
View the configuration file of a specific model, take ResNet as an example:

ls configs/resnet

insert image description here
Open the file resnet18_8xb32_in1k.py to view the specific content:

_base_ = [
    '../_base_/models/resnet18.py', '../_base_/datasets/imagenet_bs32.py',
    '../_base_/schedules/imagenet_bs256.py', '../_base_/default_runtime.py'
]

../_base_/models/resnet18.py: Model file, implement the model through configuration.
../_base_/datasets/imagenet_bs32.py: Dataset configuration.
../_base_/schedules/imagenet_bs256.py:Planning configuration
../_base_/default_runtime.py:
Configuration modification of running configuration default_runtime:

   # save checkpoint per epoch.
    checkpoint=dict(type='CheckpointHook', interval=1,max_keep_ckpts=5,sava_best="auto"),

Save the best 5 models, automatically save the best model.

Set the category, and the pre-trained weight path.
insert image description here
Set to custom dataset format
insert image description here
Configure dataset path
insert image description here
Training

mim train mmpretrain configs/resnet/resnet50.py --work-dir=./exp

insert image description here
test

mim test mmpretrain configs/resnet/resnet50.py --checkpoint exp/epoch_41.pth

insert image description here

Analysis results:
insert image description here

Guess you like

Origin blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/131081973