test_yolov3_loss_op.py 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2018 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.

15 16
from __future__ import division

17 18
import unittest
import numpy as np
19 20
from scipy.special import logit
from scipy.special import expit
21 22
from op_test import OpTest

23 24
from paddle.fluid import core

25

26 27
def mse(x, y, weight, num):
    return ((y - x)**2 * weight).sum() / num
28 29


30 31 32 33 34
def sce(x, label, weight, num):
    sigmoid_x = expit(x)
    term1 = label * np.log(sigmoid_x)
    term2 = (1.0 - label) * np.log(1.0 - sigmoid_x)
    return ((-term1 - term2) * weight).sum() / num
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59


def box_iou(box1, box2):
    b1_x1 = box1[0] - box1[2] / 2
    b1_x2 = box1[0] + box1[2] / 2
    b1_y1 = box1[1] - box1[3] / 2
    b1_y2 = box1[1] + box1[3] / 2
    b2_x1 = box2[0] - box2[2] / 2
    b2_x2 = box2[0] + box2[2] / 2
    b2_y1 = box2[1] - box2[3] / 2
    b2_y2 = box2[1] + box2[3] / 2

    b1_area = (b1_x2 - b1_x1) * (b1_y2 - b1_y1)
    b2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1)

    inter_rect_x1 = max(b1_x1, b2_x1)
    inter_rect_y1 = max(b1_y1, b2_y1)
    inter_rect_x2 = min(b1_x2, b2_x2)
    inter_rect_y2 = min(b1_y2, b2_y2)
    inter_area = max(inter_rect_x2 - inter_rect_x1, 0) * max(
        inter_rect_y2 - inter_rect_y1, 0)

    return inter_area / (b1_area + b2_area + inter_area)


60 61
def build_target(gtboxes, gtlabel, attrs, grid_size):
    n, b, _ = gtboxes.shape
62 63 64
    ignore_thresh = attrs["ignore_thresh"]
    anchors = attrs["anchors"]
    class_num = attrs["class_num"]
65
    input_size = attrs["input_size"]
66
    an_num = len(anchors) // 2
67 68 69 70 71 72
    obj_mask = np.zeros((n, an_num, grid_size, grid_size)).astype('float32')
    noobj_mask = np.ones((n, an_num, grid_size, grid_size)).astype('float32')
    tx = np.zeros((n, an_num, grid_size, grid_size)).astype('float32')
    ty = np.zeros((n, an_num, grid_size, grid_size)).astype('float32')
    tw = np.zeros((n, an_num, grid_size, grid_size)).astype('float32')
    th = np.zeros((n, an_num, grid_size, grid_size)).astype('float32')
73
    tweight = np.zeros((n, an_num, grid_size, grid_size)).astype('float32')
74 75 76 77 78 79
    tconf = np.zeros((n, an_num, grid_size, grid_size)).astype('float32')
    tcls = np.zeros(
        (n, an_num, grid_size, grid_size, class_num)).astype('float32')

    for i in range(n):
        for j in range(b):
80
            if gtboxes[i, j, :].sum() == 0:
81 82
                continue

D
dengkaipeng 已提交
83
            gt_label = gtlabel[i, j]
84 85 86 87
            gx = gtboxes[i, j, 0] * grid_size
            gy = gtboxes[i, j, 1] * grid_size
            gw = gtboxes[i, j, 2] * input_size
            gh = gtboxes[i, j, 3] * input_size
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

            gi = int(gx)
            gj = int(gy)

            gtbox = [0, 0, gw, gh]
            max_iou = 0
            for k in range(an_num):
                anchor_box = [0, 0, anchors[2 * k], anchors[2 * k + 1]]
                iou = box_iou(gtbox, anchor_box)
                if iou > max_iou:
                    max_iou = iou
                    best_an_index = k
                if iou > ignore_thresh:
                    noobj_mask[i, best_an_index, gj, gi] = 0

            obj_mask[i, best_an_index, gj, gi] = 1
            noobj_mask[i, best_an_index, gj, gi] = 0
            tx[i, best_an_index, gj, gi] = gx - gi
            ty[i, best_an_index, gj, gi] = gy - gj
            tw[i, best_an_index, gj, gi] = np.log(gw / anchors[2 *
                                                               best_an_index])
            th[i, best_an_index, gj, gi] = np.log(
                gh / anchors[2 * best_an_index + 1])
111 112
            tweight[i, best_an_index, gj, gi] = 2.0 - gtboxes[
                i, j, 2] * gtboxes[i, j, 3]
113 114 115
            tconf[i, best_an_index, gj, gi] = 1
            tcls[i, best_an_index, gj, gi, gt_label] = 1

116
    return (tx, ty, tw, th, tweight, tconf, tcls, obj_mask, noobj_mask)
117 118


D
dengkaipeng 已提交
119
def YoloV3Loss(x, gtbox, gtlabel, attrs):
120
    n, c, h, w = x.shape
121
    an_num = len(attrs['anchors']) // 2
