loss.py 6.9 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# coding: utf8
# copyright (c) 2019 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
#
#    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.

import sys
import paddle.fluid as fluid
import numpy as np
import importlib
from utils.config import cfg


23 24 25 26 27
def softmax_with_loss(logit,
                      label,
                      ignore_mask=None,
                      num_classes=2,
                      weight=None):
W
wuzewu 已提交
28 29 30 31 32 33 34 35
    ignore_mask = fluid.layers.cast(ignore_mask, 'float32')
    label = fluid.layers.elementwise_min(
        label, fluid.layers.assign(np.array([num_classes - 1], dtype=np.int32)))
    logit = fluid.layers.transpose(logit, [0, 2, 3, 1])
    logit = fluid.layers.reshape(logit, [-1, num_classes])
    label = fluid.layers.reshape(label, [-1, 1])
    label = fluid.layers.cast(label, 'int64')
    ignore_mask = fluid.layers.reshape(ignore_mask, [-1, 1])
L
LielinJiang 已提交
36 37 38 39 40 41 42
    if weight is None:
        loss, probs = fluid.layers.softmax_with_cross_entropy(
            logit,
            label,
            ignore_index=cfg.DATASET.IGNORE_INDEX,
            return_softmax=True)
    else:
C
chenguowei01 已提交
43
        label = fluid.layers.squeeze(label, axes=[-1])
44
        label_one_hot = fluid.one_hot(input=label, depth=num_classes)
L
LielinJiang 已提交
45
        if isinstance(weight, list):
46 47 48
            assert len(
                weight
            ) == num_classes, "weight length must equal num of classes"
L
LielinJiang 已提交
49
            weight = fluid.layers.assign(np.array([weight], dtype='float32'))
L
LielinJiang 已提交
50
        elif isinstance(weight, str):
51 52
            assert weight.lower(
            ) == 'dynamic', 'if weight is string, must be dynamic!'
L
LielinJiang 已提交
53
            tmp = []
54 55
            total_num = fluid.layers.cast(
                fluid.layers.shape(label)[0], 'float32')
L
LielinJiang 已提交
56 57 58 59 60 61
            for i in range(num_classes):
                cls_pixel_num = fluid.layers.reduce_sum(label_one_hot[:, i])
                ratio = total_num / (cls_pixel_num + 1)
                tmp.append(ratio)
            weight = fluid.layers.concat(tmp)
            weight = weight / fluid.layers.reduce_sum(weight) * num_classes
L
LielinJiang 已提交
62 63 64
        elif isinstance(weight, fluid.layers.Variable):
            pass
        else:
65 66 67
            raise ValueError(
                'Expect weight is a list, string or Variable, but receive {}'.
                format(type(weight)))
L
LielinJiang 已提交
68
        weight = fluid.layers.reshape(weight, [1, num_classes])
69 70
        weighted_label_one_hot = fluid.layers.elementwise_mul(
            label_one_hot, weight)
L
LielinJiang 已提交
71 72 73 74 75 76 77
        probs = fluid.layers.softmax(logit)
        loss = fluid.layers.cross_entropy(
            probs,
            weighted_label_one_hot,
            soft_label=True,
            ignore_index=cfg.DATASET.IGNORE_INDEX)
        weighted_label_one_hot.stop_gradient = True
W
wuzewu 已提交
78 79

    loss = loss * ignore_mask
80 81
    avg_loss = fluid.layers.mean(loss) / fluid.layers.mean(ignore_mask)

W
wuzewu 已提交
82 83 84 85
    label.stop_gradient = True
    ignore_mask.stop_gradient = True
    return avg_loss

L
LielinJiang 已提交
86

W
wuyefeilin 已提交
87 88 89
# to change, how to appicate ignore index and ignore mask
def dice_loss(logit, label, ignore_mask=None, epsilon=0.00001):
    if logit.shape[1] != 1 or label.shape[1] != 1 or ignore_mask.shape[1] != 1:
90 91
        raise Exception(
            "dice loss is only applicable to one channel classfication")
W
wuyefeilin 已提交
92 93
    ignore_mask = fluid.layers.cast(ignore_mask, 'float32')
    logit = fluid.layers.transpose(logit, [0, 2, 3, 1])
94
    label = fluid.layers.transpose(label, [0, 2, 3, 1])
