"git@developer.sourcefind.cn:OpenDAS/dlib.git" did not exist on "1b093f7ae126a9da6dc8b23594622990a521be4b"
README.md 3.93 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

chenzk's avatar
chenzk committed
9
![img](https://developer.sourcefind.cn/codes/modelzoo/megatron-deepspeed-vit_pytorch/-/raw/main/doc/vit.png)
sunxx1's avatar
sunxx1 committed
10
11
12
13

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

chenzk's avatar
chenzk committed
14
![img](https://developer.sourcefind.cn/codes/modelzoo/megatron-deepspeed-vit_pytorch/-/raw/main/doc/attention.png)
sunxx1's avatar
sunxx1 committed
15
16
17
18

## 环境配置
### 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=vit-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
pip install -r requirements.txt
```

### Dockerfile(方法二)
```plaintext
cd ViT-PyTorch/docker
dcuai's avatar
dcuai committed
28
29
docker build --no-cache -t vit-pytorch:latest .
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 vit-pytorch:latest bash
sunxx1's avatar
sunxx1 committed
30
31
32
33
# 若遇到Dockerfile启动的方式安装环境需要长时间等待,可注释掉里面的pip安装,启动容器后再安装python库:pip install -r requirements.txt
```

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

sunxx1's avatar
sunxx1 committed
36
```plaintext
dcuai's avatar
dcuai committed
37
38
39
40
DTK驱动:dtk24.04.1
python:python3.10
torch:2.1.0
torchvision:0.16.0
sunxx1's avatar
sunxx1 committed
41
42
43
44
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
## 数据集
chenzk's avatar
chenzk committed
51
[cifar10](https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz)
dcuai's avatar
dcuai committed
52

dcuai's avatar
dcuai committed
53
默认会自动下载数据集到data文件夹,可将cifar-10-python.tar.gz放置在data文件夹下
unknown's avatar
unknown committed
54

sunxx1's avatar
sunxx1 committed
55
56
57
58
59
60
61
62
63
64
65
```
├── 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
66
## 训练
chenych's avatar
chenych committed
67

chenzk's avatar
chenzk committed
68
下载预训练模型[ViT-B_16.npz](https://console.cloud.google.com/storage/browser/_details/vit_models/imagenet21k%2Bimagenet2012/ViT-B_16.npz?pageState=(%22StorageObjectListTable%22:(%22f%22:%22%255B%255D%22)))放在checkpoint目录下。
unknown's avatar
unknown committed
69

sunxx1's avatar
sunxx1 committed
70
### 单机单卡
chenych's avatar
chenych 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 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
### 单机多卡
chenych's avatar
chenych committed
78

sunxx1's avatar
sunxx1 committed
79
```
dcuai's avatar
dcuai committed
80
python3 -m torch.distributed.launch --nproc_per_node=8 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
81
```
Rayyyyy's avatar
Rayyyyy committed
82

sunxx1's avatar
sunxx1 committed
83
84
## result
![1695381570003](image/README/1695381570003.png)
chenych's avatar
chenych committed
85

dcuai's avatar
dcuai committed
86
### 精度
unknown's avatar
unknown committed
87
88
测试数据使用的是cifar10,使用的加速卡是DCU Z100L。

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

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

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

sunxx1's avatar
sunxx1 committed
100
### 源码仓库及问题反馈
chenzk's avatar
chenzk committed
101
- https://developer.sourcefind.cn/codes/modelzoo/vit_pytorch
sunxx1's avatar
sunxx1 committed
102

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