solov2.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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__ = ['SOLOv2']
  21. @register
  22. class SOLOv2(BaseArch):
  23. """
  24. SOLOv2 network, see https://arxiv.org/abs/2003.10152
  25. Args:
  26. backbone (object): an backbone instance
  27. solov2_head (object): an `SOLOv2Head` instance
  28. mask_head (object): an `SOLOv2MaskHead` instance
  29. neck (object): neck of network, such as feature pyramid network instance
  30. """
  31. __category__ = 'architecture'
  32. def __init__(self, backbone, solov2_head, mask_head, neck=None):
  33. super(SOLOv2, self).__init__()
  34. self.backbone = backbone
  35. self.neck = neck
  36. self.solov2_head = solov2_head
  37. self.mask_head = mask_head
  38. @classmethod
  39. def from_config(cls, cfg, *args, **kwargs):
  40. backbone = create(cfg['backbone'])
  41. kwargs = {'input_shape': backbone.out_shape}
  42. neck = create(cfg['neck'], **kwargs)
  43. kwargs = {'input_shape': neck.out_shape}
  44. solov2_head = create(cfg['solov2_head'], **kwargs)
  45. mask_head = create(cfg['mask_head'], **kwargs)
  46. return {
  47. 'backbone': backbone,
  48. 'neck': neck,
  49. 'solov2_head': solov2_head,
  50. 'mask_head': mask_head,
  51. }
  52. def model_arch(self):
  53. body_feats = self.backbone(self.inputs)
  54. body_feats = self.neck(body_feats)
  55. self.seg_pred = self.mask_head(body_feats)
  56. self.cate_pred_list, self.kernel_pred_list = self.solov2_head(
  57. body_feats)
  58. def get_loss(self, ):
  59. loss = {}
  60. # get gt_ins_labels, gt_cate_labels, etc.
  61. gt_ins_labels, gt_cate_labels, gt_grid_orders = [], [], []
  62. fg_num = self.inputs['fg_num']
  63. for i in range(len(self.solov2_head.seg_num_grids)):
  64. ins_label = 'ins_label{}'.format(i)
  65. if ins_label in self.inputs:
  66. gt_ins_labels.append(self.inputs[ins_label])
  67. cate_label = 'cate_label{}'.format(i)
  68. if cate_label in self.inputs:
  69. gt_cate_labels.append(self.inputs[cate_label])
  70. grid_order = 'grid_order{}'.format(i)
  71. if grid_order in self.inputs:
  72. gt_grid_orders.append(self.inputs[grid_order])
  73. loss_solov2 = self.solov2_head.get_loss(
  74. self.cate_pred_list, self.kernel_pred_list, self.seg_pred,
  75. gt_ins_labels, gt_cate_labels, gt_grid_orders, fg_num)
  76. loss.update(loss_solov2)
  77. total_loss = paddle.add_n(list(loss.values()))
  78. loss.update({'loss': total_loss})
  79. return loss
  80. def get_pred(self):
  81. seg_masks, cate_labels, cate_scores, bbox_num = self.solov2_head.get_prediction(
  82. self.cate_pred_list, self.kernel_pred_list, self.seg_pred,
  83. self.inputs['im_shape'], self.inputs['scale_factor'])
  84. outs = {
  85. "segm": seg_masks,
  86. "bbox_num": bbox_num,
  87. 'cate_label': cate_labels,
  88. 'cate_score': cate_scores
  89. }
  90. return outs