File size: 9,607 Bytes
23fa49c
 
 
 
 
 
 
 
 
0bd8f65
23fa49c
 
 
 
0bd8f65
 
0c14216
0bd8f65
 
0c14216
0bd8f65
 
0c14216
0bd8f65
 
 
23fa49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c14216
23fa49c
 
 
 
 
 
 
 
 
 
 
 
0c14216
 
 
 
 
 
 
 
 
 
 
 
 
 
0a8bf1b
 
0bd8f65
 
0a8bf1b
 
0c14216
0a8bf1b
 
 
 
 
23fa49c
 
 
0c14216
 
23fa49c
 
0bd8f65
 
 
 
 
 
23fa49c
 
 
 
 
 
0bd8f65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23fa49c
 
 
 
 
0bd8f65
23fa49c
 
 
 
0bd8f65
 
 
 
 
 
 
 
 
23fa49c
0bd8f65
23fa49c
 
 
 
 
 
 
 
0a8bf1b
 
 
23fa49c
 
 
 
 
 
 
 
 
 
 
0bd8f65
 
 
23fa49c
0a8bf1b
 
 
0bd8f65
0c14216
 
 
 
0bd8f65
0a8bf1b
 
 
 
 
 
 
 
0c14216
 
 
 
 
 
 
 
0bd8f65
23fa49c
 
 
 
9934ab5
 
0aa5488
0bd8f65
 
0aa5488
 
0c14216
0aa5488
 
 
 
 
 
0bd8f65
 
0aa5488
 
0c14216
0aa5488
 
 
 
 
9934ab5
0bd8f65
 
9934ab5
74bd9c8
0c14216
9934ab5
 
 
 
0aa5488
0bd8f65
 
 
 
 
0c14216
0bd8f65
 
 
 
 
 
 
 
 
 
0c14216
0bd8f65
 
 
 
 
9934ab5
 
0bd8f65
 
9934ab5
 
0c14216
9934ab5
 
 
 
 
 
0aa5488
 
8444c60
9934ab5
 
23fa49c
 
0a8bf1b
0bd8f65
 
0a8bf1b
 
0c14216
0a8bf1b
 
 
 
 
23fa49c
 
 
 
 
 
 
 
 
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
import json

import faiss
import flax
import gradio as gr
import jax
import numpy as np
import pandas as pd
import requests
from imgutils.tagging import wd14

from Models.CLIP import CLIP


def combine_embeddings(pos_img_embs, pos_tags_embs, neg_img_embs, neg_tags_embs):
    pos = pos_img_embs + pos_tags_embs
    faiss.normalize_L2(pos)

    neg = neg_img_embs + neg_tags_embs
    faiss.normalize_L2(neg)

    result = pos - neg
    faiss.normalize_L2(result)
    return result


def danbooru_id_to_url(image_id, selected_ratings, api_username="", api_key=""):
    headers = {"User-Agent": "image_similarity_tool"}
    ratings_to_letters = {
        "General": "g",
        "Sensitive": "s",
        "Questionable": "q",
        "Explicit": "e",
    }

    acceptable_ratings = [ratings_to_letters[x] for x in selected_ratings]

    image_url = f"https://danbooru.donmai.us/posts/{image_id}.json"
    if api_username != "" and api_key != "":
        image_url = f"{image_url}?api_key={api_key}&login={api_username}"

    r = requests.get(image_url, headers=headers)
    if r.status_code != 200:
        return None

    content = json.loads(r.text)
    image_url = content["large_file_url"] if "large_file_url" in content else None
    image_url = image_url if content["rating"] in acceptable_ratings else None
    return image_url


