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.

mindspore_initializers.py 7.7 kB

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

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