asvetliakov/ai-pbr-models

13

stars

208

commits

Python

primary language

Mar 26, 2026

updated

README

🏔️ Skyrim PBR Pipeline — Rev 7.0 (19 Jule 2025)


0. Legend

  • S–phases = SegFormer
  • A–phases = UNet-Albedo (UNetAlbedo in code)
  • M–phases = UNet-Maps (UNetMaps)

cond_ch = 512 ⇒ FiLM conditioning enabled from the first epoch for both U-Nets.
Class weights = 1 / √freq(class); WeightedRandomSampler active in every phase except the final hi-res stages.


1. SegFormer (S)

PhaseData mix (train)TrainablesCrop (px)Augment † (p per sample)EpochsLR & SchedulerLoss
S0 – Warm-up100 % Skyenc + dec + heads256flips, rot90 (1.0), SkyPhoto (0.6)30OneCycle LR 1e-4 → 4e-4 → 1e-5 (pct 0.15)0.8 Focal CE + 0.2 Dice
S1 – Domain focus100 % Skyenc (block 0 frozen) + dec + heads (LLRD 0.9)512same15Cosine start 1e-4, ηₘᵢₙ 8e-6same
S2 – Hi-res mix100 % Skyenc (block 0 frozen) + dec + heads (LLRD 0.9)768same12Cosine 8e-5 → ηₘᵢₙ 8e-6same
S3 – Full-res polish100 % Skyenc block 3 (0,1,2 frozen) + dec + heads (LLRD 0.9)896same4Cosine enc 3e-7, dec/head 3e-6, eta_min=1e-7same
S4 – Final100 % Skyenc block 2-3, patch_embeddings, layer_norm, dec + heads (LLRD 0.9)1024fils, rot90, SkyPhoto (0.4)8Cosine enc 1e-5, dec/head 1e-4, eta_min=1e-6same

1.1 SegFormer Class‑balancing Strategy

LayerPurposeImplementation
Per‑image WeightedRandomSamplerOver‑sample images rich in minority materials.Soft exponent + Floor  i = max(frac ** 0.4, 0.3)
normalise so mean ≈ 1.
Patch‑aware oversamplingGuarantee minority pixels in every crop for 256–768 px stages.30/20/10% crops drawn from a pre‑built minority‑tile index (freq >= 3% and >= 128px)
Per‑pixel class weightsDown‑weight stone/wood inside loss.w_c = 1 / √freq_c then normalise
Adaptive focal γExtra penalty on easy majority pixels.See table below
Loss mask drop‑outReduce gradient dominance of majority.Randomly drop 20 % of stone pixels in loss (keep & rand > p).

1.2 Gamma map

ClassPixel shareRecommended γWhy
stone36 %2.0Dominant & usually easy; strong damping keeps its gradients in check.
metal18 %1.5Still frequent but slightly harder (different hues).
wood17 %1.5Similar to metal in share and difficulty.
fabric10 %1.2Mid-tier class; moderate damping.
ground9 %1.2Same tier as fabric.
leather7 %1.0Getting sparse—leave nearly CE-like.

From S1 Apply 0.9 LLRD to encoder blocks. Loss mask dropout for majority classes: 20% in S0/S1, disabled from S2


2. UNet‑Albedo (A)

PhaseDataset mixTrainablesCrop / Feed (px)Augment†EpochsOptimiser & LR (per‑group)SchedulerLoss
A1100 % Skyfull UNet + FiLM256flips · rot, SkyPhoto 0.645AdamW — enc 2e‑4 · dec 2e‑4 · FiLM 3e‑4 · head 2.5e‑4OneCycle (pct 0.2, cos, final 1e‑5)L1 + 0.15 MS-SSIM + 0.05 LPIPS
A2100 % Skyenc + dec + FiLM512A1 aug + SkyPhoto 0.614AdamW — enc 5e‑5 · dec 8e‑5 · FiLM 1e‑4 · head 8e‑5cosine‑14, eta_min=5e-6same
A3100 % Skyenc + dec + FiLM768A1 aug + SkyPhoto 0.312AdamW — enc 5e‑6 · dec 4e‑5 · FiLM 5e‑5 · head 4e‑5cosine‑12, eta_min=3e-6same
A4100 % Skydec + head (enc frozen)1 024A1 aug + SkyPhoto 0.158AdamW — dec 1.5e‑5 · head 2e‑5cosine-5, eta_min=3e-6same

