@@ -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