rec_aster_head.py 15.1 KB
Newer Older
T
tink2123 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# copyright (c) 2020 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.
T
tink2123 已提交
14 15 16 17
"""
This code is refer from:
https://github.com/ayumiymk/aster.pytorch/blob/master/lib/models/attention_recognition_head.py
"""
T
tink2123 已提交
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import sys

import paddle
from paddle import nn
from paddle.nn import functional as F


class AsterHead(nn.Layer):
    def __init__(self,
                 in_channels,
                 out_channels,
                 sDim,
                 attDim,
                 max_len_labels,
                 time_step=25,
                 beam_width=5,
                 **kwargs):
        super(AsterHead, self).__init__()
        self.num_classes = out_channels
        self.in_planes = in_channels
        self.sDim = sDim
        self.attDim = attDim
        self.max_len_labels = max_len_labels
        self.decoder = AttentionRecognitionHead(in_channels, out_channels, sDim,
                                                attDim, max_len_labels)
        self.time_step = time_step
        self.embeder = Embedding(self.time_step, in_channels)
        self.beam_width = beam_width
T
tink2123 已提交
50
        self.eos = self.num_classes - 3
T
tink2123 已提交
51 52 53 54 55 56

    def forward(self, x, targets=None, embed=None):
        return_dict = {}
        embedding_vectors = self.embeder(x)

        if self.training:
T
tink2123 已提交
57
            rec_targets, rec_lengths, _ = targets
T
tink2123 已提交
58 59 60 61 62 63 64
            rec_pred = self.decoder([x, rec_targets, rec_lengths],
                                    embedding_vectors)
            return_dict['rec_pred'] = rec_pred
            return_dict['embedding_vectors'] = embedding_vectors
        else:
            rec_pred, rec_pred_scores = self.decoder.beam_search(
                x, self.beam_width, self.eos, embedding_vectors)
X
xiaoting 已提交
65 66
            rec_pred_scores.stop_gradient = True
            rec_pred.stop_gradient = True
T
tink2123 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            return_dict['rec_pred'] = rec_pred
            return_dict['rec_pred_scores'] = rec_pred_scores
            return_dict['embedding_vectors'] = embedding_vectors
        return return_dict


class Embedding(nn.Layer):
    def __init__(self, in_timestep, in_planes, mid_dim=4096, embed_dim=300):
        super(Embedding, self).__init__()
        self.in_timestep = in_timestep
        self.in_planes = in_planes
        self.embed_dim = embed_dim
        self.mid_dim = mid_dim
        self.eEmbed = nn.Linear(
            in_timestep * in_planes,
            self.embed_dim)  # Embed encoder output to a word-embedding like

    def forward(self, x):
        x = paddle.reshape(x, [paddle.shape(x)[0], -1])
        x = self.eEmbed(x)
        return x


class AttentionRecognitionHead(nn.Layer):
    """
  input: [b x 16 x 64 x in_planes]
  output: probability sequence: [b x T x num_classes]
  """

    def __init__(self, in_channels, out_channels, sDim, attDim, max_len_labels):
        super(AttentionRecognitionHead, self).__init__()
        self.num_classes = out_channels  # this is the output classes. So it includes the <EOS>.
        self.in_planes = in_channels
        self.sDim = sDim
        self.attDim = attDim
        self.max_len_labels = max_len_labels

        self.decoder = DecoderUnit(
            sDim=sDim, xDim=in_channels, yDim=self.num_classes, attDim=attDim)

    def forward(self, x, embed):
        x, targets, lengths = x
        batch_size = paddle.shape(x)[0]
        # Decoder
        state = self.decoder.get_initial_state(embed)
        outputs = []
        for i in range(max(lengths)):
            if i == 0:
                y_prev = paddle.full(
                    shape=[batch_size], fill_value=self.num_classes)
            else:
X
xiaoting 已提交
118

T
tink2123 已提交
119 120 121 122 123 124
                y_prev = targets[:, i - 1]
            output, state = self.decoder(x, state, y_prev)
            outputs.append(output)
        outputs = paddle.concat([_.unsqueeze(1) for _ in outputs], 1)
        return outputs

T
tink2123 已提交
125 126 127 128 129 130 131 132
    def beam_search(self, x, beam_width, eos, embed):
        def _inflate(tensor, times, dim):
            repeat_dims = [1] * tensor.dim()
            repeat_dims[dim] = times
            output = paddle.tile(tensor, repeat_dims)
            return output

        # https://github.com/IBM/pytorch-seq2seq/blob/fede87655ddce6c94b38886089e05321dc9802af/seq2seq/models/TopKDecoder.py
