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.

normalization.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. import numpy as np
  9. import megengine.functional as F
  10. from megengine import Parameter
  11. from .init import ones_, zeros_
  12. from .module import Module
  13. class GroupNorm(Module):
  14. """
  15. Simple implementation of GroupNorm. Only support 4d tensor now.
  16. Reference: https://arxiv.org/pdf/1803.08494.pdf.
  17. """
  18. def __init__(self, num_groups, num_channels, eps=1e-5, affine=True, **kwargs):
  19. super().__init__(**kwargs)
  20. assert num_channels % num_groups == 0
  21. self.num_groups = num_groups
  22. self.num_channels = num_channels
  23. self.eps = eps
  24. self.affine = affine
  25. if self.affine:
  26. self.weight = Parameter(np.ones(num_channels, dtype=np.float32))
  27. self.bias = Parameter(np.zeros(num_channels, dtype=np.float32))
  28. else:
  29. self.weight = None
  30. self.bias = None
  31. self.reset_parameters()
  32. def reset_parameters(self):
  33. if self.affine:
  34. ones_(self.weight)
  35. zeros_(self.bias)
  36. def forward(self, x):
  37. N, C, H, W = x.shape
  38. assert C == self.num_channels
  39. x = x.reshape(N, self.num_groups, -1)
  40. mean = x.mean(axis=2, keepdims=True)
  41. var = (x * x).mean(axis=2, keepdims=True) - mean * mean
  42. x = (x - mean) / F.sqrt(var + self.eps)
  43. x = x.reshape(N, C, H, W)
  44. if self.affine:
  45. x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(1, -1, 1, 1)
  46. return x
  47. def _module_info_string(self) -> str:
  48. s = (
  49. "groups={num_groups}, channels={num_channels}, "
  50. "eps={eps}, affine={affine}"
  51. )
  52. return s.format(**self.__dict__)
  53. class InstanceNorm(Module):
  54. """
  55. Simple implementation of InstanceNorm. Only support 4d tensor now.
  56. Reference: https://arxiv.org/abs/1607.08022.
  57. Note that InstanceNorm equals using GroupNome with num_groups=num_channels.
  58. """
  59. def __init__(self, num_channels, eps=1e-05, affine=True, **kwargs):
  60. super().__init__(**kwargs)
  61. self.num_channels = num_channels
  62. self.eps = eps
  63. self.affine = affine
  64. if self.affine:
  65. self.weight = Parameter(np.ones(num_channels, dtype="float32"))
  66. self.bias = Parameter(np.zeros(num_channels, dtype="float32"))
  67. else:
  68. self.weight = None
  69. self.bias = None
  70. self.reset_parameters()
  71. def reset_parameters(self):
  72. if self.affine:
  73. ones_(self.weight)
  74. zeros_(self.bias)
  75. def forward(self, x):
  76. N, C, H, W = x.shape
  77. assert C == self.num_channels
  78. x = x.reshape(N, C, -1)
  79. mean = x.mean(axis=2, keepdims=True)
  80. var = (x ** 2).mean(axis=2, keepdims=True) - mean * mean
  81. x = (x - mean) / F.sqrt(var + self.eps)
  82. x = x.reshape(N, C, H, W)
  83. if self.affine:
  84. x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(1, -1, 1, 1)
  85. return x
  86. def _module_info_string(self) -> str:
  87. s = "channels={num_channels}, eps={eps}, affine={affine}"
  88. return s.format(**self.__dict__)
  89. class LayerNorm(Module):
  90. """
  91. Simple implementation of LayerNorm. Support tensor of any shape as input.
  92. Reference: https://arxiv.org/pdf/1803.08494.pdf.
  93. """
  94. def __init__(self, normalized_shape, eps=1e-05, affine=True, **kwargs):
  95. super().__init__(**kwargs)
  96. if isinstance(normalized_shape, int):
  97. normalized_shape = (normalized_shape,)
  98. self.normalized_shape = tuple(normalized_shape)
  99. self.eps = eps
  100. self.affine = affine
  101. if self.affine:
  102. self.weight = Parameter(np.ones(self.normalized_shape, dtype="float32"))
  103. self.bias = Parameter(np.zeros(self.normalized_shape, dtype="float32"))
  104. else:
  105. self.weight = None
  106. self.bias = None
  107. self.reset_parameters()
  108. def reset_parameters(self):
  109. if self.affine:
  110. ones_(self.weight)
  111. zeros_(self.bias)
  112. def forward(self, x):
  113. x_shape = x.shape
  114. dim_delta = len(x_shape) - len(self.normalized_shape)
  115. non_flatten_shape = x_shape[:dim_delta]
  116. x = x.reshape(*non_flatten_shape, -1)
  117. mean = x.mean(axis=-1, keepdims=True)
  118. var = (x ** 2).mean(axis=-1, keepdims=True) - mean * mean
  119. x = (x - mean) / F.sqrt(var + self.eps)
  120. x = x.reshape(x_shape)
  121. if self.affine:
  122. x = self.weight * x + self.bias
  123. return x
  124. def _module_info_string(self) -> str:
  125. s = "normalized_shape={normalized_shape}, eps={eps}, affine={affine}"
  126. return s.format(**self.__dict__)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台