From 5d0b2d57a7f4f0131ce8839a5fa15515f3c74503 Mon Sep 17 00:00:00 2001 From: autoih Date: Fri, 13 Dec 2019 11:52:09 -0800 Subject: [PATCH 1/7] init testing --- .../layers/normalizations_test.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tensorflow_addons/layers/normalizations_test.py b/tensorflow_addons/layers/normalizations_test.py index f463d66762..a69f9a7fec 100644 --- a/tensorflow_addons/layers/normalizations_test.py +++ b/tensorflow_addons/layers/normalizations_test.py @@ -249,6 +249,49 @@ def test_groupnorm_conv(self): a = model.fit(x=x, y=y, epochs=1) self.assertTrue(hasattr(model.layers[0], 'gamma')) + def test_different_groups(): + norm1 = GroupNormalization(axis=1, groups=2, input_shape=(10, 6)) + norm2 = GroupNormalization(axis=1, groups=1, input_shape=(10, 6)) + norm3 = GroupNormalization(axis=1, groups=10, input_shape=(10, 6)) + model = tf.keras.models.Sequential() + model.add(norm1) + model.compile(loss='mse', optimizer='rmsprop') + x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) + model.fit(x, x, epochs=5, verbose=0) + out = model.predict(x) + out -= np.reshape(K.eval(norm1.beta), (1, 10, 1)) + out /= np.reshape(K.eval(norm1.gamma), (1, 10, 1)) + + self.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) + self.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) + + model = tf.keras.models.Sequential() + model.add(norm2) + model.compile(loss='mse', optimizer='rmsprop') + + # centered on 5.0, variance 10.0 + x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) + model.fit(x, x, epochs=5, verbose=0) + out = model.predict(x) + out -= np.reshape(K.eval(norm2.beta), (1, 10, 1)) + out /= np.reshape(K.eval(norm2.gamma), (1, 10, 1)) + + self.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) + self.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) + + model = tf.keras.models.Sequential() + model.add(norm3) + model.compile(loss='mse', optimizer='rmsprop') + + # centered on 5.0, variance 10.0 + x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) + model.fit(x, x, epochs=5, verbose=0) + out = model.predict(x) + out -= np.reshape(K.eval(norm3.beta), (1, 10, 1)) + out /= np.reshape(K.eval(norm3.gamma), (1, 10, 1)) + + self.assert_AllClose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) + self.assert_Alllcose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) if __name__ == "__main__": tf.test.main() From 32bd3d6220edd41c5e943859d795b42f36b97bd3 Mon Sep 17 00:00:00 2001 From: autoih Date: Fri, 13 Dec 2019 16:31:21 -0800 Subject: [PATCH 2/7] use np assert_allclose --- .../layers/normalizations_test.py | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/tensorflow_addons/layers/normalizations_test.py b/tensorflow_addons/layers/normalizations_test.py index a69f9a7fec..04ce1d5c23 100644 --- a/tensorflow_addons/layers/normalizations_test.py +++ b/tensorflow_addons/layers/normalizations_test.py @@ -249,7 +249,7 @@ def test_groupnorm_conv(self): a = model.fit(x=x, y=y, epochs=1) self.assertTrue(hasattr(model.layers[0], 'gamma')) - def test_different_groups(): + def test_different_groups(self): norm1 = GroupNormalization(axis=1, groups=2, input_shape=(10, 6)) norm2 = GroupNormalization(axis=1, groups=1, input_shape=(10, 6)) norm3 = GroupNormalization(axis=1, groups=10, input_shape=(10, 6)) @@ -259,39 +259,37 @@ def test_different_groups(): x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) - out -= np.reshape(K.eval(norm1.beta), (1, 10, 1)) - out /= np.reshape(K.eval(norm1.gamma), (1, 10, 1)) + out -= np.reshape(self.evaluate(norm1.beta), (1, 10, 1)) + out /= np.reshape(self.evaluate(norm1.gamma), (1, 10, 1)) - self.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) - self.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) + np.testing.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) + np.testing.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) model = tf.keras.models.Sequential() model.add(norm2) model.compile(loss='mse', optimizer='rmsprop') - # centered on 5.0, variance 10.0 x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) - out -= np.reshape(K.eval(norm2.beta), (1, 10, 1)) - out /= np.reshape(K.eval(norm2.gamma), (1, 10, 1)) + out -= np.reshape(self.evaluate(norm2.beta), (1, 10, 1)) + out /= np.reshape(self.evaluate(norm2.gamma), (1, 10, 1)) - self.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) - self.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) + np.testing.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) + np.testing.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) model = tf.keras.models.Sequential() model.add(norm3) model.compile(loss='mse', optimizer='rmsprop') - # centered on 5.0, variance 10.0 x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) - out -= np.reshape(K.eval(norm3.beta), (1, 10, 1)) - out /= np.reshape(K.eval(norm3.gamma), (1, 10, 1)) + out -= np.reshape(self.evaluate(norm3.beta), (1, 10, 1)) + out /= np.reshape(self.evaluate(norm3.gamma), (1, 10, 1)) - self.assert_AllClose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) - self.assert_Alllcose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) + np.testing.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) + np.testing.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) if __name__ == "__main__": tf.test.main() From aaae806329f8d655f7919ce3945cb9acc2ee090b Mon Sep 17 00:00:00 2001 From: autoih Date: Fri, 13 Dec 2019 16:48:58 -0800 Subject: [PATCH 3/7] add no center and scale test --- .../layers/normalizations_test.py | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/layers/normalizations_test.py b/tensorflow_addons/layers/normalizations_test.py index 04ce1d5c23..3e51e69dae 100644 --- a/tensorflow_addons/layers/normalizations_test.py +++ b/tensorflow_addons/layers/normalizations_test.py @@ -249,12 +249,13 @@ def test_groupnorm_conv(self): a = model.fit(x=x, y=y, epochs=1) self.assertTrue(hasattr(model.layers[0], 'gamma')) - def test_different_groups(self): + def test_groupnorm_different_groups(self): norm1 = GroupNormalization(axis=1, groups=2, input_shape=(10, 6)) norm2 = GroupNormalization(axis=1, groups=1, input_shape=(10, 6)) norm3 = GroupNormalization(axis=1, groups=10, input_shape=(10, 6)) model = tf.keras.models.Sequential() model.add(norm1) + # centered and variance are 5.0 and 10.0, respectively model.compile(loss='mse', optimizer='rmsprop') x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) @@ -267,8 +268,8 @@ def test_different_groups(self): model = tf.keras.models.Sequential() model.add(norm2) + # centered and variance are 5.0 and 10.0, respectively model.compile(loss='mse', optimizer='rmsprop') - x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) @@ -280,8 +281,8 @@ def test_different_groups(self): model = tf.keras.models.Sequential() model.add(norm3) + # centered and variance are 5.0 and 10.0, respectively model.compile(loss='mse', optimizer='rmsprop') - x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) @@ -291,5 +292,21 @@ def test_different_groups(self): np.testing.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) np.testing.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) + def test_groupnorm_convnet_no_center_scale(self): + model = tf.keras.models.Sequential() + norm = GroupNormalization(axis=-1, groups=2, center=False, + scale=False, input_shape=(3, 4, 4)) + model.add(norm) + model.compile(loss='mse', optimizer='sgd') + # centered and variance are 5.0 and 10.0, respectively + x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 3, 4, 4)) + model.fit(x, x, epochs=4, verbose=0) + out = model.predict(x) + + np.testing.assert_allclose(np.mean(out, axis=(0, 2, 3)), + 0.0, atol=1e-1) + np.testing.assert_allclose(np.std(out, axis=(0, 2, 3)), + 1.0, atol=1e-1) + if __name__ == "__main__": tf.test.main() From 359d6784f2e367fbb519d283de3008a56cee2627 Mon Sep 17 00:00:00 2001 From: autoih Date: Fri, 13 Dec 2019 17:06:14 -0800 Subject: [PATCH 4/7] sanitycheck --- tensorflow_addons/layers/normalizations_test.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/layers/normalizations_test.py b/tensorflow_addons/layers/normalizations_test.py index 3e51e69dae..e2af65abb5 100644 --- a/tensorflow_addons/layers/normalizations_test.py +++ b/tensorflow_addons/layers/normalizations_test.py @@ -294,8 +294,12 @@ def test_groupnorm_different_groups(self): def test_groupnorm_convnet_no_center_scale(self): model = tf.keras.models.Sequential() - norm = GroupNormalization(axis=-1, groups=2, center=False, - scale=False, input_shape=(3, 4, 4)) + norm = GroupNormalization( + axis=-1, + groups=2, + center=False, + scale=False, + input_shape=(3, 4, 4)) model.add(norm) model.compile(loss='mse', optimizer='sgd') # centered and variance are 5.0 and 10.0, respectively @@ -303,10 +307,10 @@ def test_groupnorm_convnet_no_center_scale(self): model.fit(x, x, epochs=4, verbose=0) out = model.predict(x) - np.testing.assert_allclose(np.mean(out, axis=(0, 2, 3)), - 0.0, atol=1e-1) - np.testing.assert_allclose(np.std(out, axis=(0, 2, 3)), - 1.0, atol=1e-1) + np.testing.assert_allclose( + np.mean(out, axis=(0, 2, 3)), 0.0, atol=1e-1) + np.testing.assert_allclose(np.std(out, axis=(0, 2, 3)), 1.0, atol=1e-1) + if __name__ == "__main__": tf.test.main() From 74fe69b41a92af9fa4fcbc8df279f06a7df058cd Mon Sep 17 00:00:00 2001 From: autoih Date: Tue, 11 Feb 2020 09:56:31 -0800 Subject: [PATCH 5/7] add test --- .../layers/normalizations_test.py | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/tensorflow_addons/layers/normalizations_test.py b/tensorflow_addons/layers/normalizations_test.py index 132972308b..6f9a7ffe53 100644 --- a/tensorflow_addons/layers/normalizations_test.py +++ b/tensorflow_addons/layers/normalizations_test.py @@ -246,6 +246,21 @@ def test_groupnorm_conv(self): model.fit(x=x, y=y, epochs=1) self.assertTrue(hasattr(model.layers[0], "gamma")) + def test_groupnorm_correctness_1d(self): + model = tf.keras.models.Sequential() + norm = GroupNormalization(input_shape=(10,), groups=2) + model.add(norm) + model.compile(loss="mse", optimizer="rmsprop") + + x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10)) + model.fit(x, x, epochs=5, verbose=0) + out = model.predict(x) + out -= tf.keras.backend.eval(norm.beta) + out /= tf.keras.backend.eval(norm.gamma) + + np.testing.assert_allclose(out.mean(), 0.0, atol=1e-1) + np.testing.assert_allclose(out.std(), 1.0, atol=1e-1) + def test_groupnorm_different_groups(self): norm1 = GroupNormalization(axis=1, groups=2, input_shape=(10, 6)) norm2 = GroupNormalization(axis=1, groups=1, input_shape=(10, 6)) @@ -253,7 +268,7 @@ def test_groupnorm_different_groups(self): model = tf.keras.models.Sequential() model.add(norm1) # centered and variance are 5.0 and 10.0, respectively - model.compile(loss='mse', optimizer='rmsprop') + model.compile(loss="mse", optimizer="rmsprop") x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) @@ -266,7 +281,7 @@ def test_groupnorm_different_groups(self): model = tf.keras.models.Sequential() model.add(norm2) # centered and variance are 5.0 and 10.0, respectively - model.compile(loss='mse', optimizer='rmsprop') + model.compile(loss="mse", optimizer="rmsprop") x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) @@ -279,7 +294,7 @@ def test_groupnorm_different_groups(self): model = tf.keras.models.Sequential() model.add(norm3) # centered and variance are 5.0 and 10.0, respectively - model.compile(loss='mse', optimizer='rmsprop') + model.compile(loss="mse", optimizer="rmsprop") x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) @@ -289,23 +304,35 @@ def test_groupnorm_different_groups(self): np.testing.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) np.testing.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) + def test_groupnorm_convnet(self): + model = tf.keras.models.Sequential() + norm = GroupNormalization(axis=1, input_shape=(3, 4, 4), groups=3) + model.add(norm) + model.compile(loss="mse", optimizer="sgd") + + # centered = 5.0, variance = 10.0 + x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 3, 4, 4)) + model.fit(x, x, epochs=4, verbose=0) + out = model.predict(x) + out -= np.reshape(tf.keras.backend.eval(norm.beta), (1, 3, 1, 1)) + out /= np.reshape(tf.keras.backend.eval(norm.gamma), (1, 3, 1, 1)) + + np.testing.assert_allclose(np.mean(out, axis=(0, 2, 3)), 0.0, atol=1e-1) + np.testing.assert_allclose(np.std(out, axis=(0, 2, 3)), 1.0, atol=1e-1) + def test_groupnorm_convnet_no_center_scale(self): model = tf.keras.models.Sequential() norm = GroupNormalization( - axis=-1, - groups=2, - center=False, - scale=False, - input_shape=(3, 4, 4)) + axis=-1, groups=2, center=False, scale=False, input_shape=(3, 4, 4) + ) model.add(norm) - model.compile(loss='mse', optimizer='sgd') + model.compile(loss="mse", optimizer="sgd") # centered and variance are 5.0 and 10.0, respectively x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 3, 4, 4)) model.fit(x, x, epochs=4, verbose=0) out = model.predict(x) - np.testing.assert_allclose( - np.mean(out, axis=(0, 2, 3)), 0.0, atol=1e-1) + np.testing.assert_allclose(np.mean(out, axis=(0, 2, 3)), 0.0, atol=1e-1) np.testing.assert_allclose(np.std(out, axis=(0, 2, 3)), 1.0, atol=1e-1) From 600469d58f153b098a268344d97dd68d569aa9aa Mon Sep 17 00:00:00 2001 From: autoih Date: Wed, 26 Feb 2020 21:20:06 -0800 Subject: [PATCH 6/7] revise based on comments --- .../layers/normalizations_test.py | 84 ++++++++----------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/tensorflow_addons/layers/normalizations_test.py b/tensorflow_addons/layers/normalizations_test.py index 6f9a7ffe53..72facaac7c 100644 --- a/tensorflow_addons/layers/normalizations_test.py +++ b/tensorflow_addons/layers/normalizations_test.py @@ -258,51 +258,29 @@ def test_groupnorm_correctness_1d(self): out -= tf.keras.backend.eval(norm.beta) out /= tf.keras.backend.eval(norm.gamma) - np.testing.assert_allclose(out.mean(), 0.0, atol=1e-1) - np.testing.assert_allclose(out.std(), 1.0, atol=1e-1) + self.assertAllClose(out.mean(), 0.0, atol=1e-1) + self.assertAllClose(out.std(), 1.0, atol=1e-1) - def test_groupnorm_different_groups(self): - norm1 = GroupNormalization(axis=1, groups=2, input_shape=(10, 6)) - norm2 = GroupNormalization(axis=1, groups=1, input_shape=(10, 6)) - norm3 = GroupNormalization(axis=1, groups=10, input_shape=(10, 6)) - model = tf.keras.models.Sequential() - model.add(norm1) - # centered and variance are 5.0 and 10.0, respectively - model.compile(loss="mse", optimizer="rmsprop") - x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) - model.fit(x, x, epochs=5, verbose=0) - out = model.predict(x) - out -= np.reshape(self.evaluate(norm1.beta), (1, 10, 1)) - out /= np.reshape(self.evaluate(norm1.gamma), (1, 10, 1)) - - np.testing.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) - np.testing.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) - - model = tf.keras.models.Sequential() - model.add(norm2) - # centered and variance are 5.0 and 10.0, respectively - model.compile(loss="mse", optimizer="rmsprop") - x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) - model.fit(x, x, epochs=5, verbose=0) - out = model.predict(x) - out -= np.reshape(self.evaluate(norm2.beta), (1, 10, 1)) - out /= np.reshape(self.evaluate(norm2.gamma), (1, 10, 1)) - - np.testing.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) - np.testing.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) - - model = tf.keras.models.Sequential() - model.add(norm3) - # centered and variance are 5.0 and 10.0, respectively - model.compile(loss="mse", optimizer="rmsprop") - x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 6)) - model.fit(x, x, epochs=5, verbose=0) - out = model.predict(x) - out -= np.reshape(self.evaluate(norm3.beta), (1, 10, 1)) - out /= np.reshape(self.evaluate(norm3.gamma), (1, 10, 1)) - - np.testing.assert_allclose(out.mean(axis=(0, 2)), 0.0, atol=1.1e-1) - np.testing.assert_allclose(out.std(axis=(0, 2)), 1.0, atol=1.1e-1) + def test_groupnorm_2d_different_groups(self): + groups = [2, 1, 10] + for i in groups: + model = tf.keras.models.Sequential() + norm = GroupNormalization(axis=1, groups=i, input_shape=(10, 3)) + model.add(norm) + # centered and variance are 5.0 and 10.0, respectively + model.compile(loss="mse", optimizer="rmsprop") + x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10, 3)) + model.fit(x, x, epochs=5, verbose=0) + out = model.predict(x) + out -= np.reshape(self.evaluate(norm.beta), (1, 10, 1)) + out /= np.reshape(self.evaluate(norm.gamma), (1, 10, 1)) + + self.assertAllClose( + out.mean(axis=(0, 1), dtype=np.float32), (0.0, 0.0, 0.0), atol=1.1e-1 + ) + self.assertAllClose( + out.std(axis=(0, 1), dtype=np.float32), (1.0, 1.0, 1.0), atol=1.1e-1 + ) def test_groupnorm_convnet(self): model = tf.keras.models.Sequential() @@ -317,10 +295,14 @@ def test_groupnorm_convnet(self): out -= np.reshape(tf.keras.backend.eval(norm.beta), (1, 3, 1, 1)) out /= np.reshape(tf.keras.backend.eval(norm.gamma), (1, 3, 1, 1)) - np.testing.assert_allclose(np.mean(out, axis=(0, 2, 3)), 0.0, atol=1e-1) - np.testing.assert_allclose(np.std(out, axis=(0, 2, 3)), 1.0, atol=1e-1) + self.assertAllClose( + np.mean(out, axis=(0, 2, 3), dtype=np.float32), (0.0, 0.0, 0.0), atol=1e-1 + ) + self.assertAllClose( + np.std(out, axis=(0, 2, 3), dtype=np.float32), (1.0, 1.0, 1.0), atol=1e-1 + ) - def test_groupnorm_convnet_no_center_scale(self): + def test_groupnorm_convnet_no_center_no_scale(self): model = tf.keras.models.Sequential() norm = GroupNormalization( axis=-1, groups=2, center=False, scale=False, input_shape=(3, 4, 4) @@ -332,8 +314,12 @@ def test_groupnorm_convnet_no_center_scale(self): model.fit(x, x, epochs=4, verbose=0) out = model.predict(x) - np.testing.assert_allclose(np.mean(out, axis=(0, 2, 3)), 0.0, atol=1e-1) - np.testing.assert_allclose(np.std(out, axis=(0, 2, 3)), 1.0, atol=1e-1) + self.assertAllClose( + np.mean(out, axis=(0, 2, 3), dtype=np.float32), (0.0, 0.0, 0.0), atol=1e-1 + ) + self.assertAllClose( + np.std(out, axis=(0, 2, 3), dtype=np.float32), (1.0, 1.0, 1.0), atol=1e-1 + ) if __name__ == "__main__": From cef3c22da7854ee90b214ca6a992ecb178b5f1fc Mon Sep 17 00:00:00 2001 From: autoih Date: Thu, 27 Feb 2020 10:30:04 -0800 Subject: [PATCH 7/7] add random seed --- .../layers/normalizations_test.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/layers/normalizations_test.py b/tensorflow_addons/layers/normalizations_test.py index 72facaac7c..b6aca121ef 100644 --- a/tensorflow_addons/layers/normalizations_test.py +++ b/tensorflow_addons/layers/normalizations_test.py @@ -139,6 +139,7 @@ def _test_specific_layer(self, inputs, axis, groups, center, scale): def _create_and_fit_Sequential_model(self, layer, shape): # Helperfunction for quick evaluation + np.random.seed(0x2020) model = tf.keras.models.Sequential() model.add(layer) model.add(tf.keras.layers.Dense(32)) @@ -233,6 +234,7 @@ def test_regularizations(self): def test_groupnorm_conv(self): # Check if Axis is working for CONV nets # Testing for 1 == LayerNorm, 5 == GroupNorm, -1 == InstanceNorm + np.random.seed(0x2020) groups = [-1, 5, 1] for i in groups: model = tf.keras.models.Sequential() @@ -247,6 +249,7 @@ def test_groupnorm_conv(self): self.assertTrue(hasattr(model.layers[0], "gamma")) def test_groupnorm_correctness_1d(self): + np.random.seed(0x2020) model = tf.keras.models.Sequential() norm = GroupNormalization(input_shape=(10,), groups=2) model.add(norm) @@ -255,13 +258,14 @@ def test_groupnorm_correctness_1d(self): x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10)) model.fit(x, x, epochs=5, verbose=0) out = model.predict(x) - out -= tf.keras.backend.eval(norm.beta) - out /= tf.keras.backend.eval(norm.gamma) + out -= self.evaluate(norm.beta) + out /= self.evaluate(norm.gamma) self.assertAllClose(out.mean(), 0.0, atol=1e-1) self.assertAllClose(out.std(), 1.0, atol=1e-1) def test_groupnorm_2d_different_groups(self): + np.random.seed(0x2020) groups = [2, 1, 10] for i in groups: model = tf.keras.models.Sequential() @@ -276,13 +280,14 @@ def test_groupnorm_2d_different_groups(self): out /= np.reshape(self.evaluate(norm.gamma), (1, 10, 1)) self.assertAllClose( - out.mean(axis=(0, 1), dtype=np.float32), (0.0, 0.0, 0.0), atol=1.1e-1 + out.mean(axis=(0, 1), dtype=np.float32), (0.0, 0.0, 0.0), atol=1e-1 ) self.assertAllClose( - out.std(axis=(0, 1), dtype=np.float32), (1.0, 1.0, 1.0), atol=1.1e-1 + out.std(axis=(0, 1), dtype=np.float32), (1.0, 1.0, 1.0), atol=1e-1 ) def test_groupnorm_convnet(self): + np.random.seed(0x2020) model = tf.keras.models.Sequential() norm = GroupNormalization(axis=1, input_shape=(3, 4, 4), groups=3) model.add(norm) @@ -292,8 +297,8 @@ def test_groupnorm_convnet(self): x = np.random.normal(loc=5.0, scale=10.0, size=(1000, 3, 4, 4)) model.fit(x, x, epochs=4, verbose=0) out = model.predict(x) - out -= np.reshape(tf.keras.backend.eval(norm.beta), (1, 3, 1, 1)) - out /= np.reshape(tf.keras.backend.eval(norm.gamma), (1, 3, 1, 1)) + out -= np.reshape(self.evaluate(norm.beta), (1, 3, 1, 1)) + out /= np.reshape(self.evaluate(norm.gamma), (1, 3, 1, 1)) self.assertAllClose( np.mean(out, axis=(0, 2, 3), dtype=np.float32), (0.0, 0.0, 0.0), atol=1e-1 @@ -303,6 +308,7 @@ def test_groupnorm_convnet(self): ) def test_groupnorm_convnet_no_center_no_scale(self): + np.random.seed(0x2020) model = tf.keras.models.Sequential() norm = GroupNormalization( axis=-1, groups=2, center=False, scale=False, input_shape=(3, 4, 4)