Table of Contents

Class StyleGAN<T>

Namespace
AiDotNet.NeuralNetworks
Assembly
AiDotNet.dll

Represents a StyleGAN (Style-Based Generator Architecture for GANs) that generates high-quality images with fine-grained control over image style at different levels.

public class StyleGAN<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations.

Inheritance
StyleGAN<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Inherited Members
Extension Methods

Remarks

StyleGAN introduces several key innovations: - Style-based generator with mapping network and synthesis network - Adaptive Instance Normalization (AdaIN) for style injection - Stochastic variation through noise injection - Style mixing for disentangled control - Progressive growing for high-resolution images - State-of-the-art image quality

For Beginners: StyleGAN generates incredibly realistic images with fine control.

Key innovations:

  • Mapping Network: Transforms random noise into style codes
  • Style Injection: Injects style at each layer via AdaIN
  • Noise Injection: Adds stochastic variation (hair, pores, etc.)
  • Style Mixing: Combines styles from different sources
  • Progressive Growing: Starts small, gradually adds detail

Architecture:

  1. Mapping Network (Z → W): Transforms latent code to intermediate space
  2. Synthesis Network: Generates image with style injection at each layer
  3. Each layer: Upsample → Conv → AdaIN → Noise → Conv → AdaIN → Noise

Why it's better:

  • Exceptional image quality
  • Disentangled style control (separate coarse/fine features)
  • Style mixing (combine different sources)
  • Perceptual path length is shorter

Applications:

  • High-quality face generation
  • Style transfer and manipulation
  • Image editing and synthesis
  • Creative AI applications

Reference: Karras et al., "A Style-Based Generator Architecture for Generative Adversarial Networks" (2019)

Constructors

StyleGAN(NeuralNetworkArchitecture<T>, NeuralNetworkArchitecture<T>, NeuralNetworkArchitecture<T>, int, int, InputType, ILossFunction<T>?, double, bool, double)

Initializes a new instance of the StyleGAN<T> class.

public StyleGAN(NeuralNetworkArchitecture<T> mappingNetworkArchitecture, NeuralNetworkArchitecture<T> synthesisNetworkArchitecture, NeuralNetworkArchitecture<T> discriminatorArchitecture, int latentSize, int intermediateLatentSize, InputType inputType, ILossFunction<T>? lossFunction = null, double initialLearningRate = 0.001, bool enableStyleMixing = true, double styleMixingProbability = 0.9)

Parameters

mappingNetworkArchitecture NeuralNetworkArchitecture<T>

Architecture for the mapping network (Z → W).

synthesisNetworkArchitecture NeuralNetworkArchitecture<T>

Architecture for the synthesis network.

discriminatorArchitecture NeuralNetworkArchitecture<T>

Architecture for the discriminator.

latentSize int

Size of input latent code Z.

intermediateLatentSize int

Size of intermediate latent code W.

inputType InputType

Input type.

lossFunction ILossFunction<T>

Optional loss function.

initialLearningRate double

Initial learning rate. Default is 0.001.

enableStyleMixing bool

Enable style mixing. Default is true.

styleMixingProbability double

Probability of style mixing. Default is 0.9.

Properties

Discriminator

Gets the discriminator network.

public ConvolutionalNeuralNetwork<T> Discriminator { get; }

Property Value

ConvolutionalNeuralNetwork<T>

MappingNetwork

Gets the mapping network that transforms Z to W.

public ConvolutionalNeuralNetwork<T> MappingNetwork { get; }

Property Value

ConvolutionalNeuralNetwork<T>

Remarks

The mapping network is typically an 8-layer MLP that learns to map the input latent space Z to an intermediate latent space W with better disentanglement properties.

For Beginners: The "style organizer" network.

Takes: Random noise (Z) Returns: Organized style codes (W) Why: W space has better separated features Result: Easier to control individual aspects

ParameterCount

Gets the total number of trainable parameters in the StyleGAN.

public override int ParameterCount { get; }

Property Value

int

SynthesisNetwork

Gets the synthesis network that generates images from styles.

public ConvolutionalNeuralNetwork<T> SynthesisNetwork { get; }

Property Value

ConvolutionalNeuralNetwork<T>

Remarks

The synthesis network applies styles at multiple resolutions through AdaIN. It starts from a learned constant and progressively generates higher resolutions.

For Beginners: The "image painter" network.

Process:

  1. Starts from a learned constant (4x4)
  2. Applies style via AdaIN at each layer
  3. Adds random noise for details
  4. Upsamples to next resolution
  5. Repeats until final resolution

Methods

CreateNewInstance()

Creates a new instance of the same type as this neural network.

protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

A new instance of the same neural network type.

Remarks