X
xiaoting 已提交
133
        batch_size, l, d = paddle.shape(x)
T
tink2123 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146
        x = paddle.tile(
            paddle.transpose(
                x.unsqueeze(1), perm=[1, 0, 2, 3]), [beam_width, 1, 1, 1])
        inflated_encoder_feats = paddle.reshape(
            paddle.transpose(
                x, perm=[1, 0, 2, 3]), [-1, l, d])

        # Initialize the decoder
        state = self.decoder.get_initial_state(embed, tile_times=beam_width)

        pos_index = paddle.reshape(
            paddle.arange(batch_size) * beam_width, shape=[-1, 1])
        # Initialize the scores
X
xiaoting 已提交
147

T
tink2123 已提交
148
        sequence_scores = paddle.full(
X
xiaoting 已提交
149 150 151 152
            shape=[batch_size, beam_width], fill_value=-float('Inf'))
        sequence_scores[:, 0] = 0.0
        sequence_scores = paddle.reshape(
            sequence_scores, shape=[batch_size * beam_width, 1])
T
tink2123 已提交
153 154 155 156 157 158

        # Initialize the input vector
        y_prev = paddle.full(
            shape=[batch_size * beam_width], fill_value=self.num_classes)

        # Store decisions for backtracking
X
xiaoting 已提交
159 160 161
        stored_scores = []
        stored_predecessors = []
        stored_emitted_symbols = []
T
tink2123 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

        for i in range(self.max_len_labels):
            output, state = self.decoder(inflated_encoder_feats, state, y_prev)
            state = paddle.unsqueeze(state, axis=0)
            log_softmax_output = paddle.nn.functional.log_softmax(
                output, axis=1)

            sequence_scores = _inflate(sequence_scores, self.num_classes, 1)
            sequence_scores += log_softmax_output
            scores, candidates = paddle.topk(
                paddle.reshape(sequence_scores, [batch_size, -1]),
                beam_width,
                axis=1)
            # Reshape input = (bk, 1) and sequence_scores = (bk, 1)
            y_prev = paddle.reshape(
X
xiaoting 已提交
177 178
                candidates % self.num_classes, shape=[batch_size, beam_width])
            y_prev = paddle.reshape(y_prev, shape=[batch_size * beam_width])
T
tink2123 已提交
179 180 181 182
            sequence_scores = paddle.reshape(
                scores, shape=[batch_size * beam_width, 1])

            # Update fields for next timestep
X
xiaoting 已提交
183 184
            pos_index = paddle.expand(pos_index, paddle.shape(candidates))

T
tink2123 已提交
185 186 187 188 189 190 191 192 193 194
            predecessors = paddle.cast(
                candidates / self.num_classes + pos_index, dtype='int64')
            predecessors = paddle.reshape(
                predecessors, shape=[batch_size * beam_width, 1])
            state = paddle.index_select(
                state, index=predecessors.squeeze(), axis=1)

            # Update sequence socres and erase scores for <eos> symbol so that they aren't expanded
            stored_scores.append(sequence_scores.clone())
            y_prev = paddle.reshape(y_prev, shape=[-1, 1])
X
xiaoting 已提交
195 196

            eos_prev = paddle.full(paddle.shape(y_prev), fill_value=eos)
T
tink2123 已提交
197
            mask = eos_prev == y_prev
X
xiaoting 已提交
198
            mask = paddle.cast(mask, 'int64')
T
tink2123 已提交
199
            mask = paddle.nonzero(mask)
X
xiaoting 已提交
200 201
            if len(mask) > 0:
                sequence_scores[:] = -float('inf')
T
tink2123 已提交
202 203 204 205 206 207 208 209
                sequence_scores = paddle.to_tensor(sequence_scores)

            # Cache results for backtracking
            stored_predecessors.append(predecessors)
            y_prev = paddle.squeeze(y_prev)
            stored_emitted_symbols.append(y_prev)

        # Do backtracking to return the optimal values
X
xiaoting 已提交
210
        # ====== backtrak ======#
T
tink2123 已提交
211
        # Initialize return variables given different types
X
xiaoting 已提交
212 213 214 215
        p = []

        # Placeholder for lengths of top-k sequences
        l = paddle.full([batch_size, beam_width], self.max_len_labels)
