How to correctly replace the real part of the FFT?
I try to reproduce a data augmentation method, which comes from the paper:
Qinwei Xu, Ruipeng Zhang, Ya Zhang, Yanfeng Wang and Qi Tian &quot;A Fourier-based Framework for Domain Generalization&quot; (CVPR 2021).
It is mentioned in the paper that they set the real part to a constant (the constant in the paper is 20000) to eliminate the amplitude and realize the reconstruction of the image relying only on the phase.
Below is my code:
img = process_img(&quot;./data/house.jpg&quot;, 128)
img_fft = torch.fft.fft2(img, dim=(-2, -1))
amp = torch.full(img_fft.shape, 200000)
img_fft.real = amp
img_ifft = torch.fft.ifft2(img_fft, dim=(-2, -1))

img_ifft = img_ifft.squeeze(0)
img_ifft = img_ifft.transpose(2, 0)

img_ifft = np.array(img_ifft)

cv2.imshow(&quot;&quot;, img_ifft.real)

Among them, the process_img function is only used to convert ndarray to tensor, as shown below:
loader = transforms.Compose([transforms.ToTensor()])
def process_img(img_path, img_size):
    img = cv2.imread(img_path)
    img = cv2.resize(img, (img_size, img_size))
    img = img.astype(np.float32) / 255.0
    img = loader(img)
    img = img.unsqueeze(0)
    return img

The first is the original image, the second is the image provided by the paper, and the third is the image generated by my code:

It can be seen that the images generated by my method are very different from those provided in the paper, and there are some artifacts. Why is there such a result?

You are confusing &quot;real&quot;/&quot;imaginary&quot; parts of complex numbers with &quot;amplitude&quot;/&quot;phase&quot; representation.
Here's the quick guide:
A complex number z can be expressed by either a sum of its real part x and its imaginary part y:
z = x + j y
Alternatively, once can express the same complex number z as a rotated vector with amplitude r and an angle phi:
z = r exp(j phi)
Where r = sqrt(x^2 + y^2) and phi=atan2(x,y).
This image (from Wikipedia) explain this visually:

In your code, you replace the &quot;real&quot; part, but in the paper, they suggest replacing the &quot;amplitude&quot;.
If you want to replace the amplitude:
const_amp = ... # whatever the constant amplitude you want
new_fft = const_amp * torch.exp(1j * img_fft.angle())

# reconstruct the new image from the modulated Fourier:
img_ifft = torch.fft.ifft2(new_fft, dim=(-2, -1))

This results with the following image:

