-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_layers_normalization.py
270 lines (196 loc) · 8.64 KB
/
test_layers_normalization.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import unittest
import tensorflow as tf
import tensorlayer as tl
from tensorlayer.layers import *
from tensorlayer.models import Model
from tests.utils import CustomTestCase
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
class Laye_BatchNorm_Test(CustomTestCase):
@classmethod
def setUpClass(cls):
x_0_input_shape = [None, 10]
x_1_input_shape = [None, 100, 1]
x_2_input_shape = [None, 100, 100, 3]
x_3_input_shape = [None, 100, 100, 100, 3]
batchsize = 2
cls.x0 = tf.random.normal([batchsize] + x_0_input_shape[1:])
cls.x1 = tf.random.normal([batchsize] + x_1_input_shape[1:])
cls.x2 = tf.random.normal([batchsize] + x_2_input_shape[1:])
cls.x3 = tf.random.normal([batchsize] + x_3_input_shape[1:])
## Base
ni_1 = Input(x_1_input_shape, name='test_ni1')
nn_1 = Conv1d(n_filter=32, filter_size=5, stride=2, name='test_conv1d')(ni_1)
n1_b = BatchNorm(name='test_conv')(nn_1)
cls.n1_b = n1_b
cls.base_1d = Model(inputs=ni_1, outputs=n1_b, name='test_base_1d')
ni_2 = Input(x_2_input_shape, name='test_ni2')
nn_2 = Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), name='test_conv2d')(ni_2)
n2_b = BatchNorm(name='test_bn2d')(nn_2)
cls.n2_b = n2_b
cls.base_2d = Model(inputs=ni_2, outputs=n2_b, name='test_base_2d')
ni_3 = Input(x_3_input_shape, name='test_ni2')
nn_3 = Conv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), name='test_conv3d')(ni_3)
n3_b = BatchNorm(name='test_bn3d')(nn_3)
cls.n3_b = n3_b
cls.base_3d = Model(inputs=ni_3, outputs=n3_b, name='test_base_3d')
class bn_0d_model(Model):
def __init__(self):
super(bn_0d_model, self).__init__()
self.fc = Dense(32, in_channels=10)
self.bn = BatchNorm(num_features=32, name='test_bn1d')
def forward(self, x):
x = self.bn(self.fc(x))
return x
dynamic_base = bn_0d_model()
cls.n0_b = dynamic_base(cls.x0, is_train=True)
## 0D ========================================================================
nin_0 = Input(x_0_input_shape, name='test_in1')
n0 = Dense(32)(nin_0)
n0 = BatchNorm1d(name='test_bn0d')(n0)
cls.n0 = n0
cls.static_0d = Model(inputs=nin_0, outputs=n0)
class bn_0d_model(Model):
def __init__(self):
super(bn_0d_model, self).__init__(name='test_bn_0d_model')
self.fc = Dense(32, in_channels=10)
self.bn = BatchNorm1d(num_features=32, name='test_bn1d')
def forward(self, x):
x = self.bn(self.fc(x))
return x
cls.dynamic_0d = bn_0d_model()
print("Printing BatchNorm0d")
print(cls.static_0d)
print(cls.dynamic_0d)
## 1D ========================================================================
nin_1 = Input(x_1_input_shape, name='test_in1')
n1 = Conv1d(n_filter=32, filter_size=5, stride=2, name='test_conv1d')(nin_1)
n1 = BatchNorm1d(name='test_bn1d')(n1)
cls.n1 = n1
cls.static_1d = Model(inputs=nin_1, outputs=n1)
class bn_1d_model(Model):
def __init__(self):
super(bn_1d_model, self).__init__(name='test_bn_1d_model')
self.conv = Conv1d(n_filter=32, filter_size=5, stride=2, name='test_conv1d', in_channels=1)
self.bn = BatchNorm1d(num_features=32, name='test_bn1d')
def forward(self, x):
x = self.bn(self.conv(x))
return x
cls.dynamic_1d = bn_1d_model()
print("Printing BatchNorm1d")
print(cls.static_1d)
print(cls.dynamic_1d)
## 2D ========================================================================
nin_2 = Input(x_2_input_shape, name='test_in2')
n2 = Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), name='test_conv2d')(nin_2)
n2 = BatchNorm2d(name='test_bn2d')(n2)
cls.n2 = n2
cls.static_2d = Model(inputs=nin_2, outputs=n2)
class bn_2d_model(Model):
def __init__(self):
super(bn_2d_model, self).__init__(name='test_bn_2d_model')
self.conv = Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), name='test_conv2d', in_channels=3)
self.bn = BatchNorm2d(num_features=32, name='test_bn2d')
def forward(self, x):
x = self.bn(self.conv(x))
return x
cls.dynamic_2d = bn_2d_model()
print("Printing BatchNorm1d")
print(cls.static_2d)
print(cls.dynamic_2d)
## 3D ========================================================================
nin_3 = Input(x_3_input_shape, name='test_in3')
n3 = Conv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), name='test_conv3d')(nin_3)
n3 = BatchNorm3d(name='test_bn3d', act=tf.nn.relu)(n3)
cls.n3 = n3
cls.static_3d = Model(inputs=nin_3, outputs=n3)
class bn_3d_model(Model):
def __init__(self):
super(bn_3d_model, self).__init__(name='test_bn_3d_model')
self.conv = Conv3d(
n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), name='test_conv3d', in_channels=3
)
self.bn = BatchNorm3d(num_features=32, name='test_bn3d')
def forward(self, x):
x = self.bn(self.conv(x))
return x
cls.dynamic_3d = bn_3d_model()
print("Printing BatchNorm1d")
print(cls.static_3d)
print(cls.dynamic_3d)
@classmethod
def tearDownClass(cls):
pass
# tf.reset_default_graph()
def test_BatchNorm(self):
self.assertEqual(self.n1_b.shape[1:], (50, 32))
out = self.base_1d(self.x1, is_train=True)
self.assertEqual(self.n2_b.shape[1:], (50, 50, 32))
out = self.base_2d(self.x2, is_train=True)
self.assertEqual(self.n3_b.shape[1:], (50, 50, 50, 32))
out = self.base_3d(self.x3, is_train=True)
self.assertEqual(self.n0_b.shape[1:], (32))
print("test_BatchNorm OK")
def test_BatchNorm0d(self):
self.assertEqual(self.n0.shape[1:], (32))
out = self.static_0d(self.x0, is_train=True)
out = self.dynamic_0d(self.x0, is_train=True)
def test_BatchNorm1d(self):
self.assertEqual(self.n1.shape[1:], (50, 32))
out = self.static_1d(self.x1, is_train=True)
out = self.dynamic_1d(self.x1, is_train=True)
def test_BatchNorm2d(self):
self.assertEqual(self.n2.shape[1:], (50, 50, 32))
out = self.static_2d(self.x2, is_train=True)
out = self.dynamic_2d(self.x2, is_train=True)
out = self.dynamic_2d(self.x2, is_train=False)
def test_BatchNorm3d(self):
self.assertEqual(self.n3.shape[1:], (50, 50, 50, 32))
out = self.static_3d(self.x3, is_train=True)
out = self.dynamic_3d(self.x3, is_train=True)
def test_dataformat(self):
bn1d = BatchNorm1d(data_format='channels_first', num_features=32)
bn2d = BatchNorm2d(data_format='channels_first', num_features=32)
bn3d = BatchNorm3d(data_format='channels_first', num_features=32)
bn = BatchNorm(data_format='channels_first')
try:
bn_fail = BatchNorm1d(data_format='xyz', num_features=32)
except Exception as e:
self.assertIsInstance(e, ValueError)
print(e)
def test_exception(self):
try:
bn = BatchNorm(num_features=32)
except Exception as e:
self.assertIsInstance(e, ValueError)
print(e)
try:
ni = Input([None, 100, 1], name='test_ni1')
bn = BatchNorm(decay=1.5)(ni)
except Exception as e:
self.assertIsInstance(e, ValueError)
print(e)
def test_input_shape(self):
try:
bn = BatchNorm1d(num_features=32)
out = bn(self.x2)
except Exception as e:
self.assertIsInstance(e, ValueError)
print(e)
try:
bn = BatchNorm2d(num_features=32)
out = bn(self.x3)
except Exception as e:
self.assertIsInstance(e, ValueError)
print(e)
try:
bn = BatchNorm3d(num_features=32)
out = bn(self.x1)
except Exception as e:
self.assertIsInstance(e, ValueError)
print(e)
if __name__ == '__main__':
tl.logging.set_verbosity(tl.logging.DEBUG)
unittest.main()