bilinear.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  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. import numpy as np
  15. from paddle import _C_ops, pir
  16. from ...base import core, framework, unique_name
  17. from ...base.framework import (
  18. _current_expected_place,
  19. in_dygraph_mode,
  20. in_pir_mode,
  21. )
  22. from .initializer import Initializer
  23. __all__ = []
  24. class Bilinear(Initializer):
  25. """
  26. This initializer can be used in transposed convolution operator to
  27. act as upsampling. Users can upsample a feature map with shape of
  28. (B, C, H, W) by any integer factor.
  29. Returns:
  30. Bilinear initializer instance objects.
  31. Examples:
  32. .. code-block:: python
  33. >>> import math
  34. >>> import paddle
  35. >>> import paddle.nn as nn
  36. >>> from paddle.regularizer import L2Decay
  37. >>> factor = 2
  38. >>> C = 2
  39. >>> B = 8
  40. >>> H = W = 32
  41. >>> w_attr = paddle.ParamAttr(learning_rate=0.,
  42. ... regularizer=L2Decay(0.),
  43. ... initializer=nn.initializer.Bilinear())
  44. >>> data = paddle.rand([B, 3, H, W], dtype='float32')
  45. >>> conv_up = nn.Conv2DTranspose(3,
  46. ... out_channels=C,
  47. ... kernel_size=2 * factor - factor % 2,
  48. ... padding=int(
  49. ... math.ceil((factor - 1) / 2.)),
  50. ... stride=factor,
  51. ... weight_attr=w_attr,
  52. ... bias_attr=False)
  53. >>> x = conv_up(data)
  54. Where, `out_channels=C` and `groups=C` means this is channel-wise transposed
  55. convolution. The filter shape will be (C, 1, K, K) where K is `kernel_size`,
  56. This initializer will set a (K, K) interpolation kernel for every channel
  57. of the filter identically. The resulting shape of the output feature map
  58. will be (B, C, factor * H, factor * W). Note that the learning rate and the
  59. weight decay are set to 0 in order to keep coefficient values of bilinear
  60. interpolation unchanged during training.
  61. """
  62. def __init__(self):
  63. """Constructor for BilinearInitializer."""
  64. super().__init__()
  65. def forward(self, var, block=None):
  66. """Initialize the input tensor with Bilinear initialization.
  67. Args:
  68. var(Tensor): Tensor that needs to be initialized.
  69. block(Block, optional): The block in which initialization ops
  70. should be added. Used in static graph only, default None.
  71. Returns:
  72. The initialization op
  73. """
  74. assert not (
  75. isinstance(var, framework.EagerParamBase) and var.is_dist()
  76. ), "Currently, Bilinear initializer not support lazy init for dist param."
  77. block = self._check_block(block)
  78. if not isinstance(var, (framework.Variable, pir.core.ParameterMeta)):
  79. raise ValueError(
  80. "var must be framework.Variable or pir.core.ParameterMeta."
  81. )
  82. if not isinstance(block, (framework.Block, pir.Block)):
  83. raise ValueError("block must be framework.Block or pir.Block.")
  84. shape = var.shape
  85. if len(shape) != 4:
  86. raise ValueError("the length of shape must be 4.")
  87. if shape[2] != shape[3]:
  88. raise ValueError("shape[2] must be equal to shape[3].")
  89. weight = np.zeros(np.prod(var.shape), dtype='float32')
  90. size = shape[3]
  91. # factor
  92. f = np.ceil(size / 2.0)
  93. # center
  94. c = (2 * f - 1 - f % 2) / (2.0 * f)
  95. for i in range(np.prod(shape)):
  96. x = i % size
  97. y = (i / size) % size
  98. weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
  99. weight = np.reshape(weight, shape)
  100. # to be compatible of fp16 initializers
  101. if var.dtype in [
  102. core.VarDesc.VarType.FP16,
  103. core.VarDesc.VarType.BF16,
  104. core.VarDesc.VarType.FP64,
  105. ]:
  106. out_dtype = core.VarDesc.VarType.FP32
  107. out_var = block.create_var(
  108. name=unique_name.generate(
  109. ".".join(['bilinear_init', var.name, 'tmp'])
  110. ),
  111. shape=var.shape,
  112. dtype=out_dtype,
  113. type=core.VarDesc.VarType.LOD_TENSOR,
  114. persistable=False,
  115. )
  116. elif var.dtype in [
  117. core.DataType.FLOAT16,
  118. core.DataType.BFLOAT16,
  119. core.DataType.FLOAT64,
  120. ]:
  121. out_dtype = core.DataType.FLOAT32
  122. out_var = var
  123. else:
  124. out_dtype = var.dtype
  125. out_var = var
  126. if out_dtype in (core.VarDesc.VarType.FP32, core.DataType.FLOAT32):
  127. value_name = "values"
  128. values = [float(v) for v in weight.flat]
  129. else:
  130. raise TypeError("Unsupported dtype %s", var.dtype)
  131. if np.prod(shape) > 1024 * 1024:
  132. raise ValueError("The size of input is too big. ")
  133. if in_dygraph_mode():
  134. _C_ops.assign_value_(
  135. out_var,
  136. list(shape),
  137. out_dtype,
  138. values,
  139. _current_expected_place(),
  140. )
  141. if var.dtype in [
  142. core.VarDesc.VarType.FP16,
  143. core.VarDesc.VarType.BF16,
  144. core.VarDesc.VarType.FP64,
  145. ]:
  146. var_tmp = _C_ops.cast(out_var, var.dtype)
  147. var_tmp._share_underline_tensor_to(var)
  148. else:
  149. out_var._share_underline_tensor_to(var)
  150. return None
  151. elif in_pir_mode():
  152. out_var = _C_ops.assign_value(
  153. list(shape),
  154. out_dtype,
  155. values,
  156. _current_expected_place(),
  157. )
  158. if var.dtype in [
  159. core.DataType.FLOAT16,
  160. core.DataType.BFLOAT16,
  161. core.DataType.FLOAT64,
  162. ]:
  163. out_var = _C_ops.cast(out_var, var.dtype)
  164. return out_var
  165. else:
  166. op = block.append_op(
  167. type='assign_value',
  168. outputs={'Out': [out_var]},
  169. attrs={
  170. 'dtype': out_dtype,
  171. 'shape': list(shape),
  172. value_name: values,
  173. },
  174. )
  175. if var.dtype in [
  176. core.VarDesc.VarType.FP16,
  177. core.VarDesc.VarType.BF16,
  178. core.VarDesc.VarType.FP64,
  179. ]:
  180. block.append_op(
  181. type="cast",
  182. inputs={"X": out_var},
  183. outputs={"Out": var},
  184. attrs={"in_dtype": out_var.dtype, "out_dtype": var.dtype},
  185. )
  186. var.op = op
  187. return op