forked from SmilingWolf/wdv3-jax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wdv3_jax.py
328 lines (268 loc) · 9.95 KB
/
wdv3_jax.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import os
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Callable, Optional
import flax
import jax
import numpy as np
import pandas as pd
from huggingface_hub import hf_hub_download
from huggingface_hub.utils import HfHubHTTPError
from PIL import Image
from simple_parsing import field, parse_known_args
import Models
MODEL_REPO_MAP = {
"vit": "SmilingWolf/wd-vit-tagger-v3",
"swinv2": "SmilingWolf/wd-swinv2-tagger-v3",
"convnext": "SmilingWolf/wd-convnext-tagger-v3",
}
@flax.struct.dataclass
class PredModel:
apply_fun: Callable = flax.struct.field(pytree_node=False)
params: Any = flax.struct.field(pytree_node=True)
def jit_predict(self, x):
# Not actually JITed since this is a single shot script,
# but this is the function you would decorate with @jax.jit
x = x / 127.5 - 1
x = self.apply_fun(self.params, x, train=False)
x = flax.linen.sigmoid(x)
x = jax.numpy.float32(x)
return x
def predict(self, x):
preds = self.jit_predict(x)
preds = jax.device_get(preds)
preds = preds[0]
return preds
def pil_ensure_rgb(image: Image.Image) -> Image.Image:
# convert to RGB/RGBA if not already (deals with palette images etc.)
if image.mode not in ["RGB", "RGBA"]:
image = (
image.convert("RGBA")
if "transparency" in image.info
else image.convert("RGB")
)
# convert RGBA to RGB with white background
if image.mode == "RGBA":
canvas = Image.new("RGBA", image.size, (255, 255, 255))
canvas.alpha_composite(image)
image = canvas.convert("RGB")
return image
def pil_pad_square(image: Image.Image) -> Image.Image:
w, h = image.size
# get the largest dimension so we can pad to a square
px = max(image.size)
# pad to square with white background
canvas = Image.new("RGB", (px, px), (255, 255, 255))
canvas.paste(image, ((px - w) // 2, (px - h) // 2))
return canvas
def pil_resize(image: Image.Image, target_size: int) -> Image.Image:
# Resize
max_dim = max(image.size)
if max_dim != target_size:
image = image.resize(
(target_size, target_size),
Image.BICUBIC,
)
return image
@dataclass
class LabelData:
names: list[str]
rating: list[np.int64]
general: list[np.int64]
character: list[np.int64]
def load_labels_hf(
repo_id: str,
revision: Optional[str] = None,
token: Optional[str] = None,
) -> LabelData:
try:
csv_path = hf_hub_download(
repo_id=repo_id,
filename="selected_tags.csv",
revision=revision,
token=token,
)
csv_path = Path(csv_path).resolve()
except HfHubHTTPError as e:
raise FileNotFoundError(
f"selected_tags.csv failed to download from {repo_id}"
) from e
df: pd.DataFrame = pd.read_csv(csv_path, usecols=["name", "category"])
tag_data = LabelData(
names=df["name"].tolist(),
rating=list(np.where(df["category"] == 9)[0]),
general=list(np.where(df["category"] == 0)[0]),
character=list(np.where(df["category"] == 4)[0]),
)
return tag_data
def load_model_hf(
repo_id: str,
revision: Optional[str] = None,
token: Optional[str] = None,
) -> PredModel:
weights_path = hf_hub_download(
repo_id=repo_id,
filename="model.msgpack",
revision=revision,
token=token,
)
model_config = hf_hub_download(
repo_id=repo_id,
filename="sw_jax_cv_config.json",
revision=revision,
token=token,
)
with open(weights_path, "rb") as f:
data = f.read()
restored = flax.serialization.msgpack_restore(data)["model"]
variables = {"params": restored["params"], **restored["constants"]}
with open(model_config) as f:
model_config = json.loads(f.read())
model_name = model_config["model_name"]
model_builder = Models.model_registry[model_name]()
model = model_builder.build(
config=model_builder,
**model_config["model_args"],
)
model = PredModel(model.apply, params=variables)
return model, model_config["image_size"]
def get_tags(
probs: Any,
labels: LabelData,
gen_threshold: float,
char_threshold: float,
):
# Convert indices+probs to labels
probs = list(zip(labels.names, probs))
# First 4 labels are actually ratings
rating_labels = dict([probs[i] for i in labels.rating])
# General labels, pick any where prediction confidence > threshold
gen_labels = [probs[i] for i in labels.general]
gen_labels = dict([x for x in gen_labels if x[1] > gen_threshold])
gen_labels = dict(
sorted(
gen_labels.items(),
key=lambda item: item[1],
reverse=True,
)
)
# Character labels, pick any where prediction confidence > threshold
char_labels = [probs[i] for i in labels.character]
char_labels = dict([x for x in char_labels if x[1] > char_threshold])
char_labels = dict(
sorted(
char_labels.items(),
key=lambda item: item[1],
reverse=True,
)
)
# Combine general and character labels, sort by confidence
combined_names = [x for x in gen_labels]
combined_names.extend([x for x in char_labels])
# Convert to a string suitable for use as a training caption
caption = ", ".join(combined_names)
taglist = caption.replace("_", " ").replace("(", "\(").replace(")", "\)")
return caption, taglist, rating_labels, char_labels, gen_labels
#modified version of get_tag() with type default is taglist of get_tag, removing replace("(", "\(").replace(")", "\)")
def get_caption(
probs: Any,
labels: LabelData,
gen_threshold: float,
char_threshold: float,
):
# Convert indices+probs to labels
probs = list(zip(labels.names, probs))
# First 4 labels are actually ratings
#rating_labels = dict([probs[i] for i in labels.rating])
# General labels, pick any where prediction confidence > threshold
gen_labels = [probs[i] for i in labels.general]
gen_labels = dict([x for x in gen_labels if x[1] > gen_threshold])
gen_labels = dict(
sorted(
gen_labels.items(),
key=lambda item: item[1],
reverse=True,
)
)
# Character labels, pick any where prediction confidence > threshold
char_labels = [probs[i] for i in labels.character]
char_labels = dict([x for x in char_labels if x[1] > char_threshold])
char_labels = dict(
sorted(
char_labels.items(),
key=lambda item: item[1],
reverse=True,
)
)
# Combine general and character labels, sort by confidence
combined_names = [x for x in gen_labels]
combined_names.extend([x for x in char_labels])
# Convert to a string suitable for use as a training caption
caption = ", ".join(combined_names)
taglist = caption.replace("_", " ")
return taglist
def read_file(filename):
with open(filename, "r") as f:
contents = f.read()
return contents
def write_file(filename, contents):
with open(filename, "w") as f:
f.write(contents)
f.close()
def process_directory(image_dir, labels, model, target_size, opts, recursive):
for filename in os.listdir(image_dir):
file_path = os.path.join(image_dir, filename)
if (os.path.isdir(file_path) and recursive):
process_directory(file_path, labels, model, target_size, opts, recursive)
elif filename.endswith((".png", ".jpg", ".jpeg", ".webp", ".bmp")):
# get image
img_input: Image.Image = Image.open(file_path)
# ensure image is RGB
img_input = pil_ensure_rgb(img_input)
# pad to square with white background
img_input = pil_pad_square(img_input)
img_input = pil_resize(img_input, target_size)
# convert to numpy array and add batch dimension
inputs = np.array(img_input)
inputs = np.expand_dims(inputs, axis=0)
# NHWC image RGB to BGR
inputs = inputs[..., ::-1]
#print("Running inference...")
outputs = model.predict(inputs)
#print("Processing results...")
taglist = get_caption(
probs=outputs,
labels=labels,
gen_threshold=opts.gen_threshold,
char_threshold=opts.char_threshold,
)
print(f"Caption of image : {file_path} :")
print(f"Caption of image : {filename}: {taglist}")
write_file(file_path.split(".")[0] + ".txt", taglist)
print(f"-------- end of image --------")
@dataclass
class ScriptOptions:
image_dir: Path = field(positional=True)
model: str = field(default="vit")
gen_threshold: float = field(default=0.35)
char_threshold: float = field(default=0.75)
recursive: bool = field(default=False)
def main(opts: ScriptOptions):
repo_id = MODEL_REPO_MAP.get(opts.model)
image_dirs = Path(opts.image_dir).resolve()
if not image_dirs.is_dir():
raise FileNotFoundError(f"Image directory not found or it is not directory: {image_dirs}")
print(f"Loading model '{opts.model}' from '{repo_id}'...")
model, target_size = load_model_hf(repo_id=repo_id)
print("Loading tag list...")
labels: LabelData = load_labels_hf(repo_id=repo_id)
print(f"Image Directory: {image_dirs}, Model: {repo_id}, General Threshold: {opts.gen_threshold}, Character Threshold: {opts.char_threshold}, Recursive: {opts.recursive}")
process_directory(image_dirs, labels, model, target_size, opts, opts.recursive)
print("Done!")
if __name__ == "__main__":
opts, _ = parse_known_args(ScriptOptions)
if opts.model not in MODEL_REPO_MAP:
print(f"Available models: {list(MODEL_REPO_MAP.keys())}")
raise ValueError(f"Unknown model name '{opts.model}'")
main(opts)