mochuan zhan
commited on
Commit
·
149f9ea
1
Parent(s):
0cf2f9c
initial commit from desktop
Browse files- README.md +1 -0
- vit.py +69 -0
- vit_model.pth +3 -0
README.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
This is a MNIST classifier based on vision transformer.
|
vit.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch.nn as nn
|
2 |
+
import torch
|
3 |
+
import torch.optim as optim
|
4 |
+
|
5 |
+
class ViT(nn.Module):
|
6 |
+
def __init__(self, image_size=28, patch_size=7, num_classes=10, dim=128, depth=6, heads=8, mlp_dim=256, dropout=0.1):
|
7 |
+
super(ViT, self).__init__()
|
8 |
+
|
9 |
+
assert image_size % patch_size == 0, 'image dimensions must be divisible by the patch size'
|
10 |
+
num_patches = (image_size // patch_size) ** 2
|
11 |
+
patch_dim = 1 * patch_size ** 2
|
12 |
+
|
13 |
+
# 定义线性层将图像分块并映射到嵌入空间
|
14 |
+
self.patch_embedding = nn.Linear(patch_dim, dim)
|
15 |
+
|
16 |
+
# 位置编码
|
17 |
+
# nn.Parameter是Pytorch中的一个类,用于将一个张量注册为模型的参数
|
18 |
+
self.pos_embedding = nn.Parameter(torch.randn(1, num_patches, dim))
|
19 |
+
|
20 |
+
# Dropout层
|
21 |
+
self.dropout = nn.Dropout(dropout)
|
22 |
+
|
23 |
+
# Transformer编码器
|
24 |
+
# 当 batch_first=True 时,输入和输出张量的形状为 (batch_size, seq_length, feature_dim)。当 batch_first=False 时,输入和输出张量的形状为 (seq_length, batch_size, feature_dim)。
|
25 |
+
self.transformer = nn.TransformerEncoder(
|
26 |
+
nn.TransformerEncoderLayer(
|
27 |
+
d_model=dim,
|
28 |
+
nhead=heads,
|
29 |
+
dim_feedforward=mlp_dim
|
30 |
+
# batch_first=True
|
31 |
+
),
|
32 |
+
num_layers=depth
|
33 |
+
)
|
34 |
+
|
35 |
+
# 分类头
|
36 |
+
# nn.Identity()是一个空的层,它不执行任何操作,只是返回输入
|
37 |
+
# self.to_cls_token = nn.Identity()
|
38 |
+
# self.mlp_head = nn.Linear(dim, num_classes)
|
39 |
+
self.mlp_head = nn.Sequential(
|
40 |
+
nn.LayerNorm(dim),
|
41 |
+
nn.Linear(dim, num_classes)
|
42 |
+
)
|
43 |
+
|
44 |
+
def forward(self, x):
|
45 |
+
# x shape: [batch_size, 1, 28, 28]
|
46 |
+
batch_size = x.size(0)
|
47 |
+
x = x.view(batch_size, -1, 7*7) # 将图像划分为7x7的Patch
|
48 |
+
x = self.patch_embedding(x) # [batch_size, num_patches, dim]
|
49 |
+
x += self.pos_embedding # 添加位置编码
|
50 |
+
x = self.dropout(x) # 应用Dropout
|
51 |
+
|
52 |
+
x = x.permute(1, 0, 2) # Transformer期望的输入形状:[seq_len, batch_size, embedding_dim]
|
53 |
+
x = self.transformer(x) # [序列长度, batch_size, dim]
|
54 |
+
x = x.permute(1, 0, 2) # 转回原来的形状:[batch_size, seq_len, dim]
|
55 |
+
|
56 |
+
x = x.mean(dim=1) # 对所有Patch取平均,x.mean(dim=1) 这一步是对所有 Patch 的特征向量取平均值,从而得到一个代表整个图像的全局特征向量。
|
57 |
+
x = self.mlp_head(x) # [batch_size, num_classes]
|
58 |
+
return x
|
59 |
+
|
60 |
+
# def forward(self, x):
|
61 |
+
# # x shape: (batch, 1, 28, 28)
|
62 |
+
# batch_size = x.shape[0]
|
63 |
+
# x = x.view(batch_size, -1, 7*7)
|
64 |
+
# x = self.patch_embedding(x) # (batch, num_patches, dim)
|
65 |
+
# x = x + self.pos_embedding
|
66 |
+
# x = self.transformer(x)
|
67 |
+
# x = x.mean(dim=1) # (batch, dim)
|
68 |
+
# x = self.mlp_head(x)
|
69 |
+
# return x
|
vit_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:223c6c32c2a9d4c274b09c35ef089b358ee7cf1729b9d939fca898db5765dcdb
|
3 |
+
size 3248655
|