Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
dd064724
D
DeepSpeech
项目概览
PaddlePaddle
/
DeepSpeech
大约 2 年 前同步成功
通知
210
Star
8425
Fork
1598
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
245
列表
看板
标记
里程碑
合并请求
3
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DeepSpeech
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
245
Issue
245
列表
看板
标记
里程碑
合并请求
3
合并请求
3
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
dd064724
编写于
10月 09, 2021
作者:
H
Hui Zhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
extract ctc_align into ctc_utils
上级
8b45c3e6
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
100 addition
and
181 deletion
+100
-181
deepspeech/exps/deepspeech2/model.py
deepspeech/exps/deepspeech2/model.py
+1
-1
deepspeech/exps/u2/model.py
deepspeech/exps/u2/model.py
+5
-60
deepspeech/exps/u2_kaldi/model.py
deepspeech/exps/u2_kaldi/model.py
+5
-60
deepspeech/exps/u2_st/model.py
deepspeech/exps/u2_st/model.py
+5
-60
deepspeech/utils/ctc_utils.py
deepspeech/utils/ctc_utils.py
+84
-0
未找到文件。
deepspeech/exps/deepspeech2/model.py
浏览文件 @
dd064724
...
@@ -16,7 +16,6 @@ import os
...
@@ -16,7 +16,6 @@ import os
import
time
import
time
from
collections
import
defaultdict
from
collections
import
defaultdict
from
contextlib
import
nullcontext
from
contextlib
import
nullcontext
from
pathlib
import
Path
from
typing
import
Optional
from
typing
import
Optional
import
jsonlines
import
jsonlines
...
@@ -386,6 +385,7 @@ class DeepSpeech2Tester(DeepSpeech2Trainer):
...
@@ -386,6 +385,7 @@ class DeepSpeech2Tester(DeepSpeech2Trainer):
logger
.
info
(
msg
)
logger
.
info
(
msg
)
self
.
autolog
.
report
()
self
.
autolog
.
report
()
@
paddle
.
no_grad
()
def
export
(
self
):
def
export
(
self
):
if
self
.
args
.
model_type
==
'offline'
:
if
self
.
args
.
model_type
==
'offline'
:
infer_model
=
DeepSpeech2InferModel
.
from_pretrained
(
infer_model
=
DeepSpeech2InferModel
.
from_pretrained
(
...
...
deepspeech/exps/u2/model.py
浏览文件 @
dd064724
...
@@ -14,12 +14,10 @@
...
@@ -14,12 +14,10 @@
"""Contains U2 model."""
"""Contains U2 model."""
import
json
import
json
import
os
import
os
import
sys
import
time
import
time
from
collections
import
defaultdict
from
collections
import
defaultdict
from
collections
import
OrderedDict
from
collections
import
OrderedDict
from
contextlib
import
nullcontext
from
contextlib
import
nullcontext
from
pathlib
import
Path
from
typing
import
Optional
from
typing
import
Optional
import
jsonlines
import
jsonlines
...
@@ -44,8 +42,6 @@ from deepspeech.utils import ctc_utils
...
@@ -44,8 +42,6 @@ from deepspeech.utils import ctc_utils
from
deepspeech.utils
import
error_rate
from
deepspeech.utils
import
error_rate
from
deepspeech.utils
import
layer_tools
from
deepspeech.utils
import
layer_tools
from
deepspeech.utils
import
mp_tools
from
deepspeech.utils
import
mp_tools
from
deepspeech.utils
import
text_grid
from
deepspeech.utils
import
utility
from
deepspeech.utils.log
import
Log
from
deepspeech.utils.log
import
Log
from
deepspeech.utils.utility
import
UpdateConfig
from
deepspeech.utils.utility
import
UpdateConfig
...
@@ -553,62 +549,10 @@ class U2Tester(U2Trainer):
...
@@ -553,62 +549,10 @@ class U2Tester(U2Trainer):
@
paddle
.
no_grad
()
@
paddle
.
no_grad
()
def
align
(
self
):
def
align
(
self
):
if
self
.
config
.
decoding
.
batch_size
>
1
:
ctc_utils
.
ctc_align
(
logger
.
fatal
(
'alignment mode must be running with batch_size == 1'
)
self
.
model
,
self
.
align_loader
,
self
.
config
.
decoding
.
batch_size
,
sys
.
exit
(
1
)
self
.
align_loader
.
collate_fn
.
stride_ms
,
self
.
align_loader
.
collate_fn
.
vocab_list
,
self
.
args
.
result_file
)
# xxx.align
assert
self
.
args
.
result_file
and
self
.
args
.
result_file
.
endswith
(
'.align'
)
self
.
model
.
eval
()
logger
.
info
(
f
"Align Total Examples:
{
len
(
self
.
align_loader
.
dataset
)
}
"
)
stride_ms
=
self
.
align_loader
.
collate_fn
.
stride_ms
token_dict
=
self
.
align_loader
.
collate_fn
.
vocab_list
with
open
(
self
.
args
.
result_file
,
'w'
)
as
fout
:
# one example in batch
for
i
,
batch
in
enumerate
(
self
.
align_loader
):
key
,
feat
,
feats_length
,
target
,
target_length
=
batch
# 1. Encoder
encoder_out
,
encoder_mask
=
self
.
model
.
_forward_encoder
(
feat
,
feats_length
)
# (B, maxlen, encoder_dim)
maxlen
=
encoder_out
.
shape
[
1
]
ctc_probs
=
self
.
model
.
ctc
.
log_softmax
(
encoder_out
)
# (1, maxlen, vocab_size)
# 2. alignment
ctc_probs
=
ctc_probs
.
squeeze
(
0
)
target
=
target
.
squeeze
(
0
)
alignment
=
ctc_utils
.
forced_align
(
ctc_probs
,
target
)
logger
.
info
(
f
"align ids:
{
key
[
0
]
}
{
alignment
}
"
)
fout
.
write
(
'{} {}
\n
'
.
format
(
key
[
0
],
alignment
))
# 3. gen praat
# segment alignment
align_segs
=
text_grid
.
segment_alignment
(
alignment
)
logger
.
info
(
f
"align tokens:
{
key
[
0
]
}
,
{
align_segs
}
"
)
# IntervalTier, List["start end token\n"]
subsample
=
utility
.
get_subsample
(
self
.
config
)
tierformat
=
text_grid
.
align_to_tierformat
(
align_segs
,
subsample
,
token_dict
)
# write tier
align_output_path
=
Path
(
self
.
args
.
result_file
).
parent
/
"align"
align_output_path
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
tier_path
=
align_output_path
/
(
key
[
0
]
+
".tier"
)
with
tier_path
.
open
(
'w'
)
as
f
:
f
.
writelines
(
tierformat
)
# write textgrid
textgrid_path
=
align_output_path
/
(
key
[
0
]
+
".TextGrid"
)
second_per_frame
=
1.
/
(
1000.
/
stride_ms
)
# 25ms window, 10ms stride
second_per_example
=
(
len
(
alignment
)
+
1
)
*
subsample
*
second_per_frame
text_grid
.
generate_textgrid
(
maxtime
=
second_per_example
,
intervals
=
tierformat
,
output
=
str
(
textgrid_path
))
def
load_inferspec
(
self
):
def
load_inferspec
(
self
):
"""infer model and input spec.
"""infer model and input spec.
...
@@ -630,6 +574,7 @@ class U2Tester(U2Trainer):
...
@@ -630,6 +574,7 @@ class U2Tester(U2Trainer):
]
]
return
infer_model
,
input_spec
return
infer_model
,
input_spec
@
paddle
.
no_grad
()
def
export
(
self
):
def
export
(
self
):
infer_model
,
input_spec
=
self
.
load_inferspec
()
infer_model
,
input_spec
=
self
.
load_inferspec
()
assert
isinstance
(
input_spec
,
list
),
type
(
input_spec
)
assert
isinstance
(
input_spec
,
list
),
type
(
input_spec
)
...
...
deepspeech/exps/u2_kaldi/model.py
浏览文件 @
dd064724
...
@@ -14,11 +14,9 @@
...
@@ -14,11 +14,9 @@
"""Contains U2 model."""
"""Contains U2 model."""
import
json
import
json
import
os
import
os
import
sys
import
time
import
time
from
collections
import
defaultdict
from
collections
import
defaultdict
from
contextlib
import
nullcontext
from
contextlib
import
nullcontext
from
pathlib
import
Path
from
typing
import
Optional
from
typing
import
Optional
import
jsonlines
import
jsonlines
...
@@ -39,8 +37,6 @@ from deepspeech.utils import ctc_utils
...
@@ -39,8 +37,6 @@ from deepspeech.utils import ctc_utils
from
deepspeech.utils
import
error_rate
from
deepspeech.utils
import
error_rate
from
deepspeech.utils
import
layer_tools
from
deepspeech.utils
import
layer_tools
from
deepspeech.utils
import
mp_tools
from
deepspeech.utils
import
mp_tools
from
deepspeech.utils
import
text_grid
from
deepspeech.utils
import
utility
from
deepspeech.utils.log
import
Log
from
deepspeech.utils.log
import
Log
from
deepspeech.utils.utility
import
UpdateConfig
from
deepspeech.utils.utility
import
UpdateConfig
...
@@ -527,62 +523,10 @@ class U2Tester(U2Trainer):
...
@@ -527,62 +523,10 @@ class U2Tester(U2Trainer):
@
paddle
.
no_grad
()
@
paddle
.
no_grad
()
def
align
(
self
):
def
align
(
self
):
if
self
.
config
.
decoding
.
batch_size
>
1
:
ctc_utils
.
ctc_align
(
logger
.
fatal
(
'alignment mode must be running with batch_size == 1'
)
self
.
model
,
self
.
align_loader
,
self
.
config
.
decoding
.
batch_size
,
sys
.
exit
(
1
)
self
.
align_loader
.
collate_fn
.
stride_ms
,
self
.
align_loader
.
collate_fn
.
vocab_list
,
self
.
args
.
result_file
)
# xxx.align
assert
self
.
args
.
result_file
and
self
.
args
.
result_file
.
endswith
(
'.align'
)
self
.
model
.
eval
()
logger
.
info
(
f
"Align Total Examples:
{
len
(
self
.
align_loader
.
dataset
)
}
"
)
stride_ms
=
self
.
align_loader
.
collate_fn
.
stride_ms
token_dict
=
self
.
align_loader
.
collate_fn
.
vocab_list
with
open
(
self
.
args
.
result_file
,
'w'
)
as
fout
:
# one example in batch
for
i
,
batch
in
enumerate
(
self
.
align_loader
):
key
,
feat
,
feats_length
,
target
,
target_length
=
batch
# 1. Encoder
encoder_out
,
encoder_mask
=
self
.
model
.
_forward_encoder
(
feat
,
feats_length
)
# (B, maxlen, encoder_dim)
maxlen
=
encoder_out
.
shape
[
1
]
ctc_probs
=
self
.
model
.
ctc
.
log_softmax
(
encoder_out
)
# (1, maxlen, vocab_size)
# 2. alignment
ctc_probs
=
ctc_probs
.
squeeze
(
0
)
target
=
target
.
squeeze
(
0
)
alignment
=
ctc_utils
.
forced_align
(
ctc_probs
,
target
)
logger
.
info
(
f
"align ids:
{
key
[
0
]
}
{
alignment
}
"
)
fout
.
write
(
'{} {}
\n
'
.
format
(
key
[
0
],
alignment
))
# 3. gen praat
# segment alignment
align_segs
=
text_grid
.
segment_alignment
(
alignment
)
logger
.
info
(
f
"align tokens:
{
key
[
0
]
}
,
{
align_segs
}
"
)
# IntervalTier, List["start end token\n"]
subsample
=
utility
.
get_subsample
(
self
.
config
)
tierformat
=
text_grid
.
align_to_tierformat
(
align_segs
,
subsample
,
token_dict
)
# write tier
align_output_path
=
Path
(
self
.
args
.
result_file
).
parent
/
"align"
align_output_path
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
tier_path
=
align_output_path
/
(
key
[
0
]
+
".tier"
)
with
tier_path
.
open
(
'w'
)
as
f
:
f
.
writelines
(
tierformat
)
# write textgrid
textgrid_path
=
align_output_path
/
(
key
[
0
]
+
".TextGrid"
)
second_per_frame
=
1.
/
(
1000.
/
stride_ms
)
# 25ms window, 10ms stride
second_per_example
=
(
len
(
alignment
)
+
1
)
*
subsample
*
second_per_frame
text_grid
.
generate_textgrid
(
maxtime
=
second_per_example
,
intervals
=
tierformat
,
output
=
str
(
textgrid_path
))
def
load_inferspec
(
self
):
def
load_inferspec
(
self
):
"""infer model and input spec.
"""infer model and input spec.
...
@@ -604,6 +548,7 @@ class U2Tester(U2Trainer):
...
@@ -604,6 +548,7 @@ class U2Tester(U2Trainer):
]
]
return
infer_model
,
input_spec
return
infer_model
,
input_spec
@
paddle
.
no_grad
()
def
export
(
self
):
def
export
(
self
):
infer_model
,
input_spec
=
self
.
load_inferspec
()
infer_model
,
input_spec
=
self
.
load_inferspec
()
assert
isinstance
(
input_spec
,
list
),
type
(
input_spec
)
assert
isinstance
(
input_spec
,
list
),
type
(
input_spec
)
...
...
deepspeech/exps/u2_st/model.py
浏览文件 @
dd064724
...
@@ -14,11 +14,9 @@
...
@@ -14,11 +14,9 @@
"""Contains U2 model."""
"""Contains U2 model."""
import
json
import
json
import
os
import
os
import
sys
import
time
import
time
from
collections
import
defaultdict
from
collections
import
defaultdict
from
contextlib
import
nullcontext
from
contextlib
import
nullcontext
from
pathlib
import
Path
from
typing
import
Optional
from
typing
import
Optional
import
jsonlines
import
jsonlines
...
@@ -42,8 +40,6 @@ from deepspeech.utils import bleu_score
...
@@ -42,8 +40,6 @@ from deepspeech.utils import bleu_score
from
deepspeech.utils
import
ctc_utils
from
deepspeech.utils
import
ctc_utils
from
deepspeech.utils
import
layer_tools
from
deepspeech.utils
import
layer_tools
from
deepspeech.utils
import
mp_tools
from
deepspeech.utils
import
mp_tools
from
deepspeech.utils
import
text_grid
from
deepspeech.utils
import
utility
from
deepspeech.utils.log
import
Log
from
deepspeech.utils.log
import
Log
from
deepspeech.utils.utility
import
UpdateConfig
from
deepspeech.utils.utility
import
UpdateConfig
...
@@ -547,62 +543,10 @@ class U2STTester(U2STTrainer):
...
@@ -547,62 +543,10 @@ class U2STTester(U2STTrainer):
@
paddle
.
no_grad
()
@
paddle
.
no_grad
()
def
align
(
self
):
def
align
(
self
):
if
self
.
config
.
decoding
.
batch_size
>
1
:
ctc_utils
.
ctc_align
(
logger
.
fatal
(
'alignment mode must be running with batch_size == 1'
)
self
.
model
,
self
.
align_loader
,
self
.
config
.
decoding
.
batch_size
,
sys
.
exit
(
1
)
self
.
align_loader
.
collate_fn
.
stride_ms
,
self
.
align_loader
.
collate_fn
.
vocab_list
,
self
.
args
.
result_file
)
# xxx.align
assert
self
.
args
.
result_file
and
self
.
args
.
result_file
.
endswith
(
'.align'
)
self
.
model
.
eval
()
logger
.
info
(
f
"Align Total Examples:
{
len
(
self
.
align_loader
.
dataset
)
}
"
)
stride_ms
=
self
.
align_loader
.
collate_fn
.
stride_ms
token_dict
=
self
.
align_loader
.
collate_fn
.
vocab_list
with
open
(
self
.
args
.
result_file
,
'w'
)
as
fout
:
# one example in batch
for
i
,
batch
in
enumerate
(
self
.
align_loader
):
key
,
feat
,
feats_length
,
target
,
target_length
=
batch
# 1. Encoder
encoder_out
,
encoder_mask
=
self
.
model
.
_forward_encoder
(
feat
,
feats_length
)
# (B, maxlen, encoder_dim)
maxlen
=
encoder_out
.
shape
[
1
]
ctc_probs
=
self
.
model
.
ctc
.
log_softmax
(
encoder_out
)
# (1, maxlen, vocab_size)
# 2. alignment
ctc_probs
=
ctc_probs
.
squeeze
(
0
)
target
=
target
.
squeeze
(
0
)
alignment
=
ctc_utils
.
forced_align
(
ctc_probs
,
target
)
logger
.
info
(
f
"align ids:
{
key
[
0
]
}
{
alignment
}
"
)
fout
.
write
(
'{} {}
\n
'
.
format
(
key
[
0
],
alignment
))
# 3. gen praat
# segment alignment
align_segs
=
text_grid
.
segment_alignment
(
alignment
)
logger
.
info
(
f
"align tokens:
{
key
[
0
]
}
,
{
align_segs
}
"
)
# IntervalTier, List["start end token\n"]
subsample
=
utility
.
get_subsample
(
self
.
config
)
tierformat
=
text_grid
.
align_to_tierformat
(
align_segs
,
subsample
,
token_dict
)
# write tier
align_output_path
=
Path
(
self
.
args
.
result_file
).
parent
/
"align"
align_output_path
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
tier_path
=
align_output_path
/
(
key
[
0
]
+
".tier"
)
with
tier_path
.
open
(
'w'
)
as
f
:
f
.
writelines
(
tierformat
)
# write textgrid
textgrid_path
=
align_output_path
/
(
key
[
0
]
+
".TextGrid"
)
second_per_frame
=
1.
/
(
1000.
/
stride_ms
)
# 25ms window, 10ms stride
second_per_example
=
(
len
(
alignment
)
+
1
)
*
subsample
*
second_per_frame
text_grid
.
generate_textgrid
(
maxtime
=
second_per_example
,
intervals
=
tierformat
,
output
=
str
(
textgrid_path
))
def
load_inferspec
(
self
):
def
load_inferspec
(
self
):
"""infer model and input spec.
"""infer model and input spec.
...
@@ -624,6 +568,7 @@ class U2STTester(U2STTrainer):
...
@@ -624,6 +568,7 @@ class U2STTester(U2STTrainer):
]
]
return
infer_model
,
input_spec
return
infer_model
,
input_spec
@
paddle
.
no_grad
()
def
export
(
self
):
def
export
(
self
):
infer_model
,
input_spec
=
self
.
load_inferspec
()
infer_model
,
input_spec
=
self
.
load_inferspec
()
assert
isinstance
(
input_spec
,
list
),
type
(
input_spec
)
assert
isinstance
(
input_spec
,
list
),
type
(
input_spec
)
...
...
deepspeech/utils/ctc_utils.py
浏览文件 @
dd064724
...
@@ -16,6 +16,8 @@ from typing import List
...
@@ -16,6 +16,8 @@ from typing import List
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
from
deepspeech.utils
import
text_grid
from
deepspeech.utils
import
utility
from
deepspeech.utils.log
import
Log
from
deepspeech.utils.log
import
Log
logger
=
Log
(
__name__
).
getlog
()
logger
=
Log
(
__name__
).
getlog
()
...
@@ -134,3 +136,85 @@ def forced_align(ctc_probs: paddle.Tensor, y: paddle.Tensor,
...
@@ -134,3 +136,85 @@ def forced_align(ctc_probs: paddle.Tensor, y: paddle.Tensor,
output_alignment
.
append
(
y_insert_blank
[
state_seq
[
t
,
0
]])
output_alignment
.
append
(
y_insert_blank
[
state_seq
[
t
,
0
]])
return
output_alignment
return
output_alignment
# ctc_align(
# self.model,
# self.align_loader,
# self.config.decoding.batch_size,
# self.align_loader.collate_fn.stride_ms,
# self.align_loader.collate_fn.vocab_list,
# self.args.result_file,
# )
def
ctc_align
(
model
,
dataloader
,
batch_size
,
stride_ms
,
token_dict
,
result_file
):
"""ctc alignment.
Args:
model (nn.Layer): U2 Model.
dataloader (io.DataLoader): dataloader.
batch_size (int): decoding batchsize.
stride_ms (int): audio feature stride in ms unit.
token_dict (List[str]): vocab list, e.g. ['blank', 'unk', 'a', 'b', '<eos>'].
result_file (str): alignment output file, e.g. xxx.align.
"""
if
batch_size
>
1
:
logger
.
fatal
(
'alignment mode must be running with batch_size == 1'
)
sys
.
exit
(
1
)
assert
result_file
and
result_file
.
endswith
(
'.align'
)
model
.
eval
()
logger
.
info
(
f
"Align Total Examples:
{
len
(
dataloader
.
dataset
)
}
"
)
with
open
(
result_file
,
'w'
)
as
fout
:
# one example in batch
for
i
,
batch
in
enumerate
(
dataloader
):
key
,
feat
,
feats_length
,
target
,
target_length
=
batch
# 1. Encoder
encoder_out
,
encoder_mask
=
model
.
_forward_encoder
(
feat
,
feats_length
)
# (B, maxlen, encoder_dim)
maxlen
=
encoder_out
.
shape
[
1
]
ctc_probs
=
model
.
ctc
.
log_softmax
(
encoder_out
)
# (1, maxlen, vocab_size)
# 2. alignment
ctc_probs
=
ctc_probs
.
squeeze
(
0
)
target
=
target
.
squeeze
(
0
)
alignment
=
forced_align
(
ctc_probs
,
target
)
logger
.
info
(
f
"align ids:
{
key
[
0
]
}
{
alignment
}
"
)
fout
.
write
(
'{} {}
\n
'
.
format
(
key
[
0
],
alignment
))
# 3. gen praat
# segment alignment
align_segs
=
text_grid
.
segment_alignment
(
alignment
)
logger
.
info
(
f
"align tokens:
{
key
[
0
]
}
,
{
align_segs
}
"
)
# IntervalTier, List["start end token\n"]
subsample
=
utility
.
get_subsample
(
self
.
config
)
tierformat
=
text_grid
.
align_to_tierformat
(
align_segs
,
subsample
,
token_dict
)
# write tier
align_output_path
=
Path
(
self
.
args
.
result_file
).
parent
/
"align"
align_output_path
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
tier_path
=
align_output_path
/
(
key
[
0
]
+
".tier"
)
with
tier_path
.
open
(
'w'
)
as
f
:
f
.
writelines
(
tierformat
)
# write textgrid
textgrid_path
=
align_output_path
/
(
key
[
0
]
+
".TextGrid"
)
second_per_frame
=
1.
/
(
1000.
/
stride_ms
)
# 25ms window, 10ms stride
second_per_example
=
(
len
(
alignment
)
+
1
)
*
subsample
*
second_per_frame
text_grid
.
generate_textgrid
(
maxtime
=
second_per_example
,
intervals
=
tierformat
,
output
=
str
(
textgrid_path
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录