W
wuyefeilin 已提交
95 96 97 98 99 100 101 102 103
    label = fluid.layers.cast(label, 'int64')
    ignore_mask = fluid.layers.transpose(ignore_mask, [0, 2, 3, 1])
    logit = fluid.layers.sigmoid(logit)
    logit = logit * ignore_mask
    label = label * ignore_mask
    reduce_dim = list(range(1, len(logit.shape)))
    inse = fluid.layers.reduce_sum(logit * label, dim=reduce_dim)
    dice_denominator = fluid.layers.reduce_sum(
        logit, dim=reduce_dim) + fluid.layers.reduce_sum(
104
            label, dim=reduce_dim)
W
wuyefeilin 已提交
105 106 107 108 109
    dice_score = 1 - inse * 2 / (dice_denominator + epsilon)
    label.stop_gradient = True
    ignore_mask.stop_gradient = True
    return fluid.layers.reduce_mean(dice_score)

L
LielinJiang 已提交
110

W
wuyefeilin 已提交
111 112 113 114 115 116 117 118
def bce_loss(logit, label, ignore_mask=None):
    if logit.shape[1] != 1 or label.shape[1] != 1 or ignore_mask.shape[1] != 1:
        raise Exception("bce loss is only applicable to binary classfication")
    label = fluid.layers.cast(label, 'float32')
    loss = fluid.layers.sigmoid_cross_entropy_with_logits(
        x=logit,
        label=label,
        ignore_index=cfg.DATASET.IGNORE_INDEX,
119
        normalize=True)  # or False
W
wuyefeilin 已提交
120 121 122 123 124
    loss = fluid.layers.reduce_sum(loss)
    label.stop_gradient = True
    ignore_mask.stop_gradient = True
    return loss

W
wuzewu 已提交
125

126 127 128 129 130
def multi_softmax_with_loss(logits,
                            label,
                            ignore_mask=None,
                            num_classes=2,
                            weight=None):
W
wuzewu 已提交
131 132 133
    if isinstance(logits, tuple):
        avg_loss = 0
        for i, logit in enumerate(logits):
134 135
            if label.shape[2] != logit.shape[2] or label.shape[
                    3] != logit.shape[3]:
L
LielinJiang 已提交
136 137
                label = fluid.layers.resize_nearest(label, logit.shape[2:])
            logit_mask = (label.astype('int32') !=
W
wuzewu 已提交
138
                          cfg.DATASET.IGNORE_INDEX).astype('int32')
139
            loss = softmax_with_loss(logit, label, logit_mask, num_classes)
W
wuzewu 已提交
140 141
            avg_loss += cfg.MODEL.MULTI_LOSS_WEIGHT[i] * loss
    else:
142 143
        avg_loss = softmax_with_loss(
            logits, label, ignore_mask, num_classes, weight=weight)
W
wuzewu 已提交
144 145
    return avg_loss

L
LielinJiang 已提交
146

W
wuyefeilin 已提交
147 148 149 150 151 152 153 154 155 156 157 158
def multi_dice_loss(logits, label, ignore_mask=None):
    if isinstance(logits, tuple):
        avg_loss = 0
        for i, logit in enumerate(logits):
            logit_label = fluid.layers.resize_nearest(label, logit.shape[2:])
            logit_mask = (logit_label.astype('int32') !=
                          cfg.DATASET.IGNORE_INDEX).astype('int32')
            loss = dice_loss(logit, logit_label, logit_mask)
            avg_loss += cfg.MODEL.MULTI_LOSS_WEIGHT[i] * loss
    else:
        avg_loss = dice_loss(logits, label, ignore_mask)
    return avg_loss
W
wuzewu 已提交
159

L
LielinJiang 已提交
160

W
wuyefeilin 已提交
161 162 163 164 165 166 167 168 169 170 171 172
def multi_bce_loss(logits, label, ignore_mask=None):
    if isinstance(logits, tuple):
        avg_loss = 0
        for i, logit in enumerate(logits):
            logit_label = fluid.layers.resize_nearest(label, logit.shape[2:])
            logit_mask = (logit_label.astype('int32') !=
                          cfg.DATASET.IGNORE_INDEX).astype('int32')
            loss = bce_loss(logit, logit_label, logit_mask)
            avg_loss += cfg.MODEL.MULTI_LOSS_WEIGHT[i] * loss
    else:
        avg_loss = bce_loss(logits, label, ignore_mask)
    return avg_loss