ttfnet.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2020 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 ppdet.core.workspace import register, create
  19. from .meta_arch import BaseArch
  20. __all__ = ['TTFNet']
  21. @register
  22. class TTFNet(BaseArch):
  23. """
  24. TTFNet network, see https://arxiv.org/abs/1909.00700
  25. Args:
  26. backbone (object): backbone instance
  27. neck (object): 'TTFFPN' instance
  28. ttf_head (object): 'TTFHead' instance
  29. post_process (object): 'BBoxPostProcess' instance
  30. """
  31. __category__ = 'architecture'
  32. __inject__ = ['post_process']
  33. def __init__(self,
  34. backbone='DarkNet',
  35. neck='TTFFPN',
  36. ttf_head='TTFHead',
  37. post_process='BBoxPostProcess'):
  38. super(TTFNet, self).__init__()
  39. self.backbone = backbone
  40. self.neck = neck
  41. self.ttf_head = ttf_head
  42. self.post_process = post_process
  43. @classmethod
  44. def from_config(cls, cfg, *args, **kwargs):
  45. backbone = create(cfg['backbone'])
  46. kwargs = {'input_shape': backbone.out_shape}
  47. neck = create(cfg['neck'], **kwargs)
  48. kwargs = {'input_shape': neck.out_shape}
  49. ttf_head = create(cfg['ttf_head'], **kwargs)
  50. return {
  51. 'backbone': backbone,
  52. 'neck': neck,
  53. "ttf_head": ttf_head,
  54. }
  55. def _forward(self):
  56. body_feats = self.backbone(self.inputs)
  57. body_feats = self.neck(body_feats)
  58. hm, wh = self.ttf_head(body_feats)
  59. if self.training:
  60. return hm, wh
  61. else:
  62. bbox, bbox_num = self.post_process(hm, wh, self.inputs['im_shape'],
  63. self.inputs['scale_factor'])
  64. return bbox, bbox_num
  65. def get_loss(self, ):
  66. loss = {}
  67. heatmap = self.inputs['ttf_heatmap']
  68. box_target = self.inputs['ttf_box_target']
  69. reg_weight = self.inputs['ttf_reg_weight']
  70. hm, wh = self._forward()
  71. head_loss = self.ttf_head.get_loss(hm, wh, heatmap, box_target,
  72. reg_weight)
  73. loss.update(head_loss)
  74. total_loss = paddle.add_n(list(loss.values()))
  75. loss.update({'loss': total_loss})
  76. return loss
  77. def get_pred(self):
  78. bbox_pred, bbox_num = self._forward()
  79. output = {
  80. "bbox": bbox_pred,
  81. "bbox_num": bbox_num,
  82. }
  83. return output