122 123
    class_num = attrs["class_num"]
    x = x.reshape((n, an_num, 5 + class_num, h, w)).transpose((0, 1, 3, 4, 2))
124 125
    pred_x = x[:, :, :, :, 0]
    pred_y = x[:, :, :, :, 1]
126 127
    pred_w = x[:, :, :, :, 2]
    pred_h = x[:, :, :, :, 3]
128 129
    pred_conf = x[:, :, :, :, 4]
    pred_cls = x[:, :, :, :, 5:]
130

131
    tx, ty, tw, th, tweight, tconf, tcls, obj_mask, noobj_mask = build_target(
D
dengkaipeng 已提交
132
        gtbox, gtlabel, attrs, x.shape[2])
133

134
    obj_weight = obj_mask * tweight
135 136
    obj_mask_expand = np.tile(
        np.expand_dims(obj_mask, 4), (1, 1, 1, 1, int(attrs['class_num'])))
137 138 139 140 141 142 143 144 145
    box_f = an_num * h * w
    class_f = an_num * h * w * class_num
    loss_x = sce(pred_x, tx, obj_weight, box_f)
    loss_y = sce(pred_y, ty, obj_weight, box_f)
    loss_w = mse(pred_w, tw, obj_weight, box_f)
    loss_h = mse(pred_h, th, obj_weight, box_f)
    loss_conf_target = sce(pred_conf, tconf, obj_mask, box_f)
    loss_conf_notarget = sce(pred_conf, tconf, noobj_mask, box_f)
    loss_class = sce(pred_cls, tcls, obj_mask_expand, class_f)
146

D
dengkaipeng 已提交
147 148 149 150 151
    return attrs['loss_weight_xy'] * (loss_x + loss_y) \
            + attrs['loss_weight_wh'] * (loss_w + loss_h) \
            + attrs['loss_weight_conf_target'] * loss_conf_target \
            + attrs['loss_weight_conf_notarget'] * loss_conf_notarget \
            + attrs['loss_weight_class'] * loss_class
152 153 154 155


class TestYolov3LossOp(OpTest):
    def setUp(self):
D
dengkaipeng 已提交
156 157 158 159 160
        self.loss_weight_xy = 1.0
        self.loss_weight_wh = 1.0
        self.loss_weight_conf_target = 1.0
        self.loss_weight_conf_notarget = 1.0
        self.loss_weight_class = 1.0
161 162
        self.initTestCase()
        self.op_type = 'yolov3_loss'
163
        x = logit(np.random.uniform(0, 1, self.x_shape).astype('float32'))
164
        gtbox = np.random.random(size=self.gtbox_shape).astype('float32')
D
dengkaipeng 已提交
165 166
        gtlabel = np.random.randint(0, self.class_num,
                                    self.gtbox_shape[:2]).astype('int32')
167 168 169 170 171

        self.attrs = {
            "anchors": self.anchors,
            "class_num": self.class_num,
            "ignore_thresh": self.ignore_thresh,
172
            "input_size": self.input_size,
D
dengkaipeng 已提交
173 174 175 176 177
            "loss_weight_xy": self.loss_weight_xy,
            "loss_weight_wh": self.loss_weight_wh,
            "loss_weight_conf_target": self.loss_weight_conf_target,
            "loss_weight_conf_notarget": self.loss_weight_conf_notarget,
            "loss_weight_class": self.loss_weight_class,
178 179
        }

D
dengkaipeng 已提交
180
        self.inputs = {'X': x, 'GTBox': gtbox, 'GTLabel': gtlabel}
181
        self.outputs = {
D
dengkaipeng 已提交
182 183
            'Loss': np.array(
                [YoloV3Loss(x, gtbox, gtlabel, self.attrs)]).astype('float32')
184
        }
185 186

    def test_check_output(self):
187 188
        place = core.CPUPlace()
        self.check_output_with_place(place, atol=1e-3)
189

D
dengkaipeng 已提交
190 191 192 193 194
    def test_check_grad_ignore_gtbox(self):
        place = core.CPUPlace()
        self.check_grad_with_place(
            place, ['X'],
            'Loss',
195
            no_grad_set=set(["GTBox", "GTLabel"]),
196
            max_relative_error=0.3)
197 198

    def initTestCase(self):
199
        self.anchors = [10, 13, 12, 12]
200
        self.class_num = 10
201 202
        self.ignore_thresh = 0.7
        self.input_size = 416
203
        self.x_shape = (5, len(self.anchors) // 2 * (5 + self.class_num), 7, 7)
D
dengkaipeng 已提交
204
        self.gtbox_shape = (5, 10, 4)
205
        self.loss_weight_xy = 1.4
D
dengkaipeng 已提交
206
        self.loss_weight_wh = 0.8
207 208
        self.loss_weight_conf_target = 1.1
        self.loss_weight_conf_notarget = 0.9
D
dengkaipeng 已提交
209
        self.loss_weight_class = 1.2
210 211 212 213


if __name__ == "__main__":
    unittest.main()