yolof.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (c) 2022 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__ = ['YOLOF']
  20. @register
  21. class YOLOF(BaseArch):
  22. __category__ = 'architecture'
  23. def __init__(self,
  24. backbone='ResNet',
  25. neck='DilatedEncoder',
  26. head='YOLOFHead',
  27. for_mot=False):
  28. """
  29. YOLOF network, see https://arxiv.org/abs/2103.09460
  30. Args:
  31. backbone (nn.Layer): backbone instance
  32. neck (nn.Layer): DilatedEncoder instance
  33. head (nn.Layer): YOLOFHead instance
  34. for_mot (bool): whether return other features for multi-object tracking
  35. models, default False in pure object detection models.
  36. """
  37. super(YOLOF, self).__init__()
  38. self.backbone = backbone
  39. self.neck = neck
  40. self.head = head
  41. self.for_mot = for_mot
  42. @classmethod
  43. def from_config(cls, cfg, *args, **kwargs):
  44. # backbone
  45. backbone = create(cfg['backbone'])
  46. # fpn
  47. kwargs = {'input_shape': backbone.out_shape}
  48. neck = create(cfg['neck'], **kwargs)
  49. # head
  50. kwargs = {'input_shape': neck.out_shape}
  51. head = create(cfg['head'], **kwargs)
  52. return {
  53. 'backbone': backbone,
  54. 'neck': neck,
  55. "head": head,
  56. }
  57. def _forward(self):
  58. body_feats = self.backbone(self.inputs)
  59. neck_feats = self.neck(body_feats, self.for_mot)
  60. if self.training:
  61. yolo_losses = self.head(neck_feats, self.inputs)
  62. return yolo_losses
  63. else:
  64. yolo_head_outs = self.head(neck_feats)
  65. bbox, bbox_num = self.head.post_process(yolo_head_outs,
  66. self.inputs['im_shape'],
  67. self.inputs['scale_factor'])
  68. output = {'bbox': bbox, 'bbox_num': bbox_num}
  69. return output
  70. def get_loss(self):
  71. return self._forward()
  72. def get_pred(self):
  73. return self._forward()