Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
LvanAI 36af600142 | 1 year ago | |
---|---|---|
src | 1 year ago | |
README.md | 1 year ago | |
eval.py | 1 year ago | |
train.py | 1 year ago |
Focal-Transformer通过结合细粒度的局部和粗粒度的全局交互,提出一种称为Focal attention的新注意力机制,其中每个token以细粒度关注其最近的周围token,而距离较远的token则以粗粒度关注,因此可以捕获短期和长期有效地范围视觉依赖。
使用的数据集:ImageNet2012
└─dataset
├─train # 训练数据集
└─val # 评估数据集
采用混合精度
的训练方法,使用支持单精度和半精度数据来提高深度学习神经网络的训练速度,同时保持单精度训练所能达到的网络精度。混合精度训练提高计算速度、减少内存使用的同时,支持在特定硬件上训练更大的模型或实现更大批次的训练。
├── Focal_Transformer
├── README_CN.md // Focal-Transformer相关说明
├── src
├──configs // Focal-Transformer的配置文件
├──data // 数据集配置文件
├──imagenet.py // imagenet配置文件
├──augment // 数据增强函数文件
┕──data_utils // modelarts运行时数据集复制函数文件
│ ├──models // 模型定义文件夹
├──focal // focal模型定义文件
│ ├──trainers // 自定义TrainOneStep文件
│ ├──tools // 工具文件夹
├──callback.py // 自定义回调函数,训练结束测试
├──cell.py // 一些关于cell的通用工具函数
├──criterion.py // 关于损失函数的工具函数
├──get_misc.py // 一些其他的工具函数
├──optimizer.py // 关于优化器和参数的函数
┕──schedulers.py // 学习率衰减的工具函数
├── train.py // 训练文件
├── eval.py // 评估文件
在config.py中可以同时配置训练参数和评估参数。
配置Focal-Transformer和ImageNet-1k数据集。
# Architecture
arch: small_focal_224 # Focal-Transformer结构选择
# ===== Dataset ===== #
data_url: ./data # 数据集地址
set: ImageNet # 数据集名字
num_classes: 1000 # 数据集分类数目
mix_up: 0.8 # MixUp数据增强参数
cutmix: 1.0 # CutMix数据增强参数
auto_augment: rand-m9-mstd0.5-inc1 # AutoAugment参数
interpolation: bicubic # 图像缩放插值方法
re_prob: 0.25 # 数据增强参数
re_mode: pixel # 数据增强参数
re_count: 1 # 数据增强参数
mixup_prob: 1. # 数据增强参数
switch_prob: 0.5 # 数据增强参数
mixup_mode: batch # 数据增强参数
# ===== Learning Rate Policy ======== #
optimizer: adamw # 优化器类别
base_lr: 0.0005 # 基础学习率
warmup_lr: 0.00000001 # 学习率热身初始学习率
min_lr: 0.000001 # 最小学习率
lr_scheduler: cosine_lr # 学习率衰减策略
warmup_length: 20 # 学习率热身轮数
nonlinearity: GELU # 激活函数类别
# ===== Network training config ===== #
amp_level: O1 # 混合精度策略
beta: [ 0.9, 0.999 ] # adamw参数
clip_global_norm_value: 5. # 全局梯度范数裁剪阈值
is_dynamic_loss_scale: True # 是否使用动态缩放
epochs: 300 # 训练轮数
label_smoothing: 0.1 # 标签平滑参数
weight_decay: 0.05 # 权重衰减参数
momentum: 0.9 # 优化器动量
batch_size: 128 # 批次
crop_pct: 0.815 # 测试图像resize比例
print_step: 400 # 打印损失值step
# ===== Hardware setup ===== #
num_parallel_workers: 16 # 数据预处理线程数
device_num: 8 # NPU数量
device_target: Ascend # 训练设备NPU Ascend910
# ===== Model config ===== # # 模型结构参数
drop_rate: 0.0
drop_path: 0.3
更多配置细节请参考脚本config.py
。 通过官方网站安装MindSpore后,您可以按照如下步骤进行训练和评估:
Ascend处理器环境运行
# 使用脚本启动多卡训练
mpirun -n 8 python train.py --config <config path> > train.log 2>&1 &
# 使用脚本启动单卡运行评估示例
python eval.py --config <config path>
参数 | Ascend |
---|---|
模型 | Focal-Transformer |
模型版本 | small_Focal |
资源 | Ascend 910 |
上传日期 | 2022-10-28 |
MindSpore版本 | 1.6.1 |
数据集 | ImageNet-1k Train,共1,281,167张图像 |
训练参数 | epoch=300, batch_size=128 |
优化器 | AdamWeightDecay |
损失函数 | CrossEntropy |
损失 | 0.7336 |
输出 | 概率 |
分类准确率 | 单卡:top1:83.6% top5:96.4% |
速度 | 8卡:6550毫秒/步 |
训练耗时 | 643h66min(run on ModelArts) |
如果你想参与贡献昇思的工作当中,请阅读昇思贡献指南和how_to_contribute
2022昇腾AI创新大赛昇思赛道 第一批赛题四:利用MindSpore实现Focal Transformer图像分类网络
Python
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》