This repository has been archived by the owner on Feb 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeepbooru.py
111 lines (94 loc) · 3.61 KB
/
deepbooru.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os.path
import re
import zipfile
import deepdanbooru as dd
import tensorflow as tf
import numpy as np
# TODO: let BLIP use the same DownloadFile function
from download_util import load_file_from_url
pwd = os.path.dirname(os.path.realpath(__file__))
default_deepbooru_model_path = os.path.abspath(os.path.join(pwd, "pretrained", "deepbooru"))
re_special = re.compile(r"([\\()])")
def init_deepbooru():
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
# prevent tensorflow from using all the VRAM
tf.config.experimental.set_memory_growth(gpu, True)
model, tags = get_deepbooru_tags_model(default_deepbooru_model_path)
return model, tags
# TODO: refactor this to let user specify the model path
def get_deepbooru_tags_model(model_path: str):
# why do you find DeepBooru in the fucking temp by default?
if not os.path.exists(os.path.join(model_path, "project.json")):
is_abs = os.path.isabs(model_path)
if not is_abs:
model_path = os.path.abspath(model_path)
# there is no point importing these every time
load_file_from_url(
r"https://github.com/KichangKim/DeepDanbooru/releases/download/v3-20211112-sgd-e28/deepdanbooru-v3-20211112-sgd-e28.zip",
model_path,
)
with zipfile.ZipFile(
os.path.join(model_path, "deepdanbooru-v3-20211112-sgd-e28.zip"), "r"
) as zip_ref:
zip_ref.extractall(model_path)
os.remove(os.path.join(model_path, "deepdanbooru-v3-20211112-sgd-e28.zip"))
tags = dd.project.load_tags_from_project(model_path)
model = dd.project.load_model_from_project(model_path, compile_model=False)
return model, tags
def get_deepbooru_tags_from_model(
model,
tags,
pil_image,
threshold,
alpha_sort=False,
use_spaces=True,
use_escape=True,
include_ranks=False,
log_results=False,
):
width = model.input_shape[2]
height = model.input_shape[1]
image = np.array(pil_image)
image = tf.image.resize(
image,
size=(height, width),
method=tf.image.ResizeMethod.AREA,
preserve_aspect_ratio=True,
)
image = image.numpy() # EagerTensor to np.array
image = dd.image.transform_and_pad_image(image, width, height)
image = image / 255.0
image_shape = image.shape
image = image.reshape((1, image_shape[0], image_shape[1], image_shape[2]))
y = model.predict(image)[0]
result_dict = {}
for i, tag in enumerate(tags):
result_dict[tag] = y[i]
unsorted_tags_in_theshold = []
result_tags_print = []
for tag in tags:
if result_dict[tag] >= threshold:
if tag.startswith("rating:"):
continue
unsorted_tags_in_theshold.append((result_dict[tag], tag))
result_tags_print.append(f"{result_dict[tag]} {tag}")
# sort tags
result_tags_out = []
sort_ndx = 0
if alpha_sort:
sort_ndx = 1
# sort by reverse by likelihood and normal for alpha, and format tag text as requested
unsorted_tags_in_theshold.sort(key=lambda y: y[sort_ndx], reverse=(not alpha_sort))
for weight, tag in unsorted_tags_in_theshold:
tag_outformat = tag
if use_spaces:
tag_outformat = tag_outformat.replace("_", " ")
if use_escape:
tag_outformat = re.sub(re_special, r"\\\1", tag_outformat)
if include_ranks:
tag_outformat = f"({tag_outformat}:{weight:.3f})"
result_tags_out.append(tag_outformat)
if log_results:
print("\n".join(sorted(result_tags_print, reverse=True)))
return ", ".join(result_tags_out)