提交 bdf3b392 编写于 作者: L longxiang

fix scalexy bug

上级 beaa62a7
...@@ -54,6 +54,7 @@ class IouAwareLoss(IouLoss): ...@@ -54,6 +54,7 @@ class IouAwareLoss(IouLoss):
anchors, anchors,
downsample_ratio, downsample_ratio,
batch_size, batch_size,
scale_x_y,
eps=1.e-10): eps=1.e-10):
''' '''
Args: Args:
...@@ -67,9 +68,9 @@ class IouAwareLoss(IouLoss): ...@@ -67,9 +68,9 @@ class IouAwareLoss(IouLoss):
''' '''
pred = self._bbox_transform(x, y, w, h, anchors, downsample_ratio, pred = self._bbox_transform(x, y, w, h, anchors, downsample_ratio,
batch_size, False) batch_size, False, scale_x_y, eps)
gt = self._bbox_transform(tx, ty, tw, th, anchors, downsample_ratio, gt = self._bbox_transform(tx, ty, tw, th, anchors, downsample_ratio,
batch_size, True) batch_size, True, scale_x_y, eps)
iouk = self._iou(pred, gt, ioup, eps) iouk = self._iou(pred, gt, ioup, eps)
iouk.stop_gradient = True iouk.stop_gradient = True
......
...@@ -63,6 +63,7 @@ class IouLoss(object): ...@@ -63,6 +63,7 @@ class IouLoss(object):
anchors, anchors,
downsample_ratio, downsample_ratio,
batch_size, batch_size,
scale_x_y=1.,
ioup=None, ioup=None,
eps=1.e-10): eps=1.e-10):
''' '''
...@@ -75,9 +76,9 @@ class IouLoss(object): ...@@ -75,9 +76,9 @@ class IouLoss(object):
eps (float): the decimal to prevent the denominator eqaul zero eps (float): the decimal to prevent the denominator eqaul zero
''' '''
pred = self._bbox_transform(x, y, w, h, anchors, downsample_ratio, pred = self._bbox_transform(x, y, w, h, anchors, downsample_ratio,
batch_size, False) batch_size, False, scale_x_y, eps)
gt = self._bbox_transform(tx, ty, tw, th, anchors, downsample_ratio, gt = self._bbox_transform(tx, ty, tw, th, anchors, downsample_ratio,
batch_size, True) batch_size, True, scale_x_y, eps)
iouk = self._iou(pred, gt, ioup, eps) iouk = self._iou(pred, gt, ioup, eps)
if self.loss_square: if self.loss_square:
loss_iou = 1. - iouk * iouk loss_iou = 1. - iouk * iouk
...@@ -145,7 +146,7 @@ class IouLoss(object): ...@@ -145,7 +146,7 @@ class IouLoss(object):
return diou_term + ciou_term return diou_term + ciou_term
def _bbox_transform(self, dcx, dcy, dw, dh, anchors, downsample_ratio, def _bbox_transform(self, dcx, dcy, dw, dh, anchors, downsample_ratio,
batch_size, is_gt): batch_size, is_gt, scale_x_y, eps):
grid_x = int(self._MAX_WI / downsample_ratio) grid_x = int(self._MAX_WI / downsample_ratio)
grid_y = int(self._MAX_HI / downsample_ratio) grid_y = int(self._MAX_HI / downsample_ratio)
an_num = len(anchors) // 2 an_num = len(anchors) // 2
...@@ -179,8 +180,11 @@ class IouLoss(object): ...@@ -179,8 +180,11 @@ class IouLoss(object):
cy.gradient = True cy.gradient = True
else: else:
dcx_sig = fluid.layers.sigmoid(dcx) dcx_sig = fluid.layers.sigmoid(dcx)
cx = fluid.layers.elementwise_add(dcx_sig, gi) / grid_x_act
dcy_sig = fluid.layers.sigmoid(dcy) dcy_sig = fluid.layers.sigmoid(dcy)
if (abs(scale_x_y - 1.0) > eps):
dcx_sig = scale_x_y * dcx_sig - 0.5 * (scale_x_y - 1)
dcy_sig = scale_x_y * dcy_sig - 0.5 * (scale_x_y - 1)
cx = fluid.layers.elementwise_add(dcx_sig, gi) / grid_x_act
cy = fluid.layers.elementwise_add(dcy_sig, gj) / grid_y_act cy = fluid.layers.elementwise_add(dcy_sig, gj) / grid_y_act
anchor_w_ = [anchors[i] for i in range(0, len(anchors)) if i % 2 == 0] anchor_w_ = [anchors[i] for i in range(0, len(anchors)) if i % 2 == 0]
......
...@@ -92,7 +92,7 @@ class YOLOv3Loss(object): ...@@ -92,7 +92,7 @@ class YOLOv3Loss(object):
return {'loss': sum(losses)} return {'loss': sum(losses)}
def _get_fine_grained_loss(self, outputs, targets, gt_box, batch_size, def _get_fine_grained_loss(self, outputs, targets, gt_box, batch_size,
num_classes, mask_anchors, ignore_thresh): num_classes, mask_anchors, ignore_thresh, eps=1.e-10):
""" """
Calculate fine grained YOLOv3 loss Calculate fine grained YOLOv3 loss
...@@ -136,12 +136,25 @@ class YOLOv3Loss(object): ...@@ -136,12 +136,25 @@ class YOLOv3Loss(object):
tx, ty, tw, th, tscale, tobj, tcls = self._split_target(target) tx, ty, tw, th, tscale, tobj, tcls = self._split_target(target)
tscale_tobj = tscale * tobj tscale_tobj = tscale * tobj
scale_x_y = self.scale_x_y if not isinstance(
self.scale_x_y, Sequence) else self.scale_x_y[i]
if (abs(scale_x_y - 1.0) < eps):
loss_x = fluid.layers.sigmoid_cross_entropy_with_logits( loss_x = fluid.layers.sigmoid_cross_entropy_with_logits(
x, tx) * tscale_tobj x, tx) * tscale_tobj
loss_x = fluid.layers.reduce_sum(loss_x, dim=[1, 2, 3]) loss_x = fluid.layers.reduce_sum(loss_x, dim=[1, 2, 3])
loss_y = fluid.layers.sigmoid_cross_entropy_with_logits( loss_y = fluid.layers.sigmoid_cross_entropy_with_logits(
y, ty) * tscale_tobj y, ty) * tscale_tobj
loss_y = fluid.layers.reduce_sum(loss_y, dim=[1, 2, 3]) loss_y = fluid.layers.reduce_sum(loss_y, dim=[1, 2, 3])
else:
dx = scale_x_y * fluid.layers.sigmoid(x) - 0.5 * (scale_x_y - 1.0)
dy = scale_x_y * fluid.layers.sigmoid(y) - 0.5 * (scale_x_y - 1.0)
loss_x = fluid.layers.abs(dx - tx) * tscale_tobj
loss_x = fluid.layers.reduce_sum(loss_x, dim=[1, 2, 3])
loss_y = fluid.layers.abs(dy - ty) * tscale_tobj
loss_y = fluid.layers.reduce_sum(loss_y, dim=[1, 2, 3])
# NOTE: we refined loss function of (w, h) as L1Loss # NOTE: we refined loss function of (w, h) as L1Loss
loss_w = fluid.layers.abs(w - tw) * tscale_tobj loss_w = fluid.layers.abs(w - tw) * tscale_tobj
loss_w = fluid.layers.reduce_sum(loss_w, dim=[1, 2, 3]) loss_w = fluid.layers.reduce_sum(loss_w, dim=[1, 2, 3])
...@@ -149,7 +162,7 @@ class YOLOv3Loss(object): ...@@ -149,7 +162,7 @@ class YOLOv3Loss(object):
loss_h = fluid.layers.reduce_sum(loss_h, dim=[1, 2, 3]) loss_h = fluid.layers.reduce_sum(loss_h, dim=[1, 2, 3])
if self._iou_loss is not None: if self._iou_loss is not None:
loss_iou = self._iou_loss(x, y, w, h, tx, ty, tw, th, anchors, loss_iou = self._iou_loss(x, y, w, h, tx, ty, tw, th, anchors,
downsample, self._batch_size) downsample, self._batch_size, scale_x_y)
loss_iou = loss_iou * tscale_tobj loss_iou = loss_iou * tscale_tobj
loss_iou = fluid.layers.reduce_sum(loss_iou, dim=[1, 2, 3]) loss_iou = fluid.layers.reduce_sum(loss_iou, dim=[1, 2, 3])
loss_ious.append(fluid.layers.reduce_mean(loss_iou)) loss_ious.append(fluid.layers.reduce_mean(loss_iou))
...@@ -157,14 +170,12 @@ class YOLOv3Loss(object): ...@@ -157,14 +170,12 @@ class YOLOv3Loss(object):
if self._iou_aware_loss is not None: if self._iou_aware_loss is not None:
loss_iou_aware = self._iou_aware_loss( loss_iou_aware = self._iou_aware_loss(
ioup, x, y, w, h, tx, ty, tw, th, anchors, downsample, ioup, x, y, w, h, tx, ty, tw, th, anchors, downsample,
self._batch_size) self._batch_size, scale_x_y)
loss_iou_aware = loss_iou_aware * tobj loss_iou_aware = loss_iou_aware * tobj
loss_iou_aware = fluid.layers.reduce_sum( loss_iou_aware = fluid.layers.reduce_sum(
loss_iou_aware, dim=[1, 2, 3]) loss_iou_aware, dim=[1, 2, 3])
loss_iou_awares.append(fluid.layers.reduce_mean(loss_iou_aware)) loss_iou_awares.append(fluid.layers.reduce_mean(loss_iou_aware))
scale_x_y = self.scale_x_y if not isinstance(
self.scale_x_y, Sequence) else self.scale_x_y[i]
loss_obj_pos, loss_obj_neg = self._calc_obj_loss( loss_obj_pos, loss_obj_neg = self._calc_obj_loss(
output, obj, tobj, gt_box, self._batch_size, anchors, output, obj, tobj, gt_box, self._batch_size, anchors,
num_classes, downsample, self._ignore_thresh, scale_x_y) num_classes, downsample, self._ignore_thresh, scale_x_y)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册