-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] add model script, training recipe and training weights of DeiT #511
Open
TamirBaydasov
wants to merge
4
commits into
mindspore-lab:main
Choose a base branch
from
TamirBaydasov:deit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
# DeiT | ||
> [Training data-efficient image transformers & distillation through attention](https://arxiv.org/abs/2012.12877) | ||
|
||
## Introduction | ||
|
||
DeiT: Data-efficient Image Transformers | ||
|
||
## Results | ||
|
||
**Implementation and configs for training were taken and adjusted from [this repository](https://gitee.com/cvisionlab/models/tree/deit/release/research/cv/DeiT), which implements Twins models in mindspore.** | ||
|
||
Our reproduced model performance on ImageNet-1K is reported as follows. | ||
|
||
<div align="center"> | ||
|
||
| Model | Context | Top-1 (%) | Top-5 (%) | Params (M) | Recipe | Download | | ||
|----------|----------|-----------|-----------|------------|-----------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------| | ||
| deit_base | Converted from PyTorch | 81.62 | 95.58 | - | [yaml](https://github.com/mindspore-lab/mindcv/blob/main/configs/deit/deit_b_gpu.yaml) | [weights](https://storage.googleapis.com/huawei-mindspore-hk/DeiT/Converted/deit_base_patch16_224.ckpt) | | ||
| deit_base | 8xRTX3090 | 72.29 | 89.93 | - | [yaml](https://github.com/mindspore-lab/mindcv/blob/main/configs/deit/deit_b_gpu.yaml) | [weights](https://storage.googleapis.com/huawei-mindspore-hk/DeiT/deit_base_patch16_224_acc%3D0.725.ckpt) | ||
| deit_small | Converted from PyTorch | 79.39 | 94.80 | - | [yaml](https://github.com/mindspore-lab/mindcv/blob/main/configs/deit/deit_b_gpu.yaml) | [weights](https://storage.googleapis.com/huawei-mindspore-hk/DeiT/Converted/deit_small_patch16_224.ckpt) | | ||
| deit_tiny | Converted from PyTorch | 71.58 | 90.76 | - | [yaml](https://github.com/mindspore-lab/mindcv/blob/main/configs/deit/deit_b_gpu.yaml) | [weights](https://storage.googleapis.com/huawei-mindspore-hk/DeiT/Converted/deit_tiny_patch16_224.ckpt) | | ||
|
||
</div> | ||
|
||
#### Notes | ||
|
||
- Context: The weights in the table were taken from [official repository](https://github.com/facebookresearch/deit) and converted to mindspore format | ||
- Top-1 and Top-5: Accuracy reported on the validation set of ImageNet-1K. | ||
|
||
## Quick Start | ||
|
||
### Preparation | ||
|
||
#### Installation | ||
Please refer to the [installation instruction](https://github.com/mindspore-ecosystem/mindcv#installation) in MindCV. | ||
|
||
#### Dataset Preparation | ||
Please download the [ImageNet-1K](https://www.image-net.org/challenges/LSVRC/2012/index.php) dataset for model training and validation. | ||
|
||
### Training | ||
|
||
* Distributed Training | ||
|
||
|
||
```shell | ||
# distrubted training on multiple GPU/Ascend devices | ||
mpirun -n 8 python train.py --config configs/deit/deit_b_gpu.yaml --data_dir /path/to/imagenet --distributed True | ||
``` | ||
|
||
> If the script is executed by the root user, the `--allow-run-as-root` parameter must be added to `mpirun`. | ||
|
||
Similarly, you can train the model on multiple GPU devices with the above `mpirun` command. | ||
|
||
For detailed illustration of all hyper-parameters, please refer to [config.py](https://github.com/mindspore-lab/mindcv/blob/main/config.py). | ||
|
||
**Note:** As the global batch size (batch_size x num_devices) is an important hyper-parameter, it is recommended to keep the global batch size unchanged for reproduction or adjust the learning rate linearly to a new global batch size. | ||
|
||
* Standalone Training | ||
|
||
If you want to train or finetune the model on a smaller dataset without distributed training, please run: | ||
|
||
```shell | ||
# standalone training on a CPU/GPU/Ascend device | ||
python train.py --config configs/deit/deit_b_gpu.yaml --data_dir /path/to/dataset --distribute False | ||
``` | ||
|
||
### Validation | ||
|
||
To validate the accuracy of the trained model, you can use `validate.py` and parse the checkpoint path with `--ckpt_path`. | ||
|
||
```shell | ||
python validate.py -c configs/deit/deit_b_gpu.yaml --data_dir /path/to/imagenet --ckpt_path /path/to/ckpt | ||
``` | ||
|
||
### Deployment | ||
|
||
Please refer to the [deployment tutorial](https://github.com/mindspore-lab/mindcv/blob/main/tutorials/deployment.md) in MindCV. | ||
|
||
## References | ||
|
||
Paper - https://arxiv.org/pdf/2012.12877.pdf | ||
|
||
Official repo - https://github.com/facebookresearch/deit | ||
|
||
Mindspore implementation - https://gitee.com/cvisionlab/models/tree/deit/release/research/cv/DeiT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# system | ||
mode: 0 | ||
distribute: False | ||
num_parallel_workers: 4 | ||
val_while_train: True | ||
|
||
# dataset | ||
dataset: 'imagenet' | ||
data_dir: 'path/to/imagenet/' | ||
shuffle: True | ||
dataset_download: False | ||
batch_size: 64 | ||
drop_remainder: True | ||
train_split: val/ | ||
val_split: val/ | ||
|
||
# augmentation | ||
image_resize: 224 | ||
scale: [0.08, 1.0] | ||
ratio: [0.75, 1.333] | ||
hflip: 0.5 | ||
auto_augment: 'randaug-m9-mstd0.5-inc1' | ||
interpolation: bicubic | ||
re_prob: 0.25 | ||
re_value: 'random' | ||
cutmix: 1.0 | ||
mixup: 0.8 | ||
mixup_prob: 1.0 | ||
mixup_mode: batch | ||
mixup_off_epoch: 0 | ||
switch_prob: 0.5 | ||
crop_pct: 0.875 | ||
color_jitter: 0.3 | ||
|
||
# model | ||
model: 'deit_base' | ||
num_classes: 1000 | ||
pretrained: False | ||
ckpt_path: '' | ||
|
||
keep_checkpoint_max: 10 | ||
ckpt_save_dir: './ckpt' | ||
|
||
epoch_size : 300 | ||
dataset_sink_mode: True | ||
amp_level: O2 | ||
ema: False | ||
ema_decay: 0.99996 | ||
clip_grad: False | ||
clip_value: 5.0 | ||
|
||
# loss | ||
loss: 'CE' | ||
label_smoothing: 0.1 | ||
|
||
# lr scheduler | ||
lr_scheduler: 'cosine_decay' | ||
min_lr: 1.0e-5 | ||
lr: 0.0005 | ||
warmup_epochs: 5 | ||
warmup_factor: 0.002 | ||
decay_epochs: 30 | ||
decay_rate: 0.1 | ||
|
||
# optimizer | ||
opt: 'adamw' | ||
filter_bias_and_bn: True | ||
eps: 1.0e-8 | ||
momentum: 0.9 | ||
weight_decay: 0.05 | ||
dynamic_loss_scale: True | ||
use_nesterov: False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please enrich the introduction.