Skip to content

patch_cloak_frequency_space

Classes:

Name Description
PatchCloakNoiseLayerFrequencySpace

Applies an input-dependent stochastic transformation to an image in frequency space in non-overlapping patches using Conv2d

Normalization

Bases: str, Enum

Mode corresponding to the normalization of tensors.

Methods:

Name Description
has

Return whether the string value is defined in this enumerated string class.

has classmethod

has(value: str) -> bool

Return whether the string value is defined in this enumerated string class.

Parameters:

Name Type Description Default

value

str

The string to determine whether is part of this enum.

required

Returns:

Type Description
bool

Whether the string value is part of this enum class.

PatchCloakNoiseLayerFrequencySpace

Bases: PatchCloakNoiseLayer

Applies an input-dependent stochastic transformation to an image in frequency space in non-overlapping patches using Conv2d estimators, with standard deviations parameterized by CloakStandardDeviationParameterization, optional standard deviation-based input masking using BatchwiseChannelwisePatchwisePercentMasker, and optional output clamping.

Parameters:

Name Type Description Default

color_channels

int

The number of color channels in the input.

required

patch_size

int | tuple[int, int]

Size of the patches to segment the input into. If an integer is given, a square patch is used.

required

scale

tuple[float, float]

Minimum and maximum values of the range of standard deviations of the generated stochastic transformation.

required

percent_to_mask

float

The percentage of the outputs to mask per patch.

required

shallow

float

A fixed temperature like parameter which alters the scale of the standard deviation of the stochastic transformation.

1.0

value_range

tuple[float | None, float | None]

Minimum and maximum values of the range to clamp the output into.

(-1.0, 1.0)

frequency_range

tuple[float, float]

Minimum and maximum values of the range in the frequency domain to clamp the output into.

(-inf, inf)

padding_mode

Literal['constant', 'reflect', 'replicate', 'circular']

Type of padding. One of: constant, reflect, replicate, or circular. Defaults to "constant".

'constant'

padding_value

float

Value to pad with if padding_mode is constant.

0.0

learn_locs_weights

bool

Whether to learn the weight parameters for the locs estimator. If True, only the weights will be learned, and the bias remains constant, otherwise weights are initialized to zero and only the bias is learned. Defaults to True.

True

preserve_average_color

bool

Whether to preserve the average color per patch of the original image in the perturbed output. Defaults to True.

True

normalization

Normalization

Which Normalization to apply to the pixel space representation of the perturbed output to make it a valid image. Defaults to "clamp".

<Normalization.CLAMP: 'clamp'>

seed

int | None

Seed for the random number generator used to generate the stochastic transformation. If None, the global RNG state is used.

None

Raises:

Type Description
ValueError

If the patch_size is not square.

NotImplementedError

If a known normalization mode without a corresponding normalization function is encountered.

Methods:

Name Description
__call__

Transform the input data.

__getstate__

Prepare a serializable copy of self.__dict__.

__init__
__init_subclass__

Set the default dtype to torch.float32 inside all subclass __init__ methods.

__setstate__

Restore from a serialized copy of self.__dict__.

forward

Transform the input data.

get_applied_transform_components_factory

Create a function that returns the elements of the transform components ('mean' and 'std') applied during the most recent

get_transformed_output_factory

Create a function that returns the transformed output from the most recent forward pass.

initial_seed

Return the initial seed of the CPU device's random number generator.

manual_seed

Seed each of the random number generators.

reset_parameters

Reinitialize parameters and buffers.

seed

Seed each of the random number generators using a non-deterministic random number.

Attributes:

Name Type Description
patch_size tuple[int, int]

The size of the patches to segment the input into.

patch_size property

patch_size: tuple[int, int]

The size of the patches to segment the input into.

__call__

__call__(
    input: Tensor,
    noise_mask: Tensor | None = None,
    **kwargs: Any,
) -> torch.Tensor

Transform the input data.

Parameters:

Name Type Description Default

input

Tensor

The input to transform.

required

noise_mask

Tensor | None

An optional mask that selects the elements of input to transform. Where the mask is False, the original input value is returned. Also used to select the elements of the sampled standard deviations to use to mask the input. If None, the entire input is transformed.

None

**kwargs

Any

Additional keyword arguments to the estimator modules.

required

__getstate__

__getstate__() -> dict[str, Any]

Prepare a serializable copy of self.__dict__.

