forked from auspicious3000/contentvec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01_train_valid_tsv.py
58 lines (49 loc) · 2.05 KB
/
01_train_valid_tsv.py
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
import os
import random
import soundfile as sf
from glob import glob
from tqdm import tqdm
from concurrent.futures import ProcessPoolExecutor
def main(filelist):
audio_dict = {}
for file in tqdm(filelist):
with sf.SoundFile(file) as audio:
total_frames = len(audio)
audio_dict[file] = total_frames
return audio_dict
def merge_dicts(dicts):
merged_dict = {}
for d in dicts:
merged_dict.update(d)
return merged_dict
if __name__ == '__main__':
root_dir = r'./dataset_raw'
num_processes = 10
audio_files = []
extensions = ["wav"]
for ext in extensions:
audio_files.extend(glob(os.path.join(root_dir, f'**/*.{ext}'), recursive=True))
'''
with ProcessPoolExecutor(max_workers=num_processes) as executor:
results = []
tasks = [executor.submit(main, audio_files[int(i * len(audio_files) / num_processes): int((i + 1) * len(audio_files) / num_processes)]) for i in range(num_processes)]
for future in tasks:
results.append(future.result())
audio_dict = merge_dicts(results)
'''
audio_dict = main(audio_files)
validation_set = random.sample(audio_files, 1000)
training_set = [file for file in audio_files if file not in validation_set]
os.makedirs('./data/00_filelist', exist_ok=True)
with open('./data/00_filelist/train.tsv', 'w', encoding='utf-8') as train_file:
train_file.write(f'{root_dir}\n')
for audio_path in training_set:
relative_path = os.path.relpath(audio_path, root_dir)
total_frames = audio_dict[audio_path]
train_file.write(f'{relative_path}\t{total_frames}\n')
with open('./data/00_filelist/valid.tsv', 'w', encoding='utf-8') as val_file:
val_file.write(f'{root_dir}\n')
for audio_path in validation_set:
relative_path = os.path.relpath(audio_path, root_dir)
total_frames = audio_dict[audio_path]
val_file.write(f'{relative_path}\t{total_frames}\n')