T
tink2123 已提交
216 217 218 219 220 221 222 223 224 225 226

        # the last step output of the beams are not sorted
        # thus they are sorted here
        sorted_score, sorted_idx = paddle.topk(
            paddle.reshape(
                stored_scores[-1], shape=[batch_size, beam_width]),
            beam_width)

        # initialize the sequence scores with the sorted last step beam scores
        s = sorted_score.clone()

X
xiaoting 已提交
227 228
        batch_eos_found = paddle.zeros(
            [batch_size], dtype='int32')  # the number of EOS found
T
tink2123 已提交
229 230
        # in the backward loop below for each batch
        t = self.max_len_labels - 1
X
xiaoting 已提交
231

T
tink2123 已提交
232 233 234
        # initialize the back pointer with the sorted order of the last step beams.
        # add pos_index for indexing variable with b*k as the first dimension.
        t_predecessors = paddle.reshape(
X
xiaoting 已提交
235
            sorted_idx + pos_index.expand(paddle.shape(sorted_idx)),
T
tink2123 已提交
236
            shape=[batch_size * beam_width])
X
xiaoting 已提交
237 238

        tmp_beam_width = beam_width
T
tink2123 已提交
239 240 241 242 243 244 245 246 247
        while t >= 0:
            # Re-order the variables with the back pointer
            current_symbol = paddle.index_select(
                stored_emitted_symbols[t], index=t_predecessors, axis=0)
            t_predecessors = paddle.index_select(
                stored_predecessors[t].squeeze(), index=t_predecessors, axis=0)
            eos_indices = stored_emitted_symbols[t] == eos
            eos_indices = paddle.nonzero(eos_indices)

X
xiaoting 已提交
248 249 250 251 252
            stored_predecessors_t = stored_predecessors[t]
            stored_emitted_symbols_t = stored_emitted_symbols[t]
            stored_scores_t = stored_scores[t]
            t_plus = t + 1

T
tink2123 已提交
253
            if eos_indices.dim() > 0:
X
xiaoting 已提交
254
                for j in range(eos_indices.shape[0] - 1, -1, -1):
T
tink2123 已提交
255 256 257
                    # Indices of the EOS symbol for both variables
                    # with b*k as the first dimension, and b, k for
                    # the first two dimensions
X
xiaoting 已提交
258 259
                    idx = eos_indices[j]
                    b_idx = int(idx[0] / tmp_beam_width)
T
tink2123 已提交
260 261
                    # The indices of the replacing position
                    # according to the replacement strategy noted above
X
xiaoting 已提交
262 263
                    res_k_idx = tmp_beam_width - (batch_eos_found[b_idx] %
                                                  tmp_beam_width) - 1
T
tink2123 已提交
264
                    batch_eos_found[b_idx] += 1
X
xiaoting 已提交
265
                    res_idx = b_idx * tmp_beam_width + res_k_idx
T
tink2123 已提交
266 267 268

                    # Replace the old information in return variables
                    # with the new ended sequence information
X
xiaoting 已提交
269 270 271 272 273

                    t_predecessors[res_idx] = stored_predecessors_t[idx[0]]
                    current_symbol[res_idx] = stored_emitted_symbols_t[idx[0]]
                    s[b_idx, res_k_idx] = stored_scores_t[idx[0], 0]
                    l[b_idx][res_k_idx] = t_plus
T
tink2123 已提交
274 275 276 277 278 279 280 281

            # record the back tracked results
            p.append(current_symbol)
            t -= 1

        # Sort and re-order again as the added ended sequences may change
        # the order (very unlikely)
        s, re_sorted_idx = s.topk(beam_width)
X
xiaoting 已提交
282

T
tink2123 已提交
283
        for b_idx in range(batch_size):
X
xiaoting 已提交
284 285 286 287
            tmp_tensor = paddle.full_like(l[b_idx], 0)
            for k_idx in re_sorted_idx[b_idx]:
                tmp_tensor[k_idx] = l[b_idx][k_idx]
            l[b_idx] = tmp_tensor
T
tink2123 已提交
288 289

        re_sorted_idx = paddle.reshape(
X
xiaoting 已提交
290
            re_sorted_idx + pos_index.expand(paddle.shape(re_sorted_idx)),
T
tink2123 已提交
291 292 293 294
            [batch_size * beam_width])

        # Reverse the sequences and re-order at the same time
        # It is reversed because the backtracking happens in reverse time order
