forked from ClownsharkBatwing/RES4LYF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle_transfer.py
More file actions
2182 lines (1580 loc) · 76.7 KB
/
style_transfer.py
File metadata and controls
2182 lines (1580 loc) · 76.7 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch import Tensor, FloatTensor
from typing import Optional, Callable, Tuple, Dict, List, Any, Union
import einops
from einops import rearrange
import copy
import comfy
from .latents import gaussian_blur_2d, median_blur_2d
# WIP... not yet in use...
class StyleTransfer:
def __init__(self,
style_method = "WCT",
embedder_method = None,
patch_size = 1,
pinv_dtype = torch.float64,
dtype = torch.float64,
):
self.style_method = style_method
self.embedder_method = None
self.unembedder_method = None
if embedder_method is not None:
self.set_embedder_method(embedder_method)
self.patch_size = patch_size
#if embedder_type == "conv2d":
# self.unembedder = self.invert_conv2d
self.pinv_dtype = pinv_dtype
self.dtype = dtype
self.patchify = None
self.unpatchify = None
self.orig_shape = None
self.grid_sizes = None
#self.x_embed_ndim = 0
def set_patchify_method(self, patchify_method=None):
self.patchify_method = patchify_method
def set_unpatchify_method(self, unpatchify_method=None):
self.unpatchify_method = unpatchify_method
def set_embedder_method(self, embedder_method):
self.embedder_method = copy.deepcopy(embedder_method).to(self.pinv_dtype)
self.W = self.embedder_method.weight
self.B = self.embedder_method.bias
if isinstance(embedder_method, nn.Linear):
self.unembedder_method = self.invert_linear
elif isinstance(embedder_method, nn.Conv2d):
self.unembedder_method = self.invert_conv2d
elif isinstance(embedder_method, nn.Conv3d):
self.unembedder_method = self.invert_conv3d
def set_patch_size(self, patch_size):
self.patch_size = patch_size
def unpatchify(self, x: Tensor) -> List[Tensor]:
x_arr = []
for i, img_size in enumerate(self.img_sizes): # [[64,64]] , img_sizes: List[Tuple[int, int]]
pH, pW = img_size
x_arr.append(
einops.rearrange(x[i, :pH*pW].reshape(1, pH, pW, -1), 'B H W (p1 p2 C) -> B C (H p1) (W p2)',
p1=self.patch_size, p2=self.patch_size)
)
x = torch.cat(x_arr, dim=0)
return x
def patchify(self, x: Tensor):
x = comfy.ldm.common_dit.pad_to_patch_size(x, (self.patch_size, self.patch_size))
pH, pW = x.shape[-2] // self.patch_size, x.shape[-1] // self.patch_size
self.img_sizes = [[pH, pW]] * x.shape[0]
x = einops.rearrange(x, 'B C (H p1) (W p2) -> B (H W) (p1 p2 C)', p1=self.patch_size, p2=self.patch_size)
return x
def embedder(self, x):
if isinstance(self.embedder_method, nn.Linear):
x = self.patchify(x)
self.orig_shape = x.shape
x = self.embedder_method(x)
self.grid_sizes = x.shape[2:]
#self.x_embed_ndim = x.ndim
#if x.ndim > 3:
# x = einops.rearrange(x, "B C H W -> B (H W) C")
return x
def unembedder(self, x):
#if self.x_embed_ndim > 3:
# x = einops.rearrange(x, "B (H W) C -> B C H W", W=self.orig_shape[-1])
x = self.unembedder_method(x)
return x
def invert_linear(self, x : torch.Tensor,) -> torch.Tensor:
x = x.to(self.pinv_dtype)
#x = (x - self.B.to(self.dtype)) @ torch.linalg.pinv(self.W.to(self.pinv_dtype)).T.to(self.dtype)
x = (x - self.B) @ torch.linalg.pinv(self.W).T
return x.to(self.dtype)
def invert_conv2d(self, z: torch.Tensor,) -> torch.Tensor:
z = z.to(self.pinv_dtype)
conv = self.embedder_method
B, C_in, H, W = self.orig_shape
C_out, _, kH, kW = conv.weight.shape
stride_h, stride_w = conv.stride
pad_h, pad_w = conv.padding
b = conv.bias.view(1, C_out, 1, 1).to(z)
z_nobias = z - b
W_flat = conv.weight.view(C_out, -1).to(z)
W_pinv = torch.linalg.pinv(W_flat)
Bz, Co, Hp, Wp = z_nobias.shape
z_flat = z_nobias.reshape(Bz, Co, -1)
x_patches = W_pinv @ z_flat
x_sum = F.fold(
x_patches,
output_size=(H + 2*pad_h, W + 2*pad_w),
kernel_size=(kH, kW),
stride=(stride_h, stride_w),
)
ones = torch.ones_like(x_patches)
count = F.fold(
ones,
output_size=(H + 2*pad_h, W + 2*pad_w),
kernel_size=(kH, kW),
stride=(stride_h, stride_w),
)
x_recon = x_sum / count.clamp(min=1e-6)
if pad_h > 0 or pad_w > 0:
x_recon = x_recon[..., pad_h:pad_h+H, pad_w:pad_w+W]
return x_recon.to(self.dtype)
def invert_conv3d(self, z: torch.Tensor, ) -> torch.Tensor:
z = z.to(self.pinv_dtype)
conv = self.embedder_method
grid_sizes = self.grid_sizes
B, C_in, D, H, W = self.orig_shape
pD, pH, pW = self.patch_size
sD, sH, sW = pD, pH, pW
if z.ndim == 3:
# [B, S, C_out] -> reshape to [B, C_out, D', H', W']
S = z.shape[1]
if grid_sizes is None:
Dp = D // pD
Hp = H // pH # getting actual patchified dims
Wp = W // pW
else:
Dp, Hp, Wp = grid_sizes
C_out = z.shape[2]
z = z.transpose(1, 2).reshape(B, C_out, Dp, Hp, Wp)
else:
B2, C_out, Dp, Hp, Wp = z.shape
assert B2 == B, "Batch size mismatch... ya sharked it."
b = conv.bias.view(1, C_out, 1, 1, 1) # need to kncokout bias to invert via weight
z_nobias = z - b
# 2D filter -> pinv
w3 = conv.weight # [C_out, C_in, 1, pH, pW]
w2 = w3.squeeze(2) # [C_out, C_in, pH, pW]
out_ch, in_ch, kH, kW = w2.shape
W_flat = w2.view(out_ch, -1) # [C_out, in_ch*pH*pW]
W_pinv = torch.linalg.pinv(W_flat) # [in_ch*pH*pW, C_out]
# merge depth for 2D unfold wackiness
z2 = z_nobias.permute(0,2,1,3,4).reshape(B*Dp, C_out, Hp, Wp)
# apply pinv ... get patch vectors
z_flat = z2.reshape(B*Dp, C_out, -1) # [B*Dp, C_out, L]
x_patches = W_pinv @ z_flat # [B*Dp, in_ch*pH*pW, L]
# fold -> restore spatial frames
x2 = F.fold(
x_patches,
output_size=(H, W),
kernel_size=(pH, pW),
stride=(sH, sW)
) # → [B*Dp, C_in, H, W]
# unmerge depth (de-depth charge)
x2 = x2.reshape(B, Dp, in_ch, H, W) # [B, Dp, C_in, H, W]
x_recon = x2.permute(0,2,1,3,4).contiguous() # [B, C_in, D, H, W]
return x_recon.to(self.dtype)
def adain_seq_inplace(self, content: torch.Tensor, style: torch.Tensor, eps: float = 1e-7) -> torch.Tensor:
mean_c = content.mean(1, keepdim=True)
std_c = content.std (1, keepdim=True).add_(eps)
mean_s = style.mean (1, keepdim=True)
std_s = style.std (1, keepdim=True).add_(eps)
content.sub_(mean_c).div_(std_c).mul_(std_s).add_(mean_s)
return content
class StyleWCT:
def __init__(self, dtype=torch.float64, use_svd=False,):
self.dtype = dtype
self.use_svd = use_svd
self.y0_adain_embed = None
self.mu_s = None
self.y0_color = None
self.spatial_shape = None
def whiten(self, f_s_centered: torch.Tensor, set=False):
cov = (f_s_centered.T.double() @ f_s_centered.double()) / (f_s_centered.size(0) - 1)
if self.use_svd:
U_svd, S_svd, Vh_svd = torch.linalg.svd(cov + 1e-5 * torch.eye(cov.size(0), dtype=cov.dtype, device=cov.device))
S_eig = S_svd
U_eig = U_svd
else:
S_eig, U_eig = torch.linalg.eigh(cov + 1e-5 * torch.eye(cov.size(0), dtype=cov.dtype, device=cov.device))
if set:
S_eig_root = S_eig.clamp(min=0).sqrt() # eigenvalues -> singular values
else:
S_eig_root = S_eig.clamp(min=0).rsqrt() # inverse square root
whiten = U_eig @ torch.diag(S_eig_root) @ U_eig.T
return whiten.to(f_s_centered)
def set(self, y0_adain_embed: torch.Tensor, spatial_shape=None):
if self.y0_adain_embed is None or self.y0_adain_embed.shape != y0_adain_embed.shape or torch.norm(self.y0_adain_embed - y0_adain_embed) > 0:
self.y0_adain_embed = y0_adain_embed.clone()
if spatial_shape is not None:
self.spatial_shape = spatial_shape
f_s = y0_adain_embed[0] # if y0_adain_embed.ndim > 4 else y0_adain_embed
self.mu_s = f_s.mean(dim=0, keepdim=True)
f_s_centered = f_s - self.mu_s
self.y0_color = self.whiten(f_s_centered, set=True)
def get(self, denoised_embed: torch.Tensor):
for wct_i in range(denoised_embed.shape[0]):
f_c = denoised_embed[wct_i]
mu_c = f_c.mean(dim=0, keepdim=True)
f_c_centered = f_c - mu_c
whiten = self.whiten(f_c_centered)
f_c_whitened = f_c_centered @ whiten.T
f_cs = f_c_whitened @ self.y0_color.T + self.mu_s
denoised_embed[wct_i] = f_cs
return denoised_embed
class WaveletStyleWCT(StyleWCT):
def set(self, y0_adain_embed: torch.Tensor, h_len, w_len):
if self.y0_adain_embed is None or self.y0_adain_embed.shape != y0_adain_embed.shape or torch.norm(self.y0_adain_embed - y0_adain_embed) > 0:
self.y0_adain_embed = y0_adain_embed.clone()
B, HW, C = y0_adain_embed.shape
LL, _, _, _ = haar_wavelet_decompose(y0_adain_embed.contiguous().view(B, C, h_len, w_len))
B_LL, C_LL, H_LL, W_LL = LL.shape
#flat = rearrange(LL, 'b c h w -> b (h w) c')
flat = LL.contiguous().view(B_LL, H_LL * W_LL, C_LL)
f_s = flat[0] # assuming batch size 1 or using only the first
self.mu_s = f_s.mean(dim=0, keepdim=True)
f_s_centered = f_s - self.mu_s
self.y0_color = self.whiten(f_s_centered, set=True)
#self.y0_adain_embed = flat # cache if needed
def get(self, denoised_embed: torch.Tensor, h_len, w_len, stylize_highfreq=False):
B, HW, C = denoised_embed.shape
denoised_embed = denoised_embed.contiguous().view(B, C, h_len, w_len)
for i in range(B):
x = denoised_embed[i:i+1] # [1, C, H, W]
LL, LH, HL, HH = haar_wavelet_decompose(x)
def process_band(band):
Bc, Cc, Hc, Wc = band.shape
flat = band.contiguous().view(Bc, Hc * Wc, Cc)
styled = super(WaveletStyleWCT, self).get(flat)
return styled.contiguous().view(Bc, Cc, Hc, Wc)
LL_styled = process_band(LL)
if stylize_highfreq:
LH_styled = process_band(LH)
HL_styled = process_band(HL)
HH_styled = process_band(HH)
else:
LH_styled, HL_styled, HH_styled = LH, HL, HH
recon = haar_wavelet_reconstruct(LL_styled, LH_styled, HL_styled, HH_styled)
denoised_embed[i] = recon.squeeze(0)
return denoised_embed.view(B, HW, C)
def haar_wavelet_decompose(x):
"""
Orthonormal Haar decomposition.
Input: [B, C, H, W]
Output: LL, LH, HL, HH with shape [B, C, H//2, W//2]
"""
if x.dtype != torch.float32:
x = x.float()
B, C, H, W = x.shape
assert H % 2 == 0 and W % 2 == 0, "Input must have even H, W"
# Precompute
norm = 1 / 2**0.5
x00 = x[:, :, 0::2, 0::2]
x01 = x[:, :, 0::2, 1::2]
x10 = x[:, :, 1::2, 0::2]
x11 = x[:, :, 1::2, 1::2]
LL = (x00 + x01 + x10 + x11) * norm * 0.5
LH = (x00 - x01 + x10 - x11) * norm * 0.5
HL = (x00 + x01 - x10 - x11) * norm * 0.5
HH = (x00 - x01 - x10 + x11) * norm * 0.5
return LL, LH, HL, HH
def haar_wavelet_reconstruct(LL, LH, HL, HH):
"""
Orthonormal inverse Haar reconstruction.
Input: LL, LH, HL, HH [B, C, H, W]
Output: Reconstructed [B, C, H*2, W*2]
"""
norm = 1 / 2**0.5
B, C, H, W = LL.shape
x00 = (LL + LH + HL + HH) * norm
x01 = (LL - LH + HL - HH) * norm
x10 = (LL + LH - HL - HH) * norm
x11 = (LL - LH - HL + HH) * norm
out = torch.zeros(B, C, H * 2, W * 2, device=LL.device, dtype=LL.dtype)
out[:, :, 0::2, 0::2] = x00
out[:, :, 0::2, 1::2] = x01
out[:, :, 1::2, 0::2] = x10
out[:, :, 1::2, 1::2] = x11
return out
"""
class StyleFeatures:
def __init__(self, dtype=torch.float64,):
self.dtype = dtype
def set(self, y0_adain_embed: torch.Tensor):
def get(self, denoised_embed: torch.Tensor):
return "Norpity McNerp"
"""
class Retrojector:
def __init__(self, proj=None, patch_size=2, pinv_dtype=torch.float64, dtype=torch.float64, ENDO=False):
self.proj = proj
self.patch_size = patch_size
self.pinv_dtype = pinv_dtype
self.dtype = dtype
self.LINEAR = isinstance(proj, nn.Linear)
self.CONV2D = isinstance(proj, nn.Conv2d)
self.CONV3D = isinstance(proj, nn.Conv3d)
self.ENDO = ENDO
self.W = proj.weight.data.to(dtype=pinv_dtype).cuda()
if self.LINEAR:
self.W_inv = torch.linalg.pinv(self.W.cuda())
elif self.CONV2D:
C_out, _, kH, kW = proj.weight.shape
W_flat = proj.weight.view(C_out, -1).to(dtype=pinv_dtype)
self.W_inv = torch.linalg.pinv(W_flat.cuda())
if proj.bias is None:
if self.LINEAR:
bias_size = proj.out_features
else:
bias_size = proj.out_channels
self.b = torch.zeros(bias_size, dtype=pinv_dtype, device=self.W_inv.device)
else:
self.b = proj.bias.data.to(dtype=pinv_dtype).to(self.W_inv.device)
def embed(self, img: torch.Tensor):
self.h = img.shape[-2] // self.patch_size
self.w = img.shape[-1] // self.patch_size
img = comfy.ldm.common_dit.pad_to_patch_size(img, (self.patch_size, self.patch_size))
if self.CONV2D:
self.orig_shape = img.shape # for unembed
img_embed = F.conv2d(
img.to(self.W),
weight=self.W,
bias=self.b,
stride=self.proj.stride,
padding=self.proj.padding
)
#img_embed = rearrange(img_embed, "b c (h ph) (w pw) -> b (h w) (c ph pw)", ph=self.patch_size, pw=self.patch_size)
img_embed = rearrange(img_embed, "b c (h ph) (w pw) -> b (h w) (c ph pw)", ph=1, pw=1)
elif self.LINEAR:
if img.ndim == 4:
img = rearrange(img, "b c (h ph) (w pw) -> b (h w) (c ph pw)", ph=self.patch_size, pw=self.patch_size)
if self.ENDO:
img_embed = F.linear(img.to(self.b) - self.b, self.W_inv)
else:
img_embed = F.linear(img.to(self.W), self.W, self.b)
return img_embed.to(img)
def unembed(self, img_embed: torch.Tensor):
if self.CONV2D:
#img_embed = rearrange(img_embed, "b (h w) (c ph pw) -> b c (h ph) (w pw)", h=self.h, w=self.w, ph=self.patch_size, pw=self.patch_size)
img_embed = rearrange(img_embed, "b (h w) (c ph pw) -> b c (h ph) (w pw)", h=self.h, w=self.w, ph=1, pw=1)
img = self.invert_conv2d(img_embed)
elif self.LINEAR:
if self.ENDO:
img = F.linear(img_embed.to(self.W), self.W, self.b)
else:
img = F.linear(img_embed.to(self.b) - self.b, self.W_inv)
if img.ndim == 3:
img = rearrange(img, "b (h w) (c ph pw) -> b c (h ph) (w pw)", h=self.h, w=self.w, ph=self.patch_size, pw=self.patch_size)
return img.to(img_embed)
def invert_conv2d(self, z: torch.Tensor,) -> torch.Tensor:
z_dtype = z.dtype
z = z.to(self.pinv_dtype)
conv = self.proj
B, C_in, H, W = self.orig_shape
C_out, _, kH, kW = conv.weight.shape
stride_h, stride_w = conv.stride
pad_h, pad_w = conv.padding
b = conv.bias.view(1, C_out, 1, 1).to(z)
z_nobias = z - b
#W_flat = conv.weight.view(C_out, -1).to(z)
#W_pinv = torch.linalg.pinv(W_flat)
Bz, Co, Hp, Wp = z_nobias.shape
z_flat = z_nobias.reshape(Bz, Co, -1)
x_patches = self.W_inv @ z_flat
x_sum = F.fold(
x_patches,
output_size=(H + 2*pad_h, W+ 2*pad_w),
kernel_size=(kH, kW),
stride=(stride_h, stride_w),
)
ones = torch.ones_like(x_patches)
count = F.fold(
ones,
output_size=(H + 2*pad_h, W + 2*pad_w),
kernel_size=(kH, kW),
stride=(stride_h, stride_w),
)
x_recon = x_sum / count.clamp(min=1e-6)
if pad_h > 0 or pad_w > 0:
x_recon = x_recon[..., pad_h:pad_h+H, pad_w:pad_w+W]
return x_recon.to(z_dtype)
def invert_patch_embedding(self, z: torch.Tensor, original_shape: torch.Size, grid_sizes: Optional[Tuple[int,int,int]] = None) -> torch.Tensor:
B, C_in, D, H, W = original_shape
pD, pH, pW = self.patch_size
sD, sH, sW = pD, pH, pW
if z.ndim == 3:
# [B, S, C_out] -> reshape to [B, C_out, D', H', W']
S = z.shape[1]
if grid_sizes is None:
Dp = D // pD
Hp = H // pH
Wp = W // pW
else:
Dp, Hp, Wp = grid_sizes
C_out = z.shape[2]
z = z.transpose(1, 2).reshape(B, C_out, Dp, Hp, Wp)
else:
B2, C_out, Dp, Hp, Wp = z.shape
assert B2 == B, "Batch size mismatch... ya sharked it."
# kncokout bias
b = self.patch_embedding.bias.view(1, C_out, 1, 1, 1)
z_nobias = z - b
# 2D filter -> pinv
w3 = self.patch_embedding.weight # [C_out, C_in, 1, pH, pW]
w2 = w3.squeeze(2) # [C_out, C_in, pH, pW]
out_ch, in_ch, kH, kW = w2.shape
W_flat = w2.view(out_ch, -1) # [C_out, in_ch*pH*pW]
W_pinv = torch.linalg.pinv(W_flat) # [in_ch*pH*pW, C_out]
# merge depth for 2D unfold wackiness
z2 = z_nobias.permute(0,2,1,3,4).reshape(B*Dp, C_out, Hp, Wp)
# apply pinv ... get patch vectors
z_flat = z2.reshape(B*Dp, C_out, -1) # [B*Dp, C_out, L]
x_patches = W_pinv @ z_flat # [B*Dp, in_ch*pH*pW, L]
# fold -> spatial frames
x2 = F.fold(
x_patches,
output_size=(H, W),
kernel_size=(pH, pW),
stride=(sH, sW)
) # → [B*Dp, C_in, H, W]
# un-merge depth
x2 = x2.reshape(B, Dp, in_ch, H, W) # [B, Dp, C_in, H, W]
x_recon = x2.permute(0,2,1,3,4).contiguous() # [B, C_in, D, H, W]
return x_recon
def invert_conv2d(
conv: torch.nn.Conv2d,
z: torch.Tensor,
original_shape: torch.Size,
) -> torch.Tensor:
import torch.nn.functional as F
B, C_in, H, W = original_shape
C_out, _, kH, kW = conv.weight.shape
stride_h, stride_w = conv.stride
pad_h, pad_w = conv.padding
if conv.bias is not None:
b = conv.bias.view(1, C_out, 1, 1).to(z)
z_nobias = z - b
else:
z_nobias = z
W_flat = conv.weight.view(C_out, -1).to(z)
W_pinv = torch.linalg.pinv(W_flat)
Bz, Co, Hp, Wp = z_nobias.shape
z_flat = z_nobias.reshape(Bz, Co, -1)
x_patches = W_pinv @ z_flat
x_sum = F.fold(
x_patches,
output_size=(H + 2*pad_h, W + 2*pad_w),
kernel_size=(kH, kW),
stride=(stride_h, stride_w),
)
ones = torch.ones_like(x_patches)
count = F.fold(
ones,
output_size=(H + 2*pad_h, W + 2*pad_w),
kernel_size=(kH, kW),
stride=(stride_h, stride_w),
)
x_recon = x_sum / count.clamp(min=1e-6)
if pad_h > 0 or pad_w > 0:
x_recon = x_recon[..., pad_h:pad_h+H, pad_w:pad_w+W]
return x_recon
def adain_seq_inplace(content: torch.Tensor, style: torch.Tensor, dim=1, eps: float = 1e-7) -> torch.Tensor:
mean_c = content.mean(dim, keepdim=True)
std_c = content.std (dim, keepdim=True).add_(eps) # in-place add
mean_s = style.mean (dim, keepdim=True)
std_s = style.std (dim, keepdim=True).add_(eps)
content.sub_(mean_c).div_(std_c).mul_(std_s).add_(mean_s) # in-place chain
return content
def adain_seq(content: torch.Tensor, style: torch.Tensor, eps: float = 1e-7) -> torch.Tensor:
return ((content - content.mean(1, keepdim=True)) / (content.std(1, keepdim=True) + eps)) * (style.std(1, keepdim=True) + eps) + style.mean(1, keepdim=True)
def apply_scattersort_tiled(
denoised_spatial : torch.Tensor,
y0_adain_spatial : torch.Tensor,
tile_h : int,
tile_w : int,
pad : int,
):
"""
Apply spatial scattersort between denoised_spatial and y0_adain_spatial
using local tile-wise sorted value matching.
Args:
denoised_spatial (Tensor): (B, C, H, W) tensor.
y0_adain_spatial (Tensor): (B, C, H, W) reference tensor.
tile_h (int): tile height.
tile_w (int): tile width.
pad (int): padding size to apply around tiles.
Returns:
denoised_embed (Tensor): (B, H*W, C) tensor after sortmatch.
"""
denoised_padded = F.pad(denoised_spatial, (pad, pad, pad, pad), mode='reflect')
y0_padded = F.pad(y0_adain_spatial, (pad, pad, pad, pad), mode='reflect')
denoised_padded_out = denoised_padded.clone()
_, _, h_len, w_len = denoised_spatial.shape
for ix in range(pad, h_len, tile_h):
for jx in range(pad, w_len, tile_w):
tile = denoised_padded[:, :, ix - pad:ix + tile_h + pad, jx - pad:jx + tile_w + pad]
y0_tile = y0_padded[:, :, ix - pad:ix + tile_h + pad, jx - pad:jx + tile_w + pad]
tile = rearrange(tile, "b c h w -> b c (h w)", h=tile_h + pad * 2, w=tile_w + pad * 2)
y0_tile = rearrange(y0_tile, "b c h w -> b c (h w)", h=tile_h + pad * 2, w=tile_w + pad * 2)
src_sorted, src_idx = tile.sort(dim=-1)
ref_sorted, ref_idx = y0_tile.sort(dim=-1)
new_tile = tile.scatter(dim=-1, index=src_idx, src=ref_sorted.expand(src_sorted.shape))
new_tile = rearrange(new_tile, "b c (h w) -> b c h w", h=tile_h + pad * 2, w=tile_w + pad * 2)
denoised_padded_out[:, :, ix:ix + tile_h, jx:jx + tile_w] = (
new_tile if pad == 0 else new_tile[:, :, pad:-pad, pad:-pad]
)
denoised_padded_out = denoised_padded_out if pad == 0 else denoised_padded_out[:, :, pad:-pad, pad:-pad]
return denoised_padded_out
def apply_scattersort_masked(
denoised_embed : torch.Tensor,
y0_adain_embed : torch.Tensor,
y0_style_pos_mask : torch.Tensor | None,
y0_style_pos_mask_edge : torch.Tensor | None,
h_len : int,
w_len : int
):
if y0_style_pos_mask is None:
flatmask = torch.ones((1,1,h_len,w_len)).bool().flatten().bool()
else:
flatmask = F.interpolate(y0_style_pos_mask, size=(h_len, w_len)).bool().flatten().cpu()
flatunmask = ~flatmask
if y0_style_pos_mask_edge is not None:
edgemask = F.interpolate(
y0_style_pos_mask_edge.unsqueeze(0), size=(h_len, w_len)
).bool().flatten()
flatmask = flatmask & (~edgemask)
flatunmask = flatunmask & (~edgemask)
denoised_masked = denoised_embed[:, flatmask, :].clone()
y0_adain_masked = y0_adain_embed[:, flatmask, :].clone()
src_sorted, src_idx = denoised_masked.sort(dim=-2)
ref_sorted, ref_idx = y0_adain_masked.sort(dim=-2)
denoised_embed[:, flatmask, :] = src_sorted.scatter(dim=-2, index=src_idx, src=ref_sorted.expand(src_sorted.shape))
if (flatunmask == True).any():
denoised_unmasked = denoised_embed[:, flatunmask, :].clone()
y0_adain_unmasked = y0_adain_embed[:, flatunmask, :].clone()
src_sorted, src_idx = denoised_unmasked.sort(dim=-2)
ref_sorted, ref_idx = y0_adain_unmasked.sort(dim=-2)
denoised_embed[:, flatunmask, :] = src_sorted.scatter(dim=-2, index=src_idx, src=ref_sorted.expand(src_sorted.shape))
if y0_style_pos_mask_edge is not None:
denoised_edgemasked = denoised_embed[:, edgemask, :].clone()
y0_adain_edgemasked = y0_adain_embed[:, edgemask, :].clone()
src_sorted, src_idx = denoised_edgemasked.sort(dim=-2)
ref_sorted, ref_idx = y0_adain_edgemasked.sort(dim=-2)
denoised_embed[:, edgemask, :] = src_sorted.scatter(dim=-2, index=src_idx, src=ref_sorted.expand(src_sorted.shape))
return denoised_embed
def apply_scattersort(
denoised_embed : torch.Tensor,
y0_adain_embed : torch.Tensor,
):
#src_sorted, src_idx = denoised_embed.cpu().sort(dim=-2)
src_idx = denoised_embed.argsort(dim=-2)
ref_sorted = y0_adain_embed.sort(dim=-2)[0]
denoised_embed.scatter_(dim=-2, index=src_idx, src=ref_sorted.expand(ref_sorted.shape))
return denoised_embed
def apply_scattersort_spatial(
denoised_spatial : torch.Tensor,
y0_adain_spatial : torch.Tensor,
):
denoised_embed = rearrange(denoised_spatial, "b c h w -> b (h w) c")
y0_adain_embed = rearrange(y0_adain_spatial, "b c h w -> b (h w) c")
src_sorted, src_idx = denoised_embed.sort(dim=-2)
ref_sorted, ref_idx = y0_adain_embed.sort(dim=-2)
denoised_embed = src_sorted.scatter(dim=-2, index=src_idx, src=ref_sorted.expand(src_sorted.shape))
return rearrange(denoised_embed, "b (h w) c -> b c h w", h=denoised_spatial.shape[-2], w=denoised_spatial.shape[-1])
def apply_scattersort_spatial(
x_spatial : torch.Tensor,
y_spatial : torch.Tensor,
):
x_emb = rearrange(x_spatial, "b c h w -> b (h w) c")
y_emb = rearrange(y_spatial, "b c h w -> b (h w) c")
x_sorted, x_idx = x_emb.sort(dim=-2)
y_sorted, y_idx = y_emb.sort(dim=-2)
x_emb = x_sorted.scatter(dim=-2, index=x_idx, src=y_sorted.expand(x_sorted.shape))
return rearrange(x_emb, "b (h w) c -> b c h w", h=x_spatial.shape[-2], w=x_spatial.shape[-1])
def apply_adain_spatial(
x_spatial : torch.Tensor,
y_spatial : torch.Tensor,
):
x_emb = rearrange(x_spatial, "b c h w -> b (h w) c")
y_emb = rearrange(y_spatial, "b c h w -> b (h w) c")
x_mean = x_emb.mean(-2, keepdim=True)
x_std = x_emb.std (-2, keepdim=True)
y_mean = y_emb.mean(-2, keepdim=True)
y_std = y_emb.std (-2, keepdim=True)
assert (x_std == 0).any() == 0, "Target tensor has no variance!"
assert (y_std == 0).any() == 0, "Reference tensor has no variance!"
x_emb_adain = (x_emb - x_mean) / x_std
x_emb_adain = (x_emb_adain * y_std) + y_mean
return x_emb_adain.reshape_as(x_spatial)
def adain_patchwise(content: torch.Tensor, style: torch.Tensor, sigma: float = 1.0, kernel_size: int = None, eps: float = 1e-5) -> torch.Tensor:
# this one is really slow
B, C, H, W = content.shape
device = content.device
dtype = content.dtype
if kernel_size is None:
kernel_size = int(2 * math.ceil(3 * sigma) + 1)
if kernel_size % 2 == 0:
kernel_size += 1
pad = kernel_size // 2
coords = torch.arange(kernel_size, dtype=torch.float64, device=device) - pad
gauss = torch.exp(-0.5 * (coords / sigma) ** 2)
gauss /= gauss.sum()
kernel_2d = (gauss[:, None] * gauss[None, :]).to(dtype=dtype)
weight = kernel_2d.view(1, 1, kernel_size, kernel_size)
content_padded = F.pad(content, (pad, pad, pad, pad), mode='reflect')
style_padded = F.pad(style, (pad, pad, pad, pad), mode='reflect')
result = torch.zeros_like(content)
for i in range(H):
for j in range(W):
c_patch = content_padded[:, :, i:i + kernel_size, j:j + kernel_size]
s_patch = style_padded[:, :, i:i + kernel_size, j:j + kernel_size]
w = weight.expand_as(c_patch)
c_mean = (c_patch * w).sum(dim=(-1, -2), keepdim=True)
c_std = ((c_patch - c_mean)**2 * w).sum(dim=(-1, -2), keepdim=True).sqrt() + eps
s_mean = (s_patch * w).sum(dim=(-1, -2), keepdim=True)
s_std = ((s_patch - s_mean)**2 * w).sum(dim=(-1, -2), keepdim=True).sqrt() + eps
normed = (c_patch[:, :, pad:pad+1, pad:pad+1] - c_mean) / c_std
stylized = normed * s_std + s_mean
result[:, :, i, j] = stylized.squeeze(-1).squeeze(-1)
return result
def adain_patchwise_row_batch(content: torch.Tensor, style: torch.Tensor, sigma: float = 1.0, kernel_size: int = None, eps: float = 1e-5) -> torch.Tensor:
B, C, H, W = content.shape
device, dtype = content.device, content.dtype
if kernel_size is None:
kernel_size = int(2 * math.ceil(3 * sigma) + 1)
if kernel_size % 2 == 0:
kernel_size += 1
pad = kernel_size // 2
coords = torch.arange(kernel_size, dtype=torch.float64, device=device) - pad
gauss = torch.exp(-0.5 * (coords / sigma) ** 2)
gauss = (gauss / gauss.sum()).to(dtype)
kernel_2d = (gauss[:, None] * gauss[None, :])
weight = kernel_2d.view(1, 1, kernel_size, kernel_size)
content_padded = F.pad(content, (pad, pad, pad, pad), mode='reflect')
style_padded = F.pad(style, (pad, pad, pad, pad), mode='reflect')
result = torch.zeros_like(content)
for i in range(H):
c_row_patches = torch.stack([
content_padded[:, :, i:i+kernel_size, j:j+kernel_size]
for j in range(W)
], dim=0) # [W, B, C, k, k]
s_row_patches = torch.stack([
style_padded[:, :, i:i+kernel_size, j:j+kernel_size]
for j in range(W)
], dim=0)
w = weight.expand_as(c_row_patches[0])
c_mean = (c_row_patches * w).sum(dim=(-1, -2), keepdim=True)
c_std = ((c_row_patches - c_mean) ** 2 * w).sum(dim=(-1, -2), keepdim=True).sqrt() + eps
s_mean = (s_row_patches * w).sum(dim=(-1, -2), keepdim=True)
s_std = ((s_row_patches - s_mean) ** 2 * w).sum(dim=(-1, -2), keepdim=True).sqrt() + eps
center = kernel_size // 2
central = c_row_patches[:, :, :, center:center+1, center:center+1]
normed = (central - c_mean) / c_std
stylized = normed * s_std + s_mean
result[:, :, i, :] = stylized.squeeze(-1).squeeze(-1).permute(1, 2, 0) # [B,C,W]
return result
def adain_patchwise_row_batch_med(content: torch.Tensor, style: torch.Tensor, sigma: float = 1.0, kernel_size: int = None, eps: float = 1e-5, mask: torch.Tensor = None, use_median_blur: bool = False, lowpass_weight=1.0, highpass_weight=1.0) -> torch.Tensor:
B, C, H, W = content.shape
device, dtype = content.device, content.dtype
if kernel_size is None:
kernel_size = int(2 * math.ceil(3 * abs(sigma)) + 1)
if kernel_size % 2 == 0:
kernel_size += 1
pad = kernel_size // 2
content_padded = F.pad(content, (pad, pad, pad, pad), mode='reflect')
style_padded = F.pad(style, (pad, pad, pad, pad), mode='reflect')
result = torch.zeros_like(content)
scaling = torch.ones((B, 1, H, W), device=device, dtype=dtype)
sigma_scale = torch.ones((H, W), device=device, dtype=torch.float32)
if mask is not None:
with torch.no_grad():
padded_mask = F.pad(mask.float(), (pad, pad, pad, pad), mode="reflect")
blurred_mask = F.avg_pool2d(padded_mask, kernel_size=kernel_size, stride=1, padding=pad)
blurred_mask = blurred_mask[..., pad:-pad, pad:-pad]
edge_proximity = blurred_mask * (1.0 - blurred_mask)
scaling = 1.0 - (edge_proximity / 0.25).clamp(0.0, 1.0)
sigma_scale = scaling[0, 0] # assuming single-channel mask broadcasted across B, C
if not use_median_blur:
coords = torch.arange(kernel_size, dtype=torch.float64, device=device) - pad
base_gauss = torch.exp(-0.5 * (coords / sigma) ** 2)
base_gauss = (base_gauss / base_gauss.sum()).to(dtype)
gaussian_table = {}
for s in sigma_scale.unique():
sig = float((sigma * s + eps).clamp(min=1e-3))
gauss_local = torch.exp(-0.5 * (coords / sig) ** 2)
gauss_local = (gauss_local / gauss_local.sum()).to(dtype)
kernel_2d = gauss_local[:, None] * gauss_local[None, :]
gaussian_table[s.item()] = kernel_2d
for i in range(H):
row_result = torch.zeros(B, C, W, dtype=dtype, device=device)
for j in range(W):
c_patch = content_padded[:, :, i:i+kernel_size, j:j+kernel_size]
s_patch = style_padded[:, :, i:i+kernel_size, j:j+kernel_size]
if use_median_blur:
# Median blur with residual restoration
unfolded_c = c_patch.reshape(B, C, -1)
unfolded_s = s_patch.reshape(B, C, -1)
c_median = unfolded_c.median(dim=-1, keepdim=True).values
s_median = unfolded_s.median(dim=-1, keepdim=True).values
center = kernel_size // 2
central = c_patch[:, :, center, center].view(B, C, 1)
residual = central - c_median
stylized = lowpass_weight * s_median + residual * highpass_weight
else:
k = gaussian_table[float(sigma_scale[i, j].item())]
local_weight = k.view(1, 1, kernel_size, kernel_size).expand(B, C, kernel_size, kernel_size)
c_mean = (c_patch * local_weight).sum(dim=(-1, -2), keepdim=True)
c_std = ((c_patch - c_mean) ** 2 * local_weight).sum(dim=(-1, -2), keepdim=True).sqrt() + eps
s_mean = (s_patch * local_weight).sum(dim=(-1, -2), keepdim=True)
s_std = ((s_patch - s_mean) ** 2 * local_weight).sum(dim=(-1, -2), keepdim=True).sqrt() + eps