Skip to content

Commit 284d2f8

Browse files
authored
Merge pull request #224 from DraconicDragon/prot-tags-subsets
Allow for per-subset protected tags file
2 parents eec8950 + d0db932 commit 284d2f8

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

library/config_util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class BaseSubsetParams:
7272
caption_dropout_rate: float = 0.0
7373
caption_dropout_every_n_epochs: int = 0
7474
caption_tag_dropout_rate: float = 0.0
75+
protected_tags_file: Optional[str] = None
7576
token_warmup_min: int = 1
7677
token_warmup_step: float = 0
7778
custom_attributes: Optional[Dict[str, Any]] = None
@@ -208,6 +209,7 @@ def __validate_and_convert_scalar_or_twodim(klass, value: Union[float, Sequence]
208209
"caption_dropout_every_n_epochs": int,
209210
"caption_dropout_rate": Any(float, int),
210211
"caption_tag_dropout_rate": Any(float, int),
212+
"protected_tags_file": str,
211213
}
212214
# DB means DreamBooth
213215
DB_SUBSET_ASCENDABLE_SCHEMA = {
@@ -566,6 +568,7 @@ def print_info(_datasets, dataset_type: str):
566568
info += indent(dedent(f"""\
567569
[Subset {j} of {dataset_type} {i}]
568570
image_dir: "{subset.image_dir}"
571+
protected_tags_file: "{subset.protected_tags_file}"
569572
image_count: {subset.img_count}
570573
num_repeats: {subset.num_repeats}
571574
shuffle_caption: {subset.shuffle_caption}

library/train_util.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ def __init__(
442442
token_warmup_min: int,
443443
token_warmup_step: Union[float, int],
444444
custom_attributes: Optional[Dict[str, Any]] = None,
445+
protected_tags_file: Optional[str] = None,
445446
validation_seed: Optional[int] = None,
446447
validation_split: Optional[float] = 0.0,
447448
resize_interpolation: Optional[str] = None,
@@ -470,6 +471,7 @@ def __init__(
470471
self.token_warmup_step = token_warmup_step # N(N<1ならN*max_train_steps)ステップ目でタグの数が最大になる
471472

472473
self.custom_attributes = custom_attributes if custom_attributes is not None else {}
474+
self.protected_tags_file = protected_tags_file
473475

474476
self.img_count = 0
475477

@@ -509,6 +511,7 @@ def __init__(
509511
token_warmup_min,
510512
token_warmup_step,
511513
custom_attributes: Optional[Dict[str, Any]] = None,
514+
protected_tags_file: Optional[str] = None,
512515
validation_seed: Optional[int] = None,
513516
validation_split: Optional[float] = 0.0,
514517
resize_interpolation: Optional[str] = None,
@@ -538,6 +541,7 @@ def __init__(
538541
token_warmup_min,
539542
token_warmup_step,
540543
custom_attributes=custom_attributes,
544+
protected_tags_file=protected_tags_file,
541545
validation_seed=validation_seed,
542546
validation_split=validation_split,
543547
resize_interpolation=resize_interpolation,
@@ -583,6 +587,7 @@ def __init__(
583587
token_warmup_min,
584588
token_warmup_step,
585589
custom_attributes: Optional[Dict[str, Any]] = None,
590+
protected_tags_file: Optional[str] = None,
586591
validation_seed: Optional[int] = None,
587592
validation_split: Optional[float] = 0.0,
588593
resize_interpolation: Optional[str] = None,
@@ -612,6 +617,7 @@ def __init__(
612617
token_warmup_min,
613618
token_warmup_step,
614619
custom_attributes=custom_attributes,
620+
protected_tags_file=protected_tags_file,
615621
validation_seed=validation_seed,
616622
validation_split=validation_split,
617623
resize_interpolation=resize_interpolation,
@@ -652,6 +658,7 @@ def __init__(
652658
token_warmup_min,
653659
token_warmup_step,
654660
custom_attributes: Optional[Dict[str, Any]] = None,
661+
protected_tags_file: Optional[str] = None,
655662
validation_seed: Optional[int] = None,
656663
validation_split: Optional[float] = 0.0,
657664
resize_interpolation: Optional[str] = None,
@@ -681,6 +688,7 @@ def __init__(
681688
token_warmup_min,
682689
token_warmup_step,
683690
custom_attributes=custom_attributes,
691+
protected_tags_file=protected_tags_file,
684692
validation_seed=validation_seed,
685693
validation_split=validation_split,
686694
resize_interpolation=resize_interpolation,
@@ -925,13 +933,30 @@ def replace_wildcard(match):
925933
)
926934
flex_tokens = flex_tokens[:tokens_len]
927935

928-
if not hasattr(self, "_protected_tags"):
929-
self._protected_tags = set()
930-
if hasattr(self, "protected_tags_file") and self.protected_tags_file and os.path.exists(self.protected_tags_file):
931-
logger.info(f"Loading protected tags from {self.protected_tags_file}")
936+
if not hasattr(subset, "_protected_tags"):
937+
subset._protected_tags = set()
938+
939+
# subset-specific first
940+
if hasattr(subset, "protected_tags_file") and subset.protected_tags_file:
941+
942+
protected_file = subset.protected_tags_file
943+
if os.path.exists(protected_file):
944+
logger.info(f"[Subset '{subset.image_dir}'] Loading protected tags from: {protected_file}")
945+
with open(protected_file, "r", encoding="utf-8") as f:
946+
subset._protected_tags = {line.strip().lower() for line in f if line. strip()}
947+
948+
logger.info(f"[Subset '{subset.image_dir}'] Loaded {len(subset._protected_tags)} protected tags")
949+
if self.log_caption_tag_dropout and len(subset._protected_tags) > 0:
950+
logger.info(f"[Subset '{subset.image_dir}'] Protected tags: {subset._protected_tags}")
951+
else:
952+
logger.warning(f"[Subset '{subset.image_dir}'] Protected tags file not found: {protected_file}")
953+
954+
# fallback to global protected tags file if no subset-specific file
955+
elif hasattr(self, "protected_tags_file") and self.protected_tags_file and os.path.exists(self.protected_tags_file):
956+
logger.info(f"[Subset '{subset.image_dir}'] Using global protected tags from: {self.protected_tags_file}")
932957
with open(self.protected_tags_file, "r", encoding="utf-8") as f:
933-
self._protected_tags = {line.strip().lower() for line in f if line.strip()}
934-
logger.info(f"Loaded {len(self._protected_tags)} protected tags.")
958+
subset._protected_tags = {line.strip().lower() for line in f if line.strip()}
959+
logger.info(f"[Subset '{subset.image_dir}'] Loaded {len(subset._protected_tags)} protected tags (global)")
935960

936961
log_tag_dropout = self.log_caption_tag_dropout and subset.caption_tag_dropout_rate > 0
937962

@@ -942,7 +967,8 @@ def dropout_tags(tokens, record_details=False):
942967
protected_tokens = []
943968
dropped_tokens = []
944969
for token in tokens:
945-
is_protected = token.lower() in self._protected_tags
970+
# subset-specific protected tags
971+
is_protected = token.lower() in subset._protected_tags
946972
if is_protected or random.random() >= subset.caption_tag_dropout_rate:
947973
kept.append(token)
948974
if record_details and is_protected:
@@ -2565,6 +2591,7 @@ def __init__(
25652591
subset.caption_suffix,
25662592
subset.token_warmup_min,
25672593
subset.token_warmup_step,
2594+
protected_tags_file=subset.protected_tags_file,
25682595
resize_interpolation=subset.resize_interpolation,
25692596
)
25702597
db_subsets.append(db_subset)

0 commit comments

Comments
 (0)