crackmnist_eccv.utility ¶
Pollution utility functions for CrackMNIST experiments.
Provides per-image noise injection (Gaussian, Salt & Pepper, Gaussian Blur, Blob/MVS, Uniform) and crack-tip mask enlargement used during training and evaluation.
Functions:
| Name | Description |
|---|---|
add_blob_pollution_and_interp | Mask a (1, C, H, W) tensor with blob occlusion and in-paint the holes. |
add_gaussian_noise | Add Gaussian noise to a tensor. |
add_salt_and_pepper_noise | Add salt-and-pepper noise to a tensor. |
add_uniform_noise | Add zero-centred uniform noise in [-intensity, +intensity). |
apply_gaussian_blur | Apply channel-wise Gaussian blur to a (B, C, H, W) tensor. |
apply_per_image_pollution | Apply noise to a random subset of images in a batch. |
enlarge_crack_tip | Enlarge the annotated crack tip region in a binary mask. |
generate_blob_mask_to_match_oar | Create a binary mask whose OAR lies in [target_min_oar, target_max_oar]. |
Functions:¶
add_blob_pollution_and_interp ¶
add_blob_pollution_and_interp(
tensor: Tensor,
*,
radius: int = 4,
target_min_oar: float = 0.1,
target_max_oar: float = 0.2,
interp_method: str = "linear",
) -> tuple[Tensor, ndarray, float]
Mask a (1, C, H, W) tensor with blob occlusion and in-paint the holes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | Input tensor of shape (1, C, H, W). | required |
| int | Disc radius for blob generation. | 4 |
| float | Lower OAR bound. | 0.1 |
| float | Upper OAR bound. | 0.2 |
| str | Interpolation method passed to scipy.griddata. | 'linear' |
Returns:
| Type | Description |
|---|---|
Tensor | Tuple of (tensor_filled, blob_mask, oar) where tensor_filled has NaNs |
ndarray | replaced by interpolated values, blob_mask is a (H, W) uint8 array, |
float | and oar is the final Occluded Area Ratio. |
Raises:
| Type | Description |
|---|---|
ValueError | If tensor shape is not (1, C, H, W). |
RuntimeError | If fewer than 4 valid points remain for interpolation. |
Source code in src/crackmnist_eccv/utility.py
add_gaussian_noise ¶
Add Gaussian noise to a tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | Input tensor. | required |
| float | Mean of the Gaussian noise. | 0.0 |
| float | Standard deviation of the noise. | 0.5 |
Returns:
| Type | Description |
|---|---|
Tensor | Noisy tensor of the same shape. |
Source code in src/crackmnist_eccv/utility.py
add_salt_and_pepper_noise ¶
add_salt_and_pepper_noise(
tensor: Tensor, intensity: float = 0.25, salt_to_pepper_ratio: float = 0.5
) -> Tensor
Add salt-and-pepper noise to a tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | Input tensor. | required |
| float | Fraction of pixels to corrupt. | 0.25 |
| float | Proportion of corrupted pixels set to 1.0 (salt) vs 0.0 (pepper). | 0.5 |
Returns:
| Type | Description |
|---|---|
Tensor | Noisy tensor of the same shape. |
Source code in src/crackmnist_eccv/utility.py
add_uniform_noise ¶
Add zero-centred uniform noise in [-intensity, +intensity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | Input tensor. | required |
| float | Half-range of the uniform distribution. Must be >= 0. | 0.1 |
Returns:
| Type | Description |
|---|---|
Tensor | Noisy tensor of the same shape. |
Raises:
| Type | Description |
|---|---|
ValueError | If intensity is negative. |
Source code in src/crackmnist_eccv/utility.py
apply_gaussian_blur ¶
Apply channel-wise Gaussian blur to a (B, C, H, W) tensor.
Kernel size is derived from sigma following Yoshihara et al. (2023): kernel_size = round(8 * intensity + 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | Input tensor of shape (B, C, H, W). | required |
| float | Standard deviation (sigma) of the Gaussian kernel. | 4.0 |
Returns:
| Type | Description |
|---|---|
Tensor | Blurred tensor of the same shape. |
Source code in src/crackmnist_eccv/utility.py
apply_per_image_pollution ¶
apply_per_image_pollution(
inputs: Tensor,
pollution_ratio: float = 0.5,
pollution_type: str = "gaussian",
**kwargs: float | str,
) -> Tensor
Apply noise to a random subset of images in a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| Tensor | Batch of input tensors (B, C, H, W). | required |
| float | Proportion of images to pollute. | 0.5 |
| str | One of 'gaussian', 'salt_pepper', 'gaussian_blur', 'blob', 'uniform'. | 'gaussian' |
| float | str | Extra keyword arguments forwarded to the pollution function. Values must be float, int, or str. | {} |
Returns:
| Type | Description |
|---|---|
Tensor | Tensor with selected images polluted, same shape as inputs. |
Raises:
| Type | Description |
|---|---|
ValueError | If pollution_type is not recognised. |
Source code in src/crackmnist_eccv/utility.py
enlarge_crack_tip ¶
enlarge_crack_tip(mask: ndarray, kernel_size: int = 3) -> ndarray
Enlarge the annotated crack tip region in a binary mask.
Expands each foreground pixel into a square neighbourhood of size kernel_size x kernel_size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| ndarray | 2D numpy array of shape (H, W) with foreground pixels encoded as 255 and background as 0. | required |
| int | Size of the square neighbourhood. Must be an odd integer (e.g. 1, 3, 5). Default is 3. | 3 |
Returns:
| Type | Description |
|---|---|
ndarray | New 2D mask of the same shape with the crack tip region enlarged. |
Source code in src/crackmnist_eccv/utility.py
generate_blob_mask_to_match_oar ¶
generate_blob_mask_to_match_oar(
shape: tuple[int, int],
*,
radius: int = 4,
target_min_oar: float = 0.1,
target_max_oar: float = 0.3,
max_draw_attempts: int = 2000,
rng: Generator | None = None,
) -> tuple[ndarray, float]
Create a binary mask whose OAR lies in [target_min_oar, target_max_oar].
Keeps stamping discs until the lower bound is reached, skipping discs that would push the OAR past target_max_oar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
| tuple[int, int] | (H, W) resolution of the mask. | required |
| int | Intended radius of each disc in pixels. | 4 |
| float | Lower bound of the desired OAR interval. | 0.1 |
| float | Upper bound of the desired OAR interval. | 0.3 |
| int | Safety cap on total centre-samples tried. | 2000 |
| Generator | None | Optional RNG for deterministic tests. | None |
Returns:
| Type | Description |
|---|---|
ndarray | Tuple of (mask, oar) where mask is a uint8 array and oar is |
float |
|
Raises:
| Type | Description |
|---|---|
ValueError | If target_min_oar > target_max_oar. |
RuntimeError | If max_draw_attempts is exceeded. |
Source code in src/crackmnist_eccv/utility.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |