|
| 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 | + |
| 15 | +from __future__ import absolute_import |
| 16 | +from __future__ import division |
| 17 | +from __future__ import print_function |
| 18 | + |
| 19 | +from ppdet.core.workspace import register, create |
| 20 | +from .meta_arch import BaseArch |
| 21 | + |
| 22 | +__all__ = ['PPYOLOE'] |
| 23 | +# PP-YOLOE and PP-YOLOE+ are recommended to use this architecture |
| 24 | +# PP-YOLOE and PP-YOLOE+ can also use the same architecture of YOLOv3 in yolo.py |
| 25 | + |
| 26 | + |
| 27 | +@register |
| 28 | +class PPYOLOE(BaseArch): |
| 29 | + __category__ = 'architecture' |
| 30 | + __inject__ = ['post_process'] |
| 31 | + |
| 32 | + def __init__(self, |
| 33 | + backbone='CSPResNet', |
| 34 | + neck='CustomCSPPAN', |
| 35 | + yolo_head='PPYOLOEHead', |
| 36 | + post_process='BBoxPostProcess', |
| 37 | + for_mot=False): |
| 38 | + """ |
| 39 | + PPYOLOE network, see https://arxiv.org/abs/2203.16250 |
| 40 | +
|
| 41 | + Args: |
| 42 | + backbone (nn.Layer): backbone instance |
| 43 | + neck (nn.Layer): neck instance |
| 44 | + yolo_head (nn.Layer): anchor_head instance |
| 45 | + post_process (object): `BBoxPostProcess` instance |
| 46 | + for_mot (bool): whether return other features for multi-object tracking |
| 47 | + models, default False in pure object detection models. |
| 48 | + """ |
| 49 | + super(PPYOLOE, self).__init__() |
| 50 | + self.backbone = backbone |
| 51 | + self.neck = neck |
| 52 | + self.yolo_head = yolo_head |
| 53 | + self.post_process = post_process |
| 54 | + self.for_mot = for_mot |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def from_config(cls, cfg, *args, **kwargs): |
| 58 | + # backbone |
| 59 | + backbone = create(cfg['backbone']) |
| 60 | + |
| 61 | + # fpn |
| 62 | + kwargs = {'input_shape': backbone.out_shape} |
| 63 | + neck = create(cfg['neck'], **kwargs) |
| 64 | + |
| 65 | + # head |
| 66 | + kwargs = {'input_shape': neck.out_shape} |
| 67 | + yolo_head = create(cfg['yolo_head'], **kwargs) |
| 68 | + |
| 69 | + return { |
| 70 | + 'backbone': backbone, |
| 71 | + 'neck': neck, |
| 72 | + "yolo_head": yolo_head, |
| 73 | + } |
| 74 | + |
| 75 | + def _forward(self): |
| 76 | + body_feats = self.backbone(self.inputs) |
| 77 | + neck_feats = self.neck(body_feats, self.for_mot) |
| 78 | + |
| 79 | + if self.training: |
| 80 | + yolo_losses = self.yolo_head(neck_feats, self.inputs) |
| 81 | + return yolo_losses |
| 82 | + else: |
| 83 | + yolo_head_outs = self.yolo_head(neck_feats) |
| 84 | + if self.post_process is not None: |
| 85 | + bbox, bbox_num = self.post_process( |
| 86 | + yolo_head_outs, self.yolo_head.mask_anchors, |
| 87 | + self.inputs['im_shape'], self.inputs['scale_factor']) |
| 88 | + else: |
| 89 | + bbox, bbox_num = self.yolo_head.post_process( |
| 90 | + yolo_head_outs, self.inputs['scale_factor']) |
| 91 | + output = {'bbox': bbox, 'bbox_num': bbox_num} |
| 92 | + |
| 93 | + return output |
| 94 | + |
| 95 | + def get_loss(self): |
| 96 | + return self._forward() |
| 97 | + |
| 98 | + def get_pred(self): |
| 99 | + return self._forward() |
0 commit comments