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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import numpy as np
  2. import megengine as mge
  3. import megengine.functional as F
  4. from megengine import Parameter
  5. from .init import ones_, zeros_
  6. from .module import Module
  7. class GroupNorm(Module):
  8. """Simple implementation of GroupNorm. Only support 4d tensor now.
  9. Reference: https://arxiv.org/pdf/1803.08494.pdf.
  10. """
  11. def __init__(self, num_groups, num_channels, eps=1e-5, affine=True, **kwargs):
  12. super().__init__(**kwargs)
  13. assert num_channels % num_groups == 0
  14. self.num_groups = num_groups
  15. self.num_channels = num_channels
  16. self.eps = eps
  17. self.affine = affine
  18. if self.affine:
  19. self.weight = Parameter(np.ones(num_channels, dtype=np.float32))
  20. self.bias = Parameter(np.zeros(num_channels, dtype=np.float32))
  21. else:
  22. self.weight = None
  23. self.bias = None
  24. self.reset_parameters()
  25. def reset_parameters(self):
  26. if self.affine:
  27. ones_(self.weight)
  28. zeros_(self.bias)
  29. def forward(self, x):
  30. N, C, H, W = x.shape
  31. format = x.format
  32. assert C == self.num_channels
  33. x = x.reshape(N, self.num_groups, -1)
  34. mean = x.mean(axis=2, keepdims=True)
  35. var = (x * x).mean(axis=2, keepdims=True) - mean * mean
  36. x = (x - mean) / F.sqrt(var + self.eps)
  37. x = x.reshape(N, C, H, W)
  38. if self.affine:
  39. x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(1, -1, 1, 1)
  40. # FIXME(czh): remove this after making it a builtin op.
  41. if format == "nhwc":
  42. x = mge.amp.convert_tensor_format(x, inplace=False)
  43. return x
  44. def _module_info_string(self) -> str:
  45. s = (
  46. "groups={num_groups}, channels={num_channels}, "
  47. "eps={eps}, affine={affine}"
  48. )
  49. return s.format(**self.__dict__)
  50. class InstanceNorm(Module):
  51. """Simple implementation of InstanceNorm. Only support 4d tensor now.
  52. Reference: https://arxiv.org/abs/1607.08022.
  53. Note that InstanceNorm equals using GroupNome with num_groups=num_channels.
  54. """
  55. def __init__(self, num_channels, eps=1e-05, affine=True, **kwargs):
  56. super().__init__(**kwargs)
  57. self.num_channels = num_channels
  58. self.eps = eps
  59. self.affine = affine
  60. if self.affine:
  61. self.weight = Parameter(np.ones(num_channels, dtype="float32"))
  62. self.bias = Parameter(np.zeros(num_channels, dtype="float32"))
  63. else:
  64. self.weight = None
  65. self.bias = None
  66. self.reset_parameters()
  67. def reset_parameters(self):
  68. if self.affine:
  69. ones_(self.weight)
  70. zeros_(self.bias)
  71. def forward(self, x):
  72. N, C, H, W = x.shape
  73. format = x.format
  74. assert C == self.num_channels
  75. x = x.reshape(N, C, -1)
  76. mean = x.mean(axis=2, keepdims=True)
  77. var = (x ** 2).mean(axis=2, keepdims=True) - mean * mean
  78. x = (x - mean) / F.sqrt(var + self.eps)
  79. x = x.reshape(N, C, H, W)
  80. if self.affine:
  81. x = self.weight.reshape(1, -1, 1, 1) * x + self.bias.reshape(1, -1, 1, 1)
  82. # FIXME(czh): remove this after making it a builtin op.
  83. if format == "nhwc":
  84. x = mge.amp.convert_tensor_format(x, inplace=False)
  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. """Simple implementation of LayerNorm. Support tensor of any shape as input.
  91. Reference: https://arxiv.org/pdf/1803.08494.pdf.
  92. """
  93. def __init__(self, normalized_shape, eps=1e-05, affine=True, **kwargs):
  94. super().__init__(**kwargs)
  95. if isinstance(normalized_shape, int):
  96. normalized_shape = (normalized_shape,)
  97. self.normalized_shape = tuple(normalized_shape)
  98. self.eps = eps
  99. self.affine = affine
  100. if self.affine:
  101. self.weight = Parameter(np.ones(self.normalized_shape, dtype="float32"))
  102. self.bias = Parameter(np.zeros(self.normalized_shape, dtype="float32"))
  103. else:
  104. self.weight = None
  105. self.bias = None
  106. self.reset_parameters()
  107. def reset_parameters(self):
  108. if self.affine:
  109. ones_(self.weight)
  110. zeros_(self.bias)
  111. def forward(self, x):
  112. x = F.nn.layer_norm(
  113. x, self.normalized_shape, self.affine, self.weight, self.bias, self.eps
  114. )
  115. return x
  116. def _module_info_string(self) -> str:
  117. s = "normalized_shape={normalized_shape}, eps={eps}, affine={affine}"
  118. return s.format(**self.__dict__)