README.md 3.79 KB
Newer Older
sunxx1's avatar
sunxx1 committed
1
# ViT
sunxx1's avatar
sunxx1 committed
2
## 论文
Rayyyyy's avatar
Rayyyyy committed
3
4
`An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale`
- https://arxiv.org/abs/2010.11929
unknown's avatar
unknown committed
5
6

## 模型结构
sunxx1's avatar
sunxx1 committed
7
Vision Transformer先将图像用卷积进行分块以降低计算量,再对每一块进行展平处理变成序列,然后将序列添加位置编码和cls token,再输入多层Transformer结构提取特征,最后将cls tooken取出来通过一个MLP(多层感知机)用于分类。
unknown's avatar
unknown committed
8

sunxx1's avatar
sunxx1 committed
9
10
11
12
13
14
15
16
17
18
![img](https://developer.hpccube.com/codes/modelzoo/megatron-deepspeed-vit_pytorch/-/raw/main/doc/vit.png)

## 算法原理
图像领域借鉴《Transformer is all you need!》算法论文中的Encoder结构提取特征,Transformer的核心思想是利用注意力模块attention提取特征:

![img](https://developer.hpccube.com/codes/modelzoo/megatron-deepspeed-vit_pytorch/-/raw/main/doc/attention.png)

## 环境配置
### Docker(方法一)
```plaintext
dcuai's avatar
dcuai committed
19
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
sunxx1's avatar
sunxx1 committed
20
# <your IMAGE ID>用以上拉取的docker的镜像ID替换
dcuai's avatar
dcuai committed
21
docker run --shm-size 10g --network=host --name=nit-pytorch --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined  -v /opt/hyhal:/opt/hyhal:ro -v $PWD/vit-pytorch:/home/vit-pytorch -it <your IMAGE ID> bash
sunxx1's avatar
sunxx1 committed
22
23
24
25
26
27
28
pip install -r requirements.txt
```

### Dockerfile(方法二)
```plaintext
cd ViT-PyTorch/docker
docker build --no-cache -t ViT-PyTorch:latest .
dcuai's avatar
dcuai committed
29
docker run --rm --shm-size 10g --network=host --name=megatron --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v /opt/hyhal:/opt/hyhal:ro -v $PWD/../../ViT-PyTorch:/home/ViT-PyTorch -it megatron bash
sunxx1's avatar
sunxx1 committed
30
31
32
33
34
# 若遇到Dockerfile启动的方式安装环境需要长时间等待,可注释掉里面的pip安装,启动容器后再安装python库:pip install -r requirements.txt
```

### Anaconda(方法三)
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.hpccube.com/tool/
unknown's avatar
unknown committed
35

sunxx1's avatar
sunxx1 committed
36
37
38
39
40
41
42
43
44
```plaintext
DTK驱动:dtk22.10.1
python:python3.7
torch:1.10.0
torchvision:0.10.0
Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应
```

2、其它非特殊库参照requirements.txt安装
unknown's avatar
unknown committed
45

sunxx1's avatar
sunxx1 committed
46
47
48
```plaintext
pip install -r requirements.txt
```
unknown's avatar
unknown committed
49

sunxx1's avatar
sunxx1 committed
50
## 数据集
Rayyyyy's avatar
Rayyyyy committed
51
[cifar10](http://113.200.138.88:18080/aidatasets/project-dependency/cifar)
dcuai's avatar
dcuai committed
52
默认会自动下载数据集到data文件夹,可将cifar-10-python.tar.gz放置在data文件夹下
unknown's avatar
unknown committed
53

sunxx1's avatar
sunxx1 committed
54
55
56
57
58
59
60
61
62
63
64
```
├── batches.meta
├── data_batch_1
├── data_batch_2
├── data_batch_3
├── data_batch_4
├── data_batch_5
├── readme.html
└── test_batch
```

sunxx1's avatar
sunxx1 committed
65
## 训练
unknown's avatar
unknown committed
66
67
68
69
70
下载预训练模型放在checkpoint目录下:
```
wget https://storage.googleapis.com/vit_models/imagenet21k/ViT-B_16.npz
```

sunxx1's avatar
sunxx1 committed
71
### 单机单卡
Rayyyyy's avatar
Rayyyyy committed
72
73
```
export HIP_VISIBLE_DEVICES=0
dcuai's avatar
dcuai committed
74
python3 -m torch.distributed.launch --nproc_per_node=1 --use-env train.py --name cifar10-100_500 --dataset cifar10 --model_type ViT-B_16 --pretrained_dir checkpoint/ViT-B_16.npz --train_batch_size 64 --num_steps 500
Rayyyyy's avatar
Rayyyyy committed
75
```
unknown's avatar
unknown committed
76

sunxx1's avatar
sunxx1 committed
77
78
### 单机多卡
```
dcuai's avatar
dcuai committed
79
python3 -m torch.distributed.launch --nproc_per_node=8 --use-env train.py --name cifar10-100_500 --dataset cifar10 --model_type ViT-B_16 --pretrained_dir checkpoint/ViT-B_16.npz --train_batch_size 64 --num_steps 500
sunxx1's avatar
sunxx1 committed
80
```
Rayyyyy's avatar
Rayyyyy committed
81

sunxx1's avatar
sunxx1 committed
82
83
## result
![1695381570003](image/README/1695381570003.png)
dcuai's avatar
dcuai committed
84
### 精度
unknown's avatar
unknown committed
85
86
测试数据使用的是cifar10,使用的加速卡是DCU Z100L。

sunxx1's avatar
sunxx1 committed
87
88
89
| 卡数 | 精度 |
| :------: | :------: |
| 1 | Best Accuracy=0.3051 |
sunxx1's avatar
sunxx1 committed
90

Rayyyyy's avatar
Rayyyyy committed
91
## 应用场景
sunxx1's avatar
sunxx1 committed
92
93
94
95
### 算法类别
图像分类

### 热点行业
dcuai's avatar
dcuai committed
96
制造,能源,交通,网安
sunxx1's avatar
sunxx1 committed
97

sunxx1's avatar
sunxx1 committed
98
### 源码仓库及问题反馈
dcuai's avatar
dcuai committed
99
- https://developer.hpccube.com/codes/modelzoo/vit_pytorch
sunxx1's avatar
sunxx1 committed
100

dcuai's avatar
dcuai committed
101
### 参考资料
Rayyyyy's avatar
Rayyyyy committed
102
- https://github.com/jeonsworld/ViT-pytorch