src.utils

Functions

get_lf_hf(images)

Compute the low- and high-frequency components of a batch of images.

prepare_discriminator_input(img, lf, hf)

Concatenate an image batch with its low- and high-frequency components.

weights_init_normal(m)

Initialize model weights following a normal distribution scheme.

Classes

DehazingDataset(*args, **kwargs)

A paired-image dataset for single-image dehazing tasks.

class src.utils.DehazingDataset(*args: Any, **kwargs: Any)[source]

Bases: Dataset

A paired-image dataset for single-image dehazing tasks.

This dataset assumes a directory structure:

root_dir/clear — clean (ground-truth) images root_dir/hazy — corresponding hazy/degraded images

Both subdirectories must contain images with matching filenames. The dataset returns pairs (hazy, clear) suitable for supervised training of dehazing models.

Parameters:
  • root_dir (str or Path-like) – Path to the dataset root directory containing clear/ and hazy/.

  • transform (callable, optional) – A function or transform applied to both images. It must accept and return a PIL image or a tensor. When provided, the same transform is applied to both hazy and clear images.

Variables:
  • root_dir (str) – Path to the dataset root.

  • clear_dir (str) – Directory containing the clean images.

  • hazy_dir (str) – Directory containing the hazy images.

  • images (list of str) – Filenames present in clear_dir (matching files are expected in hazy_dir).

  • transform (callable or None) – Transform applied to both images in a sample.

src.utils.get_lf_hf(images: torch.Tensor) tuple[torch.Tensor, torch.Tensor][source]

Compute the low- and high-frequency components of a batch of images.

This function extracts two complementary frequency representations from an input batch of images. The low-frequency (LF) component is obtained using a Gaussian blur, which removes fine details while preserving coarse structure. The high-frequency (HF) component is computed by applying a Laplacian filter to the grayscale version of the input and then replicating the result across the RGB channels. Because the Laplacian may produce high-magnitude values, the HF output is normalized to the range [-1, 1] using a hyperbolic tangent to ensure numerical stability during training.

Parameters:

images (torch.Tensor) – Input batch of images with shape (B, C, H, W) and RGB channels.

Returns:

A pair (lf, hf) where:

  • lf is the low-frequency component obtained via Gaussian blur.

  • hf is the high-frequency component derived from the Laplacian and normalized to [-1, 1].

Return type:

tuple[torch.Tensor, torch.Tensor]

Notes

  • The HF component is expanded to three channels so that both LF and HF can be concatenated with the original RGB image if needed (e.g., for discriminators in GAN architectures).

  • Normalization with tanh prevents unstable gradients that may arise from the Laplacian operator.

src.utils.prepare_discriminator_input(img: torch.Tensor, lf: torch.Tensor, hf: torch.Tensor) torch.Tensor[source]

Concatenate an image batch with its low- and high-frequency components.

This function prepares the input for a discriminator by concatenating, along the channel dimension, the original RGB images together with their corresponding low-frequency (LF) and high-frequency (HF) representations. The resulting tensor has nine channels: three from the original image, three from the LF component, and three from the HF component.

Parameters:
  • img (torch.Tensor) – Batch of RGB images with shape (B, 3, H, W).

  • lf (torch.Tensor) – Batch of low-frequency components with shape (B, 3, H, W).

  • hf (torch.Tensor) – Batch of high-frequency components with shape (B, 3, H, W).

Returns:

Concatenated tensor of shape (B, 9, H, W) corresponding to [img or lf or hf] along the channel dimension.

Return type:

torch.Tensor

src.utils.weights_init_normal(m: torch.nn.Module) None[source]

Initialize model weights following a normal distribution scheme.

This function is intended to be passed to nn.Module.apply to initialize convolutional and batch-normalization layers commonly used in GANs. Convolutional layers are initialized with a normal distribution centered at 0, while batch-normalization layers are initialized with weights centered at 1 and biases at 0.

Parameters:

m (nn.Module) – A PyTorch module. If m is an instance of Conv2d or ConvTranspose2d, its weights are initialized using N(0, 0.02). If m is a BatchNorm2d or BatchNorm1d layer, its weights are initialized using N(1, 0.02) and its biases are set to zero.

Notes

  • This initialization scheme follows standard practices in GAN training (e.g., DCGAN), improving stability during early optimization.

  • Use as: model.apply(weights_init_normal).