Nanny level uses PyTorch to train and evaluate its own MobileViT network tutorial

insert image description here

foreword

Project address: https://github.com/Fafa-DL/Awesome-Backbones

Operation tutorial: https://www.bilibili.com/video/BV1SY411P7Nd

MobileViT original paper: click me to jump

If you think that the warehouse only supports training one model, it is a big mistake. I put the currently supported 42 models (LeNet5, AlexNet, VGG, DenseNet, ResNet, Wide-ResNet, ResNeXt, SEResNet, SEResNeXt , RegNet, MobileNetV2, MobileNetV3, ShuffleNetV1, ShuffleNetV2, EfficientNet, RepVGG, Res2Net, ConvNeXt, HRNet, ConvMixer, CSPNet, Swin-Transformer, Vision-Transformer, Transformer-in-Transformer, MLP-Mixer, DeiT, Conformer, T2T-ViT , Twins, PoolFormer, VAN, HorNet, EfficientFormer, Swin Transformer V2, MViT V2, MobileViT, DaViT, RepLKNet, BEiT, EVA, MixMIM, EfficientNetV2), the usage is exactly the same . And at present, most of the image classification requirements are met, and students who are progressing fast even have papers under review

0. Environment construction & quick start

  • I also recently recorded a video of this step

The latest Windows configuration VSCode and Anaconda environment

"Image classification" builds from zero environment & starts quickly

  • If you don't want to watch the video, put the text version here. It is recommended to use Anaconda for environment management. The command to create the environment is as follows
conda create -n [name] python=3.6 其中[name]改成自己的环境名,如[name]->torch,conda create -n torch python=3.6
  • My test environment is as follows
torch==1.7.1
torchvision==0.8.2
scipy==1.4.1
numpy==1.19.2
matplotlib==3.2.1
opencv_python==3.4.1.15
tqdm==4.62.3
Pillow==8.4.0
h5py==3.1.0
terminaltables==3.1.0
packaging==21.3
  • First install Pytorch. The recommended version is the same as mine. Enter Pytorch official website, click install previous versions of PyTorch, take 1.7.1 as an example, the installation given on the official website is as follows, select the appropriate cuda version
# CUDA 11.0
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

# CUDA 10.2
pip install torch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2

# CUDA 10.1
pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

# CUDA 9.2
pip install torch==1.7.1+cu92 torchvision==0.8.2+cu92 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

# CPU only
pip install torch==1.7.1+cpu torchvision==0.8.2+cpu torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
  • After installing Pytorch , run
pip install -r requirements.txt
  • Download MobileNetV3-Small weights to datas
  • Terminal input under the Awesome-Backbones folder
python tools/single_test.py datas/cat-dog.png models/mobilenet/mobilenet_v3_small.py --classes-map datas/imageNet1kAnnotation.txt

1. Dataset production

1.1 Label file creation

  • Download the project code locally
    insert image description here

  • This demonstration takes the flower dataset as an example, and the directory structure is as follows:

├─flower_photos
│  ├─daisy
│  │      100080576_f52e8ee070_n.jpg
│  │      10140303196_b88d3d6cec.jpg
│  │      ...
│  ├─dandelion
│  │      10043234166_e6dd915111_n.jpg
│  │      10200780773_c6051a7d71_n.jpg
│  │      ...
│  ├─roses
│  │      10090824183_d02c613f10_m.jpg
│  │      102501987_3cdb8e5394_n.jpg
│  │      ...
│  ├─sunflowers
│  │      1008566138_6927679c8a.jpg
│  │      1022552002_2b93faf9e7_n.jpg
│  │      ...
│  └─tulips
│  │      100930342_92e8746431_n.jpg
│  │      10094729603_eeca3f2cb6.jpg
│  │      ...
  • Awesome-Backbones/datas/Create a label file in the file annotations.txt, which will be written to the file by line 类别名 索引;
daisy 0
dandelion 1
roses 2
sunflowers 3
tulips 4

insert image description here

