prepare_data.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import os
  2. import logging
  3. from multiprocessing import Pool
  4. import numpy as np
  5. import torch.distributed as dist
  6. def filter_wav_text(data_dir, dataset):
  7. wav_file = os.path.join(data_dir, dataset, "wav.scp")
  8. text_file = os.path.join(data_dir, dataset, "text")
  9. with open(wav_file) as f_wav, open(text_file) as f_text:
  10. wav_lines = f_wav.readlines()
  11. text_lines = f_text.readlines()
  12. os.rename(wav_file, "{}.bak".format(wav_file))
  13. os.rename(text_file, "{}.bak".format(text_file))
  14. wav_dict = {}
  15. for line in wav_lines:
  16. parts = line.strip().split()
  17. if len(parts) < 2:
  18. continue
  19. wav_dict[parts[0]] = parts[1]
  20. text_dict = {}
  21. for line in text_lines:
  22. parts = line.strip().split()
  23. if len(parts) < 2:
  24. continue
  25. text_dict[parts[0]] = " ".join(parts[1:]).lower()
  26. filter_count = 0
  27. with open(wav_file, "w") as f_wav, open(text_file, "w") as f_text:
  28. for sample_name, wav_path in wav_dict.items():
  29. if sample_name in text_dict.keys():
  30. f_wav.write(sample_name + " " + wav_path + "\n")
  31. f_text.write(sample_name + " " + text_dict[sample_name] + "\n")
  32. else:
  33. filter_count += 1
  34. logging.info("{}/{} samples in {} are filtered because of the mismatch between wav.scp and text".format(len(wav_lines),
  35. filter_count,
  36. dataset))
  37. def calc_shape_core(root_path, frontend_conf, speech_length_min, speech_length_max, idx):
  38. wav_scp_file = os.path.join(root_path, "wav.scp.{}".format(idx))
  39. shape_file = os.path.join(root_path, "speech_shape.{}".format(idx))
  40. with open(wav_scp_file) as f:
  41. lines = f.readlines()
  42. with open(shape_file, "w") as f:
  43. for line in lines:
  44. sample_name, wav_path = line.strip().split()
  45. n_frames, feature_dim, speech_length = wav2num_frame(wav_path, frontend_conf)
  46. write_flag = True
  47. if speech_length_min > 0 and speech_length < speech_length_min:
  48. write_flag = False
  49. if speech_length_max > 0 and speech_length > speech_length_max:
  50. write_flag = False
  51. if write_flag:
  52. f.write("{} {},{}\n".format(sample_name, str(int(np.ceil(n_frames))), str(int(feature_dim))))
  53. f.flush()
  54. def calc_shape(args, dataset, nj=32):
  55. shape_path = os.path.join(args.data_dir, dataset, "speech_shape")
  56. if os.path.exists(shape_path):
  57. print('Shape file for small dataset already exists.')
  58. return
  59. os.makedirs(shape_path, exist_ok=True)
  60. split_shape_path = os.path.join(args.data_dir, dataset, "shape_files")
  61. if os.path.exists(shape_path):
  62. assert os.path.exists(os.path.join(args.data_dir, dataset, "speech_shape"))
  63. print('Shape file for small dataset already exists.')
  64. return
  65. os.makedirs(shape_path, exist_ok=True)
  66. # split
  67. wav_scp_file = os.path.join(args.data_dir, dataset, "wav.scp")
  68. with open(wav_scp_file) as f:
  69. lines = f.readlines()
  70. num_lines = len(lines)
  71. num_job_lines = num_lines // nj
  72. start = 0
  73. for i in range(nj):
  74. end = start + num_job_lines
  75. file = os.path.join(shape_path, "wav.scp.{}".format(str(i + 1)))
  76. with open(file, "w") as f:
  77. if i == nj - 1:
  78. f.writelines(lines[start:])
  79. else:
  80. f.writelines(lines[start:end])
  81. start = end
  82. p = Pool(nj)
  83. for i in range(nj):
  84. p.apply_async(calc_shape_core,
  85. args=(shape_path, frontend_conf, speech_length_min, speech_length_max, str(i + 1)))
  86. print('Generating shape files, please wait a few minutes...')
  87. p.close()
  88. p.join()
  89. # combine
  90. file = os.path.join(data_dir, dataset, "speech_shape")
  91. with open(file, "w") as f:
  92. for i in range(nj):
  93. job_file = os.path.join(shape_path, "speech_shape.{}".format(str(i + 1)))
  94. with open(job_file) as job_f:
  95. lines = job_f.readlines()
  96. f.writelines(lines)
  97. print('Generating shape files done.')
  98. def prepare_data(args, distributed_option):
  99. distributed = distributed_option.distributed
  100. if not distributed or distributed_option.dist_rank == 0:
  101. filter_wav_text(args.data_dir, args.train_set)
  102. filter_wav_text(args.data_dir, args.dev_set)
  103. dist.barrier()
  104. if args.dataset_type == "small" and args.train_shape_file is None: