-
Notifications
You must be signed in to change notification settings - Fork 0
/
wd_tagger.py
317 lines (257 loc) · 9.64 KB
/
wd_tagger.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
import numpy as np
import onnxruntime as rt
import argparse
from PIL import Image, ImageFile
import os
import huggingface_hub
import pandas as pd
import argparse
from glob import glob
from multiprocessing import Pool, current_process
from tqdm import tqdm
import json
ImageFile.LOAD_TRUNCATED_IMAGES = True
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--dataset_path", type=str, default=".")
parser.add_argument("--resume", default=False, action="store_true")
parser.add_argument("--num_processes", type=int, default=1)
parser.add_argument("--save_path", type=str, default=None)
parser.add_argument("--rel_path", type=str, default=None)
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument("--num_gpus", type=int, default=1)
args = parser.parse_args()
if args.save_path is None:
args.save_path = os.path.join(args.dataset_path, "wd_tags.json")
else:
os.makedirs(os.path.dirname(args.save_path), exist_ok=True)
if args.rel_path is None:
args.rel_path = args.dataset_path
return args
# Dataset v3 series of models:
SWINV2_MODEL_DSV3_REPO = "SmilingWolf/wd-swinv2-tagger-v3"
CONV_MODEL_DSV3_REPO = "SmilingWolf/wd-convnext-tagger-v3"
VIT_MODEL_DSV3_REPO = "SmilingWolf/wd-vit-tagger-v3"
# Dataset v2 series of models:
MOAT_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-moat-tagger-v2"
SWIN_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-swinv2-tagger-v2"
CONV_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-convnext-tagger-v2"
CONV2_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-convnextv2-tagger-v2"
VIT_MODEL_DSV2_REPO = "SmilingWolf/wd-v1-4-vit-tagger-v2"
# Files to download from the repos
MODEL_FILENAME = "model.onnx"
LABEL_FILENAME = "selected_tags.csv"
kaomojis = [
"0_0",
"(o)_(o)",
"+_+",
"+_-",
"._.",
"<o>_<o>",
"<|>_<|>",
"=_=",
">_<",
"3_3",
"6_9",
">_o",
"@_@",
"^_^",
"o_o",
"u_u",
"x_x",
"|_|",
"||_||",
]
def load_labels(dataframe):
name_series = dataframe["name"]
name_series = name_series.map(
lambda x: x.replace("_", " ") if x not in kaomojis else x
)
tag_names = name_series.tolist()
rating_indexes = list(np.where(dataframe["category"] == 9)[0])
general_indexes = list(np.where(dataframe["category"] == 0)[0])
character_indexes = list(np.where(dataframe["category"] == 4)[0])
return tag_names, rating_indexes, general_indexes, character_indexes
def mcut_threshold(probs):
"""
Maximum Cut Thresholding (MCut)
Largeron, C., Moulin, C., & Gery, M. (2012). MCut: A Thresholding Strategy
for Multi-label Classification. In 11th International Symposium, IDA 2012
(pp. 172-183).
"""
sorted_probs = probs[probs.argsort()[::-1]]
difs = sorted_probs[:-1] - sorted_probs[1:]
t = difs.argmax()
thresh = (sorted_probs[t] + sorted_probs[t + 1]) / 2
return thresh
class Predictor:
def __init__(
self,
repo_path=VIT_MODEL_DSV3_REPO,
resume_download=False,
cache_dir=f"{os.environ['HOME']}/.cache/ai-oneflow",
device="cuda",
device_id=0,
):
os.makedirs(os.path.join(cache_dir, repo_path), exist_ok=True)
if (
os.path.exists(os.path.join(cache_dir, repo_path, MODEL_FILENAME))
and os.path.exists(os.path.join(cache_dir, repo_path, LABEL_FILENAME))
and not resume_download
):
csv_path = os.path.join(cache_dir, repo_path, LABEL_FILENAME)
model_path = os.path.join(cache_dir, repo_path, MODEL_FILENAME)
else:
csv_path = huggingface_hub.hf_hub_download(
repo_path,
LABEL_FILENAME,
local_dir=f"{cache_dir}/{repo_path}",
local_dir_use_symlinks=False,
)
model_path = huggingface_hub.hf_hub_download(
repo_path,
MODEL_FILENAME,
local_dir=f"{cache_dir}/{repo_path}",
local_dir_use_symlinks=False,
)
tags_df = pd.read_csv(csv_path)
sep_tags = load_labels(tags_df)
self.tag_names = sep_tags[0]
self.rating_indexes = sep_tags[1]
self.general_indexes = sep_tags[2]
self.character_indexes = sep_tags[3]
if device == "cpu":
providers = None
provider_options = None
else:
providers = ["CUDAExecutionProvider"]
provider_options = [{"device_id": device_id}]
model = rt.InferenceSession(
model_path, providers=providers, provider_options=provider_options
)
_, height, width, _ = model.get_inputs()[0].shape
self.model_target_size = height
self.model = model
def prepare_image(self, image):
target_size = self.model_target_size
canvas = Image.new("RGBA", image.size, (255, 255, 255))
canvas.alpha_composite(image)
image = canvas.convert("RGB")
# Pad image to square
image_shape = image.size
max_dim = max(image_shape)
pad_left = (max_dim - image_shape[0]) // 2
pad_top = (max_dim - image_shape[1]) // 2
padded_image = Image.new("RGB", (max_dim, max_dim), (255, 255, 255))
padded_image.paste(image, (pad_left, pad_top))
# Resize
if max_dim != target_size:
padded_image = padded_image.resize(
(target_size, target_size),
Image.BICUBIC,
)
# Convert to numpy array
image_array = np.asarray(padded_image, dtype=np.float32)
# Convert PIL-native RGB to BGR
image_array = image_array[:, :, ::-1]
return np.expand_dims(image_array, axis=0)
def predict(
self,
image,
general_thresh=0.35,
general_mcut_enabled=False,
character_thresh=0.85,
character_mcut_enabled=False,
):
image = self.prepare_image(image)
input_name = self.model.get_inputs()[0].name
label_name = self.model.get_outputs()[0].name
preds = self.model.run([label_name], {input_name: image})[0]
labels = list(zip(self.tag_names, preds[0].astype(float)))
# First 4 labels are actually ratings: pick one with argmax
ratings_names = [labels[i] for i in self.rating_indexes]
rating = dict(ratings_names)
# Then we have general tags: pick any where prediction confidence > threshold
general_names = [labels[i] for i in self.general_indexes]
if general_mcut_enabled:
general_probs = np.array([x[1] for x in general_names])
general_thresh = mcut_threshold(general_probs)
general_res = [x for x in general_names if x[1] > general_thresh]
general_res = dict(general_res)
# Everything else is characters: pick any where prediction confidence > threshold
character_names = [labels[i] for i in self.character_indexes]
if character_mcut_enabled:
character_probs = np.array([x[1] for x in character_names])
character_thresh = mcut_threshold(character_probs)
character_thresh = max(0.15, character_thresh)
character_res = [x for x in character_names if x[1] > character_thresh]
character_res = dict(character_res)
sorted_general_strings = sorted(
general_res.items(),
key=lambda x: x[1],
reverse=True,
)
sorted_general_strings = [x[0] for x in sorted_general_strings]
sorted_general_strings = (
", ".join(sorted_general_strings).replace("(", "\(").replace(")", "\)")
)
return sorted_general_strings, rating, character_res, general_res
def gen_tags(image_path):
global predictor
return predictor.predict(
Image.open(image_path).convert("RGBA"),
general_thresh=0.35,
general_mcut_enabled=False,
character_thresh=0.85,
character_mcut_enabled=False,
)[0]
def is_image(image_path):
image_types = ["png", "jpg", ".peg", "gif", "webp", "bmp", "jpeg"]
if image_path.split(".")[-1] not in image_types:
return False
# try:
# Image.open(image_path).convert("RGBA")
# except Exception:
# print(f"Error opening {image_path}")
# return False
else:
return True
def is_valid_image(image_path):
try:
Image.open(image_path).convert("RGBA")
except Exception:
print(f"Error opening {image_path}")
return False
else:
return True
def init_subprocess(device, num_gpus):
global predictor
predictor = Predictor(
device=device, device_id=(current_process()._identity[0] - 1) % num_gpus
)
if __name__ == "__main__":
args = parse_args()
image_paths = glob(f"{args.dataset_path}/**", recursive=True)
image_paths = [image_path for image_path in image_paths if is_image(image_path)]
if args.resume:
with open(args.save_path, "r") as f:
prompts = json.load(f)
image_paths = [
image_path
for image_path in image_paths
if os.path.relpath(image_path, args.rel_path) not in prompts.keys()
]
else:
prompts = {}
print(f"num images:{len(image_paths)}")
print("gen tags")
with Pool(
processes=args.num_processes,
initializer=init_subprocess,
initargs=(args.device, args.num_gpus),
) as p:
results = list(tqdm(p.imap(gen_tags, image_paths), total=len(image_paths)))
for image_path, prompt in zip(image_paths, results):
prompts[os.path.relpath(image_path, args.rel_path)] = prompt
with open(args.save_path, "w") as f:
json.dump(prompts, f, indent=4)