Table of Contents

Class PerceptualLoss<T>

Namespace
AiDotNet.LossFunctions
Assembly
AiDotNet.dll

Implements the Perceptual Loss function for comparing high-level features of images.

public class PerceptualLoss<T> : LossFunctionBase<T>, ILossFunction<T>

Type Parameters

T

The numeric type used for calculations (e.g., float, double).

Inheritance
PerceptualLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Perceptual Loss is a type of loss function used primarily in image processing and generative models. Unlike pixel-wise losses (like MSE) that compare images pixel by pixel, perceptual loss compares high-level features extracted from the images.

The key idea is to:

  1. Pass both the generated image and target image through a pre-trained network (like VGG)
  2. Extract features from various layers of this network
  3. Compare these features rather than raw pixels

This approach is more aligned with human perception because:

  • It focuses on semantic content rather than exact pixel values
  • It captures textures, patterns, and structures that are perceptually important
  • It allows for some flexibility in pixel-level details while preserving overall appearance

Perceptual Loss is commonly used in:

  • Style transfer algorithms
  • Super-resolution models
  • Image-to-image translation
  • Any task where the "look" of an image is more important than exact pixel reproduction

Constructors

PerceptualLoss(Func<Matrix<T>, Vector<Vector<T>>>, Vector<T>)

Initializes a new instance of the PerceptualLoss class.

public PerceptualLoss(Func<Matrix<T>, Vector<Vector<T>>> featureExtractor, Vector<T> layerWeights)

Parameters

featureExtractor Func<Matrix<T>, Vector<Vector<T>>>

Function that extracts features from images.

layerWeights Vector<T>

Weights for each feature layer.

Methods

Calculate(Matrix<T>, Matrix<T>)

Calculates the Perceptual Loss between generated and target images.

public T Calculate(Matrix<T> generated, Matrix<T> target)

Parameters

generated Matrix<T>

The generated image as a matrix.

target Matrix<T>

The target image as a matrix.

Returns

T

The perceptual loss value.

CalculateDerivative(Vector<T>, Vector<T>)

This method is not used for Perceptual Loss as it requires image matrices.

public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual (target) values vector.

Returns

Vector<T>

Throws NotSupportedException.

Exceptions

NotSupportedException

Always thrown as Perceptual Loss requires image matrices.

CalculateLoss(Vector<T>, Vector<T>)

This method is not used for Perceptual Loss as it requires image matrices.

public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted values vector.

actual Vector<T>

The actual (target) values vector.

Returns

T

Throws NotSupportedException.

Exceptions

NotSupportedException

Always thrown as Perceptual Loss requires image matrices.