centernet.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 paddlex.ppdet.core.workspace import register, create
  18. from .meta_arch import BaseArch
  19. __all__ = ['CenterNet']
  20. @register
  21. class CenterNet(BaseArch):
  22. """
  23. CenterNet network, see http://arxiv.org/abs/1904.07850
  24. Args:
  25. backbone (object): backbone instance
  26. neck (object): 'CenterDLAFPN' instance
  27. head (object): 'CenterHead' instance
  28. post_process (object): 'CenterNetPostProcess' instance
  29. for_mot (bool): whether return other features used in tracking model
  30. """
  31. __category__ = 'architecture'
  32. __inject__ = ['post_process']
  33. def __init__(self,
  34. backbone='DLA',
  35. neck='CenterDLAFPN',
  36. head='CenterHead',
  37. post_process='CenterNetPostProcess',
  38. for_mot=False):
  39. super(CenterNet, self).__init__()
  40. self.backbone = backbone
  41. self.neck = neck
  42. self.head = head
  43. self.post_process = post_process
  44. self.for_mot = for_mot
  45. @classmethod
  46. def from_config(cls, cfg, *args, **kwargs):
  47. backbone = create(cfg['backbone'])
  48. kwargs = {'input_shape': backbone.out_shape}
  49. neck = create(cfg['neck'], **kwargs)
  50. kwargs = {'input_shape': neck.out_shape}
  51. head = create(cfg['head'], **kwargs)
  52. return {'backbone': backbone, 'neck': neck, "head": head}
  53. def _forward(self):
  54. body_feats = self.backbone(self.inputs)
  55. neck_feat = self.neck(body_feats)
  56. head_out = self.head(neck_feat, self.inputs)
  57. if self.for_mot:
  58. head_out.update({'neck_feat': neck_feat})
  59. return head_out
  60. def get_pred(self):
  61. head_out = self._forward()
  62. if self.for_mot:
  63. bbox, bbox_inds = self.post_process(
  64. head_out['heatmap'],
  65. head_out['size'],
  66. head_out['offset'],
  67. im_shape=self.inputs['im_shape'],
  68. scale_factor=self.inputs['scale_factor'])
  69. output = {
  70. "bbox": bbox,
  71. "bbox_inds": bbox_inds,
  72. "neck_feat": head_out['neck_feat']
  73. }
  74. else:
  75. bbox, bbox_num = self.post_process(
  76. head_out['heatmap'],
  77. head_out['size'],
  78. head_out['offset'],
  79. im_shape=self.inputs['im_shape'],
  80. scale_factor=self.inputs['scale_factor'])
  81. output = {
  82. "bbox": bbox,
  83. "bbox_num": bbox_num,
  84. }
  85. return output
  86. def get_loss(self):
  87. return self._forward()