Skip to content

Commit d224397

Browse files
author
Yibing Liu
authored
Merge pull request #736 from kuke/decoder_wrapper
Add pybind11 wrapper for decoder
2 parents 774fd4b + 454de6c commit d224397

File tree

8 files changed

+140
-14
lines changed

8 files changed

+140
-14
lines changed

fluid/DeepASR/data_utils/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ def batch_to_ndarray(batch_samples, lod):
5050
return (batch_feature, batch_label)
5151

5252

53+
def split_infer_result(infer_seq, lod):
54+
infer_batch = []
55+
for i in xrange(0, len(lod[0]) - 1):
56+
infer_batch.append(infer_seq[lod[0][i]:lod[0][i + 1]])
57+
return infer_batch
58+
59+
5360
class DaemonProcessGroup(object):
5461
def __init__(self, proc_num, target, args):
5562
self._proc_num = proc_num

fluid/DeepASR/decoder/decoder.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* Copyright (c) 2016 PaddlePaddle 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+
#include "decoder.h"
16+
17+
std::string decode(std::vector<std::vector<float>> probs_mat) {
18+
// Add decoding logic here
19+
20+
return "example decoding result";
21+
}

fluid/DeepASR/decoder/decoder.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright (c) 2016 PaddlePaddle 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+
#include <string>
16+
#include <vector>
17+
18+
std::string decode(std::vector<std::vector<float>> probs_mat);

fluid/DeepASR/decoder/pybind.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Copyright (c) 2016 PaddlePaddle 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+
#include <pybind11/pybind11.h>
16+
#include <pybind11/stl.h>
17+
18+
#include "decoder.h"
19+
20+
namespace py = pybind11;
21+
22+
PYBIND11_MODULE(decoder, m) {
23+
m.doc() = "Decode function for Deep ASR model";
24+
25+
m.def("decode",
26+
&decode,
27+
"Decode one input probability matrix "
28+
"and return the transcription");
29+
}

fluid/DeepASR/decoder/setup.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2016 PaddlePaddle 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+
import os
16+
from distutils.core import setup, Extension
17+
from distutils.sysconfig import get_config_vars
18+
19+
args = ['-std=c++11']
20+
21+
# remove warning about -Wstrict-prototypes
22+
(opt, ) = get_config_vars('OPT')
23+
os.environ['OPT'] = " ".join(flag for flag in opt.split()
24+
if flag != '-Wstrict-prototypes')
25+
26+
ext_modules = [
27+
Extension(
28+
'decoder',
29+
['pybind.cc', 'decoder.cc'],
30+
include_dirs=['pybind11/include', '.'],
31+
language='c++',
32+
extra_compile_args=args, ),
33+
]
34+
35+
setup(
36+
name='decoder',
37+
version='0.0.1',
38+
author='Paddle',
39+
author_email='',
40+
description='Decoder for Deep ASR model',
41+
ext_modules=ext_modules, )

fluid/DeepASR/decoder/setup.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
if [ ! -d pybind11 ]; then
4+
git clone https://github.com/pybind/pybind11.git
5+
fi
6+
7+
python setup.py build_ext -i

fluid/DeepASR/infer.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import data_utils.augmentor.trans_splice as trans_splice
1111
import data_utils.data_reader as reader
1212
from data_utils.util import lodtensor_to_ndarray
13+
from data_utils.util import split_infer_result
1314

1415

1516
def parse_args():
@@ -58,13 +59,6 @@ def print_arguments(args):
5859
print('------------------------------------------------')
5960

6061

61-
def split_infer_result(infer_seq, lod):
62-
infer_batch = []
63-
for i in xrange(0, len(lod[0]) - 1):
64-
infer_batch.append(infer_seq[lod[0][i]:lod[0][i + 1]])
65-
return infer_batch
66-
67-
6862
def infer(args):
6963
""" Gets one batch of feature data and predicts labels for each sample.
7064
"""

fluid/DeepASR/infer_by_ckpt.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import data_utils.augmentor.trans_add_delta as trans_add_delta
1414
import data_utils.augmentor.trans_splice as trans_splice
1515
import data_utils.async_data_reader as reader
16+
import decoder.decoder as decoder
1617
from data_utils.util import lodtensor_to_ndarray
1718
from model_utils.model import stacked_lstmp_model
19+
from data_utils.util import split_infer_result
1820

1921

2022
def parse_args():
@@ -141,13 +143,20 @@ def infer_from_ckpt(args):
141143

142144
infer_data_reader.recycle(features, labels, lod)
143145

144-
cost, acc = exe.run(infer_program,
145-
feed={"feature": feature_t,
146-
"label": label_t},
147-
fetch_list=[avg_cost, accuracy],
148-
return_numpy=False)
149-
infer_costs.append(lodtensor_to_ndarray(cost)[0])
150-
infer_accs.append(lodtensor_to_ndarray(acc)[0])
146+
results = exe.run(infer_program,
147+
feed={"feature": feature_t,
148+
"label": label_t},
149+
fetch_list=[prediction, avg_cost, accuracy],
150+
return_numpy=False)
151+
infer_costs.append(lodtensor_to_ndarray(results[1])[0])
152+
infer_accs.append(lodtensor_to_ndarray(results[2])[0])
153+
154+
probs, lod = lodtensor_to_ndarray(results[0])
155+
infer_batch = split_infer_result(probs, lod)
156+
for index, sample in enumerate(infer_batch):
157+
print("Decoding %d: " % (batch_id * args.batch_size + index),
158+
decoder.decode(sample))
159+
151160
print(np.mean(infer_costs), np.mean(infer_accs))
152161

153162

0 commit comments

Comments
 (0)