Allow full control of caption extensions

This commit is contained in:
Jaret Burkett committed 2025-04-10 07:42:04 -06:00
1 parent 96ba2fd129
commit d8bdc03256
2 files changed
+22 -20

No files matched your search

+4 -1
View File
@@ -714,7 +714,10 @@ class DatasetConfig:
random_triggers = [line for line in random_triggers if line.strip() != ''] random_triggers = [line for line in random_triggers if line.strip() != '']
self.random_triggers: List[str] = random_triggers self.random_triggers: List[str] = random_triggers
self.random_triggers_max: int = kwargs.get('random_triggers_max', 1) self.random_triggers_max: int = kwargs.get('random_triggers_max', 1)
self.caption_ext: str = kwargs.get('caption_ext', None) self.caption_ext: str = kwargs.get('caption_ext', '.txt')
# if caption_ext doesnt start with a dot, add it
if self.caption_ext and not self.caption_ext.startswith('.'):
self.caption_ext = '.' + self.caption_ext
self.random_scale: bool = kwargs.get('random_scale', False) self.random_scale: bool = kwargs.get('random_scale', False)
self.random_crop: bool = kwargs.get('random_crop', False) self.random_crop: bool = kwargs.get('random_crop', False)
self.resolution: int = kwargs.get('resolution', 512) self.resolution: int = kwargs.get('resolution', 512)
+18 -19
View File
@@ -62,7 +62,6 @@ transforms_dict = {
'RandomEqualize': transforms.RandomEqualize(p=0.2), 'RandomEqualize': transforms.RandomEqualize(p=0.2),
} }
caption_ext_list = ['txt', 'json', 'caption']
img_ext_list = ['.jpg', '.jpeg', '.png', '.webp'] img_ext_list = ['.jpg', '.jpeg', '.png', '.webp']
@@ -91,15 +90,16 @@ def standardize_images(images):
return standardized_images return standardized_images
def clean_caption(caption): def clean_caption(caption):
# remove any newlines # this doesnt make any sense anymore in a world that is not based on comma seperated tokens
caption = caption.replace('\n', ', ') # # remove any newlines
# remove new lines for all operating systems # caption = caption.replace('\n', ', ')
caption = caption.replace('\r', ', ') # # remove new lines for all operating systems
caption_split = caption.split(',') # caption = caption.replace('\r', ', ')
# remove empty strings # caption_split = caption.split(',')
caption_split = [p.strip() for p in caption_split if p.strip()] # # remove empty strings
# join back together # caption_split = [p.strip() for p in caption_split if p.strip()]
caption = ', '.join(caption_split) # # join back together
# caption = ', '.join(caption_split)
return caption return caption
@@ -115,22 +115,17 @@ class CaptionMixin:
# check if either has a prompt file # check if either has a prompt file
path_no_ext = os.path.splitext(img_path)[0] path_no_ext = os.path.splitext(img_path)[0]
prompt_path = None prompt_path = None
for ext in caption_ext_list: ext = self.dataset_config.caption_ext
prompt_path = path_no_ext + '.' + ext prompt_path = path_no_ext + ext
if os.path.exists(prompt_path):
break
else: else:
img_path = img_path_or_tuple if isinstance(img_path_or_tuple, str) else img_path_or_tuple.path img_path = img_path_or_tuple if isinstance(img_path_or_tuple, str) else img_path_or_tuple.path
# see if prompt file exists # see if prompt file exists
path_no_ext = os.path.splitext(img_path)[0] path_no_ext = os.path.splitext(img_path)[0]
prompt_path = None prompt_path = path_no_ext + ext
for ext in caption_ext_list:
prompt_path = path_no_ext + '.' + ext
if os.path.exists(prompt_path):
break
# allow folders to have a default prompt # allow folders to have a default prompt
default_prompt_path = os.path.join(os.path.dirname(img_path), 'default.txt') default_prompt_path = os.path.join(os.path.dirname(img_path), 'default.txt')
default_prompt_path_with_ext = os.path.join(os.path.dirname(img_path), 'default' + ext)
if os.path.exists(prompt_path): if os.path.exists(prompt_path):
with open(prompt_path, 'r', encoding='utf-8') as f: with open(prompt_path, 'r', encoding='utf-8') as f:
@@ -141,6 +136,10 @@ class CaptionMixin:
if 'caption' in prompt: if 'caption' in prompt:
prompt = prompt['caption'] prompt = prompt['caption']
prompt = clean_caption(prompt)
elif os.path.exists(default_prompt_path_with_ext):
with open(default_prompt_path, 'r', encoding='utf-8') as f:
prompt = f.read()
prompt = clean_caption(prompt) prompt = clean_caption(prompt)
elif os.path.exists(default_prompt_path): elif os.path.exists(default_prompt_path):
with open(default_prompt_path, 'r', encoding='utf-8') as f: with open(default_prompt_path, 'r', encoding='utf-8') as f: