Skip to content

Commit 042fece

Browse files
committed
use L2Loss. test=develop
1 parent af124dc commit 042fece

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

paddle/fluid/operators/yolov3_loss_op.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ static T L1Loss(T x, T y) {
4141
return std::abs(y - x);
4242
}
4343

44+
template <typename T>
45+
static T L2Loss(T x, T y) {
46+
return 0.5 * (y - x) * (y - x);
47+
}
48+
4449
template <typename T>
4550
static T SCEGrad(T x, T label) {
4651
return 1.0 / (1.0 + std::exp(-x)) - label;
@@ -51,6 +56,11 @@ static T L1LossGrad(T x, T y) {
5156
return x > y ? 1.0 : -1.0;
5257
}
5358

59+
template <typename T>
60+
static T L2LossGrad(T x, T y) {
61+
return x - y;
62+
}
63+
5464
static int GetMaskIndex(std::vector<int> mask, int val) {
5565
for (size_t i = 0; i < mask.size(); i++) {
5666
if (mask[i] == val) {
@@ -130,8 +140,8 @@ static void CalcBoxLocationLoss(T* loss, const T* input, Box<T> gt,
130140
T scale = (2.0 - gt.w * gt.h) * score;
131141
loss[0] += SCE<T>(input[box_idx], tx) * scale;
132142
loss[0] += SCE<T>(input[box_idx + stride], ty) * scale;
133-
loss[0] += L1Loss<T>(input[box_idx + 2 * stride], tw) * scale;
134-
loss[0] += L1Loss<T>(input[box_idx + 3 * stride], th) * scale;
143+
loss[0] += L2Loss<T>(input[box_idx + 2 * stride], tw) * scale;
144+
loss[0] += L2Loss<T>(input[box_idx + 3 * stride], th) * scale;
135145
}
136146

137147
template <typename T>
@@ -150,9 +160,9 @@ static void CalcBoxLocationLossGrad(T* input_grad, const T loss, const T* input,
150160
input_grad[box_idx + stride] =
151161
SCEGrad<T>(input[box_idx + stride], ty) * scale * loss;
152162
input_grad[box_idx + 2 * stride] =
153-
L1LossGrad<T>(input[box_idx + 2 * stride], tw) * scale * loss;
163+
L2LossGrad<T>(input[box_idx + 2 * stride], tw) * scale * loss;
154164
input_grad[box_idx + 3 * stride] =
155-
L1LossGrad<T>(input[box_idx + 3 * stride], th) * scale * loss;
165+
L2LossGrad<T>(input[box_idx + 3 * stride], th) * scale * loss;
156166
}
157167

158168
template <typename T>

python/paddle/fluid/tests/unittests/test_yolov3_loss_op.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def l1loss(x, y):
2727
return abs(x - y)
2828

2929

30+
def l2loss(x, y):
31+
return 0.5 * (y - x) * (y - x)
32+
33+
3034
def sce(x, label):
3135
sigmoid_x = expit(x)
3236
term1 = label * np.log(sigmoid_x)
@@ -145,8 +149,8 @@ def YOLOv3Loss(x, gtbox, gtlabel, gtscore, attrs):
145149
scale = (2.0 - gtbox[i, j, 2] * gtbox[i, j, 3]) * gtscore[i, j]
146150
loss[i] += sce(x[i, an_idx, gj, gi, 0], tx) * scale
147151
loss[i] += sce(x[i, an_idx, gj, gi, 1], ty) * scale
148-
loss[i] += l1loss(x[i, an_idx, gj, gi, 2], tw) * scale
149-
loss[i] += l1loss(x[i, an_idx, gj, gi, 3], th) * scale
152+
loss[i] += l2loss(x[i, an_idx, gj, gi, 2], tw) * scale
153+
loss[i] += l2loss(x[i, an_idx, gj, gi, 3], th) * scale
150154

151155
objness[i, an_idx * h * w + gj * w + gi] = gtscore[i, j]
152156

@@ -202,27 +206,24 @@ def setUp(self):
202206

203207
def test_check_output(self):
204208
place = core.CPUPlace()
205-
self.check_output_with_place(place, atol=2e-3)
209+
self.check_output_with_place(place, atol=1e-3)
206210

207211
def test_check_grad_ignore_gtbox(self):
208212
place = core.CPUPlace()
209213
self.check_grad_with_place(
210214
place, ['X'],
211215
'Loss',
212216
no_grad_set=set(["GTBox", "GTLabel", "GTScore"]),
213-
max_relative_error=0.2)
217+
max_relative_error=0.3)
214218

215219
def initTestCase(self):
216-
self.anchors = [
217-
10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198,
218-
373, 326
219-
]
220-
self.anchor_mask = [0, 1, 2]
221-
self.class_num = 10
222-
self.ignore_thresh = 0.7
220+
self.anchors = [10, 13, 16, 30, 33, 23]
221+
self.anchor_mask = [1, 2]
222+
self.class_num = 5
223+
self.ignore_thresh = 0.5
223224
self.downsample = 32
224225
self.x_shape = (3, len(self.anchor_mask) * (5 + self.class_num), 5, 5)
225-
self.gtbox_shape = (3, 10, 4)
226+
self.gtbox_shape = (3, 5, 4)
226227
self.use_label_smooth = True
227228

228229

0 commit comments

Comments
 (0)