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_Spectrogram.py 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.Spectrogram(n_fft=64, window_fn=torch.hann_window, win_length=44, hop_length=16, pad=0, center=True, normalized='window', power=1)
  23. if version.parse(torchaudio.__version__) < version.parse('0.11.0'):
  24. # return_complex=False with power=None, skip it
  25. self.s1 = torchaudio.transforms.Spectrogram(n_fft=128, window_fn=torch.hann_window, win_length=128, hop_length=3, pad=0, center=False, onesided=True, normalized=False, power=1)
  26. else:
  27. self.s1 = torchaudio.transforms.Spectrogram(n_fft=128, window_fn=torch.hann_window, win_length=128, hop_length=3, pad=0, center=False, onesided=True, normalized=False, power=None)
  28. self.s2 = torchaudio.transforms.Spectrogram(n_fft=512, window_fn=torch.hamming_window, win_length=256, hop_length=128, pad=0, center=True, pad_mode='constant', onesided=True, normalized='frame_length', power=2)
  29. self.s3 = torchaudio.transforms.Spectrogram(n_fft=512, window_fn=torch.hamming_window, win_length=512, hop_length=128, pad=32, center=True, onesided=False, normalized=False, power=2)
  30. def forward(self, x, y):
  31. out0 = self.s0(x)
  32. out1 = self.s1(x)
  33. out2 = self.s2(y)
  34. out3 = self.s3(y)
  35. return out0, out1, out2, out3
  36. def test():
  37. net = Model()
  38. net.eval()
  39. torch.manual_seed(0)
  40. x = torch.rand(3, 2560)
  41. y = torch.rand(1000)
  42. a = net(x, y)
  43. # export torchscript
  44. mod = torch.jit.trace(net, (x, y))
  45. mod.save("test_torchaudio_Spectrogram.pt")
  46. # torchscript to pnnx
  47. import os
  48. os.system("../src/pnnx test_torchaudio_Spectrogram.pt inputshape=[3,2560],[1000]")
  49. # pnnx inference
  50. import test_torchaudio_Spectrogram_pnnx
  51. b = test_torchaudio_Spectrogram_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)