Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
ZJUTER0126 03114584c0 | 1 year ago | |
---|---|---|
.idea | 1 year ago | |
scripts | 1 year ago | |
src | 1 year ago | |
README.md | 1 year ago | |
eval.py | 1 year ago | |
requriments.txt | 1 year ago | |
train.py | 1 year ago |
继Swin Transformer之后,微软在去年11月份发布了Swin Transformer V2,目前模型的实现以及预训练模型已经开源。Swin Transformer V2的核心是将模型扩展到更大的容量和分辨率,其中最大的模型SwinV2-G参数量达到了30亿,在物体检测任务上图像分辨率达到1536x1536,基于SwinV2-G的模型也在4个任务上达到了SOTA:在图像分类数据集ImageNet V2上达到了84.0%的top1准确度,在物体检测数据集COCO上达到了63.1/54.4的box/mask mAP,在语义分割数据集上ADE20K达到了59.9 mIoU,在视频分类数据集Kinetics-400上达到了86.8%,不过目前,只有COCO和ADE20K两个数据集上还保持SOTA。当将模型进行扩展时,作者发现面临两个主要的困难:一是大模型的训练变得不稳定,二是预训练模型增大窗口大小会出现性能下降。作者通过相应的优化策略重点解决这两个问题
使用的数据集:ImageNet2012
└─imagenet
├─train # 训练数据集
└─val # 评估数据集
采用混合精度
的训练方法,使用支持单精度和半精度数据来提高深度学习神经网络的训练速度,同时保持单精度训练所能达到的网络精度。混合精度训练提高计算速度、减少内存使用的同时,支持在特定硬件上训练更大的模型或实现更大批次的训练。
└── SwinTransformerV2
├── 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
│ │ │ ├── image22kto1k.txt # imagenet22k到imagenet1k的标签映射
│ │ │ └── pth2ckpt.ipynb # pytorch预训练权重转mindspore
│ │ └── swinv2_base_patch4_window12to16_192to256_22kto1k_ft.yaml # SwinTransformer的配置文件
│ ├── 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
│ │ └── swin_transformer_v2.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 # 训练文件
在swinv2_base_patch4_window12to16_192to256_22kto1k_ft.yaml中可以同时配置训练参数和评估参数。
配置SwinTransformerV2和ImageNet-1k数据集。
# Architecture 86.2%
arch: swinv2_base_patch4_window12to16_192to256_22kto1k_ft # SwinTransformerV2结构选择
# ===== Dataset ===== #
data_url: ../data/imagenet # 数据集地址
set: ImageNet # 数据集名称
num_classes: 1000 # 分类数据的类别数
mix_up: 0.8 # MixUp系数
cutmix: 1.0 # CutMix系数
auto_augment: rand-m9-mstd0.5-inc1 # 随机数据增强参数
interpolation: bicubic # 插值方法
re_prob: 0.25 # random_erasing参数
re_mode: pixel # random_erasing参数
re_count: 1 # random_erasing参数
mixup_prob: 1.0 # mix_up的概率
switch_prob: 0.5 # mixup和cutmix的转换概率
mixup_mode: batch # mixup的模式
image_size: 256 # 图像大小
crop_pct: 0.875 # 图像缩放比例
# ===== Learning Rate Policy ======== #
optimizer: adamw # 优化器类别
base_lr: 0.00005 # 基础学习率
warmup_lr: 0.00000002 # 学习率热身初始值
min_lr: 0.0000002 # 最小学习率
lr_scheduler: cosine_lr # 学习率变换策略
warmup_length: 5 # 学习率热身轮数
# ===== Network training config ===== #
amp_level: O1 # 混合精度级别
keep_bn_fp32: True # 是否保持BN为FP32运算
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: 32 # 单卡批次大小
drop_path_rate: 0.2 # drop path概率
# ===== Hardware setup ===== #
num_parallel_workers: 16 # 数据预处理线程数
device_target: Ascend # 设备选择
# ==== Model Config ===== #
# 预训练权重地址,自定义
pretrained: s3://open-data/SwinTransformer/src/configs/pth2ckpt/swinv2_base_patch4_window12_192_22k.ckpt
更多配置细节请参考脚本config.py
。 通过官方网站安装MindSpore后,您可以按照如下步骤进行训练和评估:
Ascend处理器环境运行
# 使用python启动单卡训练
python train.py --device_id 0 --device_target Ascend --config ./src/configs/swinv2_base_patch4_window12to16_192to256_22kto1k_ft.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/swinv2_base_patch4_window12to16_192to256_22kto1k_ft.yaml \
--pretrained ./ckpt_0/swinv2_base_patch4_window12to16_192to256_22kto1k_ft.ckpt > ./eval.log 2>&1 &
# 使用脚本启动单卡运行评估示例
bash ./scripts/run_eval_ascend.sh [DEVICE_ID] [CONFIG_PATH] [CHECKPOINT_PATH]
对于分布式训练,需要提前创建JSON格式的hccl配置文件。
请遵循以下链接中的说明:
参数 | Ascend |
---|---|
模型 | SwinTransformerV2 |
模型版本 | swinv2_base_patch4_window12to16_192to256_22kto1k_ft |
资源 | Ascend 910 16卡 |
上传日期 | 2022-11-04 |
MindSpore版本 | 1.5.1 |
数据集 | ImageNet-1k Train,共1,281,167张图像 |
训练参数 | epoch=30, batch_size=512 |
优化器 | AdamWeightDecay |
损失函数 | SoftTargetCrossEntropy |
损失 | 0.627 |
输出 | 概率 |
分类准确率 | 八卡:top1:86.35% top5:97.95% |
速度 | 16卡:619.446毫秒/步 |
训练耗时 | 14h51min03s(run on OpenI) |
请浏览官网主页
2022昇腾AI创新大赛昇思赛道 第一批 赛题二:利用MindSpore实现Swin Transformer V2图像分类网络
Python Shell Jupyter Notebook Text
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》