layers_lib.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 paddle
  15. import paddle.nn as nn
  16. import paddle.nn.functional as F
  17. from ...layers.blocks import *
  18. class Add(nn.Layer):
  19. def __init__(self):
  20. super().__init__()
  21. def forward(self, x, y, name=None):
  22. return paddle.add(x, y, name)
  23. class Activation(nn.Layer):
  24. """
  25. The wrapper of activations.
  26. Args:
  27. act (str, optional): The activation name in lowercase. It must be one of ['elu', 'gelu',
  28. 'hardshrink', 'tanh', 'hardtanh', 'prelu', 'relu', 'relu6', 'selu', 'leakyrelu', 'sigmoid',
  29. 'softmax', 'softplus', 'softshrink', 'softsign', 'tanhshrink', 'logsigmoid', 'logsoftmax',
  30. 'hsigmoid']. Default: None, means identical transformation.
  31. Returns:
  32. A callable object of Activation.
  33. Raises:
  34. KeyError: When parameter `act` is not in the optional range.
  35. Examples:
  36. from paddleseg.models.common.activation import Activation
  37. relu = Activation("relu")
  38. print(relu)
  39. # <class 'paddle.nn.layer.activation.ReLU'>
  40. sigmoid = Activation("sigmoid")
  41. print(sigmoid)
  42. # <class 'paddle.nn.layer.activation.Sigmoid'>
  43. not_exit_one = Activation("not_exit_one")
  44. # KeyError: "not_exit_one does not exist in the current dict_keys(['elu', 'gelu', 'hardshrink',
  45. # 'tanh', 'hardtanh', 'prelu', 'relu', 'relu6', 'selu', 'leakyrelu', 'sigmoid', 'softmax',
  46. # 'softplus', 'softshrink', 'softsign', 'tanhshrink', 'logsigmoid', 'logsoftmax', 'hsigmoid'])"
  47. """
  48. def __init__(self, act=None):
  49. super(Activation, self).__init__()
  50. self._act = act
  51. upper_act_names = nn.layer.activation.__dict__.keys()
  52. lower_act_names = [act.lower() for act in upper_act_names]
  53. act_dict = dict(zip(lower_act_names, upper_act_names))
  54. if act is not None:
  55. if act in act_dict.keys():
  56. act_name = act_dict[act]
  57. self.act_func = eval("nn.layer.activation.{}()".format(
  58. act_name))
  59. else:
  60. raise KeyError("{} does not exist in the current {}".format(
  61. act, act_dict.keys()))
  62. def forward(self, x):
  63. if self._act is not None:
  64. return self.act_func(x)
  65. else:
  66. return x