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_F_spectrogram.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. def forward(self, x, y):
  23. x0 = torchaudio.functional.spectrogram(x, n_fft=64, window=torch.hann_window(44), win_length=44, hop_length=16, pad=0, center=True, normalized='window', power=1)
  24. if version.parse(torchaudio.__version__) < version.parse('0.11.0'):
  25. # return_complex=False with power=None, skip it
  26. x1 = torchaudio.functional.spectrogram(x, n_fft=128, window=torch.hann_window(128), win_length=128, hop_length=3, pad=0, center=False, onesided=True, normalized=False, power=1)
  27. else:
  28. x1 = torchaudio.functional.spectrogram(x, n_fft=128, window=torch.hann_window(128), win_length=128, hop_length=3, pad=0, center=False, onesided=True, normalized=False, power=None)
  29. x2 = torchaudio.functional.spectrogram(x, n_fft=512, window=torch.hamming_window(256), win_length=256, hop_length=128, pad=0, center=True, pad_mode='constant', onesided=True, normalized='frame_length', power=2)
  30. x3 = torchaudio.functional.spectrogram(x, n_fft=512, window=torch.hamming_window(512), win_length=512, hop_length=128, pad=32, center=True, onesided=False, normalized=False, power=2)
  31. y0 = torchaudio.functional.spectrogram(y, n_fft=64, window=torch.hann_window(44), win_length=44, hop_length=16, pad=0, center=True, normalized='window', power=1)
  32. if version.parse(torchaudio.__version__) < version.parse('0.11.0'):
  33. # return_complex=False with power=None, skip it
  34. y1 = torchaudio.functional.spectrogram(y, n_fft=128, window=torch.hann_window(128), win_length=128, hop_length=3, pad=0, center=False, onesided=True, normalized=False, power=1)
  35. else:
  36. y1 = torchaudio.functional.spectrogram(y, n_fft=128, window=torch.hann_window(128), win_length=128, hop_length=3, pad=0, center=False, onesided=True, normalized=False, power=None)
  37. y2 = torchaudio.functional.spectrogram(y, n_fft=512, window=torch.hamming_window(256), win_length=256, hop_length=128, pad=0, center=True, pad_mode='constant', onesided=True, normalized='frame_length', power=2)
  38. y3 = torchaudio.functional.spectrogram(y, n_fft=512, window=torch.hamming_window(512), win_length=512, hop_length=128, pad=32, center=True, onesided=False, normalized=False, power=2)
  39. return x0, x1, x2, x3, y0, y1, y2, y3
  40. def test():
  41. net = Model()
  42. net.eval()
  43. torch.manual_seed(0)
  44. x = torch.rand(3, 2560)
  45. y = torch.rand(1000)
  46. a = net(x, y)
  47. # export torchscript
  48. mod = torch.jit.trace(net, (x, y))
  49. mod.save("test_torchaudio_F_spectrogram.pt")
  50. # torchscript to pnnx
  51. import os
  52. os.system("../src/pnnx test_torchaudio_F_spectrogram.pt inputshape=[3,2560],[1000]")
  53. # pnnx inference
  54. import test_torchaudio_F_spectrogram_pnnx
  55. b = test_torchaudio_F_spectrogram_pnnx.test_inference()
  56. for a0, b0 in zip(a, b):
  57. if not torch.allclose(a0, b0, 1e-4, 1e-4):
  58. return False
  59. return True
  60. if __name__ == "__main__":
  61. if test():
  62. exit(0)
  63. else:
  64. exit(1)