blazeface.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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__ = ['BlazeFace']
  20. @register
  21. class BlazeFace(BaseArch):
  22. """
  23. BlazeFace: Sub-millisecond Neural Face Detection on Mobile GPUs,
  24. see https://arxiv.org/abs/1907.05047
  25. Args:
  26. backbone (nn.Layer): backbone instance
  27. neck (nn.Layer): neck instance
  28. blaze_head (nn.Layer): `blazeHead` instance
  29. post_process (object): `BBoxPostProcess` instance
  30. """
  31. __category__ = 'architecture'
  32. __inject__ = ['post_process']
  33. def __init__(self, backbone, blaze_head, neck, post_process):
  34. super(BlazeFace, self).__init__()
  35. self.backbone = backbone
  36. self.neck = neck
  37. self.blaze_head = blaze_head
  38. self.post_process = post_process
  39. @classmethod
  40. def from_config(cls, cfg, *args, **kwargs):
  41. # backbone
  42. backbone = create(cfg['backbone'])
  43. # fpn
  44. kwargs = {'input_shape': backbone.out_shape}
  45. neck = create(cfg['neck'], **kwargs)
  46. # head
  47. kwargs = {'input_shape': neck.out_shape}
  48. blaze_head = create(cfg['blaze_head'], **kwargs)
  49. return {
  50. 'backbone': backbone,
  51. 'neck': neck,
  52. 'blaze_head': blaze_head,
  53. }
  54. def _forward(self):
  55. # Backbone
  56. body_feats = self.backbone(self.inputs)
  57. # neck
  58. neck_feats = self.neck(body_feats)
  59. # blaze Head
  60. if self.training:
  61. return self.blaze_head(neck_feats, self.inputs['image'],
  62. self.inputs['gt_bbox'],
  63. self.inputs['gt_class'])
  64. else:
  65. preds, anchors = self.blaze_head(neck_feats, self.inputs['image'])
  66. bbox, bbox_num = self.post_process(preds, anchors,
  67. self.inputs['im_shape'],
  68. self.inputs['scale_factor'])
  69. return bbox, bbox_num
  70. def get_loss(self, ):
  71. return {"loss": self._forward()}
  72. def get_pred(self):
  73. bbox_pred, bbox_num = self._forward()
  74. output = {
  75. "bbox": bbox_pred,
  76. "bbox_num": bbox_num,
  77. }
  78. return output