centernet.py 3.3 KB

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