File size: 6,674 Bytes
11dad4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312a92e
 
 
 
0bccd0d
9d6732b
 
 
 
 
 
11dad4e
 
 
 
 
 
 
 
bb41be0
11dad4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb41be0
11dad4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

from __future__ import annotations

import argparse
import functools
import io
import os
import pathlib
import tarfile

import deepdanbooru as dd
import gradio as gr
import huggingface_hub
import numpy as np
import PIL.Image
import tensorflow as tf
from huggingface_hub import hf_hub_download

TITLE = 'TADNE Image Search with DeepDanbooru'
DESCRIPTION = '''The original TADNE site is https://thisanimedoesnotexist.ai/.

This app shows images similar to the query image from images generated
by the TADNE model with seed 0-99999.
Here, image similarity is measured by the L2 distance of the intermediate
features by the [DeepDanbooru](https://github.com/KichangKim/DeepDanbooru)
model.

The resolution of the output images in this app is 128x128, but you can
check the original 512x512 images from URLs like
https://thisanimedoesnotexist.ai/slider.html?seed=10000 using the output seeds.

Expected execution time on Hugging Face Spaces: 25s

Related Apps:
- [TADNE](https://huggingface.co/spaces/hysts/TADNE)
- [TADNE Image Viewer](https://huggingface.co/spaces/hysts/TADNE-image-viewer)
- [TADNE Image Selector](https://huggingface.co/spaces/hysts/TADNE-image-selector)
- [TADNE Interpolation](https://huggingface.co/spaces/hysts/TADNE-interpolation)
'''
ARTICLE = None

TOKEN = os.environ['TOKEN']


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument('--theme', type=str)
    parser.add_argument('--live', action='store_true')
    parser.add_argument('--share', action='store_true')
    parser.add_argument('--port', type=int)
    parser.add_argument('--disable-queue',
                        dest='enable_queue',
                        action='store_false')
    parser.add_argument('--allow-flagging', type=str, default='never')
    return parser.parse_args()


def download_image_tarball(size: int, dirname: str) -> pathlib.Path:
    path = hf_hub_download('hysts/TADNE-sample-images',
                           f'{size}/{dirname}.tar',
                           repo_type='dataset',
                           use_auth_token=TOKEN)
    return path


def load_deepdanbooru_predictions(dirname: str) -> np.ndarray:
    path = hf_hub_download(
        'hysts/TADNE-sample-images',
        f'prediction_results/deepdanbooru/intermediate_features/{dirname}.npy',
        repo_type='dataset',
        use_auth_token=TOKEN)
    return np.load(path)


def load_sample_image_paths() -> list[pathlib.Path]:
    image_dir = pathlib.Path('images')
    if not image_dir.exists():
        dataset_repo = 'hysts/sample-images-TADNE'
        path = huggingface_hub.hf_hub_download(dataset_repo,
                                               'images.tar.gz',
                                               repo_type='dataset',
                                               use_auth_token=TOKEN)
        with tarfile.open(path) as f:
            f.extractall()
    return sorted(image_dir.glob('*'))


def create_model() -> tf.keras.Model:
    path = huggingface_hub.hf_hub_download('hysts/DeepDanbooru',
                                           'model-resnet_custom_v3.h5',
                                           use_auth_token=TOKEN)
    model = tf.keras.models.load_model(path)
    model = tf.keras.Model(model.input, model.layers[-4].output)
    layer = tf.keras.layers.GlobalAveragePooling2D()
    model = tf.keras.Sequential([model, layer])
    return model


def predict(image: PIL.Image.Image, model: tf.keras.Model) -> np.ndarray:
    _, height, width, _ = model.input_shape
    image = np.asarray(image)
    image = tf.image.resize(image,
                            size=(height, width),
                            method=tf.image.ResizeMethod.AREA,
                            preserve_aspect_ratio=True)
    image = image.numpy()
    image = dd.image.transform_and_pad_image(image, width, height)
    image = image / 255.
    features = model.predict(image[None, ...])[0]
    features = features.astype(float)
    return features


def run(
    image: PIL.Image.Image,
    nrows: int,
    ncols: int,
    image_size: int,
    dirname: str,
    tarball_path: pathlib.Path,
    deepdanbooru_predictions: np.ndarray,
    model: tf.keras.Model,
) -> tuple[np.ndarray, np.ndarray]:
    features = predict(image, model)
    distances = ((deepdanbooru_predictions - features)**2).sum(axis=1)

    image_indices = np.argsort(distances)

    seeds = []
    images = []
    with tarfile.TarFile(tarball_path) as tar_file:
        for index in range(nrows * ncols):
            image_index = image_indices[index]
            seeds.append(image_index)
            member = tar_file.getmember(f'{dirname}/{image_index:07d}.jpg')
            with tar_file.extractfile(member) as f:
                data = io.BytesIO(f.read())
            image = PIL.Image.open(data)
            image = np.asarray(image)
            images.append(image)
    res = np.asarray(images).reshape(nrows, ncols, image_size, image_size,
                                     3).transpose(0, 2, 1, 3, 4).reshape(
                                         nrows * image_size,
                                         ncols * image_size, 3)
    seeds = np.asarray(seeds).reshape(nrows, ncols)

    return res, seeds


def main():
    args = parse_args()

    image_size = 128
    dirname = '0-99999'
    tarball_path = download_image_tarball(image_size, dirname)
    deepdanbooru_predictions = load_deepdanbooru_predictions(dirname)

    model = create_model()

    image_paths = load_sample_image_paths()
    examples = [[path.as_posix(), 2, 5] for path in image_paths]

    func = functools.partial(
        run,
        image_size=image_size,
        dirname=dirname,
        tarball_path=tarball_path,
        deepdanbooru_predictions=deepdanbooru_predictions,
        model=model,
    )
    func = functools.update_wrapper(func, run)

    gr.Interface(
        func,
        [
            gr.inputs.Image(type='pil', label='Input'),
            gr.inputs.Slider(1, 10, step=1, default=2, label='Number of Rows'),
            gr.inputs.Slider(
                1, 10, step=1, default=5, label='Number of Columns'),
        ],
        [
            gr.outputs.Image(type='numpy', label='Output'),
            gr.outputs.Dataframe(type='numpy', label='Seed'),
        ],
        examples=examples,
        title=TITLE,
        description=DESCRIPTION,
        article=ARTICLE,
        theme=args.theme,
        allow_flagging=args.allow_flagging,
        live=args.live,
    ).launch(
        enable_queue=args.enable_queue,
        server_port=args.port,
        share=args.share,
    )


if __name__ == '__main__':
    main()