diff --git a/fluid/DeepASR/data_utils/util.py b/fluid/DeepASR/data_utils/util.py index 4255e7537e14d899225ee2ca200257bf6751ea08..e8ccbadc0bf2106ccabd73a449eb5e53983ccf95 100644 --- a/fluid/DeepASR/data_utils/util.py +++ b/fluid/DeepASR/data_utils/util.py @@ -50,6 +50,13 @@ def batch_to_ndarray(batch_samples, lod): return (batch_feature, batch_label) +def split_infer_result(infer_seq, lod): + infer_batch = [] + for i in xrange(0, len(lod[0]) - 1): + infer_batch.append(infer_seq[lod[0][i]:lod[0][i + 1]]) + return infer_batch + + class DaemonProcessGroup(object): def __init__(self, proc_num, target, args): self._proc_num = proc_num diff --git a/fluid/DeepASR/decoder/decoder.cc b/fluid/DeepASR/decoder/decoder.cc new file mode 100644 index 0000000000000000000000000000000000000000..a99f972e2fc2341247eb2a6aa564d8d6b5e2905d --- /dev/null +++ b/fluid/DeepASR/decoder/decoder.cc @@ -0,0 +1,21 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. + +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 "decoder.h" + +std::string decode(std::vector> probs_mat) { + // Add decoding logic here + + return "example decoding result"; +} diff --git a/fluid/DeepASR/decoder/decoder.h b/fluid/DeepASR/decoder/decoder.h new file mode 100644 index 0000000000000000000000000000000000000000..4a67fa366ae31dd393d0e3b2f04d27e360596fe5 --- /dev/null +++ b/fluid/DeepASR/decoder/decoder.h @@ -0,0 +1,18 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. + +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 +#include + +std::string decode(std::vector> probs_mat); diff --git a/fluid/DeepASR/decoder/pybind.cc b/fluid/DeepASR/decoder/pybind.cc new file mode 100644 index 0000000000000000000000000000000000000000..8cd65903eae63268d9f4412bd737162c639d8910 --- /dev/null +++ b/fluid/DeepASR/decoder/pybind.cc @@ -0,0 +1,29 @@ +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. + +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 +#include + +#include "decoder.h" + +namespace py = pybind11; + +PYBIND11_MODULE(decoder, m) { + m.doc() = "Decode function for Deep ASR model"; + + m.def("decode", + &decode, + "Decode one input probability matrix " + "and return the transcription"); +} diff --git a/fluid/DeepASR/decoder/setup.py b/fluid/DeepASR/decoder/setup.py new file mode 100644 index 0000000000000000000000000000000000000000..cedd5d644e0dc1ca8855ab7e75ee1b7a30f8fcb1 --- /dev/null +++ b/fluid/DeepASR/decoder/setup.py @@ -0,0 +1,41 @@ +# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. +# +# 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 os +from distutils.core import setup, Extension +from distutils.sysconfig import get_config_vars + +args = ['-std=c++11'] + +# remove warning about -Wstrict-prototypes +(opt, ) = get_config_vars('OPT') +os.environ['OPT'] = " ".join(flag for flag in opt.split() + if flag != '-Wstrict-prototypes') + +ext_modules = [ + Extension( + 'decoder', + ['pybind.cc', 'decoder.cc'], + include_dirs=['pybind11/include', '.'], + language='c++', + extra_compile_args=args, ), +] + +setup( + name='decoder', + version='0.0.1', + author='Paddle', + author_email='', + description='Decoder for Deep ASR model', + ext_modules=ext_modules, ) diff --git a/fluid/DeepASR/decoder/setup.sh b/fluid/DeepASR/decoder/setup.sh new file mode 100644 index 0000000000000000000000000000000000000000..71fd6626efe1b7cf72a1e678ab7b74000ebfb8c3 --- /dev/null +++ b/fluid/DeepASR/decoder/setup.sh @@ -0,0 +1,7 @@ + + +if [ ! -d pybind11 ]; then + git clone https://github.com/pybind/pybind11.git +fi + +python setup.py build_ext -i diff --git a/fluid/DeepASR/infer.py b/fluid/DeepASR/infer.py index fa0c3382d6dbc2f8d6eb443f08ab75f9fc2d6756..babcb416ea884081ae249a8d1dc177f85cf1c9ba 100644 --- a/fluid/DeepASR/infer.py +++ b/fluid/DeepASR/infer.py @@ -10,6 +10,7 @@ import data_utils.augmentor.trans_add_delta as trans_add_delta import data_utils.augmentor.trans_splice as trans_splice import data_utils.data_reader as reader from data_utils.util import lodtensor_to_ndarray +from data_utils.util import split_infer_result def parse_args(): @@ -58,13 +59,6 @@ def print_arguments(args): print('------------------------------------------------') -def split_infer_result(infer_seq, lod): - infer_batch = [] - for i in xrange(0, len(lod[0]) - 1): - infer_batch.append(infer_seq[lod[0][i]:lod[0][i + 1]]) - return infer_batch - - def infer(args): """ Gets one batch of feature data and predicts labels for each sample. """ diff --git a/fluid/DeepASR/infer_by_ckpt.py b/fluid/DeepASR/infer_by_ckpt.py index 1355af89cf1bbf40baa0cd54be9d9da76e3b9e06..68dd573647d498704fd22f70a7df2255e7ac66cd 100644 --- a/fluid/DeepASR/infer_by_ckpt.py +++ b/fluid/DeepASR/infer_by_ckpt.py @@ -13,8 +13,10 @@ import data_utils.augmentor.trans_mean_variance_norm as trans_mean_variance_norm import data_utils.augmentor.trans_add_delta as trans_add_delta import data_utils.augmentor.trans_splice as trans_splice import data_utils.async_data_reader as reader +import decoder.decoder as decoder from data_utils.util import lodtensor_to_ndarray from model_utils.model import stacked_lstmp_model +from data_utils.util import split_infer_result def parse_args(): @@ -141,13 +143,20 @@ def infer_from_ckpt(args): infer_data_reader.recycle(features, labels, lod) - cost, acc = exe.run(infer_program, - feed={"feature": feature_t, - "label": label_t}, - fetch_list=[avg_cost, accuracy], - return_numpy=False) - infer_costs.append(lodtensor_to_ndarray(cost)[0]) - infer_accs.append(lodtensor_to_ndarray(acc)[0]) + results = exe.run(infer_program, + feed={"feature": feature_t, + "label": label_t}, + fetch_list=[prediction, avg_cost, accuracy], + return_numpy=False) + infer_costs.append(lodtensor_to_ndarray(results[1])[0]) + infer_accs.append(lodtensor_to_ndarray(results[2])[0]) + + probs, lod = lodtensor_to_ndarray(results[0]) + infer_batch = split_infer_result(probs, lod) + for index, sample in enumerate(infer_batch): + print("Decoding %d: " % (batch_id * args.batch_size + index), + decoder.decode(sample)) + print(np.mean(infer_costs), np.mean(infer_accs))