# Training data format The trainer reads real images with captions from bucketed WebDataset-style tar shards. `scripts/prepare_data.py` (module `dwd_klein.prepare`) builds them from your own images; `dwd_klein.data` reads them. Any images with captions work: photos, illustrations, renders, scans. ## Building a dataset ```bash # a folder of images, each with a same-stem caption file: cat.jpg + cat.txt (subfolders are included) python scripts/prepare_data.py --images ./my_images --out ./data # a JSONL or CSV file of image paths (relative to the file) and captions python scripts/prepare_data.py --captions-file pairs.jsonl --out ./data python scripts/prepare_data.py --captions-file pairs.csv --image-key file --caption-key text --out ./data # a Hugging Face dataset with an image column and a caption column (needs `pip install datasets`) python scripts/prepare_data.py --hf-dataset / --hf-split train \ --image-column image --caption-column caption --out ./data ``` | option | default | meaning | |---|---|---| | `--out` | `./data` | dataset root; pass the same path to the trainer as `--data-root` | | `--val-fraction` | `0.005` | share of images held out in `val/` (by key hash; resolution 0.0001) | | `--workers` | all CPUs | processes that decode and encode images | | `--seed` | `0` | input shuffle seed | | `--no-shuffle` | off | keep input order (by default inputs are shuffled so each shard mixes the whole input) | | `--shard-size` | `500` | samples per shard | | `--max-images` | all | read at most this many inputs | | `--source-name` | input kind | label stored in each sample's `json.source` | | `--overwrite` | off | replace an existing dataset at `--out` | | `--no-validate` | off | skip reading every shard back after writing | In JSONL, `--image-key` defaults to the first of `path`, `file_name`, `image`, `file` that is present, and `--caption-key` to the first of `caption`, `text`, `prompt`, `captions`. A JSONL caption may be a list of strings: the first becomes the sample's caption and all of them are kept in `json.captions`; the loader then picks one per sample and epoch. Images with no caption (missing or empty `.txt`, empty cell) are skipped. The output depends only on the inputs and the settings, not on `--workers`. ## Layout ``` / manifest.json shard list and totals (read this; don't glob) train//train--NNNNN.tar val//val--NNNNN.tar held-out images, never trained on ``` Paths in the manifest are relative to ``. Without a `manifest.json` the loader globs `///*.tar` instead. ## Buckets Five aspect buckets of about 1 MP. Both sides are multiples of 64, so a bucket is W/16 x H/16 DiT tokens (8x VAE downsampling, then 2x2 patches). | bucket dir | W x H | tokens | |---|---|---| | `1024x1024` | 1024 x 1024 | 64 x 64 = 4096 | | `1152x896` | 1152 x 896 (landscape) | 72 x 56 = 4032 | | `896x1152` | 896 x 1152 (portrait) | 56 x 72 = 4032 | | `1344x768` | 1344 x 768 (landscape) | 84 x 48 = 4032 | | `768x1344` | 768 x 1344 (portrait) | 48 x 84 = 4032 | Every image in `/` is exactly that size, so a micro-batch drawn from one bucket stacks without padding. ## How each image is produced 1. Read the EXIF orientation and use the displayed size. 2. Skip the image if it is under 1,000,000 px. 3. Choose the nearest bucket by log aspect ratio. A near-tie (|d1 - d2| < 0.02, e.g. 3:2 between `1152x896` and `1344x768`) is split between the two buckets by key hash. 4. Skip the image if the centre crop would remove more than ~26% of one side (|Δ log aspect| > 0.30), or if it would need upscaling to cover the bucket. 5. Convert to 8-bit sRGB RGB: embedded non-sRGB ICC profiles are converted, alpha is composited on white, 16-bit and float images are skipped. Then apply the EXIF rotation. 6. Resize, keeping the aspect ratio, to cover the bucket and centre crop, as one Lanczos resample of the centred crop box. A JPEG at least 4x larger per side than needed is first downscaled in the DCT domain, keeping at least 2x the target size before the Lanczos pass. 7. Encode as baseline JPEG, quality 95, 4:4:4 (no chroma subsampling), with no metadata. ## Shards - Plain uncompressed POSIX (ustar) tar files. - A sample is three consecutive members with the same key, in this order: `.jpg`, `.txt`, `.json`. - `shard_size` samples per shard (default 500); the last shard of each (split, bucket) can be smaller. Exact counts are in the manifest. - Keys are unique across the dataset (train and val) and contain no `.` or `/`. `prepare` builds them as `-<12 hex chars of sha1(input id)>`. **`.jpg`**: 8-bit RGB sRGB JPEG at exactly the bucket size (see above). Decode with `PIL.Image.open(...).convert("RGB")`. Conversion to normalized Oklab is the trainer's job. **`.txt`**: the caption, UTF-8, stripped, never empty. It goes through the text encoder's chat template unchanged, so any length works. **`.json`**: a UTF-8 object. | field | meaning | |---|---| | `key` | same as the tar key | | `bucket` | same as the bucket directory | | `orig_w`, `orig_h` | displayed size of the original image (after EXIF rotation) | | `source` | a label for the input (`--source-name`); the validation subset is stratified by (source, bucket) | | `source_id` | the input's path relative to the input folder or table, or `:` for Hugging Face inputs | | `captions` | only when the input had several captions: all of them, the first equal to `.txt` | The loader needs only `.jpg` (or `.jpeg`, `.png`, `.webp`) and `.txt`; it reads `json.captions` and `json.source` when present and ignores other fields, so you can add your own. ## manifest.json ```jsonc { "format_version": 1, "buckets": {"1024x1024": [1024, 1024], ...}, // [W, H] "jpeg": {"quality": 95, "subsampling": "4:4:4", "color": "sRGB"}, "settings": {"val_fraction": 0.005, "seed": 0, "shuffle": true, "shard_size": 500, "max_images": null, "source": "images"}, "totals": { "train": {"count": 0, "bytes": 0, "shards": 0, "by_bucket": {"": {"count": 0, "bytes": 0, "shards": 0}}, "by_source": {"": 0}, "by_bucket_source": {"/": 0}}, "val": {...} }, "skips": {":": 0}, // small, aspect, would_upscale, no_caption, ... "errors": [": "], // first unexpected errors, if any "shards": [{"path": "train/1344x768/train-1344x768-00000.tar", "split": "train", "bucket": "1344x768", "count": 500, "bytes": 0, "sources": {"": 500}}, ...] } ``` `shards` is sorted by (split, bucket, path); treat that order as canonical. The loader uses `totals..by_bucket..count` as the bucket sampling weights (shard counts when there is no manifest). ## How the loader reads it - Each step's bucket is drawn with probability proportional to the bucket's train sample count, from an RNG seeded by (seed, step, micro-batch), so every rank uses the same bucket (same sequence length) in a step. - Each rank streams its own share of a bucket's shards in a per-epoch permutation, in groups of shards whose samples are shuffled together. The stream position is a few integers per bucket and is checkpointed, so resume is exact. - `val/` is never trained on; it is used for evaluation. The head's least-squares init draws from `train/`. ## Invariants `prepare` reads every shard back after writing (skip with `--no-validate`; `dwd_klein.prepare.validate(root)` runs the same check) and fails if any of these does not hold: - every image is an RGB JPEG of exactly its bucket size; - every sample has the three members, a non-empty caption, and `json.key == key`, `json.bucket == `; - each shard's sample count and file size equal the manifest; - no key appears twice.