src.utils
Functions
|
Compute the low- and high-frequency components of a batch of images. |
|
Concatenate an image batch with its low- and high-frequency components. |
Initialize model weights following a normal distribution scheme. |
Classes
|
A paired-image dataset for single-image dehazing tasks. |
- class src.utils.DehazingDataset(*args: Any, **kwargs: Any)[source]
Bases:
DatasetA paired-image dataset for single-image dehazing tasks.
This dataset assumes a directory structure:
root_dir/clear— clean (ground-truth) imagesroot_dir/hazy— corresponding hazy/degraded imagesBoth subdirectories must contain images with matching filenames. The dataset returns pairs
(hazy, clear)suitable for supervised training of dehazing models.- Parameters:
root_dir (
strorPath-like) – Path to the dataset root directory containingclear/andhazy/.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 (
listofstr) – Filenames present inclear_dir(matching files are expected inhazy_dir).transform (
callableorNone) – 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:lfis the low-frequency component obtained via Gaussian blur.hfis 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
tanhprevents 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.applyto 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. Ifmis an instance ofConv2dorConvTranspose2d, its weights are initialized usingN(0, 0.02). Ifmis aBatchNorm2dorBatchNorm1dlayer, its weights are initialized usingN(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).