Logo colorization in HyperGAN and HNDL

Assets: 128×128 grayscale HG input (assets/hg-bw-128.png) · 128×128 single colorized badge (assets/hypergan-logo.png) · 512×512 4×4 colorized grid (assets/hypergan-logo-grid.png) · 736×512 workflow banner (assets/hypergan-colorizer-banner.png).
Packaged 128×128 colorization#
The wheel's colorization-128 recipe accepts an RGB image folder. It composites transparency onto white, derives luminance in an auxiliary graph, and conditions the generator on luminance and a 128-dimensional latent from a 10,000-particle prior. Run it by name or edit the matching source recipe.
hypergan validate colorization-128
hypergan train colorization-128 --set data.args.root=/path/to/images256×256 DINOv3 source example#
From a source checkout, configure the 256px DINOv3 recipe. Keep its relative HNDL paths intact. Set the image root, manifest path and digest in the training and held-out evaluation tables. Point the critic at the specified DINOv3 source revision and ViT-S/16 weights that match its recorded SHA-256. The data preparation section below describes how to generate the manifest.
hypergan validate examples/archive/logos-colorization-dinov3-256.toml --data
hypergan train examples/archive/logos-colorization-dinov3-256.toml --devices 0,1Run d1eef023 on two RTX A6000s with ParticleGAN 0.9.7 BISPF reached held-out FID-10k 6.37 at step 2,500. Between steps 4,000 and 4,027 it recorded chroma distance 0.01353, structure error 2.61e-7, diversity 0.09285 and random spread 0.9940. Training was interrupted at step 4,447. A fresh run with the 2.0.0 defaults selects EMPF. Recorded run and metric steps
Why the earlier 256×256 particle-routing recipe collapsed#
The earlier 256×256 experiment (examples/archive/logos-colorization-256.toml) had four
structural bottlenecks:
- No spatial conditioning in
G:ColorizationGenerator(z)took only a 1D vectorzwith no spatial input or skip connections frombatch.gray, forcing a single hard-routed particle indexk in {0..4095}to reconstruct the entire 2D logo geometry. - Disconnected adversarial and conditional paths: the adversarial phases
trained
G(z)unconditionally on uniform random particles, whileE(B)received only a grayscale L2 loss through a straight-through softmax surrogate and collapsed onto a single particle index. - Unconstrained RGB output: predicting raw RGB wasted generator capacity
re-learning known pixel luminance
Yand allowed saturated magenta/green tinting on white (Y = +1) backgrounds. - Pre-0.9 optimizer and critic penalty: training used raw Adam and
unnormalized
b_capthrough frozen backbones at batch size 16 prior to the ParticleGAN 0.9.2bispf_optimized+dualnorm+deterministic_orthogonalstabilization.
First-principles HNDL & hypergan-model/1 design#
examples/networks/colorization-generator-128.hndl and
examples/archive/logos-colorization-128.toml solve all four issues by construction
while showcasing HNDL and HyperGAN's declarative capabilities:
- Multi-port HNDL generator (
input_shape = { z = ["B", 128], gray = ["B", 1, 128, 128] }):- Multi-scale residual grayscale U-Net encoder (
128 -> 64 -> 32 -> 16 -> 8): Five declarativeresblock(..., norm="group_norm")stages extract full-resolution spatial edge/region features (e128,e64,e32,e16,e8). - Bounded loop (
for _ in range(2):) with RoPE + SwiGLUtransformer_blockandcross_attention: The particle latentzprojects into 8 learned palette tokens[B, 8, 256]. Inside the HNDL loop (which automatically suffixes node names0,1), the 64 spatial tokens run 2D-aware RoPE + SwiGLU self-attention and cross-attend directly to the 8 particle palette tokens. - Sub-pixel (
pixel_shuffle(2)) +resblockU-Net skip decoder with shape-inferredadaptive_norm: Each decoder stage upsamples viaconv+pixel_shuffle(2), concatenates the corresponding encoder skip (e16,e32,e64,e128), refines viaresblock, and modulates features withadaptive_norm(h, linear(z, name="style..."))where HNDL's bidirectional shape solver automatically infers2 * C(256,128,64) for eachlinear(z)projection.
- Multi-scale residual grayscale U-Net encoder (
- Bilinear particle palette synthesis (anti-mode-collapse by construction):
- At 128×128,
group_norm(h128, 8, affine=False)produces a 16-channel spatial region basisbasis[B, 16, 16384]with zero mean and unit variance across spatial positions, whilezprojects through a bias-freepalette = reshape(linear(z, 48, bias=False), 3, 16). - The raw RGB proposal is
tanh(scale(reshape(matmul(palette, basis), 3, 128, 128), 0.25)). - Because
palette_headhasbias=False,z = 0produces zero chroma (pure grayscale), so the generator cannot ignorezwithout being penalized by the chroma-sensitive critic.
- At 128×128,
- Exact 7-face polytope luminance lock in pure HNDL:
- Luminance in
ColorizationDataisY = 0.299 R + 0.587 G + 0.114 Bin[-1, 1]. - Iterating
x <- clamp(x + g_k * (gray - Y) * [1, 1, 1], -1, 1)over the 7 exact reciprocals of the non-empty subsets of{0.299, 0.587, 0.114}(1.0,1/0.886,1/0.701,1/0.587,1/0.413,1/0.299,1/0.114) projects any proposal onto the exact intersection of the RGB cube[-1, 1]^3and the luminance hyperplanew^T x = grayto float32 machine precision (< 1e-5). - White backgrounds (
gray = +1) map identically to(1, 1, 1), black (gray = -1) maps identically to(-1, -1, -1), andGrayscaleStructureDistanceis< 1e-5from step 0 without any auxiliary L2 loss. - Because
L(G(gray, z)) == grayis an exact bijection betweenX_hatand(gray, C_hat), the 3-channel multidepth DINOv3 projected criticD(X)vsD(G(gray, z))on the same batch is automatically a conditional critic overp(C | gray).
- Luminance in
Historical 256×256 particle-routing experiment (examples/archive/logos-colorization-256.toml)#
examples/archive/logos-colorization-256.toml is an experimental conditional particle
VAEGAN. It takes grayscale logos through a learned encoder, hard-routes each
query to one of 4,096 learned 128-dimensional particle centers, adds Gaussian
noise, and decodes an RGB image. There are no spatial skip connections. Changing
the noise gives multiple candidate colorizations of the same input. The narrow
particle bottleneck may also lose logo structure; useful quality and diversity
require training and evaluation, and are not established by the smoke test.
The routing follows ParticleGAN's vae_gan / particle_vae(hard=True):
the selected posterior component has the same fixed sigma as the prior and no
learned offset or variance. Sigma is calibrated once at prior initialization and
saved. For the uniform particle prior the joint KL is the constant log(4096),
so there is no optimized KL term. The hard forward route uses a soft
straight-through gradient. This is a surrogate gradient, not an unbiased
categorical estimator. See the original
ParticleGAN implementation.
GAN training samples the uniform particle prior, independently of the encoder. This keeps all particles eligible for generator training even if encoder routing collapses. The separate reconstruction objective follows the CIFAR recipe's encoder-only variant: it updates E through a frozen G and detached particle means. Particle spread regularization and lazy critic regularization remain configured separately.
B = BW(X)
z ~ uniform particle prior + fixed-sigma noise
D(X), D(G(z)) # adversarial: update D, G, prior
B -> E(B) -> selected center + noise -> G -> Xhat
L2 = mean((BW(Xhat) - B)^2) # reconstruction: update E onlyGrayscale reconstruction asks the encoder to recover structure without requiring
the original colors. It does not force each colorization to have plausible colors;
the unconditional GAN learns the overall RGB distribution. RGB reconstruction is
an available comparison: bind the objective's input to reconstruction
and its target to batch.real, and remove the unused reconstruction_gray
network. Neither form constrains aggregate encoder particle
usage. Hard routing and a moving decoder can still make the encoder collapse.
The discriminator receives a single RGB candidate, using the pretrained ResNet18 critic from the CIFAR recipe extended to 256×256:
RGB -> frozen ResNet18 layer1/layer2/layer3 -> three learned feature heads
RGB -> learned residual pixel branch with attention at 16x16
D(RGB) = (pixel_score + sum(feature_scores) / sqrt(3)) / sqrt(2)CIFARDiscriminator(image_size=256, feature_size=256) keeps the CIFAR feature
heads and score combination. Six pixel residual blocks reduce 256×256 to 4×4;
SAGAN attention remains at 16×16. The native pretrained feature maps are
64×64, 32×32 and 16×16, each projected and pooled to 4×4 by a learned head.
The existing fixed zero context and its cached pretrained features are retained
for recipe continuity; they contain no grayscale input or other training sample.
After this constant cache is populated, each candidate batch traverses the
backbone once. The pixel branch and three feature heads form one discriminator
with one scalar output and the same adversarial objective.
ResNet parameters and batch-normalization statistics stay frozen, but input
derivatives pass through the backbone, including the second derivatives needed
by critic regularization. Inputs are mapped from [-1,1] to [0,1] and ImageNet-normalized.
Training requires the cifar extra and a local, SHA256-pinned
resnet18-f37072fd.pth file; it does not download weights. The 32×32 CIFAR
architecture, initialization order and checkpoint layout retain their defaults.
This is the next discriminator experiment after the DINO + RGB owner run collapsed by step 3,428. Its earlier 1,500-step diversity result did not establish lasting stability or useful samples. ResNet is not yet a demonstrated cure. Generator, encoder, particles, losses, critic regularization and learning rates are unchanged.
The discriminator comparisons also expose these alternatives (use a fresh run when changing architecture):
DINOv3ProjectedDiscriminator(head="conv")replaces the linear head with spectrally normalized nonlinear convolutions. Its optionalpixel_width=32adds a learned RGB stem, concatenated with projected DINO features before the shared attention/head. The previous example used this configuration. It retained spread through 1,500 controlled updates, but the subsequent owner run collapsed.DINOv3MultiScaleDiscriminatorreads transformer blocks 2, 5, 8, and 11 in one backbone pass. Frozen random projections build and fuse a synthetic 32/16/8/4 pyramid, followed by four attention/convolution heads whose scalar outputs are averaged. DINOv3's native maps are all 16×16; this is a multidepth adaptation, not an exact reproduction of Projected GAN.DCGANDiscriminator256(width=32)is an RGB-only convolutional control with spectral normalization. It has no pretrained backbone.
The DINOv3 token and normalization audit, controlled comparisons, and their limits are recorded in the collapse report. The working CIFAR critic combines a learned pixel path with multiscale pretrained ResNet features, so it is not a pretrained-only counterpart to the original single-map DINO critic.
The earlier two-path DINOv3Discriminator remains available for existing
configurations and checkpoints. The new DINOv3ProjectedDiscriminator is a
different architecture: use a fresh run directory and its own configuration.
Data preparation#
Use hypergan.colorization_data.prepare_manifest(root, output) once to validate
the image inventory and record content hashes. Set manifest_sha256 in the
recipe to the returned digest. The source images are not modified. Preprocessing
applies EXIF orientation, composites transparency on white, and resizes with
aspect-preserving Lanczos fitting and white padding to the configured resolution
(128×128 or 256×256). Grayscale is 0.299 R + 0.587 G + 0.114 B; RGB and
grayscale are in [-1,1].
The SHA256 of each source image assigns approximately 95% to training and 5% to held-out evaluation. Byte-identical duplicates stay in the same split; perceptual near-duplicates can still cross it. Archives and other non-image files are inventoried but not expanded. Invalid files require explicit, recorded exclusions or source repair. Training verifies each accessed file and fails on changed or unreadable bytes. Added files do not silently enter an existing inventory.
Metrics and samples#
Use the held-out split for independent snapshot evaluation:
- Chroma distribution distance compares generated and reference color distributions. Lower is closer, but a good value cannot prove correct spatial placement or useful conditional diversity.
- Grayscale structure distance measures whether generated logos retain the source's edge structure. Under the 128×128 exact polytope luminance lock, structure distance is zero up to floating-point rounding by construction.
- Repeated-condition chroma diversity measures variation across several particle
draws (
repeats = 4) for each identical grayscale input. random_spreadmeasures pooled pixel pairwise RMS relative to the held-out reference. Near zero detects constant outputs; one matches overall spread.fid10k_heldoutevaluates 10,000 held-out samples withInceptionFID.
Keep the evaluation seed, count, held-out inventory and sample multiplicity
fixed when comparing checkpoints. Show grayscale inputs, source RGB and generated
RGB previews together. The comparison view has one example per row and labelled
X | B | X_hat columns. The separate random view shows uniform-prior samples;
g remains the conditional reconstruction. sampling.output explicitly binds
sampling and paired evaluation to reconstruction, so changing the GAN
input does not silently turn conditional metrics into unconditional comparisons.
A short smoke test establishes execution and recovery,
not a quality score. Do not select checkpoints solely by one color statistic.
Local workflow#
Run the 128×128 colorization recipe with a dedicated run directory:
hypergan train examples/image-folder-colorization.toml \
--run-dir ./runs/train-logos-colorization-128 -d 0Interrupt it with Ctrl-C to save a recoverable boundary. Repeating the same command resumes the latest complete checkpoint. Configuration and dataset/component dependency checks remain enforced on resume; HyperGAN release hashes remain provenance, not compatibility rejection keys.
Post-training single-image colorization (hypergan sample --input)#
Once trained, sample diverse colorizations of any single grayscale image (such as
the 128×128 grayscale HG monogram in docs/assets/hg-bw-128.png) from the
CLI or Python API. When a single conditioning image is passed via --input (or
inputs=... in Python), HyperGAN preprocesses it with the ColorizationData
aspect-preserving Lanczos fit and broadcasts it across all count particle draws:
hypergan sample logos-colorization-128 \
--input docs/assets/hg-bw-128.png \
--checkpoint 3000 \
--count 16 \
--seed 10000 \
--output docs/assets/hypergan-logo-grid.pngOr in Python:
import hypergan as hg
sample = hg.sample(
"logos-colorization-128",
inputs="docs/assets/hg-bw-128.png",
checkpoint=3000,
count=16,
seed=10000,
output="docs/assets/hypergan-logo-grid.png",
)To regenerate the full set of HyperGAN 2.0 logo assets (docs/assets/hg-bw-128.png,
docs/assets/hypergan-logo.png, docs/assets/hypergan-logo-grid.png, and
docs/assets/hypergan-colorizer-banner.png) with quantitative checkpoint metrics:
python scripts/generate_logo.py --run-dir logos-colorization-128