PerceptualLoss<T>

Models & Types · AiDotNet.LossFunctions

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

Inheritance: LossFunctionBase<T> → PerceptualLoss<T>

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

Constructor Summary
PerceptualLoss(Func<Matrix<T>, Vector<Vector<T>>> featureExtractor, Vector<T> layerWeights) Initializes a new instance of the PerceptualLoss class.

Methods

Method Summary
T Calculate(Matrix<T> generated, Matrix<T> target) Calculates the Perceptual Loss between generated and target images.
T CalculateLoss(Vector<T> predicted, Vector<T> actual) This method is not used for Perceptual Loss as it requires image matrices.
Tensor<T> ComputeTapeLoss(Tensor<T> predicted, Tensor<T> target)
void SetFeatureExtractorNetwork(INeuralNetwork<T> network) Sets the neural network used for tape-differentiable feature extraction.