-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformer.py
285 lines (250 loc) · 11.4 KB
/
Transformer.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'''
#!/usr/bin/env python
# -*- coding:utf-8 -*-
@File: Transformer.py
@Author: sk
@Time: 2023/5/16-14:26
@e-mail: sk1123344@163.com
'''
import torch
from torch import Tensor
import torch.nn as nn
from torch.nn import MultiheadAttention
import torch.nn.functional as F
from typing import Optional
from copy import deepcopy
def _get_activation_fn(activation):
"""Return an activation function given a string"""
if activation == "relu":
return F.relu
if activation == "gelu":
return F.gelu
if activation == "glu":
return F.glu
raise RuntimeError(F"activation should be relu/gelu, not {activation}.")
class TransformerDecoderLayer(nn.Module):
def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation="relu", use_self_attn=False):
super().__init__()
self.cross_attn = MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=True)
self.use_self_attn = use_self_attn
if use_self_attn:
self.self_attn = MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=True)
# Implementation of Feedforward model
self.linear1 = nn.Linear(d_model, dim_feedforward)
self.dropout = nn.Dropout(dropout)
self.linear2 = nn.Linear(dim_feedforward, d_model)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.activation = _get_activation_fn(activation)
def forward(self, query_embedding, spatial_feature):
"""
:param query_embedding:
:param spatial_feature: assume been viewed and permuted
:return:
"""
# norm + MSA
norm_query = self.norm1(query_embedding)
if self.use_self_attn:
norm_query = self.self_attn(norm_query, norm_query, norm_query)[0]
# cross attention + residual
query_embedding1 = self.cross_attn(norm_query, spatial_feature, spatial_feature)[0]
query_embedding = query_embedding + self.dropout1(query_embedding1)
# norm + MLP + residual
query_embedding = self.norm2(query_embedding)
query_embedding2 = self.linear2(self.dropout(self.activation(self.linear1(query_embedding))))
query_embedding = query_embedding + self.dropout2(query_embedding2)
return query_embedding
class TransformerDecoderLayer1(nn.Module):
def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation="relu", use_self_attn=False):
"""
norm(x+dropout(x)) when in and norm when out
:param d_model:
:param nhead:
:param dim_feedforward:
:param dropout:
:param activation:
:param use_self_attn:
"""
super().__init__()
self.cross_attn = MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=True)
self.use_self_attn = use_self_attn
if use_self_attn:
self.self_attn = MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=True)
# Implementation of Feedforward model
self.linear1 = nn.Linear(d_model, dim_feedforward)
self.dropout = nn.Dropout(dropout)
self.linear2 = nn.Linear(dim_feedforward, d_model)
self.dropout_in = nn.Dropout(dropout)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.norm_out = nn.LayerNorm(d_model)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.activation = _get_activation_fn(activation)
def forward(self, query_embedding, spatial_feature):
"""
:param query_embedding:
:param spatial_feature: assume been viewed and permuted
:return:
"""
# norm + MSA
norm_query = self.norm1(query_embedding + self.dropout_in(query_embedding))
if self.use_self_attn:
norm_query = self.self_attn(norm_query, norm_query, norm_query)[0]
# cross attention + residual
query_embedding1 = self.cross_attn(norm_query, spatial_feature, spatial_feature)[0]
query_embedding = query_embedding + self.dropout1(query_embedding1)
# norm + MLP + residual
query_embedding = self.norm2(query_embedding)
query_embedding2 = self.linear2(self.dropout(self.activation(self.linear1(query_embedding))))
query_embedding = self.norm_out(query_embedding + self.dropout2(query_embedding2))
return query_embedding
class TransformerDecoder(nn.Module):
def __init__(self, num_blocks, d_model, nhead, num_classes, token='task', dim_backbone=512, dim_feedforward=1024, dropout=0.1, activation="relu", use_self_attn=False, block=TransformerDecoderLayer1):
"""
:param num_blocks:
:param d_model:
:param nhead:
:param token: cls/task
:param dim_feedforward:
:param dropout:
:param activation:
:param use_self_attn:
"""
super().__init__()
self.token_type = token
self.d_model = d_model
self.num_classes = num_classes
if token == 'cls':
self.token = nn.Parameter(torch.zeros(1, num_classes, d_model))
self.adaptive_pool = nn.Linear(d_model, 1)
# self.adaptive_pool = nn.AdaptiveAvgPool1d(1)
elif token == 'task':
self.token = nn.Parameter(torch.zeros(1, 1, d_model))
self.adaptive_pool = nn.Linear(d_model, num_classes)
else:
raise NotImplementedError(token)
self.feature_embedding_layer = nn.Linear(dim_backbone, d_model)
self.pos_embedding = nn.Parameter(torch.zeros(1, 16 * 16, d_model))
self.decoder = nn.ModuleList([block(d_model, nhead, dim_feedforward, dropout, activation, use_self_attn) for i in range(num_blocks)])
def get_cls_token(self, spatial_feature, cls_index):
bs, channel, width, height = spatial_feature.size()
spatial_feature = spatial_feature.view(bs, channel, width * height).permute(0, 2, 1)
embedded_feature = self.feature_embedding_layer(spatial_feature)
query = self.token.expand(bs, -1, -1)
for decoder_layer in self.decoder:
query = decoder_layer(query, embedded_feature)
if self.token_type == 'cls':
if cls_index is None:
return query
return query[:, cls_index, :].squeeze(1)
elif self.token_type == 'task':
return query[:, 0, :].squeeze(1)
def forward(self, spatial_feature):
"""
:param spatial_feature: b c w h
:return:
"""
bs, channel, width, height = spatial_feature.size()
spatial_feature = spatial_feature.view(bs, channel, width * height).permute(0, 2, 1)
embedded_feature = self.feature_embedding_layer(spatial_feature)
embedded_feature += self.pos_embedding
query = self.token.expand(bs, -1, -1)
for decoder_layer in self.decoder:
query = decoder_layer(query, embedded_feature)
out = self.adaptive_pool(query)
if self.token_type == 'cls':
out = out.squeeze(-1)
elif self.token_type == 'task':
out = out.squeeze(1)
else:
raise NotImplementedError
return out
class TransformerDecoderFundus(nn.Module):
def __init__(self, num_blocks, d_model, nhead, num_classes, token='task', dim_backbone=512, dim_feedforward=1024, dropout=0.1, activation="relu", use_self_attn=False, block=TransformerDecoderLayer1):
"""
:param num_blocks:
:param d_model:
:param nhead:
:param token: cls/task
:param dim_feedforward:
:param dropout:
:param activation:
:param use_self_attn:
"""
super().__init__()
self.token_type = token
self.d_model = d_model
self.num_classes = num_classes
# if token == 'cls':
# self.token = nn.Parameter(torch.zeros(1, num_classes, d_model))
# self.adaptive_pool = nn.Linear(d_model, 1)
# elif token == 'task':
self.token = nn.Parameter(torch.zeros(1, 1, d_model))
self.adaptive_pool = nn.Linear(d_model, num_classes, bias=False)
# else:
# raise NotImplementedError(token)
self.feature_embedding_layer = nn.Linear(dim_backbone, d_model)
self.pos_embedding = nn.Parameter(torch.zeros(1, 16 * 16, d_model))
self.decoder = nn.ModuleList([block(d_model, nhead, dim_feedforward, dropout, activation, use_self_attn) for i in range(num_blocks)])
def forward(self, spatial_feature):
"""
:param spatial_feature: b c w h
:return:
"""
bs, channel, width, height = spatial_feature.size()
spatial_feature = spatial_feature.view(bs, channel, width * height).permute(0, 2, 1)
embedded_feature = self.feature_embedding_layer(spatial_feature)
embedded_feature += self.pos_embedding
query = self.token.expand(bs, -1, -1)
for decoder_layer in self.decoder:
query = decoder_layer(query, embedded_feature)
out = self.adaptive_pool(query)
out = out.squeeze(1)
return out, query.squeeze(1)
class TransformerDecoderFundus1(nn.Module):
def __init__(self, num_blocks, d_model, nhead, num_classes, token='task', dim_backbone=512, dim_feedforward=1024, dropout=0.1, activation="relu", use_self_attn=False, block=TransformerDecoderLayer1):
"""
use cat[feature_embedding, token] as query
:param num_blocks:
:param d_model:
:param nhead:
:param token: cls/task
:param dim_feedforward:
:param dropout:
:param activation:
:param use_self_attn:
"""
super().__init__()
self.token_type = token
self.d_model = d_model
self.num_classes = num_classes
# if token == 'cls':
# self.token = nn.Parameter(torch.zeros(1, num_classes, d_model))
# self.adaptive_pool = nn.Linear(d_model, 1)
# elif token == 'task':
self.token = nn.Parameter(torch.zeros(1, 1, d_model))
self.adaptive_pool = nn.Linear(d_model, num_classes, bias=False)
# else:
# raise NotImplementedError(token)
self.feature_embedding_layer = nn.Linear(dim_backbone, d_model)
self.pos_embedding = nn.Parameter(torch.zeros(1, 16 * 16, d_model))
self.decoder = nn.ModuleList([block(d_model, nhead, dim_feedforward, dropout, activation, use_self_attn) for i in range(num_blocks)])
def forward(self, spatial_feature):
"""
:param spatial_feature: b c w h
:return:
"""
bs, channel, width, height = spatial_feature.size()
spatial_feature = spatial_feature.view(bs, channel, width * height).permute(0, 2, 1)
embedded_feature = self.feature_embedding_layer(spatial_feature)
embedded_feature += self.pos_embedding
token = self.token.expand(bs, -1, -1)
query = torch.cat([embedded_feature, token], dim=1)
for decoder_layer in self.decoder:
query = decoder_layer(query, embedded_feature)
out = self.adaptive_pool(query[:, 0, :])
out = out.squeeze(1)
return out, query.squeeze(1)