频域Relu:如何计算空间域值为正的狄拉克斯总和?

时间:2018-05-06 02:53:03

标签: python fft relu

我尝试实施频域ReLu,详见:http://cs231n.stanford.edu/reports/2015/pdfs/tema8_final.pdf

令我困惑的公式位于第4页的左下角。我不确定我是否正确计算了dirac函数的FFT之和。我是否错误地解释了这个公式?

import numpy as np
import matplotlib.pyplot as plt

img = np.array([[-1.0,2.3],[5,7.8]])

# Dirac is essentially the shifting matrix
# So create the 2d shifting matrix values
N = 2
x = np.arange(0, N, 1)
y = np.arange(0, N, 1)
xm, ym = np.meshgrid(x, y)
shiftMat = np.exp(1j * ((2.0 * np.pi)) * (xm + ym))

# Set equal to shift mat
# In this trivial example I know that [0,0] is only negative position
# So set to 0 and compute sum of all positions in which f(x) > 0 as detailed in paper
freqRelu = shiftMat
freqRelu[0,0] = 0
freqRelu = np.sum(freqRelu)

# Fourier Convolution and IFFT
imgFFT = np.fft.fft2(img)
freqR = np.multiply(imgFFT,freqRelu)
reluedFreq = np.real(np.fft.ifft2(freqR))

# Spatial Relu For Comparision
reluedImg = img
reluedImg[0,0] = 0

plt.subplot(121)
plt.imshow(reluedImg)
plt.subplot(122)
plt.imshow(reluedFreq)
plt.show()

print(np.allclose(reluedFreq,reluedImg))
print(reluedFreq)
print(reluedImg)

1 个答案:

答案 0 :(得分:0)

作为参考,这个问题在信号堆栈交换中被回答:https://dsp.stackexchange.com/questions/49023/sum-of-diracs-in-frequency-domain