Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
dd3cf81b
M
models
项目概览
PaddlePaddle
/
models
1 年多 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
dd3cf81b
编写于
8月 09, 2017
作者:
Y
Yang yaming
提交者:
GitHub
8月 09, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #190 from pkuyym/unify_encoding
Unify encoding to 'utf-8' and optimize error rate calculation.
上级
7310baa8
2198e9c1
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
119 addition
and
47 deletion
+119
-47
deep_speech_2/data_utils/data.py
deep_speech_2/data_utils/data.py
+4
-4
deep_speech_2/data_utils/featurizer/text_featurizer.py
deep_speech_2/data_utils/featurizer/text_featurizer.py
+2
-1
deep_speech_2/data_utils/utils.py
deep_speech_2/data_utils/utils.py
+4
-3
deep_speech_2/datasets/librispeech/librispeech.py
deep_speech_2/datasets/librispeech/librispeech.py
+2
-1
deep_speech_2/error_rate.py
deep_speech_2/error_rate.py
+46
-33
deep_speech_2/tests/test_error_rate.py
deep_speech_2/tests/test_error_rate.py
+61
-5
未找到文件。
deep_speech_2/data_utils/data.py
浏览文件 @
dd3cf81b
...
...
@@ -91,7 +91,7 @@ class DataGenerator(object):
:param transcript: Transcription text.
:type transcript: basestring
:return: Tuple of audio feature tensor and list of token ids for
transcription.
transcription.
:rtype: tuple of (2darray, list)
"""
speech_segment
=
SpeechSegment
.
from_file
(
filename
,
transcript
)
...
...
@@ -111,7 +111,7 @@ class DataGenerator(object):
"""
Batch data reader creator for audio data. Return a callable generator
function to produce batches of data.
Audio features within one batch will be padded with zeros to have the
same shape, or a user-defined shape.
...
...
@@ -191,9 +191,9 @@ class DataGenerator(object):
@
property
def
feeding
(
self
):
"""Returns data reader's feeding dict.
:return: Data feeding dict.
:rtype: dict
:rtype: dict
"""
return
{
"audio_spectrogram"
:
0
,
"transcript_text"
:
1
}
...
...
deep_speech_2/data_utils/featurizer/text_featurizer.py
浏览文件 @
dd3cf81b
...
...
@@ -4,6 +4,7 @@ from __future__ import division
from
__future__
import
print_function
import
os
import
codecs
class
TextFeaturizer
(
object
):
...
...
@@ -59,7 +60,7 @@ class TextFeaturizer(object):
def
_load_vocabulary_from_file
(
self
,
vocab_filepath
):
"""Load vocabulary from file."""
vocab_lines
=
[]
with
open
(
vocab_filepath
,
'r
'
)
as
file
:
with
codecs
.
open
(
vocab_filepath
,
'r'
,
'utf-8
'
)
as
file
:
vocab_lines
.
extend
(
file
.
readlines
())
vocab_list
=
[
line
[:
-
1
]
for
line
in
vocab_lines
]
vocab_dict
=
dict
(
...
...
deep_speech_2/data_utils/utils.py
浏览文件 @
dd3cf81b
...
...
@@ -4,15 +4,16 @@ from __future__ import division
from
__future__
import
print_function
import
json
import
codecs
def
read_manifest
(
manifest_path
,
max_duration
=
float
(
'inf'
),
min_duration
=
0.0
):
"""Load and parse manifest file.
Instances with durations outside [min_duration, max_duration] will be
filtered out.
:param manifest_path: Manifest file to load and parse.
:param manifest_path: Manifest file to load and parse.
:type manifest_path: basestring
:param max_duration: Maximal duration in seconds for instance filter.
:type max_duration: float
...
...
@@ -23,7 +24,7 @@ def read_manifest(manifest_path, max_duration=float('inf'), min_duration=0.0):
:raises IOError: If failed to parse the manifest.
"""
manifest
=
[]
for
json_line
in
open
(
manifest_path
):
for
json_line
in
codecs
.
open
(
manifest_path
,
'r'
,
'utf-8'
):
try
:
json_data
=
json
.
loads
(
json_line
)
except
Exception
as
e
:
...
...
deep_speech_2/datasets/librispeech/librispeech.py
浏览文件 @
dd3cf81b
...
...
@@ -16,6 +16,7 @@ import tarfile
import
argparse
import
soundfile
import
json
import
codecs
from
paddle.v2.dataset.common
import
md5file
DATA_HOME
=
os
.
path
.
expanduser
(
'~/.cache/paddle/dataset/speech'
)
...
...
@@ -112,7 +113,7 @@ def create_manifest(data_dir, manifest_path):
'duration'
:
duration
,
'text'
:
text
}))
with
open
(
manifest_path
,
'w
'
)
as
out_file
:
with
codecs
.
open
(
manifest_path
,
'w'
,
'utf-8
'
)
as
out_file
:
for
line
in
json_lines
:
out_file
.
write
(
line
+
'
\n
'
)
...
...
deep_speech_2/error_rate.py
浏览文件 @
dd3cf81b
...
...
@@ -10,47 +10,54 @@ import numpy as np
def
_levenshtein_distance
(
ref
,
hyp
):
"""Levenshtein distance is a string metric for measuring the difference between
two sequences. Informally, the levenshtein disctance is defined as the minimum
number of single-character edits (substitutions, insertions or deletions)
required to change one word into the other. We can naturally extend the edits to
word level when calculate levenshtein disctance for two sentences.
"""Levenshtein distance is a string metric for measuring the difference
between two sequences. Informally, the levenshtein disctance is defined as
the minimum number of single-character edits (substitutions, insertions or
deletions) required to change one word into the other. We can naturally
extend the edits to word level when calculate levenshtein disctance for
two sentences.
"""
ref_len
=
len
(
ref
)
hyp_le
n
=
len
(
hyp
)
m
=
len
(
ref
)
n
=
len
(
hyp
)
# special case
if
ref
==
hyp
:
return
0
if
ref_len
==
0
:
return
hyp_le
n
if
hyp_le
n
==
0
:
return
ref_len
if
m
==
0
:
return
n
if
n
==
0
:
return
m
distance
=
np
.
zeros
((
ref_len
+
1
,
hyp_len
+
1
),
dtype
=
np
.
int32
)
if
m
<
n
:
ref
,
hyp
=
hyp
,
ref
m
,
n
=
n
,
m
# use O(min(m, n)) space
distance
=
np
.
zeros
((
2
,
n
+
1
),
dtype
=
np
.
int32
)
# initialize distance matrix
for
j
in
xrange
(
hyp_le
n
+
1
):
for
j
in
xrange
(
n
+
1
):
distance
[
0
][
j
]
=
j
for
i
in
xrange
(
ref_len
+
1
):
distance
[
i
][
0
]
=
i
# calculate levenshtein distance
for
i
in
xrange
(
1
,
ref_len
+
1
):
for
j
in
xrange
(
1
,
hyp_len
+
1
):
for
i
in
xrange
(
1
,
m
+
1
):
prev_row_idx
=
(
i
-
1
)
%
2
cur_row_idx
=
i
%
2
distance
[
cur_row_idx
][
0
]
=
i
for
j
in
xrange
(
1
,
n
+
1
):
if
ref
[
i
-
1
]
==
hyp
[
j
-
1
]:
distance
[
i
][
j
]
=
distance
[
i
-
1
][
j
-
1
]
distance
[
cur_row_idx
][
j
]
=
distance
[
prev_row_idx
][
j
-
1
]
else
:
s_num
=
distance
[
i
-
1
][
j
-
1
]
+
1
i_num
=
distance
[
i
][
j
-
1
]
+
1
d_num
=
distance
[
i
-
1
][
j
]
+
1
distance
[
i
][
j
]
=
min
(
s_num
,
i_num
,
d_num
)
s_num
=
distance
[
prev_row_idx
][
j
-
1
]
+
1
i_num
=
distance
[
cur_row_idx
][
j
-
1
]
+
1
d_num
=
distance
[
prev_row_idx
][
j
]
+
1
distance
[
cur_row_idx
][
j
]
=
min
(
s_num
,
i_num
,
d_num
)
return
distance
[
ref_len
][
hyp_le
n
]
return
distance
[
m
%
2
][
n
]
def
wer
(
reference
,
hypothesis
,
ignore_case
=
False
,
delimiter
=
' '
):
"""Calculate word error rate (WER). WER compares reference text and
"""Calculate word error rate (WER). WER compares reference text and
hypothesis text in word-level. WER is defined as:
.. math::
...
...
@@ -65,8 +72,8 @@ def wer(reference, hypothesis, ignore_case=False, delimiter=' '):
Iw is the number of words inserted,
Nw is the number of words in the reference
We can use levenshtein distance to calculate WER. Please draw an attention
that
empty items will be removed when splitting sentences by delimiter.
We can use levenshtein distance to calculate WER. Please draw an attention
that
empty items will be removed when splitting sentences by delimiter.
:param reference: The reference sentence.
:type reference: basestring
...
...
@@ -95,7 +102,7 @@ def wer(reference, hypothesis, ignore_case=False, delimiter=' '):
return
wer
def
cer
(
reference
,
hypothesis
,
ignore_case
=
False
):
def
cer
(
reference
,
hypothesis
,
ignore_case
=
False
,
remove_space
=
False
):
"""Calculate charactor error rate (CER). CER compares reference text and
hypothesis text in char-level. CER is defined as:
...
...
@@ -111,10 +118,10 @@ def cer(reference, hypothesis, ignore_case=False):
Ic is the number of characters inserted
Nc is the number of characters in the reference
We can use levenshtein distance to calculate CER. Chinese input should be
encoded to unicode. Please draw an attention that the leading and tailing
white space characters will be truncated and multiple consecutive white
space characters in a sentence will be replaced by one whit
e space character.
We can use levenshtein distance to calculate CER. Chinese input should be
encoded to unicode. Please draw an attention that the leading and tailing
space characters will be truncated and multiple consecutive space
characters in a sentence will be replaced by on
e space character.
:param reference: The reference sentence.
:type reference: basestring
...
...
@@ -122,6 +129,8 @@ def cer(reference, hypothesis, ignore_case=False):
:type hypothesis: basestring
:param ignore_case: Whether case-sensitive or not.
:type ignore_case: bool
:param remove_space: Whether remove internal space characters
:type remove_space: bool
:return: Character error rate.
:rtype: float
:raises ValueError: If the reference length is zero.
...
...
@@ -130,8 +139,12 @@ def cer(reference, hypothesis, ignore_case=False):
reference
=
reference
.
lower
()
hypothesis
=
hypothesis
.
lower
()
reference
=
' '
.
join
(
filter
(
None
,
reference
.
split
(
' '
)))
hypothesis
=
' '
.
join
(
filter
(
None
,
hypothesis
.
split
(
' '
)))
join_char
=
' '
if
remove_space
==
True
:
join_char
=
''
reference
=
join_char
.
join
(
filter
(
None
,
reference
.
split
(
' '
)))
hypothesis
=
join_char
.
join
(
filter
(
None
,
hypothesis
.
split
(
' '
)))
if
len
(
reference
)
==
0
:
raise
ValueError
(
"Length of reference should be greater than 0."
)
...
...
deep_speech_2/tests/test_error_rate.py
浏览文件 @
dd3cf81b
...
...
@@ -11,16 +11,54 @@ import error_rate
class
TestParse
(
unittest
.
TestCase
):
def
test_wer_1
(
self
):
ref
=
'i UM the PHONE IS i LEFT THE portable PHONE UPSTAIRS last night'
hyp
=
'i GOT IT TO the FULLEST i LOVE TO portable FROM OF STORES last night'
hyp
=
'i GOT IT TO the FULLEST i LOVE TO portable FROM OF STORES last '
\
'night'
word_error_rate
=
error_rate
.
wer
(
ref
,
hyp
)
self
.
assertTrue
(
abs
(
word_error_rate
-
0.769230769231
)
<
1e-6
)
def
test_wer_2
(
self
):
ref
=
'as any in england i would say said gamewell proudly that is '
\
'in his day'
hyp
=
'as any in england i would say said came well proudly that is '
\
'in his day'
word_error_rate
=
error_rate
.
wer
(
ref
,
hyp
)
self
.
assertTrue
(
abs
(
word_error_rate
-
0.1333333
)
<
1e-6
)
def
test_wer_3
(
self
):
ref
=
'the lieutenant governor lilburn w boggs afterward governor '
\
'was a pronounced mormon hater and throughout the period of '
\
'the troubles he manifested sympathy with the persecutors'
hyp
=
'the lieutenant governor little bit how bags afterward '
\
'governor was a pronounced warman hater and throughout the '
\
'period of th troubles he manifests sympathy with the '
\
'persecutors'
word_error_rate
=
error_rate
.
wer
(
ref
,
hyp
)
self
.
assertTrue
(
abs
(
word_error_rate
-
0.2692307692
)
<
1e-6
)
def
test_wer_4
(
self
):
ref
=
'the wood flamed up splendidly under the large brewing copper '
\
'and it sighed so deeply'
hyp
=
'the wood flame do splendidly under the large brewing copper '
\
'and its side so deeply'
word_error_rate
=
error_rate
.
wer
(
ref
,
hyp
)
self
.
assertTrue
(
abs
(
word_error_rate
-
0.2666666667
)
<
1e-6
)
def
test_wer_5
(
self
):
ref
=
'all the morning they trudged up the mountain path and at noon '
\
'unc and ojo sat on a fallen tree trunk and ate the last of '
\
'the bread which the old munchkin had placed in his pocket'
hyp
=
'all the morning they trudged up the mountain path and at noon '
\
'unc in ojo sat on a fallen tree trunk and ate the last of '
\
'the bread which the old munchkin had placed in his pocket'
word_error_rate
=
error_rate
.
wer
(
ref
,
hyp
)
self
.
assertTrue
(
abs
(
word_error_rate
-
0.027027027
)
<
1e-6
)
def
test_wer_6
(
self
):
ref
=
'i UM the PHONE IS i LEFT THE portable PHONE UPSTAIRS last night'
word_error_rate
=
error_rate
.
wer
(
ref
,
ref
)
self
.
assertEqual
(
word_error_rate
,
0.0
)
def
test_wer_
3
(
self
):
def
test_wer_
7
(
self
):
ref
=
' '
hyp
=
'Hypothesis sentence'
with
self
.
assertRaises
(
ValueError
):
...
...
@@ -33,22 +71,40 @@ class TestParse(unittest.TestCase):
self
.
assertTrue
(
abs
(
char_error_rate
-
0.25
)
<
1e-6
)
def
test_cer_2
(
self
):
ref
=
'werewolf'
hyp
=
'weae wolf'
char_error_rate
=
error_rate
.
cer
(
ref
,
hyp
,
remove_space
=
True
)
self
.
assertTrue
(
abs
(
char_error_rate
-
0.125
)
<
1e-6
)
def
test_cer_3
(
self
):
ref
=
'were wolf'
hyp
=
'were wolf'
char_error_rate
=
error_rate
.
cer
(
ref
,
hyp
)
self
.
assertTrue
(
abs
(
char_error_rate
-
0.0
)
<
1e-6
)
def
test_cer_4
(
self
):
ref
=
'werewolf'
char_error_rate
=
error_rate
.
cer
(
ref
,
ref
)
self
.
assertEqual
(
char_error_rate
,
0.0
)
def
test_cer_
3
(
self
):
def
test_cer_
5
(
self
):
ref
=
u
'我是中国人'
hyp
=
u
'我是 美洲人'
char_error_rate
=
error_rate
.
cer
(
ref
,
hyp
)
self
.
assertTrue
(
abs
(
char_error_rate
-
0.6
)
<
1e-6
)
def
test_cer_4
(
self
):
def
test_cer_6
(
self
):
ref
=
u
'我 是 中 国 人'
hyp
=
u
'我 是 美 洲 人'
char_error_rate
=
error_rate
.
cer
(
ref
,
hyp
,
remove_space
=
True
)
self
.
assertTrue
(
abs
(
char_error_rate
-
0.4
)
<
1e-6
)
def
test_cer_7
(
self
):
ref
=
u
'我是中国人'
char_error_rate
=
error_rate
.
cer
(
ref
,
ref
)
self
.
assertFalse
(
char_error_rate
,
0.0
)
def
test_cer_
5
(
self
):
def
test_cer_
8
(
self
):
ref
=
''
hyp
=
'Hypothesis'
with
self
.
assertRaises
(
ValueError
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录