Semantically Guided Diffusion Models for Infinitely Large Toroidal Textures

Semantično vodeni difuzijski modeli za neskončno velike toroidne teksture

Mentor: izr. prof. dr. Janez Perš1, Somentor: prof. dr. Matija Marolt2, Somentor: asist. dr. Žiga Lesar2
1University of Ljubljana, Faculty of Electrical Engineering, 2University of Ljubljana, Faculty of Computer and Information Science
Master's thesis, 2026

Seamless textures of arbitrary size

Two stages: one decides the layout, the other the appearance.

  • Stage 1 — A Markov Random Field generates a low-resolution semantic map that fixes the global class proportions.
  • Stage 2 — A fine-tuned latent diffusion model turns the map into a full-resolution texture, indexing patches modulo the canvas so the result tiles without a seam.
MRF-generated semantic mask

MRF semantic map, 5120×5120.

Diffusion-synthesised bark texture

A bark texture from the diffusion model, 5120×5120.

High-resolution bark texture (10240x10240)

One seamless texture at 10240×10240, shown downscaled.

Abstract

This thesis addresses the problem of generating large, seamless bark textures of spruce logs. The problem arises from two facts. First, a log is cylindrical along its circumference, and only a narrow strip can be photographed at high resolution. Second, diffusion models, the current state of the art for image synthesis, are typically trained on patches of 512×512 pixels, while the full circumference spans several thousand. The goal is to fine-tune a generative model that composes the full circumferential texture from limited captures, closes it seamlessly into a torus, and preserves a natural distribution of classes: bark, knots, and mechanical damage.

We propose a two-stage pipeline. In the first stage, a Markov Random Field generates a coarse semantic map that controls the global class proportions. In the second stage, an existing latent diffusion model based on the DiffInfinite approach is adapted and fine-tuned on our data to produce a high-resolution image conditioned on that map. We extend the approach with modular patch indexing and periodic blending during decoding, so that the generated texture tiles seamlessly under the chosen geometry, either toroidal or cylindrical. The U-Net architecture itself is unchanged.

Together with the Biotechnical Faculty we captured 140 photographs of 15 spruce logs in the field and built a dataset of 270 bark image and mask pairs. We fine-tuned the diffusion model for 200,000 steps; broad augmentation of the training patches lowered FID and KID substantially, and lighter augmentation in the final phase lowered them further. We also observed that after a few thousand steps the loss function no longer reflects visual quality progress, while FID and KID computed on patches continue to track it well. The generated texture was wrapped seamlessly around a demonstration cylindrical log in Blender.

Method

Four steps, from photographs to a texture on a 3D model. Generation itself is the last two: one decides what goes where, the other how it looks.

1. Field capture and unwrapping

On two field exercises with the Department of Forestry and Renewable Forest Resources at the Biotechnical Faculty, we photographed 15 spruce logs (Picea abies) at the work site, 140 photographs in total; 133 survived cropping and blur rejection. Spruce was chosen because it is economically important in Slovenia and grows straight enough to model the trunk surface as a cylinder. A rotate, crop and unwrap tool flattens the visible part of that cylinder into a strip, removing the perspective foreshortening that would otherwise distort training patches.

A log photograph unwrapped into a flat bark strip

The visible surface is resampled row by row into a flat strip of bark.

2. Semantic segmentation

Two models do the labelling. First, DeepLabv3 with a ResNet50 backbone separates the log from the background. It was bootstrapped from about 30 hand-drawn masks and improved by correcting its own failures and retraining, until it covered all 140 log photographs. The weights are on the Hugging Face Hub.

Second, a U-Net with a ResNet34 encoder labels the bark itself into three classes: bark, knots (slepice, overgrown branch scars) and mechanical damage. It is trained on 789 hand-labelled 512×512 patches with a combined Dice and weighted cross-entropy loss. Five-fold cross-validation gives a mean IoU of 0.843 ± 0.011; DeepLabv3+ with the same encoder scores 0.847 ± 0.011, close enough that we kept the U-Net. Bark exceeds 0.97 and damage reaches 0.87, but knots stay at 0.64 — recall is high (0.95) and the loss comes from over-extending knot boundaries into surrounding bark.

