Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support batch processing #51

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 68 additions & 38 deletions wd14tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_installed_models():
return models


async def tag(image, model_name, threshold=0.35, character_threshold=0.85, exclude_tags="", replace_underscore=True, trailing_comma=False, client_id=None, node=None):
async def tag(batch, model_name, threshold=0.35, character_threshold=0.85, exclude_tags="", replace_underscore=True, trailing_comma=False, client_id=None, node=None):
if model_name.endswith(".onnx"):
model_name = model_name[0:-5]
installed = list(get_installed_models())
Expand All @@ -60,16 +60,17 @@ async def tag(image, model_name, threshold=0.35, character_threshold=0.85, exclu
input = model.get_inputs()[0]
height = input.shape[1]

# Reduce to max size and pad with white
ratio = float(height)/max(image.size)
new_size = tuple([int(x*ratio) for x in image.size])
image = image.resize(new_size, Image.LANCZOS)
square = Image.new("RGB", (height, height), (255, 255, 255))
square.paste(image, ((height-new_size[0])//2, (height-new_size[1])//2))
for i in range(len(batch)):
# Reduce to max size and pad with white
ratio = float(height)/max(batch[i].size)
new_size = tuple([int(x*ratio) for x in batch[i].size])
batch[i] = batch[i].resize(new_size, Image.LANCZOS)
square = Image.new("RGB", (height, height), (255, 255, 255))
square.paste(batch[i], ((height-new_size[0])//2, (height-new_size[1])//2))

image = np.array(square).astype(np.float32)
image = image[:, :, ::-1] # RGB -> BGR
image = np.expand_dims(image, 0)
batch[i] = np.array(square).astype(np.float32)
batch[i] = batch[i][:, :, ::-1] # RGB -> BGR
batch[i] = np.expand_dims(batch[i], 0)

# Read all tags from csv and locate start of each category
tags = []
Expand All @@ -88,22 +89,32 @@ async def tag(image, model_name, threshold=0.35, character_threshold=0.85, exclu
else:
tags.append(row[1])

# imgs = np.array([im for im in batch])

probs = []
label_name = model.get_outputs()[0].name
probs = model.run([label_name], {input.name: image})[0]
for img in batch:
probs.append(model.run([label_name], {input.name: img})[0])
# probs = probs[: len(batch)]
# probs = model.run([label_name], {input.name: imgs})[0]

# print(probs)

result = list(zip(tags, probs[0]))
res = []

# rating = max(result[:general_index], key=lambda x: x[1])
general = [item for item in result[general_index:character_index] if item[1] > threshold]
character = [item for item in result[character_index:] if item[1] > character_threshold]
for i in range(len(batch)):
result = list(zip(tags, probs[i][0]))

all = character + general
remove = [s.strip() for s in exclude_tags.lower().split(",")]
all = [tag for tag in all if tag[0] not in remove]
# rating = max(result[:general_index], key=lambda x: x[1])
general = [item for item in result[general_index:character_index] if item[1] > threshold]
character = [item for item in result[character_index:] if item[1] > character_threshold]

res = ("" if trailing_comma else ", ").join((item[0].replace("(", "\\(").replace(")", "\\)") + (", " if trailing_comma else "") for item in all))
all = character + general
remove = [s.strip() for s in exclude_tags.lower().split(",")]
all = [tag for tag in all if tag[0] not in remove]

res.append(("" if trailing_comma else ", ").join((item[0].replace("(", "\\(").replace(")", "\\)") + (", " if trailing_comma else "") for item in all)))

print(res)
return res


Expand Down Expand Up @@ -172,15 +183,21 @@ class WD14Tagger:
def INPUT_TYPES(s):
extra = [name for name, _ in (os.path.splitext(m) for m in get_installed_models()) if name not in known_models]
models = known_models + extra
return {"required": {
"image": ("IMAGE", ),
"model": (models, { "default": defaults["model"] }),
"threshold": ("FLOAT", {"default": defaults["threshold"], "min": 0.0, "max": 1, "step": 0.05}),
"character_threshold": ("FLOAT", {"default": defaults["character_threshold"], "min": 0.0, "max": 1, "step": 0.05}),
"replace_underscore": ("BOOLEAN", {"default": defaults["replace_underscore"]}),
"trailing_comma": ("BOOLEAN", {"default": defaults["trailing_comma"]}),
"exclude_tags": ("STRING", {"default": defaults["exclude_tags"]}),
}}
return {
"required": {
"image": ("IMAGE", ),
"model": (models, { "default": defaults["model"] }),
"threshold": ("FLOAT", {"default": defaults["threshold"], "min": 0.0, "max": 1, "step": 0.05}),
"character_threshold": ("FLOAT", {"default": defaults["character_threshold"], "min": 0.0, "max": 1, "step": 0.05}),
"replace_underscore": ("BOOLEAN", {"default": defaults["replace_underscore"]}),
"trailing_comma": ("BOOLEAN", {"default": defaults["trailing_comma"]}),
"exclude_tags": ("STRING", {"default": defaults["exclude_tags"]}),

},
"optional": {
"batch_size": ("INT", {"default": 1, "min": 1, "max": 128}),
}
}

RETURN_TYPES = ("STRING",)
OUTPUT_IS_LIST = (True,)
Expand All @@ -189,18 +206,31 @@ def INPUT_TYPES(s):

CATEGORY = "image"

def tag(self, image, model, threshold, character_threshold, exclude_tags="", replace_underscore=False, trailing_comma=False):
tensor = image*255
tensor = np.array(tensor, dtype=np.uint8)
def tag(self, image, model, threshold, character_threshold, exclude_tags="", replace_underscore=False, trailing_comma=False, batch_size=1):
if not isinstance(image, list):
images = [image]
else:
images = image

pbar = comfy.utils.ProgressBar(tensor.shape[0])
pbar = comfy.utils.ProgressBar(len(images))
tags = []
for i in range(tensor.shape[0]):
image = Image.fromarray(tensor[i])
tags.append(wait_for_async(lambda: tag(image, model, threshold, character_threshold, exclude_tags, replace_underscore, trailing_comma)))
pbar.update(1)
return {"ui": {"tags": tags}, "result": (tags,)}
batch = []

for image in images:
tensor = image*255
tensor = np.array(tensor, dtype=np.uint8)

for i in range(tensor.shape[0]):
image = Image.fromarray(tensor[i])
batch.append(image)
if len(batch) == batch_size or i == tensor.shape[0] -1:
tags = tags + wait_for_async(lambda: tag(batch, model, threshold, character_threshold, exclude_tags, replace_underscore, trailing_comma))
pbar.update(len(batch))
batch = []

print(tags)

return {"ui": {"tags": tags}, "result": (tags,)}

NODE_CLASS_MAPPINGS = {
"WD14Tagger|pysssss": WD14Tagger,
Expand Down