icefall/egs/himia/wuw/local/compute_fbank_himia.py
2023-03-16 20:03:57 +08:00

140 lines
4.3 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright 2023 Xiaomi Corp. (Author: Liyong Guo)
#
# See ../../../../LICENSE for clarification regarding multiple authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This file computes fbank features of the HI_MIA and HI_MIA_CW dataset.
It looks for manifests in the directory data/manifests.
The generated fbank features are saved in data/fbank.
"""
import argparse
import logging
import os
from pathlib import Path
import torch
from lhotse import CutSet, Fbank, FbankConfig, LilcomHdf5Writer
from lhotse.recipes.utils import read_manifests_if_cached
from icefall.utils import get_executor, str2bool
# Torch's multithreaded behavior needs to be disabled or
# it wastes a lot of CPU and slow things down.
# Do this outside of main() in case it needs to take effect
# even when we are not invoking the main (e.g. when spawning subprocesses).
torch.set_num_threads(1)
torch.set_num_interop_threads(1)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--train-set-channel",
type=str,
default="_7_01",
help="""channel of HI_MIA dataset.
All channels are used if it is set "all".
""",
)
parser.add_argument(
"--enable-speed-perturb",
type=str2bool,
default=False,
help="""channel of training set.
""",
)
return parser.parse_args()
def compute_fbank_himia(
train_set_channel: str = None,
enable_speed_perturb: bool = True,
):
src_dir = Path("data/manifests")
output_dir = Path("data/fbank")
num_jobs = min(40, os.cpu_count())
num_mel_bins = 80
if "all" == train_set_channel:
dataset_parts = (
"train",
"dev",
"test",
"cw_test",
)
else:
dataset_parts = (
f"train{train_set_channel}",
f"dev{train_set_channel}",
f"test{train_set_channel}",
"cw_test",
)
manifests = read_manifests_if_cached(
dataset_parts=dataset_parts, prefix="himia", output_dir=src_dir
)
assert manifests is not None
extractor = Fbank(FbankConfig(num_mel_bins=num_mel_bins))
with get_executor() as ex: # Initialize the executor only once.
for partition, m in manifests.items():
if (output_dir / f"cuts_{partition}.jsonl.gz").is_file():
logging.info(f"{partition} already exists - skipping.")
continue
logging.info(f"Processing {partition}")
cut_set = CutSet.from_manifests(
recordings=m["recordings"],
supervisions=m["supervisions"],
)
if "train" in partition and enable_speed_perturb:
cut_set = (
cut_set + cut_set.perturb_speed(0.9) + cut_set.perturb_speed(1.1)
)
cut_set = cut_set.resample(16000)
cut_set = cut_set.compute_and_store_features(
extractor=extractor,
storage_path=f"{output_dir}/feats_{partition}",
# when an executor is specified, make more partitions
num_jobs=num_jobs if ex is None else 80,
executor=ex,
storage_type=LilcomHdf5Writer,
)
output_file_name = f"cuts_{partition}.jsonl.gz"
if "all" != train_set_channel:
output_file_name = f"cuts_{partition}{train_set_channel}.jsonl.gz"
cut_set.to_file(output_dir / f"{output_file_name}")
def main():
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
args = get_args()
logging.basicConfig(format=formatter, level=logging.INFO)
compute_fbank_himia(
train_set_channel=args.train_set_channel,
enable_speed_perturb=args.enable_speed_perturb,
)
if __name__ == "__main__":
main()