Are you sure you want to delete this task? Once this task is deleted, it cannot be recovered.
ZJUTER0126 9fff213da6 | 1 year ago | |
---|---|---|
scripts | 1 year ago | |
src | 1 year ago | |
README.md | 1 year ago | |
__init__.py | 1 year ago | |
eval.py | 1 year ago | |
export.py | 1 year ago | |
requriments.txt | 1 year ago | |
train.py | 1 year ago |
自从ViT提出之后,在过去的一年里(2021年),基于transformer的模型在计算机视觉各个领域全面超越CNN模型。然而,这很大程度上都归功于Local Vision Transformer模型,Swin Transformer是其中重要代表。原生的ViT模型其计算量与图像大小的平方成正比,而Local Vision Transformer模型由于采用local attention(eg. window attention),其计算量大幅度降低,除此之外,Local Vision Transformer模型往往也采用金字塔结构,这使得它更容易应用到密集任务如检测和分割中,因为密集任务往往输入图像分辨率较高,而且也需要多尺度特征(eg. FPN)。虽然Local Vision Transformer模型超越了CNN模型,但是它却越来越像CNN了:首先locality是卷积所具有的特性,其次金字塔结构也是主流CNN模型所采用的设计。
近日,MetaAI在论文A ConvNet for the 2020s中从ResNet出发并借鉴Swin Transformer提出了一种新的CNN模型:ConvNeXt,其效果无论在图像分类还是检测分割任务上均能超过Swin Transformer,而且ConvNeXt和vision transformer一样具有类似的scalability(随着数据量和模型大小增加,性能同比提升)。
使用的数据集:ImageNet2012
└─dataset
├─train # 训练数据集
└─val # 评估数据集
采用混合精度
的训练方法,使用支持单精度和半精度数据来提高深度学习神经网络的训练速度,同时保持单精度训练所能达到的网络精度。混合精度训练提高计算速度、减少内存使用的同时,支持在特定硬件上训练更大的模型或实现更大批次的训练。
├── convnext
├── README_CN.md // ConvNeXt相关说明
├── scripts
├──run_standalone_train_ascend.sh // 单卡Ascend910训练脚本
├──run_distribute_train_ascend.sh // 多卡Ascend910训练脚本
├──run_eval_ascend.sh // 测试脚本
├── src
├──configs // ConvNeXt的配置文件
├──data // 数据集配置文件
├──imagenet.py // imagenet配置文件
├──augment // 数据增强函数文件
┕──data_utils // modelarts运行时数据集复制函数文件
│ ├──models // 模型定义文件夹
┕──ConvNeXt // ConvNeXt定义文件
│ ├──trainers // 自定义TrainOneStep文件
│ ├──tools // 工具文件夹
├──callback.py // 自定义回调函数,训练结束测试
├──cell.py // 一些关于cell的通用工具函数
├──criterion.py // 关于损失函数的工具函数
├──get_misc.py // 一些其他的工具函数
├──optimizer.py // 关于优化器和参数的函数
┕──schedulers.py // 学习率衰减的工具函数
├── train.py // 训练文件
├── eval.py // 评估文件
在config.py中可以同时配置训练参数和评估参数。
配置ConvNeXt和ImageNet-1k数据集。
# Architecture
arch: convnext_tiny # ConvNeXt结构选择
# ===== 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 # AutoAugment参数
interpolation: bicubic # 图像缩放插值方法
re_prob: 0 # RandomErasing概率
re_mode: pixel # RandomErasing模式
re_count: 1 # RandomErasing重复次数
mixup_prob: 1. # MixUp概率
switch_prob: 0.5 # MixUp和CutMix切换概率
mixup_mode: batch # MixUp模式
# ===== Learning Rate Policy ======== #
optimizer: adamw # 优化器类别
base_lr: 0.0005 # 基础学习率
warmup_lr: 0.00000007 # 学习率热身初始学习率
min_lr: 0.000006 # 最小学习率
lr_scheduler: cosine_lr # 学习率衰减策略
warmup_length: 20 # 学习率热身轮数
nonlinearity: GELU # 激活函数类别
image_size: 224 # 图像大小
# ===== 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 # 批次
# ===== EMA ===== #
with_ema: False # 是否使用ema更新
ema_decay: 0.9999 # ema移动系数
# ===== Hardware setup ===== #
num_parallel_workers: 16 # 数据预处理线程数
device_target: Ascend # GPU或者Ascend
更多配置细节请参考脚本config.py
。 通过官方网站安装MindSpore后,您可以按照如下步骤进行训练和评估:
Ascend处理器环境运行
# 使用python启动单卡训练
python train.py --device_id 0 --device_target Ascend --config ./src/configs/convnext_tiny.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/convnext_tiny.yaml \
--pretrained ./ckpt_0/convnext_tiny.ckpt > ./eval.log 2>&1 &
# 使用脚本启动单卡运行评估示例
bash ./scripts/run_eval_ascend.sh [DEVICE_ID] [CONFIG_PATH] [CHECKPOINT_PATH]
对于分布式训练,需要提前创建JSON格式的hccl配置文件。
请遵循以下链接中的说明:
oepni启智集群训练
1. 选择合适的MindSpore版本, 选择启动文件为train.py, 选择数据集为公开数据集中的imagenet.zip
2. 选择合适的计算资源, 选择Ascend处理器个数
3. 配置运行参数
4. 开始运行, 启动页面会自行给定train_url和data_url, 自动启动训练脚本
# 使用openi启动单卡训练
配置 device_id 0
device_target Ascend
run_modelarts True
config convnext_tiny.yaml
device_num 1
# 使用openi启动多卡训练
配置 device_target Ascend
run_modelarts True
config convnext_tiny.yaml
device_num {根据选择的资源情况自行设置}
python export.py --pretrained [CKPT_FILE] --config [CONFIG_PATH] --device_target [DEVICE_TARGET]
导出的模型会以模型的结构名字命名并且保存在当前目录下
参数 | Ascend |
---|---|
模型 | ConvNeXt |
模型版本 | convnext_tiny |
资源 | Ascend 910 |
上传日期 | 2022-01-23 |
MindSpore版本 | 1.3.0 |
数据集 | ImageNet-1k Train,共1,281,167张图像 |
训练参数 | epoch=300, batch_size=256 |
优化器 | AdamWeightDecay |
损失函数 | SoftTargetCrossEntropy |
损失 | 0.8114 |
输出 | 概率 |
分类准确率 | 八卡:top1:82.072% top5:95.694% |
速度 | 16卡:1106.875毫秒/步 |
训练耗时 | 31h36min04s(run on ModelArts) |
请浏览官网主页
自从ViT提出之后,在过去的一年里(2021年),基于transformer的模型在计算机视觉各个领域全面超越CNN模型。然而,这很大程度上都归功于Local Vision Transformer模型,Swin Transformer是其中重要代表。原生的ViT模型其计算量与图像大小的平方成正比,而Local Vision Transformer模型由于采用local attention(eg. window attention),其计算量大幅度降低,除此之外,Local Vision Transform
Python 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》