"research/feelvos/eval.sh" did not exist on "5274ec8b0c6edb71af06ef4d0a7270918912830a"
combined_loss.py 2.46 KB
Newer Older
littletomatodonkey's avatar
littletomatodonkey committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Copyright (c) 2020 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.

import paddle
import paddle.nn as nn

18
19
20
from .rec_ctc_loss import CTCLoss
from .center_loss import CenterLoss
from .ace_loss import ACELoss
andyjpaddle's avatar
andyjpaddle committed
21
from .rec_sar_loss import SARLoss
22

littletomatodonkey's avatar
littletomatodonkey committed
23
from .distillation_loss import DistillationCTCLoss
andyjpaddle's avatar
andyjpaddle committed
24
from .distillation_loss import DistillationSARLoss
littletomatodonkey's avatar
littletomatodonkey committed
25
from .distillation_loss import DistillationDMLLoss
LDOUBLEV's avatar
LDOUBLEV committed
26
from .distillation_loss import DistillationDistanceLoss, DistillationDBLoss, DistillationDilaDBLoss
littletomatodonkey's avatar
littletomatodonkey committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


class CombinedLoss(nn.Layer):
    """
    CombinedLoss:
        a combionation of loss function
    """

    def __init__(self, loss_config_list=None):
        super().__init__()
        self.loss_func = []
        self.loss_weight = []
        assert isinstance(loss_config_list, list), (
            'operator config should be a list')
        for config in loss_config_list:
            assert isinstance(config,
                              dict) and len(config) == 1, "yaml format error"
            name = list(config)[0]
            param = config[name]
            assert "weight" in param, "weight must be in param, but param just contains {}".format(
                param.keys())
            self.loss_weight.append(param.pop("weight"))
            self.loss_func.append(eval(name)(**param))

    def forward(self, input, batch, **kargs):
        loss_dict = {}
LDOUBLEV's avatar
LDOUBLEV committed
53
        loss_all = 0.
littletomatodonkey's avatar
littletomatodonkey committed
54
55
56
57
        for idx, loss_func in enumerate(self.loss_func):
            loss = loss_func(input, batch, **kargs)
            if isinstance(loss, paddle.Tensor):
                loss = {"loss_{}_{}".format(str(loss), idx): loss}
58

littletomatodonkey's avatar
littletomatodonkey committed
59
            weight = self.loss_weight[idx]
60
61
62
63
64
65
66
67

            loss = {key: loss[key] * weight for key in loss}

            if "loss" in loss:
                loss_all += loss["loss"]
            else:
                loss_all += paddle.add_n(list(loss.values()))
            loss_dict.update(loss)
LDOUBLEV's avatar
LDOUBLEV committed
68
        loss_dict["loss"] = loss_all
littletomatodonkey's avatar
littletomatodonkey committed
69
        return loss_dict