From 0162587ebc94acdc9911ad9b699d4c44bfed6845 Mon Sep 17 00:00:00 2001 From: yanzhenxiang2020 Date: Tue, 29 Sep 2020 16:28:46 +0800 Subject: [PATCH] add CTCGreedyDecoder ops --- mindspore/ops/_op_impl/aicpu/__init__.py | 1 + .../ops/_op_impl/aicpu/ctc_greedy_decoder.py | 39 ++++++++++++ .../test_aicpu_ops/test_ctc_greedy_decoder.py | 59 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 mindspore/ops/_op_impl/aicpu/ctc_greedy_decoder.py create mode 100644 tests/st/ops/ascend/test_aicpu_ops/test_ctc_greedy_decoder.py diff --git a/mindspore/ops/_op_impl/aicpu/__init__.py b/mindspore/ops/_op_impl/aicpu/__init__.py index 54bca3da9fb..c428b6eb870 100644 --- a/mindspore/ops/_op_impl/aicpu/__init__.py +++ b/mindspore/ops/_op_impl/aicpu/__init__.py @@ -59,3 +59,4 @@ from .fused_sparse_ftrl import _fused_sparse_ftrl_aicpu from .fused_sparse_proximal_adagrad import _fused_sparse_proximal_adagrad_aicpu from .meshgrid import _meshgrid_aicpu from .trans_data import _trans_data_aicpu +from .ctc_greedy_decoder import _ctc_greedy_decoder_aicpu diff --git a/mindspore/ops/_op_impl/aicpu/ctc_greedy_decoder.py b/mindspore/ops/_op_impl/aicpu/ctc_greedy_decoder.py new file mode 100644 index 00000000000..897bc64aa1b --- /dev/null +++ b/mindspore/ops/_op_impl/aicpu/ctc_greedy_decoder.py @@ -0,0 +1,39 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +"""CTCGreedyDecoder op""" +from mindspore.ops.op_info_register import op_info_register, AiCPURegOp, DataType +ctc_greedy_decoder_op_info = AiCPURegOp("CTCGreedyDecoder") \ + .fusion_type("OPAQUE") \ + .input(0, "inputs", "required") \ + .input(1, "sequence_length", "required") \ + .output(0, "decoded_indices", "required") \ + .output(1, "decoded_values", "required") \ + .output(2, "decoded_shape", "required") \ + .output(3, "log_probability", "required") \ + .dtype_format(DataType.F32_Default, DataType.I32_Default, DataType.I64_Default, DataType.I64_Default, + DataType.I64_Default, DataType.F32_Default) \ + .dtype_format(DataType.F64_Default, DataType.I32_Default, DataType.I64_Default, DataType.I64_Default, + DataType.I64_Default, DataType.F64_Default) \ + .dtype_format(DataType.F32_NCHW, DataType.I32_NCHW, DataType.I64_NCHW, DataType.I64_NCHW, + DataType.I64_NCHW, DataType.F32_NCHW) \ + .dtype_format(DataType.F64_NCHW, DataType.I32_NCHW, DataType.I64_NCHW, DataType.I64_NCHW, + DataType.I64_NCHW, DataType.F64_NCHW) \ + .get_op_info() + +@op_info_register(ctc_greedy_decoder_op_info) +def _ctc_greedy_decoder_aicpu(): + """CTCGreedyDecoder AiCPU register""" + return diff --git a/tests/st/ops/ascend/test_aicpu_ops/test_ctc_greedy_decoder.py b/tests/st/ops/ascend/test_aicpu_ops/test_ctc_greedy_decoder.py new file mode 100644 index 00000000000..ae150a9e86d --- /dev/null +++ b/tests/st/ops/ascend/test_aicpu_ops/test_ctc_greedy_decoder.py @@ -0,0 +1,59 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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 numpy as np + +import mindspore.context as context +import mindspore.nn as nn +from mindspore import Tensor +from mindspore.ops import operations as P + +context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") + + +class Net(nn.Cell): + def __init__(self): + super(Net, self).__init__() + self.ctc = P.CTCGreedyDecoder() + + def construct(self, inputs, sequence_length): + return self.ctc(inputs, sequence_length) + + +def test_net_float32(): + x = np.random.randn(2, 2, 3).astype(np.float32) + sequence_length = np.array([2, 2]).astype(np.int32) + net = Net() + output = net(Tensor(x), Tensor(sequence_length)) + print(output) + + +def test_net_assert(): + x = np.array([[[0.44662005, 0.41900548, -0.8334965], + [-0.28560895, -0.03626213, -0.04149306]], + [[-0.70390207, 0.2977548, -0.4097819], + [-0.6942656, -0.14625494, -0.90554816]]]).astype(np.float32) + sequence_length = np.array([2, 2]).astype(np.int32) + net = Net() + output = net(Tensor(x), Tensor(sequence_length)) + print(output) + + out_expect0 = np.array([0, 0, 0, 1, 1, 0]).reshape(3, 2) + out_expect1 = np.array([0, 1, 1]) + out_expect2 = np.array([2, 2]) + out_expect3 = np.array([-0.7443749, 0.18251707]).reshape(2, 1) + assert np.array_equal(output[0].asnumpy(), out_expect0) + assert np.array_equal(output[1].asnumpy(), out_expect1) + assert np.array_equal(output[2].asnumpy(), out_expect2) + assert np.array_equal(output[3].asnumpy(), out_expect3)