pp_lcnet_variant.py 860 B

1234567891011121314151617181920212223242526272829
  1. import paddle
  2. from paddle.nn import Sigmoid
  3. from paddle.nn import Tanh
  4. from paddlex.ppcls.arch.backbone.legendary_models.pp_lcnet import PPLCNet_x2_5
  5. __all__ = ["PPLCNet_x2_5_Tanh"]
  6. class TanhSuffix(paddle.nn.Layer):
  7. def __init__(self, origin_layer):
  8. super(TanhSuffix, self).__init__()
  9. self.origin_layer = origin_layer
  10. self.tanh = Tanh()
  11. def forward(self, input, res_dict=None, **kwargs):
  12. x = self.origin_layer(input)
  13. x = self.tanh(x)
  14. return x
  15. def PPLCNet_x2_5_Tanh(pretrained=False, use_ssld=False, **kwargs):
  16. def replace_function(origin_layer):
  17. new_layer = TanhSuffix(origin_layer)
  18. return new_layer
  19. match_re = "linear_0"
  20. model = PPLCNet_x2_5(pretrained=pretrained, use_ssld=use_ssld, **kwargs)
  21. model.replace_sub(match_re, replace_function, True)
  22. return model