Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
1818b058
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看板
提交
1818b058
编写于
2月 18, 2022
作者:
K
KP
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support batch input in cls task.
上级
a8c3f6d4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
94 addition
and
12 deletion
+94
-12
demos/audio_tagging/README.md
demos/audio_tagging/README.md
+1
-1
demos/audio_tagging/README_cn.md
demos/audio_tagging/README_cn.md
+1
-1
paddlespeech/cli/cls/infer.py
paddlespeech/cli/cls/infer.py
+44
-10
paddlespeech/cli/executor.py
paddlespeech/cli/executor.py
+48
-0
未找到文件。
demos/audio_tagging/README.md
浏览文件 @
1818b058
...
@@ -65,7 +65,7 @@ wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/cat.wav https://paddlespe
...
@@ -65,7 +65,7 @@ wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/cat.wav https://paddlespe
config
=
None
,
# Set `config` and `ckpt_path` to None to use pretrained model.
config
=
None
,
# Set `config` and `ckpt_path` to None to use pretrained model.
label_file
=
None
,
label_file
=
None
,
ckpt_path
=
None
,
ckpt_path
=
None
,
audio
_file
=
'./cat.wav'
,
input
_file
=
'./cat.wav'
,
topk
=
10
,
topk
=
10
,
device
=
paddle
.
get_device
())
device
=
paddle
.
get_device
())
print
(
'CLS Result:
\n
{}'
.
format
(
result
))
print
(
'CLS Result:
\n
{}'
.
format
(
result
))
...
...
demos/audio_tagging/README_cn.md
浏览文件 @
1818b058
...
@@ -65,7 +65,7 @@ wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/cat.wav https://paddlespe
...
@@ -65,7 +65,7 @@ wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/cat.wav https://paddlespe
config
=
None
,
# Set `config` and `ckpt_path` to None to use pretrained model.
config
=
None
,
# Set `config` and `ckpt_path` to None to use pretrained model.
label_file
=
None
,
label_file
=
None
,
ckpt_path
=
None
,
ckpt_path
=
None
,
audio
_file
=
'./cat.wav'
,
input
_file
=
'./cat.wav'
,
topk
=
10
,
topk
=
10
,
device
=
paddle
.
get_device
())
device
=
paddle
.
get_device
())
print
(
'CLS Result:
\n
{}'
.
format
(
result
))
print
(
'CLS Result:
\n
{}'
.
format
(
result
))
...
...
paddlespeech/cli/cls/infer.py
浏览文件 @
1818b058
...
@@ -12,7 +12,9 @@
...
@@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
import
argparse
import
argparse
import
ast
import
os
import
os
import
sys
from
typing
import
List
from
typing
import
List
from
typing
import
Optional
from
typing
import
Optional
from
typing
import
Union
from
typing
import
Union
...
@@ -109,6 +111,11 @@ class CLSExecutor(BaseExecutor):
...
@@ -109,6 +111,11 @@ class CLSExecutor(BaseExecutor):
type
=
str
,
type
=
str
,
default
=
paddle
.
get_device
(),
default
=
paddle
.
get_device
(),
help
=
'Choose device to execute model inference.'
)
help
=
'Choose device to execute model inference.'
)
self
.
parser
.
add_argument
(
'--job_dump_result'
,
type
=
ast
.
literal_eval
,
default
=
False
,
help
=
'Save job result into file.'
)
def
_get_pretrained_path
(
self
,
tag
:
str
)
->
os
.
PathLike
:
def
_get_pretrained_path
(
self
,
tag
:
str
)
->
os
.
PathLike
:
"""
"""
...
@@ -214,7 +221,7 @@ class CLSExecutor(BaseExecutor):
...
@@ -214,7 +221,7 @@ class CLSExecutor(BaseExecutor):
ret
=
''
ret
=
''
for
idx
in
topk_idx
:
for
idx
in
topk_idx
:
label
,
score
=
self
.
_label_list
[
idx
],
result
[
idx
]
label
,
score
=
self
.
_label_list
[
idx
],
result
[
idx
]
ret
+=
f
'
{
label
}
:
{
score
}
\n
'
ret
+=
f
'
{
label
}
{
score
}
\n
'
return
ret
return
ret
def
postprocess
(
self
,
topk
:
int
)
->
Union
[
str
,
os
.
PathLike
]:
def
postprocess
(
self
,
topk
:
int
)
->
Union
[
str
,
os
.
PathLike
]:
...
@@ -234,22 +241,36 @@ class CLSExecutor(BaseExecutor):
...
@@ -234,22 +241,36 @@ class CLSExecutor(BaseExecutor):
label_file
=
parser_args
.
label_file
label_file
=
parser_args
.
label_file
cfg_path
=
parser_args
.
config
cfg_path
=
parser_args
.
config
ckpt_path
=
parser_args
.
ckpt_path
ckpt_path
=
parser_args
.
ckpt_path
audio
_file
=
parser_args
.
input
input
_file
=
parser_args
.
input
topk
=
parser_args
.
topk
topk
=
parser_args
.
topk
device
=
parser_args
.
device
device
=
parser_args
.
device
job_dump_result
=
parser_args
.
job_dump_result
try
:
try
:
res
=
self
(
audio_file
,
model_type
,
cfg_path
,
ckpt_path
,
label_file
,
if
job_dump_result
:
topk
,
device
)
assert
self
.
_is_job_input
(
logger
.
info
(
'CLS Result:
\n
{}'
.
format
(
res
))
input_file
),
'Input file should be a job file(*.job) when `job_dump_result` is True.'
job_output_file
=
os
.
path
.
abspath
(
input_file
)
+
'.done'
sys
.
stdout
=
open
(
job_output_file
,
'w'
)
print
(
self
(
input_file
,
model_type
,
cfg_path
,
ckpt_path
,
label_file
,
topk
,
device
))
if
job_dump_result
:
logger
.
info
(
f
'Results had been saved to:
{
job_output_file
}
'
)
return
True
return
True
except
Exception
as
e
:
except
Exception
as
e
:
logger
.
exception
(
e
)
logger
.
exception
(
e
)
return
False
return
False
finally
:
sys
.
stdout
.
close
()
@
stats_wrapper
@
stats_wrapper
def
__call__
(
self
,
def
__call__
(
self
,
audio
_file
:
os
.
PathLike
,
input
_file
:
os
.
PathLike
,
model
:
str
=
'panns_cnn14'
,
model
:
str
=
'panns_cnn14'
,
config
:
Optional
[
os
.
PathLike
]
=
None
,
config
:
Optional
[
os
.
PathLike
]
=
None
,
ckpt_path
:
Optional
[
os
.
PathLike
]
=
None
,
ckpt_path
:
Optional
[
os
.
PathLike
]
=
None
,
...
@@ -259,11 +280,24 @@ class CLSExecutor(BaseExecutor):
...
@@ -259,11 +280,24 @@ class CLSExecutor(BaseExecutor):
"""
"""
Python API to call an executor.
Python API to call an executor.
"""
"""
audio_file
=
os
.
path
.
abspath
(
audio
_file
)
input_file
=
os
.
path
.
abspath
(
input
_file
)
paddle
.
set_device
(
device
)
paddle
.
set_device
(
device
)
self
.
_init_from_path
(
model
,
config
,
ckpt_path
,
label_file
)
self
.
_init_from_path
(
model
,
config
,
ckpt_path
,
label_file
)
self
.
preprocess
(
audio_file
)
self
.
infer
()
if
self
.
_is_job_input
(
input_file
):
# *.job
res
=
self
.
postprocess
(
topk
)
# Retrieve result of cls.
job_outputs
=
{}
job_contents
=
self
.
_job_preprocess
(
input_file
)
for
id_
,
file
in
job_contents
.
items
():
try
:
self
.
preprocess
(
file
)
self
.
infer
()
job_outputs
[
id_
]
=
self
.
postprocess
(
topk
).
strip
()
except
Exception
as
e
:
job_outputs
[
id_
]
=
f
'
{
e
.
__class__
.
__name__
}
:
{
e
}
'
res
=
self
.
_job_postprecess
(
job_outputs
)
else
:
self
.
preprocess
(
input_file
)
self
.
infer
()
res
=
self
.
postprocess
(
topk
)
# Retrieve result of cls.
return
res
return
res
paddlespeech/cli/executor.py
浏览文件 @
1818b058
...
@@ -15,6 +15,7 @@ import os
...
@@ -15,6 +15,7 @@ import os
from
abc
import
ABC
from
abc
import
ABC
from
abc
import
abstractmethod
from
abc
import
abstractmethod
from
typing
import
Any
from
typing
import
Any
from
typing
import
Dict
from
typing
import
List
from
typing
import
List
from
typing
import
Union
from
typing
import
Union
...
@@ -100,3 +101,50 @@ class BaseExecutor(ABC):
...
@@ -100,3 +101,50 @@ class BaseExecutor(ABC):
Python API to call an executor.
Python API to call an executor.
"""
"""
pass
pass
def
_is_job_input
(
self
,
input_
:
Union
[
str
,
os
.
PathLike
])
->
bool
:
"""
Check if current input file is a job input or not.
Args:
input_ (Union[str, os.PathLike]): Input file of current task.
Returns:
bool: return `True` for job input, `False` otherwise.
"""
return
os
.
path
.
isfile
(
input_
)
and
input_
.
endswith
(
'.job'
)
def
_job_preprocess
(
self
,
job_input
:
os
.
PathLike
)
->
Dict
[
str
,
str
]:
"""
Read a job input file and return its contents in a dictionary.
Args:
job_input (os.PathLike): The job input file.
Returns:
Dict[str, str]: Contents of job input.
"""
job_contents
=
{}
with
open
(
job_input
)
as
f
:
for
line
in
f
:
line
=
line
.
strip
()
if
not
line
:
continue
k
,
v
=
line
.
split
(
' '
)
job_contents
[
k
]
=
v
return
job_contents
def
_job_postprecess
(
self
,
job_outputs
:
Dict
[
str
,
str
])
->
str
:
"""
Convert job results to string.
Args:
job_outputs (Dict[str, str]): A dictionary with job ids and results.
Returns:
str: A string object contains job outputs.
"""
ret
=
''
for
k
,
v
in
job_outputs
.
items
():
ret
+=
f
'
{
k
}
{
v
}
\n
'
return
ret
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录