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 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2020 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. import megengine.module as M
  11. from megengine import Parameter
  12. class GroupNorm(M.Module):
  13. """
  14. Simple implementation of GroupNorm.
  15. Reference: https://arxiv.org/pdf/1803.08494.pdf.
  16. """
  17. def __init__(self, num_groups, num_channels, eps=1e-5, affine=True):
  18. super().__init__()
  19. assert num_channels % num_groups == 0
  20. self.num_groups = num_groups
  21. self.num_channels = num_channels
  22. self.eps = eps
  23. self.affine = affine
  24. if self.affine:
  25. self.weight = Parameter(np.ones(num_channels, dtype=np.float32))
  26. self.bias = Parameter(np.zeros(num_channels, dtype=np.float32))
  27. else:
  28. self.weight = None
  29. self.bias = None
  30. self.reset_parameters()
  31. def reset_parameters(self):
  32. if self.affine:
  33. M.init.ones_(self.weight)
  34. M.init.zeros_(self.bias)
  35. def forward(self, x):
  36. N, C, H, W = x.shape
  37. assert C == self.num_channels
  38. x = x.reshape(N, self.num_groups, -1)
  39. mean = x.mean(axis=2, keepdims=True)
  40. var = (x * x).mean(axis=2, keepdims=True) - mean * mean
  41. x = (x - mean) / F.sqrt(var + self.eps)
  42. x = x.reshape(N, C, H, W)
  43. if self.affine:
  44. x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(1, -1, 1, 1)
  45. return x
  46. def _module_info_string(self) -> str:
  47. s = (
  48. "groups={num_groups}, channels={num_channels}, "
  49. "eps={eps}, affine={affine}"
  50. )
  51. return s.format(**self.__dict__)
  52. class InstanceNorm(M.Module):
  53. """
  54. simple implementation of InstanceNorm.
  55. Reference: https://arxiv.org/abs/1607.08022.
  56. Note that InstanceNorm equals using GroupNome with num_groups=num_channels.
  57. """
  58. def __init__(self, num_channels, eps=1e-05, affine=True):
  59. super().__init__()
  60. self.num_channels = num_channels
  61. self.eps = eps
  62. self.affine = affine
  63. if self.affine:
  64. self.weight = Parameter(np.ones(num_channels, dtype="float32"))
  65. self.bias = Parameter(np.zeros(num_channels, dtype="float32"))
  66. else:
  67. self.weight = None
  68. self.bias = None
  69. self.reset_parameters()
  70. def reset_parameters(self):
  71. if self.affine:
  72. M.init.ones_(self.weight)
  73. M.init.zeros_(self.bias)
  74. def forward(self, x):
  75. N, C, H, W = x.shape
  76. assert C == self.num_channels
  77. x = x.reshape(N, C, -1)
  78. mean = x.mean(axis=2, keepdims=True)
  79. var = (x ** 2).mean(axis=2, keepdims=True) - mean * mean
  80. x = (x - mean) / F.sqrt(var + self.eps)
  81. x = x.reshape(N, C, H, W)
  82. if self.affine:
  83. x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(1, -1, 1, 1)
  84. return x
  85. def _module_info_string(self) -> str:
  86. s = "channels={num_channels}, eps={eps}, affine={affine}"
  87. return s.format(**self.__dict__)
  88. class LayerNorm(M.Module):
  89. """
  90. simple implementation of LayerNorm.
  91. Reference: https://arxiv.org/pdf/1803.08494.pdf.
  92. Note that LayerNorm equals using GroupNorm with num_groups=1.
  93. """
  94. def __init__(self, num_channels, eps=1e-05, affine=True):
  95. super().__init__()
  96. self.num_channels = num_channels
  97. self.eps = eps
  98. self.affine = affine
  99. if self.affine:
  100. self.weight = Parameter(np.ones(num_channels, dtype="float32"))
  101. self.bias = Parameter(np.zeros(num_channels, dtype="float32"))
  102. else:
  103. self.weight = None
  104. self.bias = None
  105. self.reset_parameters()
  106. def reset_parameters(self):
  107. if self.affine:
  108. M.init.ones_(self.weight)
  109. M.init.zeros_(self.bias)
  110. def forward(self, x):
  111. N, C, H, W = x.shape
  112. assert C == self.num_channels
  113. x = x.reshape(x.shape[0], -1)
  114. # NOTE mean will keepdims in next two lines.
  115. mean = x.mean(axis=1, keepdims=1)
  116. var = (x ** 2).mean(axis=1, keepdims=1) - mean * mean
  117. x = (x - mean) / F.sqrt(var + self.eps)
  118. x = x.reshape(N, C, H, W)
  119. if self.affine:
  120. x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(1, -1, 1, 1)
  121. return x
  122. def _module_info_string(self) -> str:
  123. s = "channels={num_channels}, eps={eps}, affine={affine}"
  124. return s.format(**self.__dict__)

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