From eca9870e89baebe8ec80cfae2a809fb575041350 Mon Sep 17 00:00:00 2001 From: sunyanfang01 Date: Wed, 3 Jun 2020 16:57:39 +0800 Subject: [PATCH] for review --- paddlex/cv/models/faster_rcnn.py | 7 +++ paddlex/cv/nets/detection/bbox_head.py | 22 ++------- paddlex/cv/nets/detection/loss/diou_loss.py | 18 ++++---- .../cv/nets/detection/loss/smoothl1_loss.py | 45 +++++++++++++++++++ 4 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 paddlex/cv/nets/detection/loss/smoothl1_loss.py diff --git a/paddlex/cv/models/faster_rcnn.py b/paddlex/cv/models/faster_rcnn.py index 666aaf4..86d21db 100644 --- a/paddlex/cv/models/faster_rcnn.py +++ b/paddlex/cv/models/faster_rcnn.py @@ -53,6 +53,11 @@ class FasterRCNN(BaseAPI): ] assert backbone in backbones, "backbone should be one of {}".format( backbones) + bbox_loss_types = [ + 'SmoothL1Loss', 'CiouLoss', 'DiouLoss', 'GiouLoss' + ] + assert backbone in backbones, "bbox_loss_type should be one of {}".format( + bbox_loss_types) self.backbone = backbone self.num_classes = num_classes self.with_fpn = with_fpn @@ -84,6 +89,8 @@ class FasterRCNN(BaseAPI): layers = 101 variant = 'd' norm_type = 'affine_channel' + if self.bbox_loss_type != 'SmoothL1Loss': + norm_type = 'bn' elif backbone_name == 'HRNet_W18': backbone = paddlex.cv.nets.hrnet.HRNet( width=18, freeze_norm=True, norm_decay=0., freeze_at=0) diff --git a/paddlex/cv/nets/detection/bbox_head.py b/paddlex/cv/nets/detection/bbox_head.py index 30c1b8a..635a434 100644 --- a/paddlex/cv/nets/detection/bbox_head.py +++ b/paddlex/cv/nets/detection/bbox_head.py @@ -188,39 +188,25 @@ class BBoxHead(object): is_cls_agnostic=False, num_classes=self.num_classes, use_complete_iou_loss=True) - loss_bbox = loss_obj( - x=bbox_pred, - y=bbox_targets, - inside_weight=bbox_inside_weights, - outside_weight=bbox_outside_weights) elif self.bbox_loss_type == 'DiouLoss': from .loss.diou_loss import DiouLoss loss_obj = DiouLoss(loss_weight=12., is_cls_agnostic=False, num_classes=self.num_classes, use_complete_iou_loss=False) - loss_bbox = loss_obj( - x=bbox_pred, - y=bbox_targets, - inside_weight=bbox_inside_weights, - outside_weight=bbox_outside_weights) elif self.bbox_loss_type == 'GiouLoss': from .loss.giou_loss import GiouLoss loss_obj = GiouLoss(loss_weight=10., is_cls_agnostic=False, num_classes=self.num_classes) - loss_bbox = loss_obj( - x=bbox_pred, - y=bbox_targets, - inside_weight=bbox_inside_weights, - outside_weight=bbox_outside_weights) else: - loss_bbox = fluid.layers.smooth_l1( + from .loss.smoothl1_loss import SmoothL1Loss + loss_obj = SmoothL1Loss(self.sigma) + loss_bbox = loss_obj( x=bbox_pred, y=bbox_targets, inside_weight=bbox_inside_weights, - outside_weight=bbox_outside_weights, - sigma=self.sigma) + outside_weight=bbox_outside_weights) loss_bbox = fluid.layers.reduce_mean(loss_bbox) return {'loss_cls': loss_cls, 'loss_bbox': loss_bbox} diff --git a/paddlex/cv/nets/detection/loss/diou_loss.py b/paddlex/cv/nets/detection/loss/diou_loss.py index cf9c553..d071743 100644 --- a/paddlex/cv/nets/detection/loss/diou_loss.py +++ b/paddlex/cv/nets/detection/loss/diou_loss.py @@ -1,16 +1,16 @@ -#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. +# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. # -#Licensed under the Apache License, Version 2.0 (the "License"); -#you may not use this file except in compliance with the License. -#You may obtain a copy of the License at +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # -#Unless required by applicable law or agreed to in writing, software -#distributed under the License is distributed on an "AS IS" BASIS, -#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -#See the License for the specific language governing permissions and -#limitations under the License. +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. from __future__ import absolute_import from __future__ import division diff --git a/paddlex/cv/nets/detection/loss/smoothl1_loss.py b/paddlex/cv/nets/detection/loss/smoothl1_loss.py new file mode 100644 index 0000000..ace8b67 --- /dev/null +++ b/paddlex/cv/nets/detection/loss/smoothl1_loss.py @@ -0,0 +1,45 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import numpy as np + +from paddle import fluid + +class SmoothL1Loss(object): + ''' + SmoothL1 Loss + Args: + sigma (float): Hyper parameter of smooth L1 loss layer. + + ''' + def __init__(self, sigma): + self.sigma = sigma + + def __call__(self, + x, + y, + inside_weight=None, + outside_weight=None): + loss_bbox = fluid.layers.smooth_l1( + x=x, + y=y, + inside_weight=inside_weight, + outside_weight=outside_weight, + sigma=self.sigma) + return loss_bbox \ No newline at end of file -- GitLab