kohya-ss / kohya-ss/sd-scripts

--tag_replacement doesn't actually replace anything (variable shadowing bug)

Open Beginner friendly
#2,405 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
7.2k
Forks
1.2k
Avg merge
11m
Merged PRs (30d)
2

Description

--tag_replacement in finetune/tag_images_by_wd14_tagger.py does not actually replace any tags in the output captions, despite the log correctly printing replacing tag: X -> Y for each configured pair.

Ran into this while curating a dataset — set --tag_replacement "1girl,female;1boy,male" and the console log looks normal

But when I checked the actual .txt caption files afterward, the tags hadn't changed at all — still 1girl, 1boy, etc. Took a bit to figure out why since the log makes it look like it's working.
Traced it to process_tag_replacement() in finetune/tag_images_by_wd14_tagger.py. The loop variable used to hold each parsed source,target pair is named tags, which is also the name of the function's parameter

```python
def process_tag_replacement(tags: list[str], tag_replacements_arg: str) -> list[str]:
...
for tag_replacements_arg in tag_replacements:
tags = tag_replacements_arg.split(",") # this stomps the tags param
...
if source in tags:
tags[tags.index(source)] = target
return tags
```

Every iteration overwrites tags with a throwaway 2-item list ([source, target]), so the actual tag list passed in never gets touched.

fix (replace tag variable name):

```python
def process_tag_replacement(tags: list[str], tag_replacements_arg: str) -> list[str]:
escaped_tag_replacements = tag_replacements_arg.replace("\\,", "@@@@").replace("\\;", "####")
tag_replacements = escaped_tag_replacements.split(";")
for replacement_pair in tag_replacements:
pair = replacement_pair.split(",")
assert len(pair) == 2, f"tag replacement must be in the format of `source,target`: {tag_replacements_arg}"
source, target = [tag.replace("@@@@", ",").replace("####", ";") for tag in pair]
logger.info(f"replacing tag: {source} -> {target}")
if source in tags:
tags[tags.index(source)] = target
return tags
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in finetune/tag_images_by_wd14_tagger.py at process_tag_replacement() and inspect how the replacement-pair loop handles the incoming tag list. Verify the --tag_replacement example with 1girl,female;1boy,male, and consider the work done when the generated .txt captions contain the replacement tags instead of the originals.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.