Skip to content

Add ifft2 method to ops #20437

@markveillette

Description

@markveillette

I'm curious why there is no ops.ifft2. Given that there is already fft and fft2, implementing one is trivial.

Here is an example of what an ifft2 would look like:

import keras
def keras_ops_ifft2(fft_real,fft_imag):
    """
    Inputs are real and imaginary parts of array
    of shape [...,H,W],[...,H,W] , where the last two 
    dimensions correspond tot he image dimensions

    Returns tuple containing the real and imaginary parts
    of the ifft2 

    Test:
       from keras import ops
       X = np.random.rand( 1,1,11,11 ).astype(np.float32)
       X_real,X_imag = ops.real(X), ops.imag(X)
       X_fft_real,X_fft_imag = keras.ops.fft2((X_real,X_imag))
       X_recon,_ = keras_ops_ifft2(X_fft_real,X_fft_imag)
       np.allclose(X,X_recon,atol=1e-6)
       
    """
    H = ops.cast(ops.shape(fft_real)[-2],'float32') # height
    W = ops.cast(ops.shape(fft_real)[-1],'float32') # width

    # Conjugate the input
    real_conj, imag_conj = fft_real, -fft_imag

    # Compute FFT of conjugate
    fft = ops.fft2((real_conj, imag_conj))

    return fft[0] / (H*W), -fft[1] / (H*W)

Metadata

Metadata

Labels

stat:contributions welcomeA pull request to fix this issue would be welcome.type:featureThe user is asking for a new feature.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions