tf-models-nightly 2.19.0.dev20250108__py2.py3-none-any.whl → 2.19.0.dev20250110__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of tf-models-nightly might be problematic. Click here for more details.
- official/projects/detr/__init__.py +14 -0
- official/projects/detr/configs/__init__.py +14 -0
- official/projects/detr/configs/detr.py +277 -0
- official/projects/detr/configs/detr_test.py +51 -0
- official/projects/detr/dataloaders/__init__.py +14 -0
- official/projects/detr/dataloaders/coco.py +157 -0
- official/projects/detr/dataloaders/coco_test.py +111 -0
- official/projects/detr/dataloaders/detr_input.py +175 -0
- official/projects/detr/experiments/__init__.py +14 -0
- official/projects/detr/modeling/__init__.py +14 -0
- official/projects/detr/modeling/detr.py +345 -0
- official/projects/detr/modeling/detr_test.py +70 -0
- official/projects/detr/modeling/transformer.py +849 -0
- official/projects/detr/modeling/transformer_test.py +263 -0
- official/projects/detr/ops/__init__.py +14 -0
- official/projects/detr/ops/matchers.py +489 -0
- official/projects/detr/ops/matchers_test.py +95 -0
- official/projects/detr/optimization.py +151 -0
- official/projects/detr/serving/__init__.py +14 -0
- official/projects/detr/serving/export_module.py +103 -0
- official/projects/detr/serving/export_module_test.py +98 -0
- official/projects/detr/serving/export_saved_model.py +109 -0
- official/projects/detr/tasks/__init__.py +14 -0
- official/projects/detr/tasks/detection.py +421 -0
- official/projects/detr/tasks/detection_test.py +203 -0
- official/projects/detr/train.py +70 -0
- {tf_models_nightly-2.19.0.dev20250108.dist-info → tf_models_nightly-2.19.0.dev20250110.dist-info}/METADATA +1 -1
- {tf_models_nightly-2.19.0.dev20250108.dist-info → tf_models_nightly-2.19.0.dev20250110.dist-info}/RECORD +32 -6
- {tf_models_nightly-2.19.0.dev20250108.dist-info → tf_models_nightly-2.19.0.dev20250110.dist-info}/AUTHORS +0 -0
- {tf_models_nightly-2.19.0.dev20250108.dist-info → tf_models_nightly-2.19.0.dev20250110.dist-info}/LICENSE +0 -0
- {tf_models_nightly-2.19.0.dev20250108.dist-info → tf_models_nightly-2.19.0.dev20250110.dist-info}/WHEEL +0 -0
- {tf_models_nightly-2.19.0.dev20250108.dist-info → tf_models_nightly-2.19.0.dev20250110.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,263 @@
|
|
1
|
+
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""Tests for transformer."""
|
16
|
+
|
17
|
+
import tensorflow as tf, tf_keras
|
18
|
+
|
19
|
+
from official.projects.detr.modeling import transformer
|
20
|
+
|
21
|
+
|
22
|
+
class TransformerTest(tf.test.TestCase):
|
23
|
+
|
24
|
+
def test_transformer_encoder_block(self):
|
25
|
+
batch_size = 2
|
26
|
+
sequence_length = 100
|
27
|
+
feature_size = 256
|
28
|
+
num_attention_heads = 2
|
29
|
+
inner_dim = 256
|
30
|
+
inner_activation = 'relu'
|
31
|
+
model = transformer.TransformerEncoderBlock(num_attention_heads, inner_dim,
|
32
|
+
inner_activation)
|
33
|
+
input_tensor = tf.ones((batch_size, sequence_length, feature_size))
|
34
|
+
attention_mask = tf.ones((batch_size, sequence_length, sequence_length),
|
35
|
+
dtype=tf.int64)
|
36
|
+
pos_embed = tf.ones((batch_size, sequence_length, feature_size))
|
37
|
+
|
38
|
+
out = model([input_tensor, attention_mask, pos_embed])
|
39
|
+
self.assertAllEqual(
|
40
|
+
tf.shape(out), (batch_size, sequence_length, feature_size))
|
41
|
+
|
42
|
+
def test_transformer_encoder_block_get_config(self):
|
43
|
+
num_attention_heads = 2
|
44
|
+
inner_dim = 256
|
45
|
+
inner_activation = 'relu'
|
46
|
+
model = transformer.TransformerEncoderBlock(num_attention_heads, inner_dim,
|
47
|
+
inner_activation)
|
48
|
+
config = model.get_config()
|
49
|
+
expected_config = {
|
50
|
+
'name': 'transformer_encoder_block',
|
51
|
+
'trainable': True,
|
52
|
+
'dtype': 'float32',
|
53
|
+
'num_attention_heads': 2,
|
54
|
+
'inner_dim': 256,
|
55
|
+
'inner_activation': 'relu',
|
56
|
+
'output_dropout': 0.0,
|
57
|
+
'attention_dropout': 0.0,
|
58
|
+
'output_range': None,
|
59
|
+
'kernel_initializer': {
|
60
|
+
'class_name': 'GlorotUniform',
|
61
|
+
'config': {
|
62
|
+
'seed': None}
|
63
|
+
},
|
64
|
+
'bias_initializer': {
|
65
|
+
'class_name': 'Zeros',
|
66
|
+
'config': {}
|
67
|
+
},
|
68
|
+
'kernel_regularizer': None,
|
69
|
+
'bias_regularizer': None,
|
70
|
+
'activity_regularizer': None,
|
71
|
+
'kernel_constraint': None,
|
72
|
+
'bias_constraint': None,
|
73
|
+
'use_bias': True,
|
74
|
+
'norm_first': False,
|
75
|
+
'norm_epsilon': 1e-12,
|
76
|
+
'inner_dropout': 0.0,
|
77
|
+
'attention_initializer': {
|
78
|
+
'class_name': 'GlorotUniform',
|
79
|
+
'config': {'seed': None}
|
80
|
+
},
|
81
|
+
'attention_axes': None}
|
82
|
+
self.assertAllEqual(expected_config, config)
|
83
|
+
|
84
|
+
def test_transformer_encoder(self):
|
85
|
+
batch_size = 2
|
86
|
+
sequence_length = 100
|
87
|
+
feature_size = 256
|
88
|
+
num_layers = 2
|
89
|
+
num_attention_heads = 2
|
90
|
+
intermediate_size = 256
|
91
|
+
model = transformer.TransformerEncoder(
|
92
|
+
num_layers=num_layers,
|
93
|
+
num_attention_heads=num_attention_heads,
|
94
|
+
intermediate_size=intermediate_size)
|
95
|
+
input_tensor = tf.ones((batch_size, sequence_length, feature_size))
|
96
|
+
attention_mask = tf.ones((batch_size, sequence_length, sequence_length),
|
97
|
+
dtype=tf.int64)
|
98
|
+
pos_embed = tf.ones((batch_size, sequence_length, feature_size))
|
99
|
+
out = model(input_tensor, attention_mask, pos_embed)
|
100
|
+
self.assertAllEqual(
|
101
|
+
tf.shape(out), (batch_size, sequence_length, feature_size))
|
102
|
+
|
103
|
+
def test_transformer_encoder_get_config(self):
|
104
|
+
num_layers = 2
|
105
|
+
num_attention_heads = 2
|
106
|
+
intermediate_size = 256
|
107
|
+
model = transformer.TransformerEncoder(
|
108
|
+
num_layers=num_layers,
|
109
|
+
num_attention_heads=num_attention_heads,
|
110
|
+
intermediate_size=intermediate_size)
|
111
|
+
config = model.get_config()
|
112
|
+
expected_config = {
|
113
|
+
'name': 'transformer_encoder',
|
114
|
+
'trainable': True,
|
115
|
+
'dtype': 'float32',
|
116
|
+
'num_layers': 2,
|
117
|
+
'num_attention_heads': 2,
|
118
|
+
'intermediate_size': 256,
|
119
|
+
'activation': 'relu',
|
120
|
+
'dropout_rate': 0.0,
|
121
|
+
'attention_dropout_rate': 0.0,
|
122
|
+
'use_bias': False,
|
123
|
+
'norm_first': True,
|
124
|
+
'norm_epsilon': 1e-06,
|
125
|
+
'intermediate_dropout': 0.0
|
126
|
+
}
|
127
|
+
self.assertAllEqual(expected_config, config)
|
128
|
+
|
129
|
+
def test_transformer_decoder_block(self):
|
130
|
+
batch_size = 2
|
131
|
+
sequence_length = 100
|
132
|
+
memory_length = 200
|
133
|
+
feature_size = 256
|
134
|
+
num_attention_heads = 2
|
135
|
+
intermediate_size = 256
|
136
|
+
intermediate_activation = 'relu'
|
137
|
+
model = transformer.TransformerDecoderBlock(num_attention_heads,
|
138
|
+
intermediate_size,
|
139
|
+
intermediate_activation)
|
140
|
+
input_tensor = tf.ones((batch_size, sequence_length, feature_size))
|
141
|
+
memory = tf.ones((batch_size, memory_length, feature_size))
|
142
|
+
attention_mask = tf.ones((batch_size, sequence_length, memory_length),
|
143
|
+
dtype=tf.int64)
|
144
|
+
self_attention_mask = tf.ones(
|
145
|
+
(batch_size, sequence_length, sequence_length), dtype=tf.int64)
|
146
|
+
input_pos_embed = tf.ones((batch_size, sequence_length, feature_size))
|
147
|
+
memory_pos_embed = tf.ones((batch_size, memory_length, feature_size))
|
148
|
+
|
149
|
+
out, _ = model([
|
150
|
+
input_tensor, memory, attention_mask, self_attention_mask,
|
151
|
+
input_pos_embed, memory_pos_embed
|
152
|
+
])
|
153
|
+
self.assertAllEqual(
|
154
|
+
tf.shape(out), (batch_size, sequence_length, feature_size))
|
155
|
+
|
156
|
+
def test_transformer_decoder_block_get_config(self):
|
157
|
+
num_attention_heads = 2
|
158
|
+
intermediate_size = 256
|
159
|
+
intermediate_activation = 'relu'
|
160
|
+
model = transformer.TransformerDecoderBlock(num_attention_heads,
|
161
|
+
intermediate_size,
|
162
|
+
intermediate_activation)
|
163
|
+
config = model.get_config()
|
164
|
+
expected_config = {
|
165
|
+
'name': 'transformer_decoder_block',
|
166
|
+
'trainable': True,
|
167
|
+
'dtype': 'float32',
|
168
|
+
'num_attention_heads': 2,
|
169
|
+
'intermediate_size': 256,
|
170
|
+
'intermediate_activation': 'relu',
|
171
|
+
'dropout_rate': 0.0,
|
172
|
+
'attention_dropout_rate': 0.0,
|
173
|
+
'kernel_initializer': {
|
174
|
+
'class_name': 'GlorotUniform',
|
175
|
+
'config': {
|
176
|
+
'seed': None
|
177
|
+
}
|
178
|
+
},
|
179
|
+
'bias_initializer': {
|
180
|
+
'class_name': 'Zeros',
|
181
|
+
'config': {}
|
182
|
+
},
|
183
|
+
'kernel_regularizer': None,
|
184
|
+
'bias_regularizer': None,
|
185
|
+
'activity_regularizer': None,
|
186
|
+
'kernel_constraint': None,
|
187
|
+
'bias_constraint': None,
|
188
|
+
'use_bias': True,
|
189
|
+
'norm_first': False,
|
190
|
+
'norm_epsilon': 1e-12,
|
191
|
+
'intermediate_dropout': 0.0,
|
192
|
+
'attention_initializer': {
|
193
|
+
'class_name': 'GlorotUniform',
|
194
|
+
'config': {
|
195
|
+
'seed': None
|
196
|
+
}
|
197
|
+
}
|
198
|
+
}
|
199
|
+
self.assertAllEqual(expected_config, config)
|
200
|
+
|
201
|
+
def test_transformer_decoder(self):
|
202
|
+
batch_size = 2
|
203
|
+
sequence_length = 100
|
204
|
+
memory_length = 200
|
205
|
+
feature_size = 256
|
206
|
+
num_layers = 2
|
207
|
+
num_attention_heads = 2
|
208
|
+
intermediate_size = 256
|
209
|
+
model = transformer.TransformerDecoder(
|
210
|
+
num_layers=num_layers,
|
211
|
+
num_attention_heads=num_attention_heads,
|
212
|
+
intermediate_size=intermediate_size)
|
213
|
+
input_tensor = tf.ones((batch_size, sequence_length, feature_size))
|
214
|
+
memory = tf.ones((batch_size, memory_length, feature_size))
|
215
|
+
attention_mask = tf.ones((batch_size, sequence_length, memory_length),
|
216
|
+
dtype=tf.int64)
|
217
|
+
self_attention_mask = tf.ones(
|
218
|
+
(batch_size, sequence_length, sequence_length), dtype=tf.int64)
|
219
|
+
input_pos_embed = tf.ones((batch_size, sequence_length, feature_size))
|
220
|
+
memory_pos_embed = tf.ones((batch_size, memory_length, feature_size))
|
221
|
+
|
222
|
+
outs = model(
|
223
|
+
input_tensor,
|
224
|
+
memory,
|
225
|
+
self_attention_mask,
|
226
|
+
attention_mask,
|
227
|
+
return_all_decoder_outputs=True,
|
228
|
+
input_pos_embed=input_pos_embed,
|
229
|
+
memory_pos_embed=memory_pos_embed)
|
230
|
+
self.assertLen(outs, 2) # intermeidate decoded outputs.
|
231
|
+
for out in outs:
|
232
|
+
self.assertAllEqual(
|
233
|
+
tf.shape(out), (batch_size, sequence_length, feature_size))
|
234
|
+
|
235
|
+
def test_transformer_decoder_get_config(self):
|
236
|
+
num_layers = 2
|
237
|
+
num_attention_heads = 2
|
238
|
+
intermediate_size = 256
|
239
|
+
model = transformer.TransformerDecoder(
|
240
|
+
num_layers=num_layers,
|
241
|
+
num_attention_heads=num_attention_heads,
|
242
|
+
intermediate_size=intermediate_size)
|
243
|
+
config = model.get_config()
|
244
|
+
expected_config = {
|
245
|
+
'name': 'transformer_decoder',
|
246
|
+
'trainable': True,
|
247
|
+
'dtype': 'float32',
|
248
|
+
'num_layers': 2,
|
249
|
+
'num_attention_heads': 2,
|
250
|
+
'intermediate_size': 256,
|
251
|
+
'activation': 'relu',
|
252
|
+
'dropout_rate': 0.0,
|
253
|
+
'attention_dropout_rate': 0.0,
|
254
|
+
'use_bias': False,
|
255
|
+
'norm_first': True,
|
256
|
+
'norm_epsilon': 1e-06,
|
257
|
+
'intermediate_dropout': 0.0
|
258
|
+
}
|
259
|
+
self.assertAllEqual(expected_config, config)
|
260
|
+
|
261
|
+
|
262
|
+
if __name__ == '__main__':
|
263
|
+
tf.test.main()
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|