custom_trainer.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 paddlers
  16. from paddlers.tasks.change_detector import BaseChangeDetector
  17. from attach_tools import Attach
  18. attach = Attach.to(paddlers.tasks.change_detector)
  19. def make_trainer(net_type, *args, **kwargs):
  20. def _init_func(self,
  21. num_classes=2,
  22. use_mixed_loss=False,
  23. losses=None,
  24. **params):
  25. super().__init__(
  26. model_name=net_type.__name__,
  27. num_classes=num_classes,
  28. use_mixed_loss=use_mixed_loss,
  29. losses=losses,
  30. **params)
  31. if not issubclass(net_type, paddle.nn.Layer):
  32. raise TypeError("net must be a subclass of paddle.nn.Layer")
  33. trainer_name = net_type.__name__
  34. trainer_type = type(trainer_name, (BaseChangeDetector, ),
  35. {'__init__': _init_func})
  36. return trainer_type(*args, **kwargs)
  37. @attach
  38. class CustomTrainer(BaseChangeDetector):
  39. def __init__(self,
  40. num_classes=2,
  41. use_mixed_loss=False,
  42. losses=None,
  43. in_channels=3,
  44. att_types='ct',
  45. use_dropout=False,
  46. **params):
  47. params.update({
  48. 'in_channels': in_channels,
  49. 'att_types': att_types,
  50. 'use_dropout': use_dropout
  51. })
  52. super().__init__(
  53. model_name='CustomModel',
  54. num_classes=num_classes,
  55. use_mixed_loss=use_mixed_loss,
  56. losses=losses,
  57. **params)