__init__

__init__(color_channels: int, patch_size: int | tuple[int, int], scale: tuple[float, float], percent_to_mask: float, shallow: float = 1.0, value_range: tuple[float | None, float | None] = (-1.0, 1.0), frequency_range: tuple[float, float] = (-inf, inf), padding_mode: Literal['constant', 'reflect', 'replicate', 'circular'] = 'constant', padding_value: float = 0.0, learn_locs_weights: bool = True, preserve_average_color: bool = True, normalization: Normalization = <Normalization.CLAMP: 'clamp'>, seed: int | None = None) -> None

Changed in version 0.10.0: `threshold` and `percent_threshold` parameters were removed in favor of `percent_to_mask`

__init_subclass__

__init_subclass__() -> None

Set the default dtype to torch.float32 inside all subclass __init__ methods.

__setstate__

__setstate__(state: dict[str, Any]) -> None

Restore from a serialized copy of self.__dict__.

forward

forward(
    input: Tensor,
    noise_mask: Tensor | None = None,
    **kwargs: Any,
) -> torch.Tensor

Transform the input data.

Parameters:

Name Type Description Default

input

Tensor

The input to transform.

required

noise_mask

Tensor | None

An optional mask that selects the elements of input to transform. Where the mask is 0, the original input value is returned. Also used to select the elements of the sampled standard deviations to use to mask the input. If None, the entire input is transformed.

None

**kwargs

Any

Additional keyword arguments to the estimator modules.

required

Returns:

Type Description
torch.Tensor

The transformed input data.

get_applied_transform_components_factory

get_applied_transform_components_factory() -> Callable[
    [], dict[str, torch.Tensor]
]

Create a function that returns the elements of the transform components ('mean' and 'std') applied during the most recent forward pass.

Specifically, the applied elements are those selected by the noise mask (if supplied) and standard deviation mask (if std_estimator.masker is not None). If no masks are used, all elements are returned.

The applied transform components are returned flattened.

This function is intended to be used to log histograms of the transform components.

Returns:

Type Description
Callable[[], dict[str, torch.Tensor]]

A function that returns the the elements of the transform components applied during the most recent forward pass.

Examples:

>>> from torch import nn
>>> from stainedglass_core import model as sg_model, noise_layer as sg_noise_layer
>>> base_model = nn.Linear(20, 2)
>>> noisy_model = sg_model.NoisyModel(
...     sg_noise_layer.CloakNoiseLayer1,
...     base_model,
...     target_parameter="input",
... )
>>> get_applied_transform_components = (
...     noisy_model.noise_layer.get_applied_transform_components_factory()
... )
>>> input = torch.ones(1, 20)
>>> noise_mask = torch.tensor(5 * [False] + 15 * [True])
>>> output = noisy_model(input, noise_mask=noise_mask)
>>> applied_transform_components = get_applied_transform_components()
>>> applied_transform_components
{'mean': tensor(...), 'std': tensor(...)}
>>> {
...     component_name: component.shape
...     for component_name, component in applied_transform_components.items()
... }
{'mean': torch.Size([15]), 'std': torch.Size([15])}

get_transformed_output_factory

get_transformed_output_factory() -> Callable[
    [], torch.Tensor
]

Create a function that returns the transformed output from the most recent forward pass.

If super batching is active, only the transformed half of the super batch output is returned.

Returns:

Type Description
Callable[[], torch.Tensor]

A function that returns the transformed output from the most recent forward pass.

Examples:

>>> from stainedglass_core import noise_layer as sg_noise_layer
>>> noise_layer = sg_noise_layer.CloakNoiseLayer1()
>>> get_transformed_output = noise_layer.get_transformed_output_factory()
>>> input = torch.ones(2, 3, 32, 32)
>>> output = noise_layer(input)
>>> transformed_output = get_transformed_output()
>>> assert output.equal(transformed_output)

initial_seed

initial_seed() -> int

Return the initial seed of the CPU device's random number generator.

manual_seed

manual_seed(seed: int | None) -> None

Seed each of the random number generators.

Setting seed to None will destroy any existing generators.

Parameters:

Name Type Description Default

seed

int | None

The seed to set.

required

reset_parameters

reset_parameters() -> None

Reinitialize parameters and buffers.

This method is useful for initializing tensors created on the meta device.

seed

seed() -> None

Seed each of the random number generators using a non-deterministic random number.