tood.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) 2021 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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. from ppdet.core.workspace import register, create
  18. from .meta_arch import BaseArch
  19. __all__ = ['TOOD']
  20. @register
  21. class TOOD(BaseArch):
  22. """
  23. TOOD: Task-aligned One-stage Object Detection, see https://arxiv.org/abs/2108.07755
  24. Args:
  25. backbone (nn.Layer): backbone instance
  26. neck (nn.Layer): 'FPN' instance
  27. head (nn.Layer): 'TOODHead' instance
  28. """
  29. __category__ = 'architecture'
  30. def __init__(self, backbone, neck, head):
  31. super(TOOD, self).__init__()
  32. self.backbone = backbone
  33. self.neck = neck
  34. self.head = head
  35. @classmethod
  36. def from_config(cls, cfg, *args, **kwargs):
  37. backbone = create(cfg['backbone'])
  38. kwargs = {'input_shape': backbone.out_shape}
  39. neck = create(cfg['neck'], **kwargs)
  40. kwargs = {'input_shape': neck.out_shape}
  41. head = create(cfg['head'], **kwargs)
  42. return {
  43. 'backbone': backbone,
  44. 'neck': neck,
  45. "head": head,
  46. }
  47. def _forward(self):
  48. body_feats = self.backbone(self.inputs)
  49. fpn_feats = self.neck(body_feats)
  50. head_outs = self.head(fpn_feats)
  51. if not self.training:
  52. bboxes, bbox_num = self.head.post_process(
  53. head_outs, self.inputs['im_shape'], self.inputs['scale_factor'])
  54. return bboxes, bbox_num
  55. else:
  56. loss = self.head.get_loss(head_outs, self.inputs)
  57. return loss
  58. def get_loss(self):
  59. return self._forward()
  60. def get_pred(self):
  61. bbox_pred, bbox_num = self._forward()
  62. output = {'bbox': bbox_pred, 'bbox_num': bbox_num}
  63. return output