1.2 Dataset division

  • OpenAwesome-Backbones/tools/split_data.py
  • Modification 原始数据集路径and 划分后的保存路径, it is strongly recommended not to change the saved path after division datasets. In the next step, the operation is based on the folder by default.
init_dataset = 'A:/flower_photos' # 改为你自己的数据路径
new_dataset = 'A:/Awesome-Backbones/datasets'
  • OpenAwesome-Backbones/ a terminal and enter the command:
python tools/split_data.py
  • The format of the divided data set is as follows:
├─...
├─datasets
│  ├─test
│  │  ├─daisy
│  │  ├─dandelion
│  │  ├─roses
│  │  ├─sunflowers
│  │  └─tulips
│  └─train
│      ├─daisy
│      ├─dandelion
│      ├─roses
│      ├─sunflowers
│      └─tulips
├─...

1.3 Preparation of dataset information files

  • Make sure that the divided data set is Awesome-Backbones/datasetsunder , if not, get_annotation.pymodify the data set path under;
datasets_path   = '你的数据集路径'
  • OpenAwesome-Backbones/ a terminal and enter the command:
python tools/get_annotation.py
  • Awesome-Backbones/datasThe generated dataset information file is obtained train.txtundertest.txt
    insert image description here

2. Modify the parameter file

  • Each model corresponds to its own configuration file, which is saved Awesome-Backbones/modelsin the following

  • A complete model is formed by backbone, neck, head,head.loss

  • Find the MobileViT parameter configuration file, you can see 所有支持的类型it all here,且每个模型均提供预训练权重
    insert image description here

  • model_cfgModify it num_classesto your own data set category size in

  • According to the performance of your own computer, data_cfgmodify batch_sizeandnum_workers

  • If there is a pre-training weight, you can pretrained_weightsset it to Trueand assign the path of the pre-training weight topretrained_weights

  • If you need to freeze the training, freeze_flag set it to True, the options for freezing are backbone, neck, head

  • optimizer_cfgModify the initial learning rate in , according to your own debugging batch size, 若使用了预训练权重, it is recommended学习率调小

  • See the learning rate update for details core/optimizers/lr_update.py, and also prepare the video "Image Classification" learning rate update strategy|optimizer

  • For more specific configuration file modification, please refer to the configuration file explanation , and also prepare the supplementary description of the video "image classification" configuration file

3. Training

  • Confirm Awesome-Backbones/datas/annotations.txtlabel is ready
  • Confirm and correspond to Awesome-Backbones/datas/_train.txttest.txtannotations.txt
  • Select the model you want to train, find the corresponding configuration file Awesome-Backbones/models/below , take mobilevit_s as an example
  • 配置文件解释Modify parameters according to
  • Awesome-BackbonesOpen a terminal in the path and run
python tools/train.py models/mobilevit/mobilevit_s.py

insert image description here

4. Evaluation

  • Confirm Awesome-Backbones/datas/annotations.txtlabel is ready
  • Confirm and correspond Awesome-Backbones/datas/_test.txtannotations.txt
  • Awesome-Backbones/models/Find the corresponding configuration file under
  • In the parameter configuration file 修改权重路径,其余不变
ckpt = '你的训练权重路径'
  • Awesome-BackbonesOpen a terminal in the path and run
python tools/evaluation.py models/mobilevit/mobilevit_s.py

insert image description here

  • Single image test , run in Awesome-Backbonesopen terminal
python tools/single_test.py datasets/test/dandelion/14283011_3e7452c5b2_n.jpg models/mobilevit/mobilevit_s.py

insert image description here
So far, if it is not running, go to station B to watch the video teaching that I will guide you to run~

5. Other Tutorials

In addition to the above, I have also prepared other necessary operation tutorials for you, all of which are placed on the GitHub project homepage, and pasted here for your convenience

Any updates will be notified on Github and Station B, remember to follow Star and Sanlian~

Guess you like

Origin blog.csdn.net/zzh516451964zzh/article/details/129442005