konfai.metric package

Submodules

konfai.metric.measure module

Criterion and metric implementations used by KonfAI workflows.

class konfai.metric.measure.Criterion[source]

Bases: Module, ABC

get_name()[source]
abstractmethod forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.CriterionWithInit[source]

Bases: Criterion

accepts_init = True
abstractmethod init(model, output_group, target_group)[source]
Return type:

str

class konfai.metric.measure.CriterionWithAttribute[source]

Bases: Criterion

accepts_attributes = True
abstractmethod forward(output, *targets, attributes)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.MaskedLoss(loss, mode_image_masked)[source]

Bases: Criterion

static get_mask(targets)[source]
Return type:

Tensor | None

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

tuple[Tensor, float]

class konfai.metric.measure.MSE(reduction='mean')[source]

Bases: MaskedLoss

class konfai.metric.measure.MAE(reduction='mean')[source]

Bases: MaskedLoss

class konfai.metric.measure.ME[source]

Bases: MaskedLoss

class konfai.metric.measure.MAESaveMap(reduction='mean', dataset=None, group=None)[source]

Bases: MAE

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_name()[source]
Return type:

str

class konfai.metric.measure.PSNR(dynamic_range=None)[source]

Bases: MaskedLoss

class konfai.metric.measure.SSIM(dynamic_range=None)[source]

Bases: MaskedLoss

class konfai.metric.measure.LPIPS(model='alex')[source]

Bases: MaskedLoss

static normalize(tensor)[source]
Return type:

Tensor

static preprocessing(tensor)[source]
Return type:

Tensor

class konfai.metric.measure.TRE[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class konfai.metric.measure.Dice(labels=None)[source]

Bases: Criterion

static flatten(tensor)[source]
Return type:

Tensor

static dice_per_channel(tensor, target)[source]
Return type:

Tensor

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

tuple[Tensor, float]

class konfai.metric.measure.DiceSaveMap(labels=None, dataset=None, group=None)[source]

Bases: Dice

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_name()[source]
Return type:

str

class konfai.metric.measure.GradientImages[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.BCE(target=0)[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.PatchGanLoss(target=0)[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.WGP[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.Gram[source]

Bases: Criterion

static compute_gram(tensor)[source]
forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.PerceptualLoss(model_loader=<konfai.network.network.ModelLoader object>, path_model='name', modules={'UNetBlock_0.DownConvBlock.Activation_1': <konfai.metric.measure.PerceptualLoss.Module object>}, shape=[128, 128, 128])[source]

Bases: Criterion

class Module(losses={'Gram': 1, 'torch:nn:L1Loss': 1})[source]

Bases: object

get_loss()[source]
Return type:

dict[Module, float]

modules_loss: dict[str, dict[Module, float]]
models: dict[int, Module]
preprocessing(tensor)[source]
Return type:

Tensor

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.KLDivergence(shape, dim=100, mu=0, std=1)[source]

Bases: CriterionWithInit

init(model, output_group, target_group)[source]
Return type:

str

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.Accuracy[source]

Bases: Criterion

n: int
forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.TripletLoss[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.L1LossRepresentation[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.FocalLoss(gamma=2.0, alpha=[0.5, 2.0, 0.5, 0.5, 1], reduction='mean')[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.FID[source]

Bases: Criterion

class InceptionV3[source]

Bases: Module

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

static preprocess_images(image)[source]
Return type:

Tensor

static get_features(images, model)[source]
Return type:

ndarray

static calculate_fid(real_features, generated_features)[source]
Return type:

float

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.MutualInformationLoss(num_bins=23, sigma_ratio=0.5, smooth_nr=1e-07, smooth_dr=1e-07)[source]

Bases: Module

parzen_windowing(pred, target)[source]
Return type:

tuple[Tensor, Tensor, Tensor, Tensor]

parzen_windowing_gaussian(img)[source]
Return type:

tuple[Tensor, Tensor]

forward(pred, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.CrossEntropyLoss(weight=None, reduction='mean')[source]

Bases: Criterion

forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.IMPACTReg(name='Reg', model_name='TS/M291.pt', shape=[0, 0], in_channels=3, loss='torch:nn:L1Loss', weights=[0, 1])[source]

Bases: CriterionWithAttribute

class Weights(weights=[0, 1])[source]

Bases: object

modules_loss: dict[str, dict[Module, float]]
model: Module
preprocessing(tensor, attribute)[source]
Return type:

list[Tensor]

get_name()[source]
forward(output, *targets, attributes)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

tuple[Tensor, float]

class konfai.metric.measure.IMPACTSynth(model_content_name, model_style_name, shape_content=[0, 0], shape_style=[0, 0], in_channels_content=1, in_channels_style=1, weights_criterion_content=[0, 0, 1], weights_criterion_style=[1, 1, 1])[source]

Bases: CriterionWithAttribute

class Weights(weights=[0, 1])[source]

Bases: object

model_content: Module | None
model_style: Module | None
forward(output, *targets, attributes)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

tuple[Tensor, float]

class konfai.metric.measure.SAM_Perceptual[source]

Bases: CriterionWithAttribute

model: Module | None
preprocessing(tensor, attribute)[source]
Return type:

list[Tensor]

forward(output, *targets, attributes)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

tuple[Tensor, float]

class konfai.metric.measure.Variance(name='Variance')[source]

Bases: Criterion

get_name()[source]
forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

class konfai.metric.measure.Mean(name='Mean')[source]

Bases: Criterion

get_name()[source]
forward(output, *targets)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

konfai.metric.schedulers module

Schedulers used to modulate metric and loss weights during training.

class konfai.metric.schedulers.Scheduler(start_value)[source]

Bases: object

Base class for scalar schedulers used by KonfAI criteria.

step(it)[source]
abstractmethod get_value()[source]
Return type:

float

class konfai.metric.schedulers.Constant(value=1)[source]

Bases: Scheduler

Scheduler returning a constant value for all iterations.

get_value()[source]
Return type:

float

class konfai.metric.schedulers.CosineAnnealing(start_value=1, eta_min=1e-05, t_max=100)[source]

Bases: Scheduler

Cosine annealing scheduler for criterion weights.

get_value()[source]
class konfai.metric.schedulers.Warmup(optimizer, warmup_steps=10, last_epoch=-1, verbose='deprecated')[source]

Bases: LambdaLR

Learning-rate warmup wrapper compatible with PyTorch optimizers.

static warmup(warmup_steps, step)[source]
Return type:

float

class konfai.metric.schedulers.PolyLRScheduler(optimizer, initial_lr, max_steps, exponent=0.9, current_step=None)[source]

Bases: _LRScheduler

step(current_step=None)[source]

Step the scheduler.

Parameters:

epoch (int, optional) –

Deprecated since version 1.4: If provided, sets last_epoch to epoch and uses _get_closed_form_lr() if it is available. This is not universally supported. Use step() without arguments instead.

Note

Call this method after calling the optimizer’s step().

get_last_lr()[source]

Get the most recent learning rates computed by this scheduler.

Returns:

A list of learning rates with entries for each of the optimizer’s param_groups, with the same types as their group["lr"]s.

Return type:

list[float | Tensor]

Note

The returned Tensors are copies, and never alias the optimizer’s group["lr"]s.

Module contents

Metric and scheduler primitives used by KonfAI training and evaluation.