class Predictor:
    def __init__(self):
        self.loaded_variant = None
        self.base_model = "wd-v1-4-convnext-tagger-v2"

        self.model = CLIP()

        self.tags_df = pd.read_csv("data/selected_tags.csv")

        self.images_ids = np.load("index/cosine_ids.npy")
        self.knn_index = faiss.read_index("index/cosine_knn.index")

        config = json.loads(open("index/cosine_infos.json").read())["index_param"]
        faiss.ParameterSpace().set_index_parameters(self.knn_index, config)

    def load_params(self, variant):
        if self.loaded_variant == variant:
            return

        if variant == "CLIP":
            with open(f"data/{self.base_model}/clip.msgpack", "rb") as f:
                data = f.read()
        elif variant == "SigLIP":
            with open(f"data/{self.base_model}/siglip.msgpack", "rb") as f:
                data = f.read()

        self.params = flax.serialization.msgpack_restore(data)["model"]
        self.loaded_variant = variant

    def predict(
        self,
        pos_img_input,
        neg_img_input,
        positive_tags,
        negative_tags,
        selected_model,
        selected_ratings,
        n_neighbours,
        api_username,
        api_key,
    ):
        tags_df = self.tags_df
        model = self.model

        self.load_params(selected_model)

        num_classes = len(tags_df)

        output_shape = model.out_units
        pos_img_embs = np.zeros((1, output_shape), dtype=np.float32)
        neg_img_embs = np.zeros((1, output_shape), dtype=np.float32)
        pos_tags_embs = np.zeros((1, output_shape), dtype=np.float32)
        neg_tags_embs = np.zeros((1, output_shape), dtype=np.float32)

        positive_tags = positive_tags.split(",")
        negative_tags = negative_tags.split(",")

        positive_tags_idxs = tags_df.index[tags_df["name"].isin(positive_tags)].tolist()
        negative_tags_idxs = tags_df.index[tags_df["name"].isin(negative_tags)].tolist()

        if pos_img_input is not None:
            pos_img_embs = wd14.get_wd14_tags(
                pos_img_input,
                model_name="ConvNext",
                fmt=("embedding"),
            )
            pos_img_embs = np.expand_dims(pos_img_embs, 0)
            faiss.normalize_L2(pos_img_embs)

        if neg_img_input is not None:
            neg_img_embs = wd14.get_wd14_tags(
                neg_img_input,
                model_name="ConvNext",
                fmt=("embedding"),
            )
            neg_img_embs = np.expand_dims(neg_img_embs, 0)
            faiss.normalize_L2(neg_img_embs)

        if len(positive_tags_idxs) > 0:
            tags = np.zeros((1, num_classes), dtype=np.float32)
            tags[0][positive_tags_idxs] = 1

            pos_tags_embs = model.apply(
                {"params": self.params},
                tags,
                method=model.encode_text,
            )
            pos_tags_embs = jax.device_get(pos_tags_embs)
            faiss.normalize_L2(pos_tags_embs)

        if len(negative_tags_idxs) > 0:
            tags = np.zeros((1, num_classes), dtype=np.float32)
            tags[0][negative_tags_idxs] = 1

            neg_tags_embs = model.apply(
                {"params": self.params},
                tags,
                method=model.encode_text,
            )
            neg_tags_embs = jax.device_get(neg_tags_embs)
            faiss.normalize_L2(neg_tags_embs)

        embeddings = combine_embeddings(
            pos_img_embs,
            pos_tags_embs,
            neg_img_embs,
            neg_tags_embs,
        )

        dists, indexes = self.knn_index.search(embeddings, k=n_neighbours)
        neighbours_ids = self.images_ids[indexes][0]
        neighbours_ids = [int(x) for x in neighbours_ids]

        captions = []
        image_urls = []
        for image_id, dist in zip(neighbours_ids, dists[0]):
            current_url = danbooru_id_to_url(
                image_id,
                selected_ratings,
                api_username,
                api_key,
            )
            if current_url is not None:
                image_urls.append(current_url)
                captions.append(f"{image_id}/{dist:.2f}")
        return list(zip(image_urls, captions))


def main():
    predictor = Predictor()

    with gr.Blocks() as demo:
        with gr.Row():
            pos_img_input = gr.Image(type="pil", label="Positive input")
            neg_img_input = gr.Image(type="pil", label="Negative input")
        with gr.Row():
            with gr.Column():
                positive_tags = gr.Textbox(label="Positive tags")
                negative_tags = gr.Textbox(label="Negative tags")
            with gr.Column():
                selected_model = gr.Radio(
                    choices=["CLIP", "SigLIP"],
                    value="CLIP",
                    label="Model",
                )
                n_neighbours = gr.Slider(
                    minimum=1,
                    maximum=20,
                    value=5,
                    step=1,
                    label="# of images",
                )
            with gr.Column():
                selected_ratings = gr.CheckboxGroup(
                    choices=["General", "Sensitive", "Questionable", "Explicit"],
                    value=["General", "Sensitive"],
                    label="Ratings",
                )
                with gr.Row():
                    api_username = gr.Textbox(label="Danbooru API Username")
                    api_key = gr.Textbox(label="Danbooru API Key")

        find_btn = gr.Button("Find similar images")

        similar_images = gr.Gallery(label="Similar images", columns=[5])

        examples = gr.Examples(
            [
                [
                    None,
                    None,
                    "marcille_donato",
                    "",
                    "CLIP",
                    ["General", "Sensitive"],
                    5,
                    "",
                    "",
                ],
                [
                    None,
                    None,
                    "yellow_eyes,red_horns",
                    "",
                    "CLIP",
                    ["General", "Sensitive"],
                    5,
                    "",
                    "",
                ],
                [
                    None,
                    None,
                    "artoria_pendragon_(fate),solo",
                    "green_eyes",
                    "CLIP",
                    ["General", "Sensitive"],
                    5,
                    "",
                    "",
                ],
                [
                    "examples/60378883_p0.jpg",
                    None,
                    "fujimaru_ritsuka_(female)",
                    "solo",
                    "CLIP",
                    ["General", "Sensitive"],
                    5,
                    "",
                    "",
                ],
                [
                    "examples/DaRlExxUwAAcUOS-orig.jpg",
                    "examples/46657164_p1.jpg",
                    "",
                    "",
                    "CLIP",
                    ["General", "Sensitive"],
                    5,
                    "",
                    "",
                ],
            ],
            inputs=[
                pos_img_input,
                neg_img_input,
                positive_tags,
                negative_tags,
                selected_model,
                selected_ratings,
                n_neighbours,
                api_username,
                api_key,
            ],
            outputs=[similar_images],
            fn=predictor.predict,
            run_on_click=True,
            cache_examples=False,
        )

        find_btn.click(
            fn=predictor.predict,
            inputs=[
                pos_img_input,
                neg_img_input,
                positive_tags,
                negative_tags,
                selected_model,
                selected_ratings,
                n_neighbours,
                api_username,
                api_key,
            ],
            outputs=[similar_images],
        )

    demo.queue()
    demo.launch()


if __name__ == "__main__":
    main()