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.

combined.py 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Copyright 2020-2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Combined cells."""
  16. from mindspore import nn
  17. from mindspore.ops.primitive import Primitive
  18. from mindspore._checkparam import Validator
  19. from .normalization import BatchNorm2d, BatchNorm1d
  20. from .activation import get_activation, LeakyReLU
  21. from ..cell import Cell
  22. __all__ = [
  23. 'Conv2dBnAct',
  24. 'DenseBnAct'
  25. ]
  26. class Conv2dBnAct(Cell):
  27. r"""
  28. A combination of convolution, Batchnorm, and activation layer.
  29. This part is a more detailed overview of Conv2d operation.
  30. Args:
  31. in_channels (int): The number of input channel :math:`C_{in}`.
  32. out_channels (int): The number of output channel :math:`C_{out}`.
  33. kernel_size (Union[int, tuple]): The data type is int or a tuple of 2 integers. Specifies the height
  34. and width of the 2D convolution window. Single int means the value is for both height and width of
  35. the kernel. A tuple of 2 ints means the first value is for the height and the other is for the
  36. width of the kernel.
  37. stride (int): Specifies stride for all spatial dimensions with the same value. The value of stride must be
  38. greater than or equal to 1 and lower than any one of the height and width of the input. Default: 1.
  39. pad_mode (str): Specifies padding mode. The optional values are "same", "valid", "pad". Default: "same".
  40. padding (int): Implicit paddings on both sides of the input. Default: 0.
  41. dilation (int): Specifies the dilation rate to use for dilated convolution. If set to be :math:`k > 1`,
  42. there will be :math:`k - 1` pixels skipped for each sampling location. Its value must be greater than
  43. or equal to 1 and lower than any one of the height and width of the input. Default: 1.
  44. group (int): Splits filter into groups, `in_ channels` and `out_channels` must be
  45. divisible by the number of groups. Default: 1.
  46. has_bias (bool): Specifies whether the layer uses a bias vector. Default: False.
  47. weight_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the convolution kernel.
  48. It can be a Tensor, a string, an Initializer or a number. When a string is specified,
  49. values from 'TruncatedNormal', 'Normal', 'Uniform', 'HeUniform' and 'XavierUniform' distributions as well
  50. as constant 'One' and 'Zero' distributions are possible. Alias 'xavier_uniform', 'he_uniform', 'ones'
  51. and 'zeros' are acceptable. Uppercase and lowercase are both acceptable. Refer to the values of
  52. Initializer for more details. Default: 'normal'.
  53. bias_init (Union[Tensor, str, Initializer, numbers.Number]): Initializer for the bias vector. Possible
  54. Initializer and string are the same as 'weight_init'. Refer to the values of
  55. Initializer for more details. Default: 'zeros'.
  56. has_bn (bool): Specifies to used batchnorm or not. Default: False.
  57. momentum (float): Momentum for moving average for batchnorm, must be [0, 1]. Default:0.9
  58. eps (float): Term added to the denominator to improve numerical stability for batchnorm, should be greater
  59. than 0. Default: 1e-5.
  60. activation (Union[str, Cell, Primitive]): Specifies activation type. The optional values are as following:
  61. 'softmax', 'logsoftmax', 'relu', 'relu6', 'tanh', 'gelu', 'sigmoid',
  62. 'prelu', 'leakyrelu', 'hswish', 'hsigmoid'. Default: None.
  63. alpha (float): Slope of the activation function at x < 0 for LeakyReLU. Default: 0.2.
  64. after_fake(bool): Determine whether there must be a fake quantization operation after Cond2dBnAct.
  65. Inputs:
  66. - **input** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
  67. Outputs:
  68. Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})`.
  69. Raises:
  70. TypeError: If `in_channels`, `out_channels`, `stride`, `padding` or `dilation` is not an int.
  71. TypeError: If `has_bias` is not a bool.
  72. ValueError: If `in_channels` or `out_channels` `stride`, `padding` or `dilation` is less than 1.
  73. ValueError: If `pad_mode` is not one of 'same', 'valid', 'pad'.
  74. Supported Platforms:
  75. ``Ascend`` ``GPU``
  76. Examples:
  77. >>> net = nn.Conv2dBnAct(120, 240, 4, has_bn=True, activation='relu')
  78. >>> input = Tensor(np.ones([1, 120, 1024, 640]), mindspore.float32)
  79. >>> result = net(input)
  80. >>> output = result.shape
  81. >>> print(output)
  82. (1, 240, 1024, 640)
  83. """
  84. def __init__(self,
  85. in_channels,
  86. out_channels,
  87. kernel_size,
  88. stride=1,
  89. pad_mode='same',
  90. padding=0,
  91. dilation=1,
  92. group=1,
  93. has_bias=False,
  94. weight_init='normal',
  95. bias_init='zeros',
  96. has_bn=False,
  97. momentum=0.997,
  98. eps=1e-5,
  99. activation=None,
  100. alpha=0.2,
  101. after_fake=True):
  102. super(Conv2dBnAct, self).__init__()
  103. self.conv = nn.Conv2d(in_channels,
  104. out_channels,
  105. kernel_size=kernel_size,
  106. stride=stride,
  107. pad_mode=pad_mode,
  108. padding=padding,
  109. dilation=dilation,
  110. group=group,
  111. has_bias=has_bias,
  112. weight_init=weight_init,
  113. bias_init=bias_init)
  114. self.has_bn = Validator.check_bool(has_bn, "has_bn")
  115. self.has_act = activation is not None
  116. self.after_fake = Validator.check_bool(after_fake, "after_fake")
  117. if has_bn:
  118. self.batchnorm = BatchNorm2d(out_channels, eps, momentum)
  119. if activation == "leakyrelu":
  120. self.activation = LeakyReLU(alpha)
  121. else:
  122. self.activation = get_activation(activation) if isinstance(activation, str) else activation
  123. if activation is not None and not isinstance(self.activation, (Cell, Primitive)):
  124. raise TypeError("The activation must be str or Cell or Primitive,"" but got {}.".format(activation))
  125. def construct(self, x):
  126. x = self.conv(x)
  127. if self.has_bn:
  128. x = self.batchnorm(x)
  129. if self.has_act:
  130. x = self.activation(x)
  131. return x
  132. class DenseBnAct(Cell):
  133. r"""
  134. A combination of Dense, Batchnorm, and the activation layer.
  135. This part is a more detailed overview of Dense op.
  136. Args:
  137. in_channels (int): The number of channels in the input space.
  138. out_channels (int): The number of channels in the output space.
  139. weight_init (Union[Tensor, str, Initializer, numbers.Number]): The trainable weight_init parameter. The dtype
  140. is same as input. The values of str refer to the function `initializer`. Default: 'normal'.
  141. bias_init (Union[Tensor, str, Initializer, numbers.Number]): The trainable bias_init parameter. The dtype is
  142. same as input. The values of str refer to the function `initializer`. Default: 'zeros'.
  143. has_bias (bool): Specifies whether the layer uses a bias vector. Default: True.
  144. has_bn (bool): Specifies to use batchnorm or not. Default: False.
  145. momentum (float): Momentum for moving average for batchnorm, must be [0, 1]. Default:0.9
  146. eps (float): Term added to the denominator to improve numerical stability for batchnorm, should be greater
  147. than 0. Default: 1e-5.
  148. activation (Union[str, Cell, Primitive]): Specifies activation type. The optional values are as following:
  149. 'softmax', 'logsoftmax', 'relu', 'relu6', 'tanh', 'gelu', 'sigmoid',
  150. 'prelu', 'leakyrelu', 'hswish', 'hsigmoid'. Default: None.
  151. alpha (float): Slope of the activation function at x < 0 for LeakyReLU. Default: 0.2.
  152. after_fake(bool): Determine whether there must be a fake quantization operation after DenseBnAct.
  153. Inputs:
  154. - **input** (Tensor) - Tensor of shape :math:`(N, in\_channels)`.
  155. Outputs:
  156. Tensor of shape :math:`(N, out\_channels)`.
  157. Raises:
  158. TypeError: If `in_channels` or `out_channels` is not an int.
  159. TypeError: If `has_bias`, `has_bn` or `after_fake` is not a bool.
  160. TypeError: If `momentum` or `eps` is not a float.
  161. ValueError: If `momentum` is not in range [0, 1.0].
  162. Supported Platforms:
  163. ``Ascend`` ``GPU``
  164. Examples:
  165. >>> net = nn.DenseBnAct(3, 4)
  166. >>> input = Tensor(np.random.randint(0, 255, [2, 3]), mindspore.float32)
  167. >>> result = net(input)
  168. >>> output = result.shape
  169. >>> print(output)
  170. (2, 4)
  171. """
  172. def __init__(self,
  173. in_channels,
  174. out_channels,
  175. weight_init='normal',
  176. bias_init='zeros',
  177. has_bias=True,
  178. has_bn=False,
  179. momentum=0.9,
  180. eps=1e-5,
  181. activation=None,
  182. alpha=0.2,
  183. after_fake=True):
  184. super(DenseBnAct, self).__init__()
  185. self.dense = nn.Dense(
  186. in_channels,
  187. out_channels,
  188. weight_init,
  189. bias_init,
  190. has_bias)
  191. self.has_bn = Validator.check_bool(has_bn, "has_bn")
  192. self.has_act = activation is not None
  193. self.after_fake = Validator.check_bool(after_fake, "after_fake")
  194. if has_bn:
  195. self.batchnorm = BatchNorm1d(out_channels, eps, momentum)
  196. if activation == "leakyrelu":
  197. self.activation = LeakyReLU(alpha)
  198. else:
  199. self.activation = get_activation(activation) if isinstance(activation, str) else activation
  200. if activation is not None and not isinstance(self.activation, (Cell, Primitive)):
  201. raise TypeError("The activation must be str or Cell or Primitive,"" but got {}.".format(activation))
  202. def construct(self, x):
  203. x = self.dense(x)
  204. if self.has_bn:
  205. x = self.batchnorm(x)
  206. if self.has_act:
  207. x = self.activation(x)
  208. return x