You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_torchaudio_InverseSpectrogram.py 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Tencent is pleased to support the open source community by making ncnn available.
  2. #
  3. # Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
  4. #
  5. # Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. # in compliance with the License. You may obtain a copy of the License at
  7. #
  8. # https://opensource.org/licenses/BSD-3-Clause
  9. #
  10. # Unless required by applicable law or agreed to in writing, software distributed
  11. # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. # CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. # specific language governing permissions and limitations under the License.
  14. import torch
  15. import torch.nn as nn
  16. import torch.nn.functional as F
  17. import torchaudio
  18. from packaging import version
  19. class Model(nn.Module):
  20. def __init__(self):
  21. super(Model, self).__init__()
  22. self.s0 = torchaudio.transforms.InverseSpectrogram(n_fft=64, window_fn=torch.hann_window, win_length=44, hop_length=16, pad=0, center=True, normalized='window')
  23. self.s1 = torchaudio.transforms.InverseSpectrogram(n_fft=128, window_fn=torch.hann_window, win_length=128, hop_length=3, pad=0, center=True, onesided=True, normalized=False)
  24. self.s2 = torchaudio.transforms.InverseSpectrogram(n_fft=512, window_fn=torch.hamming_window, win_length=256, hop_length=128, pad=0, center=True, onesided=True, normalized='frame_length')
  25. self.s3 = torchaudio.transforms.InverseSpectrogram(n_fft=512, window_fn=torch.hamming_window, win_length=512, hop_length=128, pad=0, center=True, onesided=False, normalized=False)
  26. def forward(self, x, y, z, w):
  27. out0 = self.s0(x)
  28. out1 = self.s1(y)
  29. out2 = self.s2(z)
  30. out3 = self.s3(w)
  31. return out0, out1, out2, out3
  32. def test():
  33. if version.parse(torchaudio.__version__) < version.parse('0.10.0'):
  34. return True
  35. net = Model()
  36. net.eval()
  37. torch.manual_seed(0)
  38. x = torch.rand(3, 33, 161, dtype=torch.complex64)
  39. y = torch.rand(1, 65, 77, dtype=torch.complex64)
  40. z = torch.rand(257, 8, dtype=torch.complex64)
  41. w = torch.rand(512, 4, dtype=torch.complex64)
  42. a = net(x, y, z, w)
  43. # export torchscript
  44. mod = torch.jit.trace(net, (x, y, z, w))
  45. mod.save("test_torchaudio_InverseSpectrogram.pt")
  46. # torchscript to pnnx
  47. import os
  48. os.system("../src/pnnx test_torchaudio_InverseSpectrogram.pt inputshape=[3,33,161]c64,[1,65,77]c64,[257,8]c64,[512,4]c64")
  49. # pnnx inference
  50. import test_torchaudio_InverseSpectrogram_pnnx
  51. b = test_torchaudio_InverseSpectrogram_pnnx.test_inference()
  52. for a0, b0 in zip(a, b):
  53. if not torch.allclose(a0, b0, 1e-4, 1e-4):
  54. return False
  55. return True
  56. if __name__ == "__main__":
  57. if test():
  58. exit(0)
  59. else:
  60. exit(1)