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.

tensorflow_initializers.py 7.3 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import numpy as np
  4. import tensorlayer as tl
  5. __all__ = [
  6. 'Initializer', 'Zeros', 'Ones', 'Constant', 'RandomUniform', 'RandomNormal', 'TruncatedNormal',
  7. 'deconv2d_bilinear_upsampling_initializer', 'HeNormal'
  8. ]
  9. class Initializer(object):
  10. """Initializer base class: all initializers inherit from this class.
  11. """
  12. def __call__(self, shape, dtype=None):
  13. """Returns a tensor object initialized as specified by the initializer.
  14. Parameters
  15. ----------
  16. shape : tuple of int.
  17. The shape of the tensor.
  18. dtype : Optional dtype of the tensor.
  19. If not provided will return tensor of `tl.float32`.
  20. Returns
  21. -------
  22. """
  23. raise NotImplementedError
  24. def get_config(self):
  25. """Returns the configuration of the initializer as a JSON-serializable dict.
  26. Returns
  27. -------
  28. A JSON-serializable Python dict.
  29. """
  30. return {}
  31. @classmethod
  32. def from_config(cls, config):
  33. """Instantiates an initializer from a configuration dictionary.
  34. Parameters
  35. ----------
  36. config : A python dictionary.
  37. It will typically be the output of `get_config`.
  38. Returns
  39. -------
  40. An Initializer instance.
  41. """
  42. if 'dtype' in config:
  43. config.pop('dtype')
  44. return cls(**config)
  45. class Zeros(Initializer):
  46. """Initializer that generates tensors initialized to 0.
  47. """
  48. def __call__(self, shape, dtype=tl.float32):
  49. return tl.zeros(shape, dtype=dtype)
  50. class Ones(Initializer):
  51. """Initializer that generates tensors initialized to 1.
  52. """
  53. def __call__(self, shape, dtype=tl.float32):
  54. return tl.ones(shape, dtype=dtype)
  55. class Constant(Initializer):
  56. """Initializer that generates tensors initialized to a constant value.
  57. Parameters
  58. ----------
  59. value : A python scalar or a numpy array.
  60. The assigned value.
  61. """
  62. def __init__(self, value=0):
  63. self.value = value
  64. def __call__(self, shape, dtype=tl.float32):
  65. return tl.constant(self.value, shape=shape, dtype=dtype)
  66. def get_config(self):
  67. return {"value": self.value}
  68. class RandomUniform(Initializer):
  69. """Initializer that generates tensors with a uniform distribution.
  70. Parameters
  71. ----------
  72. minval : A python scalar or a scalar tensor.
  73. Lower bound of the range of random values to generate.
  74. maxval : A python scalar or a scalar tensor.
  75. Upper bound of the range of random values to generate.
  76. seed : A Python integer.
  77. Used to seed the random generator.
  78. """
  79. def __init__(self, minval=-0.05, maxval=0.05, seed=None):
  80. self.minval = minval
  81. self.maxval = maxval
  82. self.seed = seed
  83. def __call__(self, shape, dtype=tl.float32):
  84. return tl.random_uniform(shape, self.minval, self.maxval, dtype=dtype, seed=self.seed)
  85. def get_config(self):
  86. return {"minval": self.minval, "maxval": self.maxval, "seed": self.seed}
  87. class RandomNormal(Initializer):
  88. """Initializer that generates tensors with a normal distribution.
  89. Parameters
  90. ----------
  91. mean : A python scalar or a scalar tensor.
  92. Mean of the random values to generate.
  93. stddev : A python scalar or a scalar tensor.
  94. Standard deviation of the random values to generate.
  95. seed : A Python integer.
  96. Used to seed the random generator.
  97. """
  98. def __init__(self, mean=0.0, stddev=0.05, seed=None):
  99. self.mean = mean
  100. self.stddev = stddev
  101. self.seed = seed
  102. def __call__(self, shape, dtype=tl.float32):
  103. return tl.random_normal(shape, self.mean, self.stddev, dtype=dtype, seed=self.seed)
  104. def get_config(self):
  105. return {"mean": self.mean, "stddev": self.stddev, "seed": self.seed}
  106. class TruncatedNormal(Initializer):
  107. """Initializer that generates a truncated normal distribution.
  108. These values are similar to values from a `RandomNormal`
  109. except that values more than two standard deviations from the mean
  110. are discarded and re-drawn. This is the recommended initializer for
  111. neural network weights and filters.
  112. Parameters
  113. ----------
  114. mean : A python scalar or a scalar tensor.
  115. Mean of the random values to generate.
  116. stddev : A python scalar or a scalar tensor.
  117. Standard deviation of the andom values to generate.
  118. seed : A Python integer.
  119. Used to seed the random generator.
  120. """
  121. def __init__(self, mean=0.0, stddev=0.05, seed=None):
  122. self.mean = mean
  123. self.stddev = stddev
  124. self.seed = seed
  125. def __call__(self, shape, dtype=tl.float32):
  126. return tl.truncated_normal(shape, self.mean, self.stddev, dtype=dtype, seed=self.seed)
  127. def get_config(self):
  128. return {"mean": self.mean, "stddev": self.stddev, "seed": self.seed}
  129. class HeNormal(Initializer):
  130. """He normal initializer.
  131. Parameters
  132. ----------
  133. seed : A Python integer.
  134. Used to seed the random generator.
  135. """
  136. def __init__(self, seed=None):
  137. self.seed = seed
  138. def __call__(self, shape, dtype=tl.float32):
  139. return tl.he_normal(seed=self.seed, shape=shape, dtype=dtype)
  140. def get_config(self):
  141. return {"seed", self.seed}
  142. def deconv2d_bilinear_upsampling_initializer(shape):
  143. """Returns the initializer that can be passed to DeConv2dLayer for initializing the
  144. weights in correspondence to channel-wise bilinear up-sampling.
  145. Used in segmentation approaches such as [FCN](https://arxiv.org/abs/1605.06211)
  146. Parameters
  147. ----------
  148. shape : tuple of int
  149. The shape of the filters, [height, width, output_channels, in_channels].
  150. It must match the shape passed to DeConv2dLayer.
  151. Returns
  152. -------
  153. ``tf.constant_initializer``
  154. A constant initializer with weights set to correspond to per channel bilinear upsampling
  155. when passed as W_int in DeConv2dLayer
  156. """
  157. if shape[0] != shape[1]:
  158. raise Exception('deconv2d_bilinear_upsampling_initializer only supports symmetrical filter sizes')
  159. if shape[3] < shape[2]:
  160. raise Exception(
  161. 'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels '
  162. )
  163. filter_size = shape[0]
  164. num_out_channels = shape[2]
  165. num_in_channels = shape[3]
  166. # Create bilinear filter kernel as numpy array
  167. bilinear_kernel = np.zeros([filter_size, filter_size], dtype=np.float32)
  168. scale_factor = (filter_size + 1) // 2
  169. if filter_size % 2 == 1:
  170. center = scale_factor - 1
  171. else:
  172. center = scale_factor - 0.5
  173. for x in range(filter_size):
  174. for y in range(filter_size):
  175. bilinear_kernel[x, y] = (1 - abs(x - center) / scale_factor) * (1 - abs(y - center) / scale_factor)
  176. weights = np.zeros((filter_size, filter_size, num_out_channels, num_in_channels), dtype=np.float32)
  177. for i in range(num_out_channels):
  178. weights[:, :, i, i] = bilinear_kernel
  179. # assign numpy array to constant_initalizer and pass to get_variable
  180. return Constant(value=weights)

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