face_head.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. import paddle
  15. import paddle.nn as nn
  16. from ppdet.core.workspace import register
  17. from ..layers import AnchorGeneratorSSD
  18. from ..cls_utils import _get_class_default_kwargs
  19. @register
  20. class FaceHead(nn.Layer):
  21. """
  22. Head block for Face detection network
  23. Args:
  24. num_classes (int): Number of output classes.
  25. in_channels (int): Number of input channels.
  26. anchor_generator(object): instance of anchor genertor method.
  27. kernel_size (int): kernel size of Conv2D in FaceHead.
  28. padding (int): padding of Conv2D in FaceHead.
  29. conv_decay (float): norm_decay (float): weight decay for conv layer weights.
  30. loss (object): loss of face detection model.
  31. """
  32. __shared__ = ['num_classes']
  33. __inject__ = ['anchor_generator', 'loss']
  34. def __init__(self,
  35. num_classes=80,
  36. in_channels=[96, 96],
  37. anchor_generator=_get_class_default_kwargs(AnchorGeneratorSSD),
  38. kernel_size=3,
  39. padding=1,
  40. conv_decay=0.,
  41. loss='SSDLoss'):
  42. super(FaceHead, self).__init__()
  43. # add background class
  44. self.num_classes = num_classes + 1
  45. self.in_channels = in_channels
  46. self.anchor_generator = anchor_generator
  47. self.loss = loss
  48. if isinstance(anchor_generator, dict):
  49. self.anchor_generator = AnchorGeneratorSSD(**anchor_generator)
  50. self.num_priors = self.anchor_generator.num_priors
  51. self.box_convs = []
  52. self.score_convs = []
  53. for i, num_prior in enumerate(self.num_priors):
  54. box_conv_name = "boxes{}".format(i)
  55. box_conv = self.add_sublayer(
  56. box_conv_name,
  57. nn.Conv2D(
  58. in_channels=self.in_channels[i],
  59. out_channels=num_prior * 4,
  60. kernel_size=kernel_size,
  61. padding=padding))
  62. self.box_convs.append(box_conv)
  63. score_conv_name = "scores{}".format(i)
  64. score_conv = self.add_sublayer(
  65. score_conv_name,
  66. nn.Conv2D(
  67. in_channels=self.in_channels[i],
  68. out_channels=num_prior * self.num_classes,
  69. kernel_size=kernel_size,
  70. padding=padding))
  71. self.score_convs.append(score_conv)
  72. @classmethod
  73. def from_config(cls, cfg, input_shape):
  74. return {'in_channels': [i.channels for i in input_shape], }
  75. def forward(self, feats, image, gt_bbox=None, gt_class=None):
  76. box_preds = []
  77. cls_scores = []
  78. prior_boxes = []
  79. for feat, box_conv, score_conv in zip(feats, self.box_convs,
  80. self.score_convs):
  81. box_pred = box_conv(feat)
  82. box_pred = paddle.transpose(box_pred, [0, 2, 3, 1])
  83. box_pred = paddle.reshape(box_pred, [0, -1, 4])
  84. box_preds.append(box_pred)
  85. cls_score = score_conv(feat)
  86. cls_score = paddle.transpose(cls_score, [0, 2, 3, 1])
  87. cls_score = paddle.reshape(cls_score, [0, -1, self.num_classes])
  88. cls_scores.append(cls_score)
  89. prior_boxes = self.anchor_generator(feats, image)
  90. if self.training:
  91. return self.get_loss(box_preds, cls_scores, gt_bbox, gt_class,
  92. prior_boxes)
  93. else:
  94. return (box_preds, cls_scores), prior_boxes
  95. def get_loss(self, boxes, scores, gt_bbox, gt_class, prior_boxes):
  96. return self.loss(boxes, scores, gt_bbox, gt_class, prior_boxes)