-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcvt_custom_layers.py
More file actions
302 lines (255 loc) · 8.63 KB
/
Copy pathcvt_custom_layers.py
File metadata and controls
302 lines (255 loc) · 8.63 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""Transformer model components including Transformer, RearrangeLayer, and LastStage layers."""
import tensorflow as tf
from einops import rearrange
from tensorflow.keras import layers, mixed_precision
from cvt_modules import ConvAttention, FeedForward, PreLayerNorm
policy = mixed_precision.Policy("mixed_float16")
mixed_precision.set_global_policy(policy)
class Transformer(layers.Layer):
"""
Implement a Transformer with multiple layers of attention and feed-forward blocks.
Attributes:
dim (int): Input dimension.
length (int): Input length.
width (int): Input width.
depth (int): Number of Transformer layers.
heads (int): Number of attention heads.
dim_head (int): Dimension per attention head.
mlp_dim (int): Hidden dimension of feed-forward layer.
dropout (float): Dropout rate.
last_stage (bool): Whether this is the last stage.
"""
def __init__(
self,
dim,
length,
width,
depth,
heads,
dim_head,
mlp_dim,
dropout=0.0,
last_stage=False,
**kwargs,
):
"""
Initialize the Transformer layer.
Args:
dim (int): Input dimension.
length (int): Input length.
width (int): Input width.
depth (int): Number of Transformer layers.
heads (int): Number of attention heads.
dim_head (int): Dimension per attention head.
mlp_dim (int): Hidden dimension of feed-forward layer.
dropout (float, optional): Dropout rate. Defaults to 0.0.
last_stage (bool, optional): Whether this is the last stage. Defaults to False.
**kwargs: Additional keyword arguments.
"""
super().__init__(**kwargs)
self.dim = dim
self.length = length
self.width = width
self.depth = depth
self.heads = heads
self.dim_head = dim_head
self.mlp_dim = mlp_dim
self.dropout = dropout
self.last_stage = last_stage
self.layers = []
def build(self, input_shape):
"""
Build the Transformer layers.
Args:
input_shape (tuple): Shape of input tensor.
"""
for _ in range(self.depth):
self.layers.append(
[
PreLayerNorm(
fn=ConvAttention(
dim=self.dim,
length=self.length,
width=self.width,
heads=self.heads,
dim_head=self.dim_head,
dropout=self.dropout,
last_stage=self.last_stage,
)
),
PreLayerNorm(
fn=FeedForward(
dim=self.dim, hidden_dim=self.mlp_dim, dropout=self.dropout
)
),
]
)
def call(self, x, training=False):
"""
Perform forward pass through the Transformer layers.
Args:
x (tf.Tensor): Input tensor.
training (bool, optional): Whether layer is in training mode. Defaults to False.
Returns:
tf.Tensor: Output tensor after attention and feed-forward layers.
"""
for attn, ff in self.layers:
# Apply attention and residual connection
x = attn(x, training=training) + x
# Apply feed-forward and residual connection
x = ff(x, training=training) + x
return x
def get_config(self):
"""
Return configuration of the Transformer layer.
Returns:
dict: Configuration dictionary.
"""
config = super(Transformer, self).get_config()
config.update(
{
"dim": self.dim,
"length": self.length,
"width": self.width,
"depth": self.depth,
"heads": self.heads,
"dim_head": self.dim_head,
"mlp_dim": self.mlp_dim,
"dropout": self.dropout,
"last_stage": self.last_stage,
}
)
return config
@classmethod
def from_config(cls, config):
"""
Create a Transformer layer from configuration.
Args:
config (dict): Configuration dictionary.
Returns:
Transformer: Instantiated Transformer layer.
"""
return cls(**config)
class RearrangeLayer(layers.Layer):
"""
Rearrange tensor dimensions with optional compression.
Attributes:
dim (int): Channel dimension of input tensor.
length (int): Spatial length dimension.
width (int): Spatial width dimension.
compression (bool): Whether to compress tensor before rearranging.
"""
def __init__(self, dim, length, width, compression=False, **kwargs):
"""
Initialize the Rearrange layer.
Args:
dim (int): Channel dimension of input tensor.
length (int): Spatial length dimension.
width (int): Spatial width dimension.
compression (bool, optional): Whether to compress tensor. Defaults to False.
**kwargs: Additional keyword arguments.
"""
super().__init__(**kwargs)
self.dim = dim
self.length = length
self.width = width
self.compression = compression
def call(self, x):
"""
Rearrange the input tensor dimensions.
Args:
x (tf.Tensor): Input tensor.
Returns:
tf.Tensor: Rearranged tensor.
"""
if self.compression:
x = rearrange(x, "b l w c -> b (l w) c", l=self.length, w=self.width)
else:
x = rearrange(x, "b (l w) c -> b l w c", l=self.length, w=self.width)
return x
def get_config(self):
"""
Return configuration of the Rearrange layer.
Returns:
dict: Configuration dictionary.
"""
config = super(RearrangeLayer, self).get_config()
config.update(
{
"dim": self.dim,
"length": self.length,
"width": self.width,
"compression": self.compression,
}
)
return config
@classmethod
def from_config(cls, config):
"""
Create a Rearrange layer from configuration.
Args:
config (dict): Configuration dictionary.
Returns:
RearrangeLayer: Instantiated Rearrange layer.
"""
return cls(**config)
class LastStage(layers.Layer):
"""
Implement the last stage of the model with class token addition.
Attributes:
dim (int): Dimension of input tensor.
batch_size (int): Batch size.
"""
def __init__(self, dim, batch_size, **kwargs):
"""
Initialize the LastStage layer.
Args:
dim (int): Dimension of input tensor.
batch_size (int): Batch size.
**kwargs: Additional keyword arguments.
"""
super().__init__(**kwargs)
self.dim = dim
self.batch_size = batch_size
def build(self, input_shape):
"""
Build the LastStage layer by creating class token variable.
Args:
input_shape (tf.TensorShape): Shape of input tensor.
"""
self.cls_token = tf.Variable(
tf.random.normal([1, 1, self.dim], dtype=tf.float16), trainable=True
)
def call(self, x):
"""
Append class token to the input tensor.
Args:
x (tf.Tensor): Input tensor.
Returns:
tf.Tensor: Tensor with class token prepended.
"""
b = self.batch_size
cls_tokens = tf.tile(self.cls_token, [b, 1, 1])
x = tf.concat([cls_tokens, x], axis=1)
return x
def get_config(self):
"""
Return configuration of the LastStage layer.
Returns:
dict: Configuration dictionary.
"""
config = super(LastStage, self).get_config()
config.update({"dim": self.dim, "batch_size": self.batch_size})
return config
@classmethod
def from_config(cls, config):
"""
Create a LastStage layer from configuration.
Args:
config (dict): Configuration dictionary.
Returns:
LastStage: Instantiated LastStage layer.
"""
batch_size = config.get("batch_size")
config["batch_size"] = batch_size
return cls(**config)