vgg_variant.py 822 B

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