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.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright 2024 Tencent
  2. # SPDX-License-Identifier: BSD-3-Clause
  3. import torch
  4. import torch.nn as nn
  5. import torch.nn.functional as F
  6. import torchaudio
  7. from packaging import version
  8. class Model(nn.Module):
  9. def __init__(self):
  10. super(Model, self).__init__()
  11. 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')
  12. 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)
  13. 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')
  14. 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)
  15. def forward(self, x, y, z, w):
  16. out0 = self.s0(x)
  17. out1 = self.s1(y)
  18. out2 = self.s2(z)
  19. out3 = self.s3(w)
  20. return out0, out1, out2, out3
  21. def test():
  22. if version.parse(torchaudio.__version__) < version.parse('0.10.0'):
  23. return True
  24. net = Model()
  25. net.eval()
  26. torch.manual_seed(0)
  27. x = torch.rand(3, 33, 161, dtype=torch.complex64)
  28. y = torch.rand(1, 65, 77, dtype=torch.complex64)
  29. z = torch.rand(257, 8, dtype=torch.complex64)
  30. w = torch.rand(512, 4, dtype=torch.complex64)
  31. a = net(x, y, z, w)
  32. # export torchscript
  33. mod = torch.jit.trace(net, (x, y, z, w))
  34. mod.save("test_torchaudio_InverseSpectrogram.pt")
  35. # torchscript to pnnx
  36. import os
  37. os.system("../src/pnnx test_torchaudio_InverseSpectrogram.pt inputshape=[3,33,161]c64,[1,65,77]c64,[257,8]c64,[512,4]c64")
  38. # pnnx inference
  39. import test_torchaudio_InverseSpectrogram_pnnx
  40. b = test_torchaudio_InverseSpectrogram_pnnx.test_inference()
  41. for a0, b0 in zip(a, b):
  42. if not torch.allclose(a0, b0, 1e-4, 1e-4):
  43. return False
  44. return True
  45. if __name__ == "__main__":
  46. if test():
  47. exit(0)
  48. else:
  49. exit(1)