face_head.py 4.1 KB

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