ctc_decode_op.cc 3.1 KB
Newer Older
W
wanghaoshuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* Copyright (c) 2016 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. */

#include "paddle/operators/ctc_greedy_decode_op.h"

namespace paddle {
namespace operators {

class CTCGreedyDecodeOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("Input"),
                   "Input of CTCGreedyDecodeOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Output"),
                   "Output of CTCGreedyDecodeOp should not be null.");

    auto input_dims = ctx->GetInputDim("Input");

    // TODO(wanghaoshuang): it is tricky to set the wrong dimension here.
33
    ctx->SetOutputDim("Output", input_dims);
W
wanghaoshuang 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<Tensor>("Input")->type()),
        ctx.device_context());
  }
};

class CTCGreedyDecodeOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  CTCGreedyDecodeOpMaker(OpProto* proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("Input",
50 51 52
             "(LodTensor, default: LoDTensor<int>), Its shape is "
             "[Lp, 1], where Lp is the sum of all input sequences' length.");
    AddOutput("Output", "(Tensor, default: Tensor<int>), The decode result.");
W
wanghaoshuang 已提交
53 54
    AddAttr<int>("blank",
                 "(int, default: 0), the blank label setted in Connectionist "
55
                 "Temporal Classification (CTC) op.")
W
wanghaoshuang 已提交
56 57 58 59 60 61
        .SetDefault(0);
    AddAttr<bool>("merge_repeated",
                  "(bool, default: true), whether to "
                  "merge repeated elements between two blanks. ")
        .SetDefault(true);
    AddComment(R"DOC(
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
CTCDecoder is used to merge repeated elements between two blanks
and then delete all blanks in sequence.

Given:
    Input.data = [0, 1, 2, 2, 0, 4, 0, 4, 5, 0, 6,
                  6, 0, 0, 7, 7, 7, 0]
    Input.dims = {18, 1}
    Input.LoD = [[0, 11, 18]]

And:
    blank = 0
    merge_repeated = True

Then:
    Output.data = [1, 2, 4, 4, 5, 6,
                   6, 7]
    Output.dims = {8, 1}
    Output.LoD = [[0, 6, 8]]

W
wanghaoshuang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(ctc_greedy_decode, ops::CTCGreedyDecodeOp,
                  ops::CTCGreedyDecodeOpMaker,
                  paddle::framework::EmptyGradOpMaker);
REGISTER_OP_CPU_KERNEL(
    ctc_greedy_decode,
94
    ops::CTCGreedyDecodeKernel<paddle::platform::CPUDeviceContext, int>);