utils.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # *****************************************************************************
  2. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of the NVIDIA CORPORATION nor the
  12. # names of its contributors may be used to endorse or promote products
  13. # derived from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  19. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #
  26. # *****************************************************************************
  27. import os
  28. from pathlib import Path
  29. from typing import Optional
  30. import numpy as np
  31. import torch
  32. from scipy.io.wavfile import read
  33. def mask_from_lens(lens, max_len: Optional[int] = None):
  34. if max_len is None:
  35. max_len = int(lens.max().item())
  36. ids = torch.arange(0, max_len, device=lens.device, dtype=lens.dtype)
  37. mask = torch.lt(ids, lens.unsqueeze(1))
  38. return mask
  39. def load_wav_to_torch(full_path):
  40. sampling_rate, data = read(full_path)
  41. return torch.FloatTensor(data.astype(np.float32)), sampling_rate
  42. def load_filepaths_and_text(dataset_path, filename, split="|"):
  43. def split_line(root, line):
  44. parts = line.strip().split(split)
  45. paths, text = parts[:-1], parts[-1]
  46. return tuple(os.path.join(root, p) for p in paths) + (text,)
  47. with open(filename, encoding='utf-8') as f:
  48. filepaths_and_text = [split_line(dataset_path, line) for line in f]
  49. return filepaths_and_text
  50. def stats_filename(dataset_path, filelist_path, feature_name):
  51. stem = Path(filelist_path).stem
  52. return Path(dataset_path, f'{feature_name}_stats__{stem}.json')
  53. def to_gpu(x):
  54. x = x.contiguous()
  55. if torch.cuda.is_available():
  56. x = x.cuda(non_blocking=True)
  57. return torch.autograd.Variable(x)
  58. def to_device_async(tensor, device):
  59. return tensor.to(device, non_blocking=True)
  60. def to_numpy(x):
  61. return x.cpu().numpy() if isinstance(x, torch.Tensor) else x