rec_vl_loss.py 2.8 KB
Newer Older
A
add vl  
andyjpaddle 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# copyright (c) 2022 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.
A
add ref  
andyjpaddle 已提交
14 15 16 17
"""
This code is refer from: 
https://github.com/wangyuxin87/VisionLAN
"""
A
add vl  
andyjpaddle 已提交
18 19 20 21 22 23 24 25 26 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import paddle
from paddle import nn


class VLLoss(nn.Layer):
    def __init__(self, mode='LF_1', weight_res=0.5, weight_mas=0.5, **kwargs):
        super(VLLoss, self).__init__()
        self.loss_func = paddle.nn.loss.CrossEntropyLoss(reduction="mean")
        assert mode in ['LF_1', 'LF_2', 'LA']
        self.mode = mode
        self.weight_res = weight_res
        self.weight_mas = weight_mas

    def flatten_label(self, target):
        label_flatten = []
        label_length = []
        for i in range(0, target.shape[0]):
            cur_label = target[i].tolist()
            label_flatten += cur_label[:cur_label.index(0) + 1]
            label_length.append(cur_label.index(0) + 1)
        label_flatten = paddle.to_tensor(label_flatten, dtype='int64')
        label_length = paddle.to_tensor(label_length, dtype='int32')
        return (label_flatten, label_length)

    def _flatten(self, sources, lengths):
        return paddle.concat([t[:l] for t, l in zip(sources, lengths)])

    def forward(self, predicts, batch):
        text_pre = predicts[0]
        target = batch[1].astype('int64')
        label_flatten, length = self.flatten_label(target)
        text_pre = self._flatten(text_pre, length)
        if self.mode == 'LF_1':
            loss = self.loss_func(text_pre, label_flatten)
        else:
            text_rem = predicts[1]
            text_mas = predicts[2]
            target_res = batch[2].astype('int64')
            target_sub = batch[3].astype('int64')
            label_flatten_res, length_res = self.flatten_label(target_res)
            label_flatten_sub, length_sub = self.flatten_label(target_sub)
            text_rem = self._flatten(text_rem, length_res)
            text_mas = self._flatten(text_mas, length_sub)
            loss_ori = self.loss_func(text_pre, label_flatten)
            loss_res = self.loss_func(text_rem, label_flatten_res)
            loss_mas = self.loss_func(text_mas, label_flatten_sub)
            loss = loss_ori + loss_res * self.weight_res + loss_mas * self.weight_mas
        return {'loss': loss}