-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvit.py
176 lines (152 loc) · 6.54 KB
/
vit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from typing import Optional
import torch
from pytorch_pretrained_vit.configs import PRETRAINED_MODELS
from pytorch_pretrained_vit.transformer import Transformer
from pytorch_pretrained_vit.utils import load_pretrained_weights, as_tuple
from torch import nn
import numpy as np
class PositionalEmbedding1D(nn.Module):
"""Adds (optionally learned) positional embeddings to the inputs."""
def __init__(self, seq_len, dim):
super().__init__()
self.pos_embedding = nn.Parameter(torch.zeros(1, seq_len, dim))
def forward(self, x):
"""Input has shape `(batch_size, seq_len, emb_dim)`"""
return x + self.pos_embedding
class Vit(nn.Module):
"""
Args:
name (str): Model name, e.g. 'B_16'
pretrained (bool): Load pretrained weights
in_channels (int): Number of channels in input data
num_classes (int): Number of classes, default 1000
References:
[1] https://openreview.net/forum?id=YicbFdNTTy
"""
def __init__(
self,
name: Optional[str] = None,
pretrained: bool = False,
patches: int = 1,
dim: int = 768,
ff_dim: int = 3072,
num_heads: int = 12,
num_layers: int = 12,
attention_dropout_rate: float = 0.0,
dropout_rate: float = 0.1,
representation_size: Optional[int] = None,
load_repr_layer: bool = False,
classifier: str = 'token',
positional_embedding: str = '1d',
in_channels: int = 3,
image_size: Optional[int] = None,
num_classes: Optional[int] = None,
):
super().__init__()
# Configuration
if name is None:
check_msg = 'must specify name of pretrained model'
assert not pretrained, check_msg
# assert not resize_positional_embedding, check_msg
if num_classes is None:
num_classes = 1000
if image_size is None:
image_size = 384
else: # load pretrained model
assert name in PRETRAINED_MODELS.keys(), \
'name should be in: ' + ', '.join(PRETRAINED_MODELS.keys())
config = PRETRAINED_MODELS[name]['config']
# patches = config['patches']
dim = config['dim']
ff_dim = config['ff_dim']
num_heads = num_heads
num_layers = num_layers
attention_dropout_rate = config['attention_dropout_rate']
dropout_rate = config['dropout_rate']
representation_size = config['representation_size']
classifier = config['classifier']
if image_size is None:
image_size = PRETRAINED_MODELS[name]['image_size']
if num_classes is None:
num_classes = PRETRAINED_MODELS[name]['num_classes']
self.image_size = image_size
# Image and patch sizes
h, w = as_tuple(image_size) # image sizes
fh, fw = as_tuple(patches) # patch sizes
gh, gw = h // fh, w // fw # number of patches
seq_len = gh * gw
# Patch embedding
self.patch_embedding = nn.Conv2d(in_channels, dim, kernel_size=(fh, fw), stride=(fh, fw))
# Class token
if classifier == 'token':
self.class_token = nn.Parameter(torch.zeros(1, 1, dim))
seq_len += 1
# Positional embedding
if positional_embedding.lower() == '1d':
self.positional_embedding = PositionalEmbedding1D(seq_len, dim)
else:
raise NotImplementedError()
# Transformer
self.transformer = Transformer(num_layers=num_layers, dim=dim, num_heads=num_heads,
ff_dim=ff_dim, dropout=dropout_rate)
# Representation layer
if representation_size and load_repr_layer:
self.pre_logits = nn.Linear(dim, representation_size)
pre_logits_size = representation_size
else:
pre_logits_size = dim
# Classifier head
self.norm = nn.LayerNorm(pre_logits_size, eps=1e-6)
self.fc = nn.Linear(pre_logits_size, num_classes)
# Initialize weights
self.init_weights()
# Load pretrained model
if pretrained:
pretrained_num_channels = 3
pretrained_num_classes = PRETRAINED_MODELS[name]['num_classes']
pretrained_image_size = PRETRAINED_MODELS[name]['image_size']
load_pretrained_weights(
self, name,
load_first_conv=(in_channels == pretrained_num_channels),
load_fc=(num_classes == pretrained_num_classes),
load_repr_layer=load_repr_layer,
resize_positional_embedding=(image_size != pretrained_image_size),
)
@torch.no_grad()
def init_weights(self):
def _init(m):
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(
m.weight) # _trunc_normal(m.weight, std=0.02) # from .initialization import _trunc_normal
if hasattr(m, 'bias') and m.bias is not None:
nn.init.normal_(m.bias, std=1e-6) # nn.init.constant(m.bias, 0)
self.apply(_init)
nn.init.constant_(self.fc.weight, 0)
nn.init.constant_(self.fc.bias, 0)
nn.init.normal_(self.positional_embedding.pos_embedding,
std=0.02) # _trunc_normal(self.positional_embedding.pos_embedding, std=0.02)
nn.init.constant_(self.class_token, 0)
def forward(self, x):
"""Breaks image into patches, applies transformer, applies MLP head.
Args:
x (tensor): `b,c,fh,fw`
"""
b, c, fh, fw = x.shape
x = self.patch_embedding(x) # b,d,gh,gw (1,768,4,4)
x = x.flatten(2).transpose(1, 2) # b,gh*gw,d
if hasattr(self, 'class_token'):
x = torch.cat((self.class_token.expand(b, -1, -1), x), dim=1) # b,gh*gw+1,d (1,17,768)
if hasattr(self, 'positional_embedding'):
x = self.positional_embedding(x) # b,gh*gw+1,d
x = self.transformer(x) # b,gh*gw+1,d
if hasattr(self, 'pre_logits'):
x = self.pre_logits(x)
x = torch.tanh(x)
x = torch.permute(x[:, 1:, :], (0, 2, 1))
result = x.view((b, x.shape[1], int(np.sqrt(x.shape[2])), int(np.sqrt(x.shape[2]))))
return result
if __name__ == '__main__':
test = torch.ones((64, 3, 64, 64))
model = Vit('B_16_imagenet1k', pretrained=False, image_size=64,patches=16)
print(model(test).shape)
nn.LeakyReLU