-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstylemc.py
195 lines (166 loc) · 6.73 KB
/
stylemc.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
"""
Approach: "StyleMC: Multi-Channel Based Fast Text-Guided Image Generation and Manipulation"
Original source code:
https://github.com/autonomousvision/stylegan_xl/blob/f9be58e98110bd946fcdadef2aac8345466faaf3/run_stylemc.py#
Modified by Håkon Hukkelås
"""
import click
from pathlib import Path
import tqdm
from dp2 import utils
import tops
from timeit import default_timer as timer
import torch
import torch.nn.functional as F
from torchvision.transforms.functional import resize, normalize
import clip
from dp2.gan_trainer import AverageMeter
from tops.config import instantiate
from dp2.utils import vis_utils
def spherical_dist_loss(x, y):
x = F.normalize(x, dim=-1)
y = F.normalize(y, dim=-1)
return (x - y).norm(dim=-1).div(2).arcsin().pow(2).mul(2)
def prompts_dist_loss(x, targets):
loss = spherical_dist_loss
if len(targets) == 1:
return loss(x, targets[0])
distances = [loss(x, target) for target in targets]
return torch.stack(distances, dim=-1).sum(dim=-1)
affine_modules = None
max_ch = None
@torch.no_grad()
def init_affine_modules(G, batch):
global affine_modules, max_ch
affine_modules = []
max_ch = 0
def forward_hook(block, input_, output_):
global max_ch
affine_modules.append(block)
max_ch = max(max_ch, block.affine.out_features * (1 + hasattr(block, "affine_beta")))
removable_handles = []
for block in G.modules():
if hasattr(block, "affine") and hasattr(block.affine, "weight"):
removable_handles.append(block.register_forward_hook(forward_hook))
G(**batch)
for hook in removable_handles:
hook.remove()
@torch.no_grad()
def get_stylesW(w):
global affine_modules, max_ch
assert affine_modules is not None, "Have to run init_affine_modules first"
all_styles = torch.zeros((len(affine_modules), max_ch), device=w.device, dtype=torch.float32)
for i, block in enumerate(affine_modules):
gamma0 = block.affine(w)
if hasattr(block, "affine_beta"):
beta0 = block.affine_beta(w)
gamma0 = torch.cat((gamma0, beta0), dim=1)
all_styles[i] = F.pad(gamma0, ((0, max_ch - gamma0.shape[-1])), "constant", 0)
return all_styles
@torch.no_grad()
def get_styles(seed, G: torch.nn.Module, batch, truncation_value=1):
global affine_modules, max_ch
if affine_modules is None:
init_affine_modules(G, batch)
w = G.style_net.get_truncated(truncation_value, n=batch["condition"].shape[0], seed=seed)
all_styles = torch.zeros((len(affine_modules), max_ch), device=batch["img"].device, dtype=torch.float32)
for i, block in enumerate(affine_modules):
gamma0 = block.affine(w)
if hasattr(block, "affine_beta"):
beta0 = block.affine_beta(w)
gamma0 = torch.cat((gamma0, beta0), dim=1)
all_styles[i] = F.pad(gamma0, ((0, max_ch - gamma0.shape[-1])), "constant", 0)
return all_styles
def get_and_cache_direction(output_dir: Path, dl_val, G, text_prompt):
cache_path = output_dir.joinpath("stylemc_cache", text_prompt.replace(" ", "_") + ".torch")
if cache_path.is_file():
return torch.load(cache_path)
direction = find_direction(G, text_prompt, dl_val=iter(dl_val))
cache_path.parent.mkdir(exist_ok=True, parents=True)
torch.save(direction, cache_path)
return direction
@torch.enable_grad()
@torch.cuda.amp.autocast()
def find_direction(
G,
text_prompt,
n_iterations=128 * 8 * 10,
batch_size=8,
dl_val=None
):
time_start = timer()
clip_model = clip.load("ViT-B/16", device=tops.get_device())[0]
target = [clip_model.encode_text(clip.tokenize(text_prompt).to(tops.get_device())).float()]
first_batch = next(dl_val)
first_batch["embedding"] = None if "embedding" not in first_batch else first_batch["embedding"]
s = get_styles(0, G, first_batch, 0)
# stats tracker
tracker = AverageMeter()
n_iterations = n_iterations // batch_size
# initalize styles direction
direction = torch.zeros(s.shape, device=tops.get_device())
direction.requires_grad_()
utils.set_requires_grad(G, False)
direction_tracker = torch.zeros_like(direction)
opt = torch.optim.AdamW([direction], lr=0.05, betas=(0., 0.999), weight_decay=0.25)
grads = []
for seed_idx in tqdm.trange(n_iterations):
# forward pass through synthesis network with new styles
if seed_idx == 0:
batch = first_batch
else:
batch = next(dl_val)
batch["embedding"] = None if "embedding" not in batch else batch["embedding"]
styles = get_styles(seed_idx, G, batch) + direction
img = G(**batch, s=iter(styles))["img"]
# clip loss
img = (img + 1) / 2
img = normalize(img, mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711))
img = resize(img, (224, 224))
embeds = clip_model.encode_image(img)
cos_sim = prompts_dist_loss(embeds, target)
cos_sim.backward(retain_graph=True)
# track stats
tracker.update(dict(cos_sim=cos_sim, norm=torch.norm(direction)))
if not (seed_idx % batch_size):
opt.step()
grads.append(direction.grad.clone())
direction.grad.data.zero_()
print(tracker.get_average())
tracker = AverageMeter()
# throw out fluctuating channels
direction = direction.detach()
direction[direction_tracker > n_iterations / 4] = 0
print(direction)
print(f"Time for direction search: {timer() - time_start:.2f} s")
return direction
@click.command()
@click.argument("config_path")
@click.argument("text_prompt")
@click.option("-n", default=50, type=int)
def main(config_path: str, text_prompt: str, n: int):
from dp2.infer import build_trained_generator
from PIL import Image
cfg = utils.load_config(config_path)
G = build_trained_generator(cfg)
cfg.train.batch_size = 1
dl_val = instantiate(cfg.data.val.loader)
direction = get_and_cache_direction(cfg.output_dir, dl_val, G, text_prompt)
output_dir = Path("stylemc_results")
output_dir.mkdir(exist_ok=True, parents=True)
strenghts = [0, 0.05, 0.1, 0.2, 0.3, 0.4, 1.0]
for i, batch in enumerate(iter(dl_val)):
imgs = []
img = vis_utils.visualize_batch(**batch)
img = tops.im2numpy(img, False)[0]
imgs.append(img)
if i > n:
break
for strength in strenghts:
styles = get_styles(i, G, batch, truncation_value=0) + direction * strength
img = G(**batch, s=iter(styles))["img"]
imgs.append(utils.im2numpy(img, True, True)[0])
img = tops.np_make_image_grid(imgs, nrow=1)
Image.fromarray(img).save(output_dir.joinpath(f"results_{i}.png"))
if __name__ == "__main__":
main()