• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/framework/op.h"
17 #include "tensorflow/core/framework/shape_inference.h"
18 
19 namespace tensorflow {
20 
21 using shape_inference::DimensionHandle;
22 using shape_inference::InferenceContext;
23 using shape_inference::ShapeHandle;
24 
25 // CTC is Connectionist Temporal Classification.  See util/ctc/ for details.
26 
27 REGISTER_OP("CTCLoss")
28     .Input("inputs: float")
29     .Input("labels_indices: int64")
30     .Input("labels_values: int32")
31     .Input("sequence_length: int32")
32     .Attr("preprocess_collapse_repeated: bool = false")
33     .Attr("ctc_merge_repeated: bool = true")
34     .Attr("ignore_longer_outputs_than_inputs: bool = false")
35     .Output("loss: float")
36     .Output("gradient: float")
__anon78c05cd60102(InferenceContext* c) 37     .SetShapeFn([](InferenceContext* c) {
38       ShapeHandle inputs;
39       ShapeHandle labels_indices;
40       ShapeHandle labels_values;
41       ShapeHandle sequence_length;
42 
43       TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 3, &inputs));
44       TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 2, &labels_indices));
45       TF_RETURN_IF_ERROR(c->WithRank(c->input(2), 1, &labels_values));
46       TF_RETURN_IF_ERROR(c->WithRank(c->input(3), 1, &sequence_length));
47 
48       DimensionHandle unused;
49       TF_RETURN_IF_ERROR(c->Merge(c->Dim(labels_indices, 0),
50                                   c->Dim(labels_values, 0), &unused));
51 
52       // Get batch size from inputs and sequence_length, and update inputs
53       // with the merged batch_size since it is returned.
54       DimensionHandle batch_size;
55       TF_RETURN_IF_ERROR(
56           c->Merge(c->Dim(inputs, 1), c->Dim(sequence_length, 0), &batch_size));
57       TF_RETURN_IF_ERROR(c->ReplaceDim(inputs, 1, batch_size, &inputs));
58 
59       c->set_output(0, c->Vector(batch_size));
60       c->set_output(1, inputs);
61       return Status::OK();
62     });
63 
64 REGISTER_OP("CTCGreedyDecoder")
65     .Input("inputs: float")
66     .Input("sequence_length: int32")
67     .Attr("merge_repeated: bool = false")
68     .Output("decoded_indices: int64")
69     .Output("decoded_values: int64")
70     .Output("decoded_shape: int64")
71     .Output("log_probability: float")
__anon78c05cd60202(InferenceContext* c) 72     .SetShapeFn([](InferenceContext* c) {
73       ShapeHandle inputs;
74       ShapeHandle sequence_length;
75 
76       TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 3, &inputs));
77       TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 1, &sequence_length));
78 
79       // Get batch size from inputs and sequence_length.
80       DimensionHandle batch_size;
81       TF_RETURN_IF_ERROR(
82           c->Merge(c->Dim(inputs, 1), c->Dim(sequence_length, 0), &batch_size));
83 
84       DimensionHandle total_decoded_outputs = c->UnknownDim();
85       c->set_output(0, c->Matrix(total_decoded_outputs, 2));
86       c->set_output(1, c->Vector(total_decoded_outputs));
87       c->set_output(2, c->Vector(2));
88       c->set_output(3, c->Matrix(batch_size, 1));
89       return Status::OK();
90     });
91 
92 REGISTER_OP("CTCBeamSearchDecoder")
93     .Input("inputs: float")
94     .Input("sequence_length: int32")
95     .Attr("beam_width: int >= 1")
96     .Attr("top_paths: int >= 1")
97     .Attr("merge_repeated: bool = true")
98     .Output("decoded_indices: top_paths * int64")
99     .Output("decoded_values: top_paths * int64")
100     .Output("decoded_shape: top_paths * int64")
101     .Output("log_probability: float")
__anon78c05cd60302(InferenceContext* c) 102     .SetShapeFn([](InferenceContext* c) {
103       ShapeHandle inputs;
104       ShapeHandle sequence_length;
105 
106       TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 3, &inputs));
107       TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 1, &sequence_length));
108 
109       // Get batch size from inputs and sequence_length.
110       DimensionHandle batch_size;
111       TF_RETURN_IF_ERROR(
112           c->Merge(c->Dim(inputs, 1), c->Dim(sequence_length, 0), &batch_size));
113 
114       int32 top_paths;
115       TF_RETURN_IF_ERROR(c->GetAttr("top_paths", &top_paths));
116 
117       // Outputs.
118       int out_idx = 0;
119       for (int i = 0; i < top_paths; ++i) {  // decoded_indices
120         c->set_output(out_idx++, c->Matrix(InferenceContext::kUnknownDim, 2));
121       }
122       for (int i = 0; i < top_paths; ++i) {  // decoded_values
123         c->set_output(out_idx++, c->Vector(InferenceContext::kUnknownDim));
124       }
125       ShapeHandle shape_v = c->Vector(2);
126       for (int i = 0; i < top_paths; ++i) {  // decoded_shape
127         c->set_output(out_idx++, shape_v);
128       }
129       c->set_output(out_idx++, c->Matrix(batch_size, top_paths));
130       return Status::OK();
131     });
132 
133 }  // namespace tensorflow
134