Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
763954650@qq.com 9f82b11abb | 1 year ago | |
---|---|---|
scripts | 1 year ago | |
src | 1 year ago | |
README.md | 1 year ago | |
eval.py | 1 year ago | |
train.py | 1 year ago |
BEIT的流程如图1所示,它的上侧是一个dVAE模型,下侧是一个类似BERT的Encoder。dVAE由Tokenizer和Decoder组成,其中Tokenizer的作用是将图像的每个Patch编码成一个视觉标志,Decoder
的作用将视觉标志恢复成输入图像,dVAE的这一部分是借鉴了DALL-E的思想。BEIT的下面部分是BERT,它的输入是含有被掩码的图像的所有patch,预测的是dVAE生成的视觉标志,这一部分是借鉴的MAE的思想,不同的是MAE预测的是归一化后的图像细节。
使用的数据集:ImageNet2012
└─imagenet
├─train # 训练数据集
└─val # 评估数据集
采用混合精度
的训练方法,使用支持单精度和半精度数据来提高深度学习神经网络的训练速度,同时保持单精度训练所能达到的网络精度。混合精度训练提高计算速度、减少内存使用的同时,支持在特定硬件上训练更大的模型或实现更大批次的训练。
└── BEIT
├── eval.py # 推理文件
├── export.py # 权重导出文件
├── README_CN.md # 说明文件
├── requriments.txt #
├── scripts
│ ├── run_distribute_train_ascend.sh # 卡Ascend910训练脚本
│ ├── run_eval_ascend.sh # Ascend测试脚本
│ └── run_standalone_train_ascend.sh # 单卡Ascend910训练脚本
├── src
│ ├── args.py # 参数文件
│ ├── configs
│ │ ├── parser.py
│ │ ├── pth2ckpt
│ │ │ └── pth2ckpt.ipynb # pytorch预训练权重转mindspore
│ │ └── beit_base_patch16_224.yaml # BEIT的配置文件
│ ├── data
│ │ ├── augment # 数据增强文件
│ │ │ ├── auto_augment.py
│ │ │ ├── __init__.py
│ │ │ ├── mixup.py
│ │ │ ├── random_erasing.py
│ │ │ └── transforms.py
│ │ ├── data_utils # obs交互文件
│ │ │ ├── __init__.py
│ │ │ └── moxing_adapter.py
│ │ ├── imagenet.py # ImageNet数据类
│ │ └── __init__.py
│ ├── models # 模型定义
│ │ ├── __init__.py
│ │ ├── layers
│ │ │ ├── drop_path.py
│ │ │ └── identity.py
│ │ └── beit.py
│ ├── tools
│ │ ├── callback.py # 回调函数
│ │ ├── cell.py # 关于cell的自定义类
│ │ ├── criterion.py # 损失函数
│ │ ├── get_misc.py # 功能函数
│ │ ├── __init__.py
│ │ ├── optimizer.py # 优化器文件
│ │ └── schedulers.py # 学习率策略
│ └── trainer
│ └── train_one_step.py # 自定义单步训练
└── train.py # 训练文件
在beit_base_patch16_224.yaml中可以同时配置训练参数和评估参数。
配置BEIT和ImageNet-1k数据集。
# Architecture 85.2%
arch: beit_base_patch16_224
# ===== Dataset ===== #
data_url: ../data/imagenet
set: ImageNet
num_classes: 1000
mix_up: 0.
cutmix: 0.
auto_augment: rand-m9-mstd0.5-inc1
interpolation: bicubic
re_prob: 0.25
re_mode: pixel
re_count: 1
mixup_prob: 1.0
switch_prob: 0.5
mixup_mode: batch
image_size: 224
crop_pct: 0.875
# ===== Learning Rate Policy ======== #
optimizer: adamw
base_lr: 0.00002
warmup_lr: 0.000001
min_lr: 0.000001
lr_scheduler: cosine_lr
warmup_length: 5
layer_decay: 0.85
# ===== Network training config ===== #
amp_level: O1
keep_bn_fp32: True
beta: [ 0.9, 0.999 ]
clip_global_norm_value: 5.
is_dynamic_loss_scale: True
epochs: 30
cooldown_epochs: 0
label_smoothing: 0.1
weight_decay: 0.00000001
momentum: 0.9
batch_size: 64
drop_path_rate: 0.1
pretrained: s3://open-data/beit/src/beit_base_patch16_224_pt22k_ft22k.ckpt
# ===== Hardware setup ===== #
num_parallel_workers: 16
device_target: Ascend
# ===== Model Config ===== #
rel_pos_bias: True
abs_pos_emb: False
layer_scale_init_value: 0.1
更多配置细节请参考脚本beit_base_patch16_224.yaml
。 通过官方网站安装MindSpore后,您可以按照如下步骤进行训练和评估:
Ascend处理器环境运行
# 使用python启动单卡训练
python train.py --device_id 0 --device_target Ascend --config ./src/configs/beit_base_patch16_224.yaml \
> train.log 2>&1 &
# 使用脚本启动单卡训练
bash ./scripts/run_standalone_train_ascend.sh [DEVICE_ID] [CONFIG_PATH]
# 使用脚本启动多卡训练
bash ./scripts/run_distribute_train_ascend.sh [RANK_TABLE_FILE] [CONFIG_PATH]
# 使用python启动单卡运行评估示例
python eval.py --device_id 0 --device_target Ascend --config ./src/configs/beit_base_patch16_224.yaml \
--pretrained ./ckpt_0/beit_base_patch16_224.ckpt > ./eval.log 2>&1 &
# 使用脚本启动单卡运行评估示例
bash ./scripts/run_eval_ascend.sh [DEVICE_ID] [CONFIG_PATH] [CHECKPOINT_PATH]
对于分布式训练,需要提前创建JSON格式的hccl配置文件。
请遵循以下链接中的说明:
参数 | Ascend |
---|---|
模型 | BEIT |
模型版本 | beit_base_patch16_224 |
资源 | Ascend 910 8卡 |
上传日期 | 2022-11-04 |
MindSpore版本 | 1.5.1 |
数据集 | ImageNet-1k Train,共1,281,167张图像 |
训练参数 | epoch=30, batch_size=512 |
优化器 | AdamWeightDecay |
损失函数 | SoftTargetCrossEntropy |
损失 | 1.502 |
输出 | 概率 |
分类准确率 | 八卡:top1:85.27% top5:97.67% |
速度 | 8卡:546.446毫秒/步 |
训练耗时 | 13h45min03s(run on OpenI) |
请浏览官网主页
2022昇腾AI创新大赛昇思赛道 第一批 赛题十:利用MindSpore实现BEiT图像分类网络
Python Jupyter Notebook Shell
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》