blazeface.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 paddlers.models.ppdet.core.workspace import register, create
  18. from .meta_arch import BaseArch
  19. import paddle
  20. import paddle.nn.functional as F
  21. __all__ = ['BlazeFace']
  22. @register
  23. class BlazeFace(BaseArch):
  24. """
  25. BlazeFace: Sub-millisecond Neural Face Detection on Mobile GPUs,
  26. see https://arxiv.org/abs/1907.05047
  27. Args:
  28. backbone (nn.Layer): backbone instance
  29. neck (nn.Layer): neck instance
  30. blaze_head (nn.Layer): `blazeHead` instance
  31. post_process (object): `BBoxPostProcess` instance
  32. """
  33. __category__ = 'architecture'
  34. __inject__ = ['post_process']
  35. def __init__(self, backbone, blaze_head, neck, post_process):
  36. super(BlazeFace, self).__init__()
  37. self.backbone = backbone
  38. self.neck = neck
  39. self.blaze_head = blaze_head
  40. self.post_process = post_process
  41. @classmethod
  42. def from_config(cls, cfg, *args, **kwargs):
  43. # backbone
  44. backbone = create(cfg['backbone'])
  45. # fpn
  46. kwargs = {'input_shape': backbone.out_shape}
  47. neck = create(cfg['neck'], **kwargs)
  48. # head
  49. kwargs = {'input_shape': neck.out_shape}
  50. blaze_head = create(cfg['blaze_head'], **kwargs)
  51. return {
  52. 'backbone': backbone,
  53. 'neck': neck,
  54. 'blaze_head': blaze_head,
  55. }
  56. def _forward(self):
  57. # Backbone
  58. body_feats = self.backbone(self.inputs)
  59. # neck
  60. neck_feats = self.neck(body_feats)
  61. # blaze Head
  62. if self.training:
  63. return self.blaze_head(neck_feats, self.inputs['image'],
  64. self.inputs['gt_bbox'],
  65. self.inputs['gt_class'])
  66. else:
  67. preds, anchors = self.blaze_head(neck_feats, self.inputs['image'])
  68. bbox, bbox_num, nms_keep_idx = self.post_process(
  69. preds, anchors, self.inputs['im_shape'],
  70. self.inputs['scale_factor'])
  71. if self.use_extra_data:
  72. extra_data = {} # record the bbox output before nms, such like scores and nms_keep_idx
  73. """extra_data:{
  74. 'scores': predict scores,
  75. 'nms_keep_idx': bbox index before nms,
  76. }
  77. """
  78. preds_logits = preds[1] # [[1xNumBBoxNumClass]]
  79. extra_data['scores'] = F.softmax(paddle.concat(
  80. preds_logits, axis=1)).transpose([0, 2, 1])
  81. extra_data['logits'] = paddle.concat(
  82. preds_logits, axis=1).transpose([0, 2, 1])
  83. extra_data['nms_keep_idx'] = nms_keep_idx # bbox index before nms
  84. return bbox, bbox_num, extra_data
  85. else:
  86. return bbox, bbox_num
  87. def get_loss(self, ):
  88. return {"loss": self._forward()}
  89. def get_pred(self):
  90. if self.use_extra_data:
  91. bbox_pred, bbox_num, extra_data = self._forward()
  92. output = {
  93. "bbox": bbox_pred,
  94. "bbox_num": bbox_num,
  95. "extra_data": extra_data
  96. }
  97. else:
  98. bbox_pred, bbox_num = self._forward()
  99. output = {
  100. "bbox": bbox_pred,
  101. "bbox_num": bbox_num,
  102. }
  103. return output