File size: 4,306 Bytes
ea476a5
 
 
 
 
19d3041
 
 
 
ea476a5
 
 
19d3041
 
 
 
 
ea476a5
 
19d3041
ea476a5
 
 
 
 
 
 
 
 
 
 
 
 
8a8b805
ea476a5
 
 
 
8a8b805
 
ea476a5
 
 
 
 
 
 
 
8a8b805
ea476a5
 
 
 
 
8a8b805
 
ea476a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a8b805
 
 
 
 
ea476a5
 
 
8a8b805
ea476a5
 
 
 
 
 
 
 
 
 
 
 
 
 
8a8b805
ea476a5
 
8a8b805
ea476a5
 
8a8b805
ea476a5
 
 
 
 
 
 
8a8b805
ea476a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a8b805
ea476a5
 
 
 
 
8a8b805
ea476a5
 
 
 
 
8a8b805
ea476a5
 
 
 
8a8b805
ea476a5
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
import gradio as gr
from .model_loader import Model
from PIL import Image
import cv2
import io
from huggingface_hub import snapshot_download

models_path = snapshot_download(repo_id="radames/UserControllableLT", repo_type="model")


# models fron pretrained/latent_transformer folder
models_files = {
    "anime": "anime.pt",
    "car": "car.pt",
    "cat": "cat.pt",
    "church": "church.pt",
    "ffhq": "ffhq.pt",
}

models = {name: Model(models_path + "/" + path) for name, path in models_files.items()}


def cv_to_pil(img):
    return Image.fromarray(cv2.cvtColor(img.astype("uint8"), cv2.COLOR_BGR2RGB))


def random_sample(model_name: str):
    model = models[model_name]
    img, latents = model.random_sample()
    pil_img = cv_to_pil(img)
    return pil_img, model_name, latents


def zoom(model_state, latents_state, dx=0, dy=0, dz=0, sxsy=[128, 128]):
    model = models[model_state]
    dx = dx
    dy = dy
    dz = dz
    sx = sxsy[0]
    sy = sxsy[1]
    stop_points = []
    img, latents_state = model.zoom(
        latents_state, dz, sxsy=[sx, sy], stop_points=stop_points
    )  # dz, sxsy=[sx, sy], stop_points=stop_points)
    pil_img = cv_to_pil(img)
    return pil_img, latents_state


def translate(model_state, latents_state, dx=0, dy=0, dz=0, sxsy=[128, 128]):
    model = models[model_state]

    dx = dx
    dy = dy
    dz = dz
    sx = sxsy[0]
    sy = sxsy[1]
    stop_points = []
    zi = False
    zo = False

    img, latents_state = model.translate(
        latents_state,
        [dx, dy],
        sxsy=[sx, sy],
        stop_points=stop_points,
        zoom_in=zi,
        zoom_out=zo,
    )

    pil_img = cv_to_pil(img)
    return pil_img, latents_state


def change_style(image: Image.Image, model_state, latents_state):
    model = models[model_state]
    img, latents_state = model.change_style(latents_state)
    pil_img = cv_to_pil(img)
    return pil_img, latents_state


def reset(model_state, latents_state):
    model = models[model_state]
    img, latents_state = model.reset(latents_state)
    pil_img = cv_to_pil(img)
    return pil_img, latents_state


def image_click(evt: gr.SelectData):
    click_pos = evt.index
    return click_pos


with gr.Blocks() as block:
    model_state = gr.State(value="cat")
    latents_state = gr.State({})
    sxsy = gr.State([128, 128])
    gr.Markdown("# UserControllableLT: User controllable latent transformer")
    gr.Markdown("## Select model")
    with gr.Row():
        with gr.Column():
            model_name = gr.Dropdown(
                choices=list(models_files.keys()),
                label="Select Pretrained Model",
                value="cat",
            )
            with gr.Row():
                button = gr.Button("Random sample")
                reset_btn = gr.Button("Reset")

            dx = gr.Slider(
                minimum=-256, maximum=256, step_size=0.1, label="dx", value=0.0
            )
            dy = gr.Slider(
                minimum=-256, maximum=256, step_size=0.1, label="dy", value=0.0
            )
            dz = gr.Slider(
                minimum=-256, maximum=256, step_size=0.1, label="dz", value=0.0
            )

            with gr.Row():
                change_style_bt = gr.Button("Change style")

        with gr.Column():
            image = gr.Image(type="pil", label="")
    image.select(image_click, inputs=None, outputs=sxsy)
    button.click(
        random_sample, inputs=[model_name], outputs=[image, model_state, latents_state]
    )
    reset_btn.click(
        reset,
        inputs=[model_state, latents_state],
        outputs=[image, latents_state],
    )

    change_style_bt.click(
        change_style,
        inputs=[image, model_state, latents_state],
        outputs=[image, latents_state],
    )
    dx.change(
        translate,
        inputs=[model_state, latents_state, dx, dy, dz, sxsy],
        outputs=[image, latents_state],
        show_progress=False,
    )
    dy.change(
        translate,
        inputs=[model_state, latents_state, dx, dy, dz, sxsy],
        outputs=[image, latents_state],
        show_progress=False,
    )
    dz.change(
        zoom,
        inputs=[model_state, latents_state, dx, dy, dz, sxsy],
        outputs=[image, latents_state],
        show_progress=False,
    )

block.queue()
block.launch()