mirror of
https://github.com/csukuangfj/kaldifeat.git
synced 2025-08-16 20:52:16 +00:00
Add PlpOptions.
This commit is contained in:
parent
518846a74e
commit
97b77bec39
@ -199,7 +199,12 @@ Please refer to
|
|||||||
- [kaldifeat/python/tests/test_mfcc.py](kaldifeat/python/tests/test_mfcc.py)
|
- [kaldifeat/python/tests/test_mfcc.py](kaldifeat/python/tests/test_mfcc.py)
|
||||||
- [kaldifeat/python/tests/test_plp.py](kaldifeat/python/tests/test_plp.py)
|
- [kaldifeat/python/tests/test_plp.py](kaldifeat/python/tests/test_plp.py)
|
||||||
- [kaldifeat/python/tests/test_spectrogram.py](kaldifeat/python/tests/test_spectrogram.py)
|
- [kaldifeat/python/tests/test_spectrogram.py](kaldifeat/python/tests/test_spectrogram.py)
|
||||||
- [kaldifeat/python/tests/test_options.py](kaldifeat/python/tests/test_options.py)
|
- [kaldifeat/python/tests/test_frame_extraction_options.py](kaldifeat/python/tests/test_frame_extraction_options.py)
|
||||||
|
- [kaldifeat/python/tests/test_mel_bank_options.py](kaldifeat/python/tests/test_mel_bank_options.py)
|
||||||
|
- [kaldifeat/python/tests/test_fbank_options.py](kaldifeat/python/tests/test_fbank_options.py)
|
||||||
|
- [kaldifeat/python/tests/test_mfcc_options.py](kaldifeat/python/tests/test_mfcc_options.py)
|
||||||
|
- [kaldifeat/python/tests/test_spectrogram_options.py](kaldifeat/python/tests/test_spectrogram_options.py)
|
||||||
|
- [kaldifeat/python/tests/test_plp_options.py](kaldifeat/python/tests/test_plp_options.py)
|
||||||
|
|
||||||
for more examples.
|
for more examples.
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "kaldifeat/csrc/feature-plp.h"
|
#include "kaldifeat/csrc/feature-plp.h"
|
||||||
|
#include "kaldifeat/python/csrc/utils.h"
|
||||||
|
|
||||||
namespace kaldifeat {
|
namespace kaldifeat {
|
||||||
|
|
||||||
@ -36,7 +37,12 @@ void PybindPlpOptions(py::module &m) {
|
|||||||
self.device = torch::Device(s);
|
self.device = torch::Device(s);
|
||||||
})
|
})
|
||||||
.def("__str__",
|
.def("__str__",
|
||||||
[](const PyClass &self) -> std::string { return self.ToString(); });
|
[](const PyClass &self) -> std::string { return self.ToString(); })
|
||||||
|
.def("as_dict",
|
||||||
|
[](const PyClass &self) -> py::dict { return AsDict(self); })
|
||||||
|
.def_static("from_dict", [](py::dict dict) -> PyClass {
|
||||||
|
return PlpOptionsFromDict(dict);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PybindPlp(py::module &m) {
|
static void PybindPlp(py::module &m) {
|
||||||
|
@ -195,6 +195,56 @@ py::dict AsDict(const SpectrogramOptions &opts) {
|
|||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PlpOptions PlpOptionsFromDict(py::dict dict) {
|
||||||
|
PlpOptions opts;
|
||||||
|
|
||||||
|
if (dict.contains("frame_opts")) {
|
||||||
|
opts.frame_opts = FrameExtractionOptionsFromDict(dict["frame_opts"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dict.contains("mel_opts")) {
|
||||||
|
opts.mel_opts = MelBanksOptionsFromDict(dict["mel_opts"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
FROM_DICT(int_, lpc_order);
|
||||||
|
FROM_DICT(int_, num_ceps);
|
||||||
|
FROM_DICT(bool_, use_energy);
|
||||||
|
FROM_DICT(float_, energy_floor);
|
||||||
|
FROM_DICT(bool_, raw_energy);
|
||||||
|
FROM_DICT(float_, compress_factor);
|
||||||
|
FROM_DICT(int_, cepstral_lifter);
|
||||||
|
FROM_DICT(float_, cepstral_scale);
|
||||||
|
FROM_DICT(bool_, htk_compat);
|
||||||
|
|
||||||
|
if (dict.contains("device")) {
|
||||||
|
opts.device = torch::Device(std::string(py::str(dict["device"])));
|
||||||
|
}
|
||||||
|
|
||||||
|
return opts;
|
||||||
|
}
|
||||||
|
|
||||||
|
py::dict AsDict(const PlpOptions &opts) {
|
||||||
|
py::dict dict;
|
||||||
|
|
||||||
|
dict["frame_opts"] = AsDict(opts.frame_opts);
|
||||||
|
dict["mel_opts"] = AsDict(opts.mel_opts);
|
||||||
|
|
||||||
|
AS_DICT(lpc_order);
|
||||||
|
AS_DICT(num_ceps);
|
||||||
|
AS_DICT(use_energy);
|
||||||
|
AS_DICT(energy_floor);
|
||||||
|
AS_DICT(raw_energy);
|
||||||
|
AS_DICT(compress_factor);
|
||||||
|
AS_DICT(cepstral_lifter);
|
||||||
|
AS_DICT(cepstral_scale);
|
||||||
|
AS_DICT(htk_compat);
|
||||||
|
|
||||||
|
auto torch_device = py::module_::import("torch").attr("device");
|
||||||
|
dict["device"] = torch_device(opts.device.str());
|
||||||
|
|
||||||
|
return dict;
|
||||||
|
}
|
||||||
|
|
||||||
#undef FROM_DICT
|
#undef FROM_DICT
|
||||||
#undef AS_DICT
|
#undef AS_DICT
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "kaldifeat/csrc/feature-fbank.h"
|
#include "kaldifeat/csrc/feature-fbank.h"
|
||||||
#include "kaldifeat/csrc/feature-mfcc.h"
|
#include "kaldifeat/csrc/feature-mfcc.h"
|
||||||
|
#include "kaldifeat/csrc/feature-plp.h"
|
||||||
#include "kaldifeat/csrc/feature-spectrogram.h"
|
#include "kaldifeat/csrc/feature-spectrogram.h"
|
||||||
#include "kaldifeat/csrc/feature-window.h"
|
#include "kaldifeat/csrc/feature-window.h"
|
||||||
#include "kaldifeat/csrc/mel-computations.h"
|
#include "kaldifeat/csrc/mel-computations.h"
|
||||||
@ -41,6 +42,9 @@ py::dict AsDict(const MfccOptions &opts);
|
|||||||
SpectrogramOptions SpectrogramOptionsFromDict(py::dict dict);
|
SpectrogramOptions SpectrogramOptionsFromDict(py::dict dict);
|
||||||
py::dict AsDict(const SpectrogramOptions &opts);
|
py::dict AsDict(const SpectrogramOptions &opts);
|
||||||
|
|
||||||
|
PlpOptions PlpOptionsFromDict(py::dict dict);
|
||||||
|
py::dict AsDict(const PlpOptions &opts);
|
||||||
|
|
||||||
} // namespace kaldifeat
|
} // namespace kaldifeat
|
||||||
|
|
||||||
#endif // KALDIFEAT_PYTHON_CSRC_UTILS_H_
|
#endif // KALDIFEAT_PYTHON_CSRC_UTILS_H_
|
||||||
|
@ -23,8 +23,8 @@ set(py_test_files
|
|||||||
test_mel_bank_options.py
|
test_mel_bank_options.py
|
||||||
test_mfcc.py
|
test_mfcc.py
|
||||||
test_mfcc_options.py
|
test_mfcc_options.py
|
||||||
test_options.py
|
|
||||||
test_plp.py
|
test_plp.py
|
||||||
|
test_plp_options.py
|
||||||
test_spectrogram.py
|
test_spectrogram.py
|
||||||
test_spectrogram_options.py
|
test_spectrogram_options.py
|
||||||
)
|
)
|
||||||
|
@ -1,81 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
#
|
|
||||||
# Copyright (c) 2021 Xiaomi Corporation (authors: Fangjun Kuang)
|
|
||||||
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
cur_dir = Path(__file__).resolve().parent
|
|
||||||
kaldi_feat_dir = cur_dir.parent.parent.parent
|
|
||||||
|
|
||||||
|
|
||||||
import torch
|
|
||||||
|
|
||||||
sys.path.insert(0, f"{kaldi_feat_dir}/build/lib")
|
|
||||||
|
|
||||||
import kaldifeat
|
|
||||||
|
|
||||||
|
|
||||||
def test_spectogram_options():
|
|
||||||
opts = kaldifeat.SpectrogramOptions()
|
|
||||||
opts.energy_floor = 0.0
|
|
||||||
opts.raw_energy = True
|
|
||||||
|
|
||||||
frame_opts = opts.frame_opts
|
|
||||||
frame_opts.blackman_coeff = 0.42
|
|
||||||
frame_opts.dither = 1
|
|
||||||
frame_opts.frame_length_ms = 25
|
|
||||||
frame_opts.frame_shift_ms = 10
|
|
||||||
frame_opts.preemph_coeff = 0.97
|
|
||||||
frame_opts.remove_dc_offset = True
|
|
||||||
frame_opts.round_to_power_of_two = True
|
|
||||||
frame_opts.samp_freq = 16000
|
|
||||||
frame_opts.snip_edges = True
|
|
||||||
frame_opts.window_type = "povey"
|
|
||||||
|
|
||||||
print(opts)
|
|
||||||
|
|
||||||
|
|
||||||
def test_plp_options():
|
|
||||||
opts = kaldifeat.PlpOptions()
|
|
||||||
opts.lpc_order = 12
|
|
||||||
opts.num_ceps = 13
|
|
||||||
opts.use_energy = True
|
|
||||||
opts.energy_floor = 0.0
|
|
||||||
opts.raw_energy = True
|
|
||||||
opts.compress_factor = 0.33333
|
|
||||||
opts.cepstral_lifter = 22
|
|
||||||
opts.cepstral_scale = 1.0
|
|
||||||
opts.htk_compat = False
|
|
||||||
opts.device = torch.device("cpu")
|
|
||||||
|
|
||||||
frame_opts = opts.frame_opts
|
|
||||||
frame_opts.blackman_coeff = 0.42
|
|
||||||
frame_opts.dither = 1
|
|
||||||
frame_opts.frame_length_ms = 25
|
|
||||||
frame_opts.frame_shift_ms = 10
|
|
||||||
frame_opts.preemph_coeff = 0.97
|
|
||||||
frame_opts.remove_dc_offset = True
|
|
||||||
frame_opts.round_to_power_of_two = True
|
|
||||||
frame_opts.samp_freq = 16000
|
|
||||||
frame_opts.snip_edges = True
|
|
||||||
frame_opts.window_type = "povey"
|
|
||||||
|
|
||||||
mel_opts = opts.mel_opts
|
|
||||||
mel_opts.debug_mel = True
|
|
||||||
mel_opts.high_freq = 0
|
|
||||||
mel_opts.low_freq = 20
|
|
||||||
mel_opts.num_bins = 23
|
|
||||||
mel_opts.vtln_high = -500
|
|
||||||
mel_opts.vtln_low = 100
|
|
||||||
|
|
||||||
print(opts)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
test_spectogram_options()
|
|
||||||
test_plp_options()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
205
kaldifeat/python/tests/test_plp_options.py
Executable file
205
kaldifeat/python/tests/test_plp_options.py
Executable file
@ -0,0 +1,205 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
#
|
||||||
|
# Copyright (c) 2021 Xiaomi Corporation (authors: Fangjun Kuang)
|
||||||
|
|
||||||
|
|
||||||
|
import torch
|
||||||
|
|
||||||
|
import kaldifeat
|
||||||
|
|
||||||
|
|
||||||
|
def test_default():
|
||||||
|
opts = kaldifeat.PlpOptions()
|
||||||
|
assert opts.frame_opts.samp_freq == 16000
|
||||||
|
assert opts.frame_opts.frame_shift_ms == 10.0
|
||||||
|
assert opts.frame_opts.frame_length_ms == 25.0
|
||||||
|
assert opts.frame_opts.dither == 1.0
|
||||||
|
assert abs(opts.frame_opts.preemph_coeff - 0.97) < 1e-6
|
||||||
|
assert opts.frame_opts.remove_dc_offset is True
|
||||||
|
assert opts.frame_opts.window_type == "povey"
|
||||||
|
assert opts.frame_opts.round_to_power_of_two is True
|
||||||
|
assert abs(opts.frame_opts.blackman_coeff - 0.42) < 1e-6
|
||||||
|
assert opts.frame_opts.snip_edges is True
|
||||||
|
|
||||||
|
assert opts.mel_opts.num_bins == 23
|
||||||
|
assert opts.mel_opts.low_freq == 20
|
||||||
|
assert opts.mel_opts.high_freq == 0
|
||||||
|
assert opts.mel_opts.vtln_low == 100
|
||||||
|
assert opts.mel_opts.vtln_high == -500
|
||||||
|
assert opts.mel_opts.debug_mel is False
|
||||||
|
assert opts.mel_opts.htk_mode is False
|
||||||
|
|
||||||
|
assert opts.lpc_order == 12
|
||||||
|
assert opts.num_ceps == 13
|
||||||
|
assert opts.use_energy is True
|
||||||
|
assert opts.energy_floor == 0.0
|
||||||
|
assert opts.raw_energy is True
|
||||||
|
assert abs(opts.compress_factor - 0.33333) < 1e-6
|
||||||
|
assert opts.cepstral_lifter == 22
|
||||||
|
assert opts.cepstral_scale == 1.0
|
||||||
|
assert opts.htk_compat is False
|
||||||
|
assert opts.device.type == "cpu"
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_get():
|
||||||
|
opts = kaldifeat.PlpOptions()
|
||||||
|
|
||||||
|
opts.lpc_order = 11
|
||||||
|
assert opts.lpc_order == 11
|
||||||
|
|
||||||
|
opts.num_ceps = 1
|
||||||
|
assert opts.num_ceps == 1
|
||||||
|
|
||||||
|
opts.use_energy = False
|
||||||
|
assert opts.use_energy is False
|
||||||
|
|
||||||
|
opts.energy_floor = 1
|
||||||
|
assert opts.energy_floor == 1
|
||||||
|
|
||||||
|
opts.raw_energy = False
|
||||||
|
assert opts.raw_energy is False
|
||||||
|
|
||||||
|
opts.compress_factor = 0.5
|
||||||
|
assert opts.compress_factor == 0.5
|
||||||
|
|
||||||
|
opts.cepstral_lifter = 2
|
||||||
|
assert opts.cepstral_lifter == 2
|
||||||
|
|
||||||
|
opts.cepstral_scale = 3
|
||||||
|
assert opts.cepstral_scale == 3
|
||||||
|
|
||||||
|
opts.htk_compat = True
|
||||||
|
assert opts.htk_compat is True
|
||||||
|
|
||||||
|
opts.device = "cuda:10"
|
||||||
|
assert opts.device == torch.device("cuda", 10)
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_get_frame_opts():
|
||||||
|
opts = kaldifeat.PlpOptions()
|
||||||
|
|
||||||
|
opts.frame_opts.samp_freq = 44100
|
||||||
|
assert opts.frame_opts.samp_freq == 44100
|
||||||
|
|
||||||
|
opts.frame_opts.frame_shift_ms = 20.5
|
||||||
|
assert opts.frame_opts.frame_shift_ms == 20.5
|
||||||
|
|
||||||
|
opts.frame_opts.frame_length_ms = 1
|
||||||
|
assert opts.frame_opts.frame_length_ms == 1
|
||||||
|
|
||||||
|
opts.frame_opts.dither = 0.5
|
||||||
|
assert opts.frame_opts.dither == 0.5
|
||||||
|
|
||||||
|
opts.frame_opts.preemph_coeff = 0.25
|
||||||
|
assert opts.frame_opts.preemph_coeff == 0.25
|
||||||
|
|
||||||
|
opts.frame_opts.remove_dc_offset = False
|
||||||
|
assert opts.frame_opts.remove_dc_offset is False
|
||||||
|
|
||||||
|
opts.frame_opts.window_type = "hanning"
|
||||||
|
assert opts.frame_opts.window_type == "hanning"
|
||||||
|
|
||||||
|
opts.frame_opts.round_to_power_of_two = False
|
||||||
|
assert opts.frame_opts.round_to_power_of_two is False
|
||||||
|
|
||||||
|
opts.frame_opts.blackman_coeff = 0.25
|
||||||
|
assert opts.frame_opts.blackman_coeff == 0.25
|
||||||
|
|
||||||
|
opts.frame_opts.snip_edges = False
|
||||||
|
assert opts.frame_opts.snip_edges is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_get_mel_opts():
|
||||||
|
opts = kaldifeat.PlpOptions()
|
||||||
|
|
||||||
|
opts.mel_opts.num_bins = 100
|
||||||
|
assert opts.mel_opts.num_bins == 100
|
||||||
|
|
||||||
|
opts.mel_opts.low_freq = 22
|
||||||
|
assert opts.mel_opts.low_freq == 22
|
||||||
|
|
||||||
|
opts.mel_opts.high_freq = 1
|
||||||
|
assert opts.mel_opts.high_freq == 1
|
||||||
|
|
||||||
|
opts.mel_opts.vtln_low = 101
|
||||||
|
assert opts.mel_opts.vtln_low == 101
|
||||||
|
|
||||||
|
opts.mel_opts.vtln_high = -100
|
||||||
|
assert opts.mel_opts.vtln_high == -100
|
||||||
|
|
||||||
|
opts.mel_opts.debug_mel = True
|
||||||
|
assert opts.mel_opts.debug_mel is True
|
||||||
|
|
||||||
|
opts.mel_opts.htk_mode = True
|
||||||
|
assert opts.mel_opts.htk_mode is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_from_empty_dict():
|
||||||
|
opts = kaldifeat.PlpOptions.from_dict({})
|
||||||
|
opts2 = kaldifeat.PlpOptions()
|
||||||
|
|
||||||
|
assert str(opts) == str(opts2)
|
||||||
|
|
||||||
|
|
||||||
|
def test_from_dict_partial():
|
||||||
|
d = {
|
||||||
|
"energy_floor": 10.5,
|
||||||
|
"htk_compat": True,
|
||||||
|
"mel_opts": {"num_bins": 80, "vtln_low": 1},
|
||||||
|
"frame_opts": {"window_type": "hanning"},
|
||||||
|
}
|
||||||
|
opts = kaldifeat.PlpOptions.from_dict(d)
|
||||||
|
assert opts.energy_floor == 10.5
|
||||||
|
assert opts.htk_compat is True
|
||||||
|
assert opts.mel_opts.num_bins == 80
|
||||||
|
assert opts.mel_opts.vtln_low == 1
|
||||||
|
assert opts.frame_opts.window_type == "hanning"
|
||||||
|
|
||||||
|
mel_opts = kaldifeat.MelBanksOptions.from_dict(d["mel_opts"])
|
||||||
|
assert str(opts.mel_opts) == str(mel_opts)
|
||||||
|
|
||||||
|
frame_opts = kaldifeat.FrameExtractionOptions.from_dict(d["frame_opts"])
|
||||||
|
assert str(opts.frame_opts) == str(frame_opts)
|
||||||
|
|
||||||
|
|
||||||
|
def test_from_dict_full_and_as_dict():
|
||||||
|
opts = kaldifeat.PlpOptions()
|
||||||
|
opts.htk_compat = True
|
||||||
|
opts.mel_opts.num_bins = 80
|
||||||
|
opts.frame_opts.samp_freq = 10
|
||||||
|
|
||||||
|
d = opts.as_dict()
|
||||||
|
assert d["htk_compat"] is True
|
||||||
|
assert d["mel_opts"]["num_bins"] == 80
|
||||||
|
assert d["frame_opts"]["samp_freq"] == 10
|
||||||
|
|
||||||
|
mel_opts = kaldifeat.MelBanksOptions()
|
||||||
|
mel_opts.num_bins = 80
|
||||||
|
assert d["mel_opts"] == mel_opts.as_dict()
|
||||||
|
|
||||||
|
frame_opts = kaldifeat.FrameExtractionOptions()
|
||||||
|
frame_opts.samp_freq = 10
|
||||||
|
assert d["frame_opts"] == frame_opts.as_dict()
|
||||||
|
|
||||||
|
opts2 = kaldifeat.PlpOptions.from_dict(d)
|
||||||
|
assert str(opts2) == str(opts)
|
||||||
|
|
||||||
|
d["htk_compat"] = False
|
||||||
|
d["device"] = torch.device("cuda", 2)
|
||||||
|
opts3 = kaldifeat.PlpOptions.from_dict(d)
|
||||||
|
assert opts3.htk_compat is False
|
||||||
|
assert opts3.device == torch.device("cuda", 2)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
test_default()
|
||||||
|
test_set_get()
|
||||||
|
test_set_get_frame_opts()
|
||||||
|
test_set_get_mel_opts()
|
||||||
|
test_from_empty_dict()
|
||||||
|
test_from_dict_partial()
|
||||||
|
test_from_dict_full_and_as_dict()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
x
Reference in New Issue
Block a user