biapy.models.dfcan

This module implements the Deep Fourier Channel Attention Network (DFCAN) for super-resolution, along with its core building blocks.

The DFCAN model leverages Fourier domain processing and channel attention mechanisms to enhance image details, particularly in microscopy images.

Key components include:

  • fftshift2d and fftshift3d: Functions for shifting the zero-frequency component in 2D and 3D Fourier transforms.

  • RCAB (Residual Channel Attention Block): A fundamental block incorporating Fourier-based channel attention.

  • ResGroup: A sequential group of RCABs with a residual connection.

  • DFCAN: The main super-resolution model integrating these components.

The implementation is adapted from the original DFCAN-pytorch repository.

biapy.models.dfcan.fftshift2d(img, size_psc=128)[source]

Shifts the zero-frequency component of a 2D Fourier transform to the center of the spectrum.

This function rearranges the quadrants of a 2D tensor (image) after a Fourier transform so that the zero-frequency component is at the center. This is a common operation in Fourier optics and signal processing.

Parameters:
  • img (torch.Tensor) – The input 2D tensor (image) in frequency domain, typically after torch.fft.fftn. Expected shape: (batch_size, channels, height, width).

  • size_psc (int, optional) – Placeholder parameter, not directly used in the current implementation but might indicate expected patch size. Defaults to 128.

Returns:

The frequency-shifted 2D tensor with the same shape as the input.

Return type:

torch.Tensor

biapy.models.dfcan.fftshift3d(img, size_psc=128)[source]

Shifts the zero-frequency component of a 3D Fourier transform to the center of the spectrum.

This function rearranges the octants of a 3D tensor (volume) after a Fourier transform so that the zero-frequency component is at the center.

Parameters:
  • img (torch.Tensor) – The input 3D tensor (volume) in frequency domain, typically after torch.fft.fftn. Expected shape: (batch_size, channels, depth, height, width).

  • size_psc (int, optional) – Placeholder parameter, not directly used in the current implementation but might indicate expected patch size. Defaults to 128.

Returns:

The frequency-shifted 3D tensor with the same shape as the input.

Return type:

torch.Tensor

class biapy.models.dfcan.RCAB(size_psc=128, conv: ~typing.Type[~torch.nn.modules.conv.Conv2d | ~torch.nn.modules.conv.Conv3d] = <class 'torch.nn.modules.conv.Conv2d'>)[source]

Bases: Module

Residual Channel Attention Block (RCAB).

This block enhances features by incorporating a Fourier-based channel attention mechanism within a residual structure. It operates by transforming features into the frequency domain, applying an attention mechanism, and then combining the result with the original features.

forward(x, gamma=0.8)[source]

Perform the forward pass of the Residual Channel Attention Block.

Processes the input x through initial convolutions, then transforms it to the frequency domain using FFT, applies a power law (gamma correction) and frequency shifting. An attention map is generated from this frequency domain representation and then used to scale the features. A residual connection adds the processed features back to the original input.

Parameters:
  • x (torch.Tensor) – The input feature tensor. Expected shape for 2D: (B, 64, H, W); for 3D: (B, 64, D, H, W).

  • gamma (float, optional) – The power law exponent applied to the absolute values of the Fourier transformed features. Defaults to 0.8.

Returns:

The output feature tensor after applying the RCAB. Same shape as input x.

Return type:

torch.Tensor

class biapy.models.dfcan.ResGroup(n_RCAB=4, size_psc=128, conv: ~typing.Type[~torch.nn.modules.conv.Conv2d | ~torch.nn.modules.conv.Conv3d] = <class 'torch.nn.modules.conv.Conv2d'>)[source]

Bases: Module

A group of Residual Channel Attention Blocks (RCABs).

This module stacks multiple RCABs and incorporates a residual connection around the entire group, allowing for deeper networks while maintaining gradient flow.

forward(x)[source]

Perform the forward pass of the Residual Group.

Passes the input x through the sequence of RCAB blocks and then adds the original input x to the result, forming a residual connection.

Parameters:

x (torch.Tensor) – The input feature tensor.

Returns:

The output feature tensor after processing through the residual group. Same shape as input x.

Return type:

torch.Tensor

class biapy.models.dfcan.DFCAN(ndim, input_shape, scale=2, n_ResGroup=4, n_RCAB=4)[source]

Bases: Module

Fourier Channel Attention Network (DFCAN) for Super-Resolution.

DFCAN is a deep learning architecture designed for single image super-resolution, leveraging Fourier domain processing and channel attention mechanisms to enhance image details. It is composed of an initial feature extraction block, multiple Residual Groups (each containing Residual Channel Attention Blocks), a sub-pixel convolution for upsampling, and a final convolutional layer.

References

forward(x) Tensor[source]

Perform the forward pass of the DFCAN model.

The input x first undergoes initial feature extraction. It then passes through a series of Residual Groups (RGs) for hierarchical feature refinement. After the RGs, a convolution prepares the features for upsampling via PixelShuffle. Finally, another convolution produces the super-resolved output, often with a sigmoid activation for intensity normalization.

Parameters:

x (torch.Tensor) – The input low-resolution image tensor. Expected shape for 2D: (batch_size, channels, height, width). Expected shape for 3D: (batch_size, channels, depth, height, width).

Returns:

The super-resolved output image tensor. The spatial dimensions will be scaled by the scale factor, and the number of channels will match input_shape[-1].

Return type:

torch.Tensor