Spaces:
Sleeping
Sleeping
File size: 6,857 Bytes
0883aa1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# This module is from [WeNet](https://github.com/wenet-e2e/wenet).
# ## Citations
# ```bibtex
# @inproceedings{yao2021wenet,
# title={WeNet: Production oriented Streaming and Non-streaming End-to-End Speech Recognition Toolkit},
# author={Yao, Zhuoyuan and Wu, Di and Wang, Xiong and Zhang, Binbin and Yu, Fan and Yang, Chao and Peng, Zhendong and Chen, Xiaoyu and Xie, Lei and Lei, Xin},
# booktitle={Proc. Interspeech},
# year={2021},
# address={Brno, Czech Republic },
# organization={IEEE}
# }
# @article{zhang2022wenet,
# title={WeNet 2.0: More Productive End-to-End Speech Recognition Toolkit},
# author={Zhang, Binbin and Wu, Di and Peng, Zhendong and Song, Xingchen and Yao, Zhuoyuan and Lv, Hang and Xie, Lei and Yang, Chao and Pan, Fuping and Niu, Jianwei},
# journal={arXiv preprint arXiv:2203.15455},
# year={2022}
# }
#
import random
import torch
import torch.distributed as dist
from torch.utils.data import IterableDataset
import wenet.dataset.processor as processor
from wenet.utils.file_utils import read_lists
class Processor(IterableDataset):
def __init__(self, source, f, *args, **kw):
assert callable(f)
self.source = source
self.f = f
self.args = args
self.kw = kw
def set_epoch(self, epoch):
self.source.set_epoch(epoch)
def __iter__(self):
"""Return an iterator over the source dataset processed by the
given processor.
"""
assert self.source is not None
assert callable(self.f)
return self.f(iter(self.source), *self.args, **self.kw)
def apply(self, f):
assert callable(f)
return Processor(self, f, *self.args, **self.kw)
class DistributedSampler:
def __init__(self, shuffle=True, partition=True):
self.epoch = -1
self.update()
self.shuffle = shuffle
self.partition = partition
def update(self):
assert dist.is_available()
if dist.is_initialized():
self.rank = dist.get_rank()
self.world_size = dist.get_world_size()
else:
self.rank = 0
self.world_size = 1
worker_info = torch.utils.data.get_worker_info()
if worker_info is None:
self.worker_id = 0
self.num_workers = 1
else:
self.worker_id = worker_info.id
self.num_workers = worker_info.num_workers
return dict(
rank=self.rank,
world_size=self.world_size,
worker_id=self.worker_id,
num_workers=self.num_workers,
)
def set_epoch(self, epoch):
self.epoch = epoch
def sample(self, data):
"""Sample data according to rank/world_size/num_workers
Args:
data(List): input data list
Returns:
List: data list after sample
"""
data = list(range(len(data)))
# TODO(Binbin Zhang): fix this
# We can not handle uneven data for CV on DDP, so we don't
# sample data by rank, that means every GPU gets the same
# and all the CV data
if self.partition:
if self.shuffle:
random.Random(self.epoch).shuffle(data)
data = data[self.rank :: self.world_size]
data = data[self.worker_id :: self.num_workers]
return data
class DataList(IterableDataset):
def __init__(self, lists, shuffle=True, partition=True):
self.lists = lists
self.sampler = DistributedSampler(shuffle, partition)
def set_epoch(self, epoch):
self.sampler.set_epoch(epoch)
def __iter__(self):
sampler_info = self.sampler.update()
indexes = self.sampler.sample(self.lists)
for index in indexes:
# yield dict(src=src)
data = dict(src=self.lists[index])
data.update(sampler_info)
yield data
def Dataset(
data_type,
data_list_file,
symbol_table,
conf,
bpe_model=None,
non_lang_syms=None,
partition=True,
):
"""Construct dataset from arguments
We have two shuffle stage in the Dataset. The first is global
shuffle at shards tar/raw file level. The second is global shuffle
at training samples level.
Args:
data_type(str): raw/shard
bpe_model(str): model for english bpe part
partition(bool): whether to do data partition in terms of rank
"""
assert data_type in ["raw", "shard"]
lists = read_lists(data_list_file)
shuffle = conf.get("shuffle", True)
dataset = DataList(lists, shuffle=shuffle, partition=partition)
if data_type == "shard":
dataset = Processor(dataset, processor.url_opener)
dataset = Processor(dataset, processor.tar_file_and_group)
else:
dataset = Processor(dataset, processor.parse_raw)
dataset = Processor(
dataset,
processor.tokenize,
symbol_table,
bpe_model,
non_lang_syms,
conf.get("split_with_space", False),
)
filter_conf = conf.get("filter_conf", {})
dataset = Processor(dataset, processor.filter, **filter_conf)
resample_conf = conf.get("resample_conf", {})
dataset = Processor(dataset, processor.resample, **resample_conf)
speed_perturb = conf.get("speed_perturb", False)
if speed_perturb:
dataset = Processor(dataset, processor.speed_perturb)
feats_type = conf.get("feats_type", "fbank")
assert feats_type in ["fbank", "mfcc"]
if feats_type == "fbank":
fbank_conf = conf.get("fbank_conf", {})
dataset = Processor(dataset, processor.compute_fbank, **fbank_conf)
elif feats_type == "mfcc":
mfcc_conf = conf.get("mfcc_conf", {})
dataset = Processor(dataset, processor.compute_mfcc, **mfcc_conf)
spec_aug = conf.get("spec_aug", True)
spec_sub = conf.get("spec_sub", False)
spec_trim = conf.get("spec_trim", False)
if spec_aug:
spec_aug_conf = conf.get("spec_aug_conf", {})
dataset = Processor(dataset, processor.spec_aug, **spec_aug_conf)
if spec_sub:
spec_sub_conf = conf.get("spec_sub_conf", {})
dataset = Processor(dataset, processor.spec_sub, **spec_sub_conf)
if spec_trim:
spec_trim_conf = conf.get("spec_trim_conf", {})
dataset = Processor(dataset, processor.spec_trim, **spec_trim_conf)
if shuffle:
shuffle_conf = conf.get("shuffle_conf", {})
dataset = Processor(dataset, processor.shuffle, **shuffle_conf)
sort = conf.get("sort", True)
if sort:
sort_conf = conf.get("sort_conf", {})
dataset = Processor(dataset, processor.sort, **sort_conf)
batch_conf = conf.get("batch_conf", {})
dataset = Processor(dataset, processor.batch, **batch_conf)
dataset = Processor(dataset, processor.padding)
return dataset
|