相当于最新 PyTorch 版本中的 torch.rfft()

发布于 2025-01-15 02:38:48 字数 340 浏览 1 评论 0原文

我想估计给定尺寸 BxCxWxH 的图像的傅里叶变换

在以前的 torch 版本中,以下工作完成了这项工作:

fft_im = torch.rfft(img, signal_ndim=2, onesided=False)

并且输出的大小为:

BxCxWxHx2

但是,使用新版本的 rfft :

fft_im = torch.fft.rfft2(img, dim=2, norm=None)

我不这样做得到相同的结果。我错过了什么吗?

I want to estimate the fourier transform for a given image of size BxCxWxH

In previous torch version the following did the job:

fft_im = torch.rfft(img, signal_ndim=2, onesided=False)

and the output was of size:

BxCxWxHx2

However, with the new version of rfft :

fft_im = torch.fft.rfft2(img, dim=2, norm=None)

I do not get the same results. Do I miss something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2025-01-22 02:38:48

一些问题

  1. 您提供的 dim 参数是无效类型,它应该是两个数字的元组或者应该被省略。实际上 PyTorch 应该引发异常。我认为这无一例外地运行的事实是 PyTorch 中的一个错误(我开了一张票,说明了这一点)。
  2. PyTorch 现在支持complex 张量类型,因此 FFT 函数返回这些而不是为实部/虚部添加新的维度。您可以使用 torch.view_as_real 转换为旧的表示形式。还值得指出的是,view_as_real 不会复制数据,因为它返回一个视图,因此不会以任何明显的方式减慢速度。
  3. PyTorch 不再提供禁用 RFFT 中单边计算的选项。可能是因为禁用单侧会使结果与 torch.fft.fft2 相同,这与 PEP 20 的第 13 条格言相冲突。提供 FFT 的特殊实值版本的全部意义是你只需要计算每个维度的一半值,因为其余的可以通过 Hermition 对称属性推断出来。

因此,

fft_im = torch.view_as_real(torch.fft.fft2(img))

如果您要将 fft_im 传递给 torch.fft 中的其他函数(例如 < code>fft.ifft 或 fft.fftshift),那么您需要使用 torch.view_as_complex 所以这些函数不要将最后一个维度解释为信号维度。

A few issues

  1. The dim argument you provided is an invalid type, it should be a tuple of two numbers or should be omitted. Really PyTorch should raise an exception. I would argue that the fact this ran without exception is a bug in PyTorch (I opened a ticket stating as much).
  2. PyTorch now supports complex tensor types, so FFT functions return those instead of adding a new dimension for the real/imaginary parts. You can use torch.view_as_real to convert to the old representation. Also worth pointing out that view_as_real doesn't copy data since it returns a view so shouldn't slow things down in any noticeable way.
  3. PyTorch no longer gives the option of disabling one-sided calculation in RFFT. Probably because disabling one-sided makes the result identical to torch.fft.fft2, which is in conflict with the 13th aphorism of PEP 20. The whole point of providing a special real-valued version of the FFT is that you need only compute half the values for each dimension, since the rest can be inferred via the Hermition symmetric property.

So from all that you should be able to use

fft_im = torch.view_as_real(torch.fft.fft2(img))

Important If you're going to pass fft_im to other functions in torch.fft (like fft.ifft or fft.fftshift) then you'll need to convert back to the complex representation using torch.view_as_complex so those functions don't interpret the last dimension as a signal dimension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文