libs.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # coding: utf8
  2. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. import paddle
  19. import paddle.fluid as fluid
  20. import contextlib
  21. bn_regularizer = fluid.regularizer.L2DecayRegularizer(regularization_coeff=0.0)
  22. name_scope = ""
  23. @contextlib.contextmanager
  24. def scope(name):
  25. global name_scope
  26. bk = name_scope
  27. name_scope = name_scope + name + '/'
  28. yield
  29. name_scope = bk
  30. def max_pool(input, kernel, stride, padding):
  31. data = fluid.layers.pool2d(
  32. input,
  33. pool_size=kernel,
  34. pool_type='max',
  35. pool_stride=stride,
  36. pool_padding=padding)
  37. return data
  38. def avg_pool(input, kernel, stride, padding=0):
  39. data = fluid.layers.pool2d(
  40. input,
  41. pool_size=kernel,
  42. pool_type='avg',
  43. pool_stride=stride,
  44. pool_padding=padding)
  45. return data
  46. def group_norm(input, G, eps=1e-5, param_attr=None, bias_attr=None):
  47. N, C, H, W = input.shape
  48. if C % G != 0:
  49. for d in range(10):
  50. for t in [d, -d]:
  51. if G + t <= 0: continue
  52. if C % (G + t) == 0:
  53. G = G + t
  54. break
  55. if C % G == 0:
  56. break
  57. assert C % G == 0, "group can not divide channle"
  58. x = fluid.layers.group_norm(
  59. input,
  60. groups=G,
  61. param_attr=param_attr,
  62. bias_attr=bias_attr,
  63. name=name_scope + 'group_norm')
  64. return x
  65. def bn(*args,
  66. norm_type='bn',
  67. eps=1e-5,
  68. bn_momentum=0.99,
  69. group_norm=32,
  70. **kargs):
  71. if norm_type == 'bn':
  72. with scope('BatchNorm'):
  73. return fluid.layers.batch_norm(
  74. *args,
  75. epsilon=eps,
  76. momentum=bn_momentum,
  77. param_attr=fluid.ParamAttr(
  78. name=name_scope + 'gamma', regularizer=bn_regularizer),
  79. bias_attr=fluid.ParamAttr(
  80. name=name_scope + 'beta', regularizer=bn_regularizer),
  81. moving_mean_name=name_scope + 'moving_mean',
  82. moving_variance_name=name_scope + 'moving_variance',
  83. **kargs)
  84. elif norm_type == 'gn':
  85. with scope('GroupNorm'):
  86. return group_norm(
  87. args[0],
  88. group_norm,
  89. eps=eps,
  90. param_attr=fluid.ParamAttr(
  91. name=name_scope + 'gamma', regularizer=bn_regularizer),
  92. bias_attr=fluid.ParamAttr(
  93. name=name_scope + 'beta', regularizer=bn_regularizer))
  94. else:
  95. raise Exception("Unsupport norm type:" + norm_type)
  96. def bn_relu(data, norm_type='bn', eps=1e-5):
  97. return fluid.layers.relu(bn(data, norm_type=norm_type, eps=eps))
  98. def relu(data):
  99. return fluid.layers.relu(data)
  100. def conv(*args, **kargs):
  101. kargs['param_attr'] = name_scope + 'weights'
  102. if 'bias_attr' in kargs and kargs['bias_attr']:
  103. kargs['bias_attr'] = fluid.ParamAttr(
  104. name=name_scope + 'biases',
  105. regularizer=None,
  106. initializer=fluid.initializer.ConstantInitializer(value=0.0))
  107. else:
  108. kargs['bias_attr'] = False
  109. return fluid.layers.conv2d(*args, **kargs)
  110. def deconv(*args, **kargs):
  111. kargs['param_attr'] = name_scope + 'weights'
  112. if 'bias_attr' in kargs and kargs['bias_attr']:
  113. kargs['bias_attr'] = name_scope + 'biases'
  114. else:
  115. kargs['bias_attr'] = False
  116. return fluid.layers.conv2d_transpose(*args, **kargs)
  117. def separate_conv(input,
  118. channel,
  119. stride,
  120. filter,
  121. dilation=1,
  122. act=None,
  123. eps=1e-5):
  124. param_attr = fluid.ParamAttr(
  125. name=name_scope + 'weights',
  126. regularizer=fluid.regularizer.L2DecayRegularizer(
  127. regularization_coeff=0.0),
  128. initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.33))
  129. with scope('depthwise'):
  130. input = conv(
  131. input,
  132. input.shape[1],
  133. filter,
  134. stride,
  135. groups=input.shape[1],
  136. padding=(filter // 2) * dilation,
  137. dilation=dilation,
  138. use_cudnn=False,
  139. param_attr=param_attr)
  140. input = bn(input, eps=eps)
  141. if act: input = act(input)
  142. param_attr = fluid.ParamAttr(
  143. name=name_scope + 'weights',
  144. regularizer=None,
  145. initializer=fluid.initializer.TruncatedNormal(loc=0.0, scale=0.06))
  146. with scope('pointwise'):
  147. input = conv(
  148. input, channel, 1, 1, groups=1, padding=0, param_attr=param_attr)
  149. input = bn(input, eps=eps)
  150. if act: input = act(input)
  151. return input
  152. def conv_bn_layer(input,
  153. filter_size,
  154. num_filters,
  155. stride,
  156. padding,
  157. channels=None,
  158. num_groups=1,
  159. if_act=True,
  160. name=None,
  161. use_cudnn=True):
  162. conv = fluid.layers.conv2d(
  163. input=input,
  164. num_filters=num_filters,
  165. filter_size=filter_size,
  166. stride=stride,
  167. padding=padding,
  168. groups=num_groups,
  169. act=None,
  170. use_cudnn=use_cudnn,
  171. param_attr=fluid.ParamAttr(name=name + '_weights'),
  172. bias_attr=False)
  173. bn_name = name + '_bn'
  174. bn = fluid.layers.batch_norm(
  175. input=conv,
  176. param_attr=fluid.ParamAttr(name=bn_name + "_scale"),
  177. bias_attr=fluid.ParamAttr(name=bn_name + "_offset"),
  178. moving_mean_name=bn_name + '_mean',
  179. moving_variance_name=bn_name + '_variance')
  180. if if_act:
  181. return fluid.layers.relu6(bn)
  182. else:
  183. return bn
  184. def sigmoid_to_softmax(input):
  185. """
  186. one channel to two channel
  187. """
  188. logit = fluid.layers.sigmoid(input)
  189. logit_back = 1 - logit
  190. logit = fluid.layers.concat([logit_back, logit], axis=1)
  191. return logit