3. Separate Unet per map (M)

3.1 Training plan

Height/AO - Start from scratch Roughness/Metallic - Import weights from A4

PhaseCropEpochsEnc LRDec LROptimizerScheduler
M0256202e‑42e‑4AdamWwarmup linearLR 2 epoch, start_lr=0.3, cosine t_max=epochs-1,eta_min=enc_lr*0.1
M1512131e‑4 (LLRD 0.9^d)2e‑4AdamWsame, but warmup 1 epoch
M2768138e‑5 (LLDR 0.9^d)1.6e‑4AdamWsame
M310246frozen1.0e‑4AdamWsame

3.2 Unet-Maps input

MapInput
heightnormal + mean curvature + poisson-coarse
aonormal + mean curvature + poisson-coarse
roughnessalbedo + normal + segformer mask (K channels, gated by confidence) (both channel & FiLM)
metallicalbedo + normal + segformer mask (K channels, gated by confidence) (both as channel and FiLM)

3.3 Per‑map network & loss recipes

maploss = λᵢ·termᵢ
Height1.0·L1 + 0.25·GradDiff + 0.06·TV + 0.15->0.10(decay in P2)·Normal‑Reproj + 0.06·MS‑SSIM + 0.1*Laplacian-Pyr
AO1.0·L1 + 0.15·Sobel + 0.1·MS‑SSIM
Rough1.0·Focal-Relative-L1 + 0.15·MS‑SSIM + 0.05·Sobel
Metal1.0*Focal BCE(a=0.25,g=2.0) + 0.3*Focal-Tversky(a=0.7,b=0.3,g=1.5) + 0.05*Sobel + 0.1*L1 + 0.1*MS-SSIM + 0.15*Material-Penalty

4. Augmentation key

  • Global safe – h/v flip, 90° rot (all phases)
  • SkyPhotometric(p=0.6) – light tint, γ, grain; p = 0.5 in hi-res stages

Contributors

asvetliakov

208 commits

asvetliakov/ai-pbr-models

13

stars

208

commits

Python

primary language

Mar 26, 2026

updated

README

🏔️ Skyrim PBR Pipeline — Rev 7.0 (19 Jule 2025)


0. Legend

  • S–phases = SegFormer
  • A–phases = UNet-Albedo (UNetAlbedo in code)
  • M–phases = UNet-Maps (UNetMaps)

cond_ch = 512 ⇒ FiLM conditioning enabled from the first epoch for both U-Nets.
Class weights = 1 / √freq(class); WeightedRandomSampler active in every phase except the final hi-res stages.


1. SegFormer (S)

PhaseData mix (train)TrainablesCrop (px)Augment † (p per sample)EpochsLR & SchedulerLoss
S0 – Warm-up100 % Skyenc + dec + heads256flips, rot90 (1.0), SkyPhoto (0.6)30OneCycle LR 1e-4 → 4e-4 → 1e-5 (pct 0.15)0.8 Focal CE + 0.2 Dice
S1 – Domain focus100 % Skyenc (block 0 frozen) + dec + heads (LLRD 0.9)512same15Cosine start 1e-4, ηₘᵢₙ 8e-6same
S2 – Hi-res mix100 % Skyenc (block 0 frozen) + dec + heads (LLRD 0.9)768same12Cosine 8e-5 → ηₘᵢₙ 8e-6same
S3 – Full-res polish100 % Skyenc block 3 (0,1,2 frozen) + dec + heads (LLRD 0.9)896same4Cosine enc 3e-7, dec/head 3e-6, eta_min=1e-7same
S4 – Final100 % Skyenc block 2-3, patch_embeddings, layer_norm, dec + heads (LLRD 0.9)1024fils, rot90, SkyPhoto (0.4)8Cosine enc 1e-5, dec/head 1e-4, eta_min=1e-6same

1.1 SegFormer Class‑balancing Strategy

