cloak
Module for cloak loss functions.
Functions:
| Name | Description |
|---|---|
composite_cloak_loss_factory |
Create a loss function to train a Stained Glass Transform using |
image_similarity_cloak_loss_factory |
Create a loss function combining a task loss with the image-similarity privacy loss. |
composite_cloak_loss_factory
¶
composite_cloak_loss_factory(
noisy_model: NoisyModel[ModuleT, ..., NoiseLayerT],
loss_function: Callable[LossFunctionP, Tensor],
alpha: float,
respect_std_mask: bool = True,
) -> tuple[
Callable[sg_transform_loss.LossFunctionP, torch.Tensor],
Callable[[], ComponentLossesDict],
Callable[[], HyperparametersDict],
]
Create a loss function to train a Stained Glass Transform using negative_log_mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NoisyModel[ModuleT, ..., NoiseLayerT]
|
The model containing both the base model and the Stained Glass Transform. |
required |
|
Callable[LossFunctionP, Tensor]
|
The base model task loss function to wrap. |
required |
|
float
|
The interpolation factor between the task loss (maximizing task performance) and the Stained Glass Transform loss (maximizing transformation strength). Should be in the range [0, 1], where 0 corresponds to higher task performance and 1 corresponds to higher transformation strength. |
required |
|
bool
|
Some NoiseLayers' |
True
|
Returns:
| Type | Description |
|---|---|
tuple[Callable[sg_transform_loss.LossFunctionP, torch.Tensor], Callable[[], ComponentLossesDict], Callable[[], HyperparametersDict]]
|
A tuple of 3 functions: the composite loss function, a function to retrieve the loss components, and a function to retrieve the |
tuple[Callable[sg_transform_loss.LossFunctionP, torch.Tensor], Callable[[], ComponentLossesDict], Callable[[], HyperparametersDict]]
|
hyperparameters. These functions may be called at most once each after a forward pass through both models. |
image_similarity_cloak_loss_factory
¶
image_similarity_cloak_loss_factory(
noisy_model: NoisyModel[ModuleT, ..., NoiseLayerT],
loss_function: Callable[LossFunctionP, Tensor],
*,
ms_ssim_weight: float,
pearson_weight: float,
image_mean: Sequence[float],
image_std: Sequence[float],
super_batched: bool = False,
gamma_values: Sequence[float] = (0.5, 2.0),
blur_sigma: float = 2.0,
wavelet_threshold: float = 0.1
) -> tuple[
Callable[sg_transform_loss.LossFunctionP, torch.Tensor],
Callable[[], ComponentLossesDict],
Callable[[], HyperparametersDict],
]
Create a loss function combining a task loss with the image-similarity privacy loss.
Registers two activation hooks on noisy_model.noise_layer to capture both its
first positional input (the clean image) and its forward output (the noisy image),
then composes
multi_attacker_privacy_loss
with the supplied task loss as task_loss + ms_ssim_weight * ms_ssim_total + pearson_weight * pearson_total.
Inputs to the noise layer are assumed to be channel-normalized (e.g. ImageNet
normalization) — supply image_mean and image_std so the captured tensors can
be denormalized into [0, 1] before MS-SSIM. Privacy: the captured tensors are
only used to compute the loss; callers should not log them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NoisyModel[ModuleT, ..., NoiseLayerT]
|
The model containing both the base model and the Stained Glass Transform. |
required |
|
Callable[LossFunctionP, Tensor]
|
The base model task loss function to wrap. Must return a scalar tensor. |
required |
|
float
|
Weight on the aggregate MS-SSIM privacy term (mean of nine MS-SSIM components). |
required |
|
float
|
Weight on the aggregate squared-Pearson privacy term (mean of six components). |
required |
|
Sequence[float]
|
Per-channel mean used to denormalize the captured images into |
required |
|
Sequence[float]
|
Per-channel std used to denormalize the captured images into |
required |
|
bool
|
Set to |
False
|
|
Sequence[float]
|
Forwarded to
|
(0.5, 2.0)
|
|
float
|
Forwarded to
|
2.0
|
|
float
|
Forwarded to
|
0.1
|
Returns:
| Type | Description |
|---|---|
tuple[Callable[sg_transform_loss.LossFunctionP, torch.Tensor], Callable[[], ComponentLossesDict], Callable[[], HyperparametersDict]]
|
A tuple of 3 functions: the composite loss function, a function to retrieve the loss components |
tuple[Callable[sg_transform_loss.LossFunctionP, torch.Tensor], Callable[[], ComponentLossesDict], Callable[[], HyperparametersDict]]
|
(including each of the 15 named components plus the two totals and the composite loss), and a |
tuple[Callable[sg_transform_loss.LossFunctionP, torch.Tensor], Callable[[], ComponentLossesDict], Callable[[], HyperparametersDict]]
|
function to retrieve the hyperparameters. These functions may be called at most once each after |
tuple[Callable[sg_transform_loss.LossFunctionP, torch.Tensor], Callable[[], ComponentLossesDict], Callable[[], HyperparametersDict]]
|
a forward pass through both models. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Added in version v3.39.0. Cloak loss factory wrapping the image-similarity privacy loss.