-
Notifications
You must be signed in to change notification settings - Fork 3
/
pfg_model.py
146 lines (124 loc) · 5.1 KB
/
pfg_model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class StochDepth(nn.Module):
"""Batchwise Dropout used in EfficientNet, optionally sans rescaling."""
def __init__(self, drop_rate, scale_by_keep=False):
super().__init__()
self.drop_rate = drop_rate
self.scale_by_keep = scale_by_keep
def forward(self, x):
if not self.training:
return x
batch_size = x.shape[0]
r = torch.rand([batch_size, 1, 1], dtype=x.dtype, device=x.device)
keep_prob = 1.0 - self.drop_rate
binary_tensor = torch.floor(keep_prob + r)
if self.scale_by_keep:
x = x / keep_prob
return x * binary_tensor
class PosEmbed(nn.Module):
def __init__(self, input_size):
super().__init__()
self.pos_embed = nn.Parameter(
torch.empty(input_size, dtype=torch.float32)
)
torch.nn.init.trunc_normal_(self.pos_embed,mean=0.0, std=0.02)
def forward(self, x):
return x + self.pos_embed.unsqueeze(0)
class MLPBlock(nn.Module):
def __init__(self, input_dim, mlp_dim, stochdepth_rate):
super().__init__()
self.stochdepth_rate = stochdepth_rate
self.fc1 = nn.Linear(input_dim, mlp_dim)
self.fc2 = nn.Linear(mlp_dim, input_dim)
if stochdepth_rate > 0.0:
self.stochdepth = StochDepth(stochdepth_rate, scale_by_keep=True)
else:
self.stochdepth = None
def forward(self, x):
out = F.gelu(self.fc1(x))
if self.stochdepth:
out = self.stochdepth(out)
out = self.fc2(out)
return out
class SkipInitChannelwise(nn.Module):
def __init__(self, channels, init_val=1e-6):
super().__init__()
self.channels = channels
self.init_val = init_val
self.skip = nn.Parameter(torch.ones(channels) * init_val)
def forward(self, x):
return x * self.skip
class ViTBlock(nn.Module):
def __init__(self, input_dim, heads, key_dim, mlp_dim, layerscale_init, stochdepth_rate):
super().__init__()
self.norm1 = nn.LayerNorm(input_dim,eps=1e-3)
self.attn = nn.MultiheadAttention(input_dim, heads, batch_first=True)
self.skip1 = SkipInitChannelwise(key_dim, init_val=layerscale_init)
self.stochdepth1 = StochDepth(stochdepth_rate, scale_by_keep=True) if stochdepth_rate > 0.0 else None
self.norm2 = nn.LayerNorm(input_dim,eps=1e-3)
self.mlp = MLPBlock(input_dim, mlp_dim, stochdepth_rate)
self.skip2 = SkipInitChannelwise(key_dim, init_val=layerscale_init)
self.stochdepth2 = StochDepth(stochdepth_rate, scale_by_keep=True) if stochdepth_rate > 0.0 else None
def forward(self, x):
out = self.norm1(x)
out = self.attn(out, out, out)[0]
out = self.skip1(out)
if self.stochdepth1:
out = self.stochdepth1(out)
x = out + x
out = self.norm2(x)
out = self.mlp(out)
out = self.skip2(out)
if self.stochdepth2:
out = self.stochdepth2(out)
out = out + x
return out
class ViT(nn.Module):
def __init__(self, in_channels=3, img_size=320, out_classes=2000, definition_name="B16"):
super().__init__()
self.definitions = {
"B16": {
"num_blocks": 12,
"patch_size": 16,
"key_dim": 768,
"mlp_dim": 3072,
"heads": 12,
"stochdepth_rate": 0.05,
},
# Other definitions removed for simplicity
}
definition = self.definitions[definition_name]
self.blocks = nn.ModuleList()
num_blocks = definition["num_blocks"]
patch_size = definition["patch_size"]
key_dim = definition["key_dim"]
mlp_dim = definition["mlp_dim"]
heads = definition["heads"]
stochdepth_rate = definition["stochdepth_rate"]
layerscale_init = 0.1 # Replacing CaiT_LayerScale_init(num_blocks)
self.conv = nn.Conv2d(in_channels, key_dim, kernel_size=patch_size, stride=patch_size)
self.pos_embed = PosEmbed(((img_size // patch_size) ** 2,key_dim))
for i in range(num_blocks):
self.blocks.append(
ViTBlock(key_dim, heads, key_dim, mlp_dim, layerscale_init, stochdepth_rate)
)
self.norm = nn.LayerNorm(key_dim,eps=1e-3)
self.avgpool = nn.AdaptiveAvgPool1d(1)
self.fc = nn.Linear(key_dim, out_classes)
self.act = nn.Sigmoid()
def forward(self, x):
x = (x - 127.5) / 127.5
x = self.conv(x)
b, c, h, w = x.shape
x = x.view(b, c, h*w).permute(0, 2, 1) # (B, H*W, C)
x = self.pos_embed(x)
for block in self.blocks:
x = block(x)
x = self.norm(x)
x = self.avgpool(x.transpose(1, 2)).squeeze(-1) # (B, C)
# pfg uses output of last pooling layer.
#x = self.fc(x)
#x = self.act(x)
return x