Bark patches with their three-class masks

Bark patches and their masks. Yellow marks knots, blue mechanical damage, black ordinary bark.

3. Semantic map generation

The map is coarse: one element covers 50×50 pixels of the original image, so an average spruce log needs roughly 300×500 elements, with the 300 closing around the circumference. A Markov Random Field sampled with Gibbs sampling generates it. A temperature parameter trades local order against variety, and the field is sampled with wrap-around neighbourhoods so the map is already periodic. We also implemented Wave Function Collapse as an alternative; it produces plausible layouts but matches the measured class proportions less closely, so MRF is the method used throughout.

4. Toroidal latent diffusion

A latent diffusion model based on Stable Diffusion, fine-tuned on our bark patches, renders the map. Sampling follows DiffInfinite: the latent canvas starts as noise, overlapping patches are drawn at random and denoised, and each latent element carries its own diffusion timestep, so a canvas of any size stays globally consistent.

DiffInfinite produces arbitrarily large images, but they do not tile. Our change is small and entirely outside the network: patch coordinates and the Hann blending window are indexed with modular arithmetic, so a patch running off one edge continues on the other. Wrapping both axes gives a torus, wrapping only the horizontal axis gives a cylinder whose width equals the log circumference. The U-Net weights and architecture are untouched.

Modular patch indexing for toroidal and cylindrical geometry

Patches that cross an edge are indexed modulo the canvas, so opposite edges are denoised as neighbours and no seam can form.

Seamless textures

A 320×384 semantic map from the MRF, and the 2560×3072 texture the diffusion model generates from it. The classes land where the map puts them.

MRF semantic map and the bark texture generated from it

Tiled 2×2, with the meeting point of the four copies enlarged on the right. The texture is continuous across the join, so it repeats without a seam.

The texture tiled 2x2 with the seam region enlarged

Wrapped around a demonstration log in Blender, shaded with a normal map derived from the texture with a Sobel operator. The seam around the circumference is not visible.

Generated bark texture wrapped around a cylindrical log in Blender

Training progression

Drag the slider to scrub through 40 checkpoints saved every 5,000 steps of training, from 5k up to 200k steps, all generated from the same semantic map.

Early checkpoint (5k steps).

Early checkpoint (5,000 steps)

Loading...

5,000 steps

Final checkpoint (200k steps).

Final checkpoint (200,000 steps)


Quantitative evaluation

FID and KID are computed on 512×512 patches against held-out real bark. The training loss flattens by 20,000 steps while the samples keep improving, so it is useless as a stopping criterion here; FID and KID keep tracking the improvement and are what we monitored instead.

CheckpointFIDKID
5,000 steps3400.36
200,000 steps, full augmentation2250.16
200,000 steps, colour augmentation dropped at 140k (final model)2170.15

Both metrics fall steeply to about 150,000 steps and then flatten. Dropping colour augmentation for the last 60,000 steps buys a small further gain, and that checkpoint is the released model.

Dataset and Code

The dataset is 270 bark and mask pairs at 1024×1024, from which 512×512 patches are cropped during diffusion training, plus 789 pre-cut 512×512 pairs used to train the segmenter. It is on the Hugging Face Hub together with the model weights. The capture, unwrapping, segmentation, semantic-map and training code is on GitHub, ordered by pipeline stage.

The DiffInfinite-derived diffusion code lives in a separate diffinfinite-bark fork, included as a submodule, so that upstream attribution and our modifications (P2 loss, learning-rate schedule, toroidal indexing) remain clearly visible in the commit history.

BibTeX

The thesis is published in the Repository of the University of Ljubljana (RUL).

@mastersthesis{kreft2026toroidal,
  author  = {Kreft, Jakob},
  title   = {Semantično vodeni difuzijski modeli za neskončno velike toroidne teksture},
  school  = {University of Ljubljana, Faculty of Electrical Engineering},
  type    = {Master's thesis},
  address = {Ljubljana, Slovenia},
  year    = {2026},
  url     = {https://hdl.handle.net/20.500.12556/RUL-185875}
}