Skip to content

Honor keep_tokens_separator / caption_separator when recording tag frequency metadata - #2443

Open
kv-248 wants to merge 1 commit into
kohya-ss:devfrom
kv-248:fix-tag-frequency-keep-tokens-separator
Open

kv-248 wants to merge 1 commit into
kohya-ss:devfrom
kv-248:fix-tag-frequency-keep-tokens-separator

Conversation

@kv-248

@kv-248 kv-248 commented Sep 28, 2026

Copy link
Copy Markdown

Fixes #2323.

Problem

BaseDataset.set_tag_frequency() splits captions on a hard-coded ",". When shuffle_caption, caption_tag_dropout_rate or token_warmup_step is active, training splits them differently: it first splits on the subset's keep_tokens_separator, then on caption_separator (library/dataset.py, the shuffle/keep-tokens block). As a result, the ss_tag_frequency metadata in the saved model does not match the tags that were trained, which is the case described in the issue. For example, with keep_tokens_separator="|||":

captions: ["1boy ||| standing, red", "1boy, blue ||| smile"]
before:   {'1boy ||| standing': 1, 'red': 1, '1boy': 1, 'blue ||| smile': 1}
after:    {'1boy': 2, 'standing': 1, 'red': 1, 'blue': 1, 'smile': 1}

(Output of calling BaseDataset.set_tag_frequency directly on dev d02db9a, before and after this change.)

Change

  • set_tag_frequency() accepts optional caption_separator="," and keep_tokens_separator=None. When keep_tokens_separator is set, it is replaced with caption_separator before splitting. The defaults keep the current behavior for any other caller.
  • DreamBoothDataset and FineTuningDataset pass their subset's caption_separator and keep_tokens_separator.
  • New tests/library/test_tag_frequency.py: the default comma case is unchanged, keep_tokens_separator splits tags, and a custom caption_separator is honored.

Scope

The issue mentions that other caption modifiers may be affected as well. This PR only aligns the two separators, which is where the mismatch is unambiguous. secondary_separator, wildcards and multi-line captions are left as they are. I'm happy to follow up on those if you want the metadata to track them too.

Validation

  • python -m pytest tests/library/test_tag_frequency.py: 3 passed on CPU (Windows, Python 3.12). Before the fix, 2 of the 3 tests failed.
  • I have not run a full training job end to end. The change only affects the metadata dictionary, not the captions used for training.

This patch was prepared with the help of an AI coding assistant (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants