StyleGAN<T>

Models & Types · AiDotNet.NeuralNetworks

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

Inheritance: NeuralNetworkBase<T> → DeclaredModelLayoutBase<T> → ImageGeneratorModelLayoutBase<T> → StyleGAN<T>

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)

How It Works

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

Example

var options = new StyleGANOptions { };
var noise = Tensor<float>.CreateRandom(new[] { 1, 512 });
var trainX = Tensor<float>.CreateRandom(4, 8);
var trainY = Tensor<float>.CreateRandom(4, 2);
var mappingNetworkArchitecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 8);
var synthesisNetworkArchitecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 1);
var discriminatorArchitecture = new NeuralNetworkArchitecture<float>(inputFeatures: 8, outputSize: 1);
var result = new AiModelBuilder<float, Tensor<float>, Tensor<float>>()
    .ConfigureModel(new StyleGAN<float>(
        mappingNetworkArchitecture, synthesisNetworkArchitecture,
        discriminatorArchitecture,
        latentSize: 512, intermediateLatentSize: 512,
        inputType: InputType.OneDimensional))
    .Build(trainX, trainY);
var generated = result.Predict(noise);

Constructors

Constructor Summary
StyleGAN(NeuralNetworkArchitecture<T> mappingNetworkArchitecture, NeuralNetworkArchitecture<T> synthesisNetworkArchitecture, NeuralNetworkArchitecture<T> discriminatorArchitecture, int latentSize, int intermediateLatentSize, InputType inputType, StyleGANOptions options, ILossFunction<T> lossFunction) Initializes a new instance of the StyleGAN class.

Properties

Property Summary
NeuralNetworkBase<T> Discriminator { get; set; } Gets the discriminator network.
NeuralNetworkBase<T> MappingNetwork { get; set; } Gets the mapping network that transforms Z to W.
NeuralNetworkBase<T> SynthesisNetwork { get; set; } Gets the synthesis network that generates images from styles.

Methods

Method Summary
Dictionary<string, Tensor<T>> GetNamedLayerActivations(Tensor<T> input)
ModelMetadata<T> GetModelMetadata()
Tensor<T> Generate(Tensor<T> latentCodes) Generates images from latent codes.
Tensor<T> GenerateRandomLatentCodes(int batchSize) Generates random latent codes using vectorized Gaussian noise generation.
Tensor<T> GenerateWithStyleMixing(Tensor<T> latentCodes1, Tensor<T> latentCodes2) Generates images with style mixing.
ValueTuple<T, T> TrainStep(Tensor<T> realImages, Tensor<T> latentCodes) Performs one training step for StyleGAN.