LayerPurposeImplementation
Per‑image WeightedRandomSamplerOver‑sample images rich in minority materials.Soft exponent + Floor  i = max(frac ** 0.4, 0.3)
normalise so mean ≈ 1.
Patch‑aware oversamplingGuarantee minority pixels in every crop for 256–768 px stages.30/20/10% crops drawn from a pre‑built minority‑tile index (freq >= 3% and >= 128px)
Per‑pixel class weightsDown‑weight stone/wood inside loss.w_c = 1 / √freq_c then normalise
Adaptive focal γExtra penalty on easy majority pixels.See table below
Loss mask drop‑outReduce gradient dominance of majority.Randomly drop 20 % of stone pixels in loss (keep & rand > p).

1.2 Gamma map

ClassPixel shareRecommended γWhy
stone36 %2.0Dominant & usually easy; strong damping keeps its gradients in check.
metal18 %1.5Still frequent but slightly harder (different hues).
wood17 %1.5Similar to metal in share and difficulty.
fabric10 %1.2Mid-tier class; moderate damping.
ground9 %1.2Same tier as fabric.
leather7 %1.0Getting sparse—leave nearly CE-like.

From S1 Apply 0.9 LLRD to encoder blocks. Loss mask dropout for majority classes: 20% in S0/S1, disabled from S2


2. UNet‑Albedo (A)

PhaseDataset mixTrainablesCrop / Feed (px)Augment†EpochsOptimiser & LR (per‑group)SchedulerLoss
A1100 % Skyfull UNet + FiLM256flips · rot, SkyPhoto 0.645AdamW — enc 2e‑4 · dec 2e‑4 · FiLM 3e‑4 · head 2.5e‑4OneCycle (pct 0.2, cos, final 1e‑5)L1 + 0.15 MS-SSIM + 0.05 LPIPS
A2100 % Skyenc + dec + FiLM512A1 aug + SkyPhoto 0.614AdamW — enc 5e‑5 · dec 8e‑5 · FiLM 1e‑4 · head 8e‑5cosine‑14, eta_min=5e-6same
A3100 % Skyenc + dec + FiLM768A1 aug + SkyPhoto 0.312AdamW — enc 5e‑6 · dec 4e‑5 · FiLM 5e‑5 · head 4e‑5cosine‑12, eta_min=3e-6same
A4100 % Skydec + head (enc frozen)1 024A1 aug + SkyPhoto 0.158AdamW — dec 1.5e‑5 · head 2e‑5cosine-5, eta_min=3e-6same

3. Separate Unet per map (M)

3.1 Training plan

Height/AO - Start from scratch Roughness/Metallic - Import weights from A4

PhaseCropEpochsEnc LRDec LROptimizerScheduler
M0256202e‑42e‑4AdamWwarmup linearLR 2 epoch, start_lr=0.3, cosine t_max=epochs-1,eta_min=enc_lr*0.1
M1512131e‑4 (LLRD 0.9^d)2e‑4AdamWsame, but warmup 1 epoch
M2768138e‑5 (LLDR 0.9^d)1.6e‑4AdamWsame
M310246frozen1.0e‑4AdamWsame

3.2 Unet-Maps input

MapInput
heightnormal + mean curvature + poisson-coarse
aonormal + mean curvature + poisson-coarse
roughnessalbedo + normal + segformer mask (K channels, gated by confidence) (both channel & FiLM)
metallicalbedo + normal + segformer mask (K channels, gated by confidence) (both as channel and FiLM)

3.3 Per‑map network & loss recipes

maploss = λᵢ·termᵢ
Height1.0·L1 + 0.25·GradDiff + 0.06·TV + 0.15->0.10(decay in P2)·Normal‑Reproj + 0.06·MS‑SSIM + 0.1*Laplacian-Pyr
AO1.0·L1 + 0.15·Sobel + 0.1·MS‑SSIM
Rough1.0·Focal-Relative-L1 + 0.15·MS‑SSIM + 0.05·Sobel
Metal1.0*Focal BCE(a=0.25,g=2.0) + 0.3*Focal-Tversky(a=0.7,b=0.3,g=1.5) + 0.05*Sobel + 0.1*L1 + 0.1*MS-SSIM + 0.15*Material-Penalty

4. Augmentation key

  • Global safe – h/v flip, 90° rot (all phases)
  • SkyPhotometric(p=0.6) – light tint, γ, grain; p = 0.5 in hi-res stages

Contributors

asvetliakov

208 commits

Languages

Python

86.2%

C++

13.0%