For Beginners: This creates a blank version of the same type of neural network.

It's used internally by methods like DeepCopy and Clone to create the right type of network before copying the data into it.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes network-specific data that was not covered by the general deserialization process.

protected override void DeserializeNetworkSpecificData(BinaryReader reader)

Parameters

reader BinaryReader

The BinaryReader to read the data from.

Remarks

This method is called at the end of the general deserialization process to allow derived classes to read any additional data specific to their implementation.

For Beginners: Continuing the suitcase analogy, this is like unpacking that special compartment. After the main deserialization method has unpacked the common items (layers, parameters), this method allows each specific type of neural network to unpack its own unique items that were stored during serialization.

Generate(Tensor<T>)

Generates images from latent codes.

public Tensor<T> Generate(Tensor<T> latentCodes)

Parameters

latentCodes Tensor<T>

Latent codes Z. Can be any rank - will be reshaped/padded as needed.

Returns

Tensor<T>

Generated images.

GenerateRandomLatentCodes(int)

Generates random latent codes using vectorized Gaussian noise generation. Uses Engine.GenerateGaussianNoise for SIMD/GPU acceleration.

public Tensor<T> GenerateRandomLatentCodes(int batchSize)

Parameters

batchSize int

Returns

Tensor<T>

GenerateWithStyleMixing(Tensor<T>, Tensor<T>)

Generates images with style mixing.

public Tensor<T> GenerateWithStyleMixing(Tensor<T> latentCodes1, Tensor<T> latentCodes2)

Parameters

latentCodes1 Tensor<T>

First set of latent codes (for coarse features). Can be any rank.

latentCodes2 Tensor<T>

Second set of latent codes (for fine features). Can be any rank.

Returns

Tensor<T>

Generated images with mixed styles.

GetModelMetadata()

Gets the metadata for this neural network model.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetaData object containing information about the model.

InitializeLayers()

Initializes the layers of the neural network based on the architecture.

protected override void InitializeLayers()

Remarks

For Beginners: This method sets up all the layers in your neural network according to the architecture you've defined. It's like assembling the parts of your network before you can use it.

Predict(Tensor<T>)

Makes a prediction using the neural network.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

The input data to process.

Returns

Tensor<T>

The network's prediction.

Remarks

For Beginners: This is the main method you'll use to get results from your trained neural network. You provide some input data (like an image or text), and the network processes it through all its layers to produce an output (like a classification or prediction).

SerializeNetworkSpecificData(BinaryWriter)

Serializes network-specific data that is not covered by the general serialization process.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

The BinaryWriter to write the data to.

Remarks

This method is called at the end of the general serialization process to allow derived classes to write any additional data specific to their implementation.

For Beginners: Think of this as packing a special compartment in your suitcase. While the main serialization method packs the common items (layers, parameters), this method allows each specific type of neural network to pack its own unique items that other networks might not have.

Train(Tensor<T>, Tensor<T>)

Trains the neural network on a single input-output pair.

public override void Train(Tensor<T> input, Tensor<T> expectedOutput)

Parameters

input Tensor<T>

The input data.

expectedOutput Tensor<T>

The expected output for the given input.

Remarks

This method performs one training step on the neural network using the provided input and expected output. It updates the network's parameters to reduce the error between the network's prediction and the expected output.

For Beginners: This is how your neural network learns. You provide: - An input (what the network should process) - The expected output (what the correct answer should be)

The network then:

  1. Makes a prediction based on the input
  2. Compares its prediction to the expected output
  3. Calculates how wrong it was (the loss)
  4. Adjusts its internal values to do better next time

After training, you can get the loss value using the GetLastLoss() method to see how well the network is learning.

TrainStep(Tensor<T>, Tensor<T>)

Performs one training step for StyleGAN.

public (T discriminatorLoss, T generatorLoss) TrainStep(Tensor<T> realImages, Tensor<T> latentCodes)

Parameters

realImages Tensor<T>

Real images.

latentCodes Tensor<T>

Random latent codes Z.

Returns

(T Accuracy, T Loss)

Tuple of (discriminator loss, generator loss).

Remarks

StyleGAN training follows the standard GAN training procedure but with the style-based generator. Style mixing is applied during training.

For Beginners: One round of StyleGAN training.

Steps:

  1. Map latent codes Z to style codes W
  2. Optionally apply style mixing
  3. Generate images using styles
  4. Train discriminator on real/fake images
  5. Train generator to fool discriminator

UpdateParameters(Vector<T>)

Updates the parameters of all networks in the StyleGAN.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The new parameters vector containing parameters for all networks.

Exceptions

ArgumentNullException

Thrown when parameters is null.

ArgumentException

Thrown when parameters length doesn't match expected total.