libs.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # coding: utf8
  2. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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 qsigmoid(data):
  99. return fluid.layers.relu6(data + 3) * 0.16667
  100. def relu(data):
  101. return fluid.layers.relu(data)
  102. def conv(*args, **kargs):
  103. kargs['param_attr'] = name_scope + 'weights'
  104. if 'bias_attr' in kargs and kargs['bias_attr']:
  105. kargs['bias_attr'] = fluid.ParamAttr(
  106. name=name_scope + 'biases',
  107. regularizer=None,
  108. initializer=fluid.initializer.ConstantInitializer(value=0.0))
  109. else:
  110. kargs['bias_attr'] = False
  111. return fluid.layers.conv2d(*args, **kargs)
  112. def deconv(*args, **kargs):
  113. kargs['param_attr'] = name_scope + 'weights'
  114. if 'bias_attr' in kargs and kargs['bias_attr']:
  115. kargs['bias_attr'] = name_scope + 'biases'
  116. else:
  117. kargs['bias_attr'] = False
  118. return fluid.layers.conv2d_transpose(*args, **kargs)
  119. def separate_conv(input,
  120. channel,
  121. stride,
  122. filter,
  123. dilation=1,
  124. act=None,
  125. eps=1e-5):
  126. param_attr = fluid.ParamAttr(
  127. name=name_scope + 'weights',
  128. regularizer=fluid.regularizer.L2DecayRegularizer(
  129. regularization_coeff=0.0),
  130. initializer=fluid.initializer.TruncatedNormal(
  131. loc=0.0, scale=0.33))
  132. with scope('depthwise'):
  133. input = conv(
  134. input,
  135. input.shape[1],
  136. filter,
  137. stride,
  138. groups=input.shape[1],
  139. padding=(filter // 2) * dilation,
  140. dilation=dilation,
  141. use_cudnn=False,
  142. param_attr=param_attr)
  143. input = bn(input, eps=eps)
  144. if act: input = act(input)
  145. param_attr = fluid.ParamAttr(
  146. name=name_scope + 'weights',
  147. regularizer=None,
  148. initializer=fluid.initializer.TruncatedNormal(
  149. loc=0.0, scale=0.06))
  150. with scope('pointwise'):
  151. input = conv(
  152. input, channel, 1, 1, groups=1, padding=0, param_attr=param_attr)
  153. input = bn(input, eps=eps)
  154. if act: input = act(input)
  155. return input
  156. def conv_bn_layer(input,
  157. filter_size,
  158. num_filters,
  159. stride,
  160. padding,
  161. channels=None,
  162. num_groups=1,
  163. if_act=True,
  164. name=None,
  165. use_cudnn=True):
  166. conv = fluid.layers.conv2d(
  167. input=input,
  168. num_filters=num_filters,
  169. filter_size=filter_size,
  170. stride=stride,
  171. padding=padding,
  172. groups=num_groups,
  173. act=None,
  174. use_cudnn=use_cudnn,
  175. param_attr=fluid.ParamAttr(name=name + '_weights'),
  176. bias_attr=False)
  177. bn_name = name + '_bn'
  178. bn = fluid.layers.batch_norm(
  179. input=conv,
  180. param_attr=fluid.ParamAttr(name=bn_name + "_scale"),
  181. bias_attr=fluid.ParamAttr(name=bn_name + "_offset"),
  182. moving_mean_name=bn_name + '_mean',
  183. moving_variance_name=bn_name + '_variance')
  184. if if_act:
  185. return fluid.layers.relu6(bn)
  186. else:
  187. return bn
  188. def sigmoid_to_softmax(input):
  189. """
  190. one channel to two channel
  191. """
  192. logit = fluid.layers.sigmoid(input)
  193. logit_back = 1 - logit
  194. logit = fluid.layers.concat([logit_back, logit], axis=1)
  195. return logit