X
xiaoting 已提交
295 296 297 298 299 300 301 302 303 304 305
        reversed_p = p[::-1]

        q = []
        for step in reversed_p:
            q.append(
                paddle.reshape(
                    paddle.index_select(step, re_sorted_idx, 0),
                    shape=[batch_size, beam_width, -1]))

        q = paddle.concat(q, -1)[:, 0, :]
        return q, paddle.ones_like(q)
T
tink2123 已提交
306

T
tink2123 已提交
307 308 309 310 311 312 313 314 315

class AttentionUnit(nn.Layer):
    def __init__(self, sDim, xDim, attDim):
        super(AttentionUnit, self).__init__()

        self.sDim = sDim
        self.xDim = xDim
        self.attDim = attDim

T
tink2123 已提交
316 317 318
        self.sEmbed = nn.Linear(sDim, attDim)
        self.xEmbed = nn.Linear(xDim, attDim)
        self.wEmbed = nn.Linear(attDim, 1)
T
tink2123 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

    def forward(self, x, sPrev):
        batch_size, T, _ = x.shape  # [b x T x xDim]
        x = paddle.reshape(x, [-1, self.xDim])  # [(b x T) x xDim]
        xProj = self.xEmbed(x)  # [(b x T) x attDim]
        xProj = paddle.reshape(xProj, [batch_size, T, -1])  # [b x T x attDim]

        sPrev = sPrev.squeeze(0)
        sProj = self.sEmbed(sPrev)  # [b x attDim]
        sProj = paddle.unsqueeze(sProj, 1)  # [b x 1 x attDim]
        sProj = paddle.expand(sProj,
                              [batch_size, T, self.attDim])  # [b x T x attDim]

        sumTanh = paddle.tanh(sProj + xProj)
        sumTanh = paddle.reshape(sumTanh, [-1, self.attDim])

        vProj = self.wEmbed(sumTanh)  # [(b x T) x 1]
        vProj = paddle.reshape(vProj, [batch_size, T])
        alpha = F.softmax(
            vProj, axis=1)  # attention weights for each sample in the minibatch
        return alpha


class DecoderUnit(nn.Layer):
    def __init__(self, sDim, xDim, yDim, attDim):
        super(DecoderUnit, self).__init__()
        self.sDim = sDim
        self.xDim = xDim
        self.yDim = yDim
        self.attDim = attDim
        self.emdDim = attDim

        self.attention_unit = AttentionUnit(sDim, xDim, attDim)
        self.tgt_embedding = nn.Embedding(
            yDim + 1, self.emdDim, weight_attr=nn.initializer.Normal(
                std=0.01))  # the last is used for <BOS>
        self.gru = nn.GRUCell(input_size=xDim + self.emdDim, hidden_size=sDim)
        self.fc = nn.Linear(
            sDim,
            yDim,
            weight_attr=nn.initializer.Normal(std=0.01),
            bias_attr=nn.initializer.Constant(value=0))
        self.embed_fc = nn.Linear(300, self.sDim)

    def get_initial_state(self, embed, tile_times=1):
        assert embed.shape[1] == 300
        state = self.embed_fc(embed)  # N * sDim
        if tile_times != 1:
            state = state.unsqueeze(1)
            trans_state = paddle.transpose(state, perm=[1, 0, 2])
            state = paddle.tile(trans_state, repeat_times=[tile_times, 1, 1])
            trans_state = paddle.transpose(state, perm=[1, 0, 2])
            state = paddle.reshape(trans_state, shape=[-1, self.sDim])
        state = state.unsqueeze(0)  # 1 * N * sDim
        return state

    def forward(self, x, sPrev, yPrev):
        # x: feature sequence from the image decoder.
        batch_size, T, _ = x.shape
        alpha = self.attention_unit(x, sPrev)
        context = paddle.squeeze(paddle.matmul(alpha.unsqueeze(1), x), axis=1)
        yPrev = paddle.cast(yPrev, dtype="int64")
        yProj = self.tgt_embedding(yPrev)

        concat_context = paddle.concat([yProj, context], 1)
        sPrev = paddle.squeeze(sPrev, 0)
X
xiaoting 已提交
385

T
tink2123 已提交
386 387 388
        output, state = self.gru(concat_context, sPrev)
        output = paddle.squeeze(output, axis=1)
        output = self.fc(output)
X
xiaoting 已提交
389
        return output, state