问题描述
目前我正在尝试实施 Fourier CNN.
Currently I'm trying to work on implementing a Fourier CNN.
对我来说,第一步是了解 real傅立叶变换.
The first step for me is to understand the output shape generated by the real fourier transform.
我在下面有一些代码片段:
I have some code snippets below:
input = Input(shape=(150, 150, 3))
f = Lambda(lambda x: tf.signal.rfft2d(x))(input)
print(f.shape, f.dtype)
# > (None, 150, 150, 2) <dtype: 'complex64'>
最后一个维度:2从哪里来?输出不应该仍然有导致 (None, 150, 150, 3) 的三个通道吗?
Where does the last dimension: 2 come from? Shouldn't the output still have three channels resulting in (None, 150, 150, 3)?
我尝试了另一个 fft_length 来看看它是否能给我一些启发,但它让我比开始时更加困惑
I tried another fft_length to see if it would shed some light for me, but it has left me more confused than I was when I started
input = Input(shape=(150, 150, 3))
f = Lambda(lambda x: tf.signal.rfft2d(x, fft_length=[100, 100]))(input)
print(f.shape, f.dtype)
# > (None, 150, 100, 51) <dtype: 'complex64'>
我的问题主要是:形状 (None, 150, 150, 2) 从何而来?
My question is primarily:where does the shape (None, 150, 150, 2) come from?
作为奖励,我也很想了解为什么第二个代码片段会导致 (None, 150, 100, 51)
as a bonus I'd love to also understand why the second code snippet results in (None, 150, 100, 51)
谢谢!
推荐答案
我已经能够通过一些额外的测试来解决我的问题.
I've been able to solve my question through some additional testing.
看起来您需要确保额外的通道必须位于最内层的维度上.我已经能够让上面的代码进行以下修改:
It looks like you need to ensure that additional channels must be on the innermost dimension. I've been able to get the code above to work with the following modifications:
def fft_on_axis(x):
x = tf.transpose(x, perm=[0, 3, 1, 2],)
x_fft = tf.signal.rfft2d(x)
result = tf.transpose(x_fft, perm=[0, 2, 3, 1])
return result
i = Input(shape=(150, 150, 3), name='Input')
x = Lambda(fft_on_axis, name='fft2d')(i)
print(x.shape)
> (None, 150, 76, 3)
这与我对文档的期望一致!对于零频率项,第二维上的 fft_length 减半加一.
This lines up with what I'd expect from the documentation! The fft_length on the second dimension is cut in half plus one for the zero frequency term.
这篇关于tf.signal.rfft2d 输出形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!