You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_layers_normalization.py 6.8 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import unittest
  5. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
  6. import tensorlayer as tl
  7. from tensorlayer.layers import *
  8. from tests.utils import CustomTestCase
  9. class Laye_BatchNorm_Test(CustomTestCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. x_0_input_shape = [None, 10]
  13. x_1_input_shape = [None, 100, 1]
  14. x_2_input_shape = [None, 100, 100, 3]
  15. x_3_input_shape = [None, 100, 100, 100, 3]
  16. batchsize = 2
  17. cls.x0 = tl.ops.truncated_normal(shape=[batchsize] + x_0_input_shape[1:])
  18. cls.x1 = tl.ops.truncated_normal([batchsize] + x_1_input_shape[1:])
  19. cls.x2 = tl.ops.truncated_normal([batchsize] + x_2_input_shape[1:])
  20. cls.x3 = tl.ops.truncated_normal([batchsize] + x_3_input_shape[1:])
  21. ## Base
  22. ni_1 = Input(x_1_input_shape, name='test_ni1')
  23. nn_1 = Conv1d(n_filter=32, filter_size=5, stride=2, name='test_conv1d')(ni_1)
  24. n1_b = BatchNorm(name='test_conv')(nn_1)
  25. cls.n1_b = n1_b
  26. ni_2 = Input(x_2_input_shape, name='test_ni2')
  27. nn_2 = Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), name='test_conv2d')(ni_2)
  28. n2_b = BatchNorm(name='test_bn2d')(nn_2)
  29. cls.n2_b = n2_b
  30. ni_3 = Input(x_3_input_shape, name='test_ni2')
  31. nn_3 = Conv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), name='test_conv3d')(ni_3)
  32. n3_b = BatchNorm(name='test_bn3d')(nn_3)
  33. cls.n3_b = n3_b
  34. class bn_0d_model(tl.layers.Module):
  35. def __init__(self):
  36. super(bn_0d_model, self).__init__()
  37. self.fc = Dense(32, in_channels=10)
  38. self.bn = BatchNorm(num_features=32, name='test_bn1d')
  39. def forward(self, x):
  40. x = self.bn(self.fc(x))
  41. return x
  42. dynamic_base = bn_0d_model()
  43. cls.n0_b = dynamic_base(cls.x0)
  44. ## 0D ========================================================================
  45. nin_0 = Input(x_0_input_shape, name='test_in1')
  46. n0 = Dense(32)(nin_0)
  47. n0 = BatchNorm1d(name='test_bn0d')(n0)
  48. cls.n0 = n0
  49. class bn_0d_model(tl.layers.Module):
  50. def __init__(self):
  51. super(bn_0d_model, self).__init__(name='test_bn_0d_model')
  52. self.fc = Dense(32, in_channels=10)
  53. self.bn = BatchNorm1d(num_features=32, name='test_bn1d')
  54. def forward(self, x):
  55. x = self.bn(self.fc(x))
  56. return x
  57. cls.dynamic_0d = bn_0d_model()
  58. ## 1D ========================================================================
  59. nin_1 = Input(x_1_input_shape, name='test_in1')
  60. n1 = Conv1d(n_filter=32, filter_size=5, stride=2, name='test_conv1d')(nin_1)
  61. n1 = BatchNorm1d(name='test_bn1d')(n1)
  62. cls.n1 = n1
  63. class bn_1d_model(tl.layers.Module):
  64. def __init__(self):
  65. super(bn_1d_model, self).__init__(name='test_bn_1d_model')
  66. self.conv = Conv1d(n_filter=32, filter_size=5, stride=2, name='test_conv1d', in_channels=1)
  67. self.bn = BatchNorm1d(num_features=32, name='test_bn1d')
  68. def forward(self, x):
  69. x = self.bn(self.conv(x))
  70. return x
  71. cls.dynamic_1d = bn_1d_model()
  72. ## 2D ========================================================================
  73. nin_2 = Input(x_2_input_shape, name='test_in2')
  74. n2 = Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), name='test_conv2d')(nin_2)
  75. n2 = BatchNorm2d(name='test_bn2d')(n2)
  76. cls.n2 = n2
  77. class bn_2d_model(tl.layers.Module):
  78. def __init__(self):
  79. super(bn_2d_model, self).__init__(name='test_bn_2d_model')
  80. self.conv = Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), name='test_conv2d', in_channels=3)
  81. self.bn = BatchNorm2d(num_features=32, name='test_bn2d')
  82. def forward(self, x):
  83. x = self.bn(self.conv(x))
  84. return x
  85. cls.dynamic_2d = bn_2d_model()
  86. ## 3D ========================================================================
  87. nin_3 = Input(x_3_input_shape, name='test_in3')
  88. n3 = Conv3d(n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), name='test_conv3d')(nin_3)
  89. n3 = BatchNorm3d(name='test_bn3d', act=tl.ReLU)(n3)
  90. cls.n3 = n3
  91. class bn_3d_model(tl.layers.Module):
  92. def __init__(self):
  93. super(bn_3d_model, self).__init__(name='test_bn_3d_model')
  94. self.conv = Conv3d(
  95. n_filter=32, filter_size=(3, 3, 3), strides=(2, 2, 2), name='test_conv3d', in_channels=3
  96. )
  97. self.bn = BatchNorm3d(num_features=32, name='test_bn3d')
  98. def forward(self, x):
  99. x = self.bn(self.conv(x))
  100. return x
  101. cls.dynamic_3d = bn_3d_model()
  102. @classmethod
  103. def tearDownClass(cls):
  104. pass
  105. # tf.reset_default_graph()
  106. def test_BatchNorm(self):
  107. self.assertEqual(self.n1_b.shape[1:], (50, 32))
  108. self.assertEqual(self.n2_b.shape[1:], (50, 50, 32))
  109. self.assertEqual(self.n3_b.shape[1:], (50, 50, 50, 32))
  110. self.assertEqual(self.n0_b.shape[1:], (32))
  111. print("test_BatchNorm OK")
  112. def test_BatchNorm0d(self):
  113. self.assertEqual(self.n0.shape[1:], (32))
  114. def test_BatchNorm1d(self):
  115. self.assertEqual(self.n1.shape[1:], (50, 32))
  116. def test_BatchNorm2d(self):
  117. self.assertEqual(self.n2.shape[1:], (50, 50, 32))
  118. def test_BatchNorm3d(self):
  119. self.assertEqual(self.n3.shape[1:], (50, 50, 50, 32))
  120. def test_dataformat(self):
  121. bn1d = BatchNorm1d(data_format='channels_first', num_features=32)
  122. bn2d = BatchNorm2d(data_format='channels_first', num_features=32)
  123. bn3d = BatchNorm3d(data_format='channels_first', num_features=32)
  124. bn = BatchNorm(data_format='channels_first')
  125. try:
  126. bn_fail = BatchNorm1d(data_format='xyz', num_features=32)
  127. except Exception as e:
  128. self.assertIsInstance(e, ValueError)
  129. print(e)
  130. def test_exception(self):
  131. try:
  132. bn = BatchNorm(num_features=32)
  133. except Exception as e:
  134. self.assertIsInstance(e, ValueError)
  135. print(e)
  136. try:
  137. ni = Input([None, 100, 1], name='test_ni1')
  138. bn = BatchNorm(decay=1.5)(ni)
  139. except Exception as e:
  140. self.assertIsInstance(e, ValueError)
  141. print(e)
  142. if __name__ == '__main__':
  143. tl.logging.set_verbosity(tl.logging.DEBUG)
  144. unittest.main()

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.