-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathspipf.py
More file actions
1335 lines (1101 loc) · 38.7 KB
/
spipf.py
File metadata and controls
1335 lines (1101 loc) · 38.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 inspect
import torch.nn.functional as F
from typing import TypeAlias, Iterable, Callable, _CallableGenericAlias, _GenericAlias
from types import GenericAlias, UnionType
from dataclasses import dataclass
from enum import Enum
from math import sqrt, log, pi
from torch import Tensor
from scipy.special import erfinv
# #
# === TYPES === #
# #
Filter: TypeAlias = Callable[[Tensor], Tensor]
# #
# === BOUNDING BOX (BBOX) === #
# #
# BBox stores the widths of segments i.e. left is the width of the segment to
# the left of the box, width is the width of the box, and right is the width of
# the segment to the right of the box.
@dataclass
class BBox:
left: int
width: int
right: int
up: int
height: int
down: int
@property
def total_width(self):
return self.left + self.width + self.right
@property
def total_height(self):
return self.up + self.height + self.down
@property
def down_offset(self):
return self.up + self.height
@property
def right_offset(self):
return self.left + self.width
@property
def valid(self) -> bool:
return \
self.left >= 0 and \
self.width >= 0 and \
self.right >= 0 and \
self.up >= 0 and \
self.height >= 0 and \
self.down >= 0
def bbox_from_2d_binary(tensor: Tensor) -> BBox:
_check([
"tensor.dim() == 2",
"tensor.dtype == torch.bool",
])
u, h, d = _h_min_max(tensor)
l, w, r = _h_min_max(tensor.transpose(0, 1))
bbox = BBox(l, w, r, u, h, d)
return bbox
def _h_min_max(tensor: Tensor) -> tuple[int, int, int]:
th, _ = tensor.shape
h_index = torch.arange(0, th)
h_index_reversed = (th - 1) - h_index
crushed = torch.sum(tensor, 1, dtype=torch.bool)
down_offset = int(torch.amax(crushed * h_index)) + 1
up_offset = int(torch.amax(crushed * h_index_reversed)) + 1
down = th - down_offset
up = th - up_offset
height = th - (down + up)
return (up, height, down)
def bbox_expand_area(bbox: BBox, area_multiplier: float) -> BBox:
_check([
"bbox.valid",
"area_multiplier >= 0",
])
side_multiplier = sqrt(area_multiplier)
_, _, _, am = _axis_expand(bbox.left, bbox.width, bbox.right, side_multiplier)
u, h, d, am = _axis_expand(bbox.up, bbox.height, bbox.down, area_multiplier / am)
l, w, r, __ = _axis_expand(bbox.left, bbox.width, bbox.right, area_multiplier / am)
expanded = BBox(l, w, r, u, h, d)
return expanded
def bbox_outer_square(bbox: BBox) -> BBox:
_check([
"bbox.valid",
])
w_mul = max(bbox.height / bbox.width, 1)
h_mul = max(bbox.width / bbox.height, 1)
nl, nw, nr, _ = _axis_expand(bbox.left, bbox.width, bbox.right, w_mul)
nu, nh, nd, _ = _axis_expand(bbox.up, bbox.height, bbox.down, h_mul)
new_bbox = BBox(nl, nw, nr, nu, nh, nd)
return new_bbox
def _axis_expand(a: int, b: int, c: int, m: float) -> tuple[int, int, int, float]:
total = a + b + c
new_b = min(total, round(b * m))
actual_m = new_b / b
delta = new_b - b
new_a = max(0, a - delta // 2)
new_c = max(0, total - (new_a + new_b))
new_a = total - (new_b + new_c)
return (new_a, new_b, new_c, actual_m)
# #
# === COLORSPACE === #
# #
# Reference: http://www.ericbrasseur.org/gamma.html?i=1#formulas
# NB: Colorspace conversion should generally be done with 64-bit floats and
# conversions back to gamma should be quantized to the number of colors that
# the final encoding supports (e.g. quantized to 256 values if the final
# encoding is 8-bits per channel).
def colorspace_srgb_linear_from_gamma(tensor: Tensor) -> Tensor:
_check([
"tensor.is_floating_point()"
])
piece_lo = tensor / 12.92
A = 0.055
piece_hi = torch.pow((tensor + A) / (1 + A), 2.4)
linear = torch.where(tensor <= 0.04045, piece_lo, piece_hi)
return linear
def colorspace_srgb_gamma_from_linear(tensor: Tensor) -> Tensor:
_check([
"tensor.is_floating_point()"
])
piece_lo = tensor * 12.92
A = 0.055
piece_hi = (1 + A) * torch.pow(tensor, 1/2.4) - A
gamma = torch.where(tensor <= 0.0031308, piece_lo, piece_hi)
return gamma
def colorspace_linear_from_gamma(tensor: Tensor, gamma: float = 2.2) -> Tensor:
_check([
"tensor.is_floating_point()"
])
linear = torch.pow(tensor, gamma)
return linear
def colorspace_gamma_from_linear(tensor: Tensor, gamma: float = 2.2) -> Tensor:
_check([
"tensor.is_floating_point()"
])
gamma = torch.pow(tensor, 1 / gamma)
return gamma
def colorspace_log2_from_linear(tensor: Tensor, eps: float = 0.00001) -> Tensor:
_check([
"tensor.is_floating_point()"
])
log = torch.log2(tensor + eps)
return log
def colorspace_linear_from_log2(tensor: Tensor, eps: float = 0.00001) -> Tensor:
_check([
"tensor.is_floating_point()"
])
linear = torch.exp2(tensor) - eps
return linear
# #
# === CONVERSION === #
# #
def convert_luminance_from_linear_srgb(tensor: Tensor, channel_dim: int) -> Tensor:
_check([
"tensor.is_floating_point()",
"_valid_dim(tensor, channel_dim)",
"tensor.shape[channel_dim] == 3",
])
coef_shape = [1] * tensor.dim()
coef_shape[channel_dim] = 3
luminance_coef = torch.tensor((0.2126, 0.7152, 0.0722)).reshape(*coef_shape)
luminance = (tensor * luminance_coef).sum(channel_dim, keepdims=True)
return luminance
def convert_component_coloring_from_2d_binary(tensor: Tensor, diagonals: bool = False) -> Tensor:
_check([
"tensor.dim() == 2",
"tensor.dtype == torch.bool",
])
h, w = tensor.shape
integer = tensor.to(torch.int)
# 1D component coloring
prepend = torch.zeros(h, 1, dtype=torch.int)
diffed = torch.diff(integer, dim=1, prepend=prepend).clamp(0, 1)
cumsum = torch.cumsum(diffed.flatten(), 0).reshape(h, w)
coloring_1d = cumsum * integer
coloring_2d = coloring_1d.clone()
max_unique = 0
# Find equivalences between strips
adjacent_middle = torch.logical_and(tensor[:-1, :], tensor[1:, :])
equ_m_u = coloring_1d[:-1, :][adjacent_middle]
equ_m_d = coloring_1d[1:, :][adjacent_middle]
equivalences = torch.stack((equ_m_u, equ_m_d), dim=1)
if diagonals: # 8-way equivalance
adjacent_left = torch.logical_and(tensor[:-1, :-1], tensor[1:, 1:])
adjacent_right = torch.logical_and(tensor[:-1, 1:], tensor[1:, :-1])
equ_l_u = coloring_1d[:-1, :-1][adjacent_left]
equ_r_u = coloring_1d[:-1, 1:][adjacent_right]
equ_l_d = coloring_1d[1:, 1:][adjacent_left]
equ_r_d = coloring_1d[1:, :-1][adjacent_right]
equ_l = torch.stack((equ_l_u, equ_l_d), dim=1)
equ_r = torch.stack((equ_r_u, equ_r_d), dim=1)
equivalences = torch.cat((equivalences, equ_l, equ_r), dim=0)
# Find all of the strip coloring identifiers
unique = torch.unique_consecutive(cumsum).tolist()
if unique[0] == 0:
unique = unique[1:]
if len(unique) == 0:
coloring_2d[:, :] = 0
return (coloring_2d, max_unique)
# Find all of the sets of 1D colorings that should be tha same color
umax = int(unique[-1])
parent = list(range(umax + 1))
size = [1] * (umax + 1)
for pair in equivalences.tolist():
p0, p1 = pair
_disjoint_set_union(p0, p1, parent, size)
for x in range(umax + 1):
parent[x] = _disjoint_set_find(x, parent)
# Relabel the previously found groups so that they're sequential integers
parent = Tensor(parent)
parent_unique = torch.unique(parent)
max_unique = max(max_unique, int(parent_unique.shape[0]) - 1)
index_map = torch.zeros((umax + 1, ), dtype=torch.long)
index_map[parent_unique] = torch.arange(0, parent_unique.shape[0])
parent_relabel = index_map.index_select(0, parent)
# Recolor the 1D colorings to their grouped and relabeled 2D coloring
coloring_2d.reshape(-1)[:] = parent_relabel.index_select(0, coloring_2d.reshape(-1))
return (coloring_2d, max_unique)
def _disjoint_set_union(x, y, parents, size):
px = _disjoint_set_find(x, parents)
py = _disjoint_set_find(y, parents)
if px != py:
if size[px] < size[py]:
(px, py) = (py, px)
parents[py] = px
size[px] += size[py]
def _disjoint_set_find(x, parents):
while parents[x] != x:
parents[x] = parents[parents[x]]
x = parents[x]
return x
def convert_gradient_suppression_mask(gtf_r: Tensor, gtf_theta: Tensor) -> Tensor:
padded = transform_pad_dim_reflect(transform_pad_dim_reflect(gtf_r, 3, (1, 1)), 2, (1, 1))
views = []
for i in range(9):
x, y = i % 3, i // 3
nx, ny = _ztn(-(2-x)), _ztn(-(2-y))
views += [padded[:, :, y:ny, x:nx]]
comparisons = []
for i in range(4):
lower = views[4] >= views[i]
upper = views[4] >= views[8-i]
comparisons += [torch.logical_and(lower, upper)]
angle_index = (((gtf_theta + (9 * pi / 8)) * (4 / pi)).to(torch.int) + 3) % 4
odd = (angle_index % 2).to(torch.bool)
l = torch.where(odd, comparisons[1], comparisons[0])
u = torch.where(odd, comparisons[3], comparisons[2])
mask = torch.where((angle_index // 2).to(torch.bool), u, l)
return mask
def _ztn(value: int) -> int | None:
return None if value == 0 else value
# https://en.wikipedia.org/wiki/Otsu%27s_method
def convert_otsus_method(gtf: Tensor, bins: int) -> Tensor:
b, c, h, w = gtf.shape
binned = (util_range_normalize(gtf, (2, 3)) * (bins - 1)).to(torch.int)
batches = []
for bi in range(b):
channels = []
for ci in range(c):
n = torch.bincount(binned[bi, ci].flatten(), minlength=bins)
n_cumsum = torch.cumsum(n, 0)
N = n_cumsum[-1]
omega = n_cumsum / N
mu = torch.cumsum(n_cumsum * torch.arange(bins), 0) / N
mu_T = mu[-1]
sigma_2_B = (mu_T * omega - mu)**2 / (omega * (1 - omega))
threshold = torch.argmax(sigma_2_B.nan_to_num()) / (bins - 1)
if threshold.dim() == 1:
return threshold[0]
channels += [threshold.reshape(1, 1)]
batches += [torch.stack(channels)]
thresholds = torch.stack(batches)
return thresholds
# #
# === FILTERING === #
# #
def filter_convolve_2d(tensor: Tensor, kernel: Tensor, dims: tuple[int, int]) -> Tensor:
_check([
"_valid_dims(tensor, dims)",
"_ascending(dims)",
"kernel.dim() == 2",
"kernel.shape[0] % 2 == 1 and kernel.shape[1] % 2 == 1",
])
kh, kw = kernel.shape
kernel_reshaped = kernel.reshape(1, 1, kh, kw)
ph, pw = kh // 2, kw // 2
permuted = utils_dims_to_end(tensor, dims)
padded = transform_pad_dim2_reflect(permuted, (tensor.dim() - 2, tensor.dim() - 1), ((ph, ph), (pw, pw)))
flattened = padded.flatten(0,-3).unsqueeze(0)
convolved = F.conv2d(flattened, kernel_reshaped, groups=flattened.shape[0])
reshaped = convolved.reshape(*permuted.shape)
unpermuted = utils_dims_from_end(reshaped, dims)
return unpermuted
def filter_convolve_2d_separable(tensor: Tensor, kernels: tuple[Tensor, Tensor], dims: tuple[int, int]) -> Tensor:
_check([
"kernels[0].dim() == 1 and kernels[1].dim() == 1",
"kernels[0].shape[0] % 2 == 1 and kernels[1].shape[0] % 2 == 1",
"_ascending(dims)",
"_valid_dims(tensor, dims)",
])
c0 = tensor
c1 = filter_convolve_2d(c0, kernels[0].reshape(-1, 1), dims)
c2 = filter_convolve_2d(c1, kernels[1].reshape(1, -1), dims)
return c2
def filter_hysteresis_threshold(gtf_weak: Tensor, gtf_strong: Tensor) -> Tensor:
b, c, h, w = gtf_weak.shape
coloring, max_unique = TF.component_coloring(gtf_weak, True)
strong_components = gtf_strong.to(torch.bool) * coloring
batches = []
for bi in range(b):
channels = []
for ci in range(c):
strong_select = torch.zeros(max_unique + 1)
strong_select[strong_components[bi, ci].flatten()] = 1
strong_select[0] = 0
result = strong_select.index_select(0, coloring[bi, ci].flatten()).reshape(h, w)
channels += [result]
batches += [torch.stack(channels)]
thresholded = torch.stack(batches)
return thresholded
def filter_patch_min(gtf: Tensor, radius: int) -> Tensor:
k = 2 * radius + 1
padded = transform_pad_dim2_reflect(gtf, (2, 3), (radius, radius))
minimum = -F.max_pool2d(-padded, k, stride=1)
return minimum
def filter_patch_max(gtf: Tensor, radius: int) -> Tensor:
k = 2 * radius + 1
padded = transform_pad_dim2_reflect(gtf, (2, 3), (radius, radius))
maximum = F.max_pool2d(padded, k, stride=1)
return maximum
def filter_patch_median(gtf: Tensor, radius: int) -> Tensor:
def unfold(gtf: Tensor, kh: int, kw: int) -> Tensor:
unfolded = gtf.unfold(3 , kw, 1).unfold(2, kh, 1)
return unfolded
b, c, h, w = gtf.shape
k = 2 * radius + 1
padded = transform_pad_dim2_reflect(gtf, (2, 3), (radius, radius))
unfolded = unfold(padded, k, k).flatten(-2)
median = unfolded.median(dim=-1, keepdim=False).values
return median
def filter_patch_range_normalize(gtf: Tensor, radius: int) -> Tensor:
r = radius
k = 2 * r + 1
minimum = patch_min(gtf, radius)
ln = gtf - minimum
maximum = patch_max(ln, radius)
normalized = ln / maximum
return normalized
# MORPHOLOGICAL
def filter_morpho_dilate(tensor: Tensor, radius: int, dims: tuple[int, int]) -> Tensor:
_check([
"_valid_dims(tensor, dims)",
"radius > 0",
])
kernel_size = 2 * radius - 1
padding = radius - 1
permuted = utils_dims_to_end(tensor, dims)
reshaped = permuted.flatten(0, -3).unsqueeze(0)
dilated = F.max_pool2d(reshaped, kernel_size, stride=1, padding=padding)
unpermuted = utils_dims_from_end(dilated.reshape(*permuted.shape), dims)
return unpermuted
# def filter_morpho_erode(tensor: Tensor, radius: int) -> Tensor:
# eroded = _invert(dilate(_invert(tensor), radius))
# return eroded
#
#
# def filter_morpho_close(tensor: Tensor, radius: int) -> Tensor:
# closed = erode(dilate(tensor, radius), radius)
# return closed
#
#
# def filter_morpho_open(tensor: Tensor, radius: int) -> Tensor:
# opened = dilate(erode(tensor, radius), radius)
# return opened
#
#
# def _invert(tensor: Tensor) -> Tensor:
# inverted = (1.0 - tensor)
# return inverted
# OTHER
class RoundingMode(Enum):
ROUND = torch.round
FLOOR = torch.floor
CEILING = torch.ceil
def filter_quantize(tensor: Tensor, steps: int, mode: RoundingMode) -> Tensor:
_check([
"tensor.is_floating_point()",
"steps > 1",
])
max_val = steps - 1
quantized = mode.value(tensor * max_val) / max_val
return quantized
# #
# === RESAMPLING === #
# #
# NEAREST NEIGHBOR
def resample_nearest_neighbor_2d(
tensor: Tensor,
new_lens: tuple[int, int],
dims: tuple[int, int]
) -> Tensor:
_check([
"_valid_dims(tensor, dims)",
"_positive(new_lens)",
])
D_0 = _nearest_neighbor_1d(tensor, new_lens[0], dims[0])
D_1 = _nearest_neighbor_1d(D_0, new_lens[1], dims[1])
return D_1
def _nearest_neighbor_indices(
I_t: Tensor,
l: int,
L: int
) -> Tensor:
i_snn = ((2 * I_t + 1) * l) // (2 * L)
return i_snn
def _nearest_neighbor_1d(
d_s: Tensor,
L: int,
dim: int = 0
) -> Tensor:
L = L
l = int(d_s.shape[dim])
I_t = torch.arange(L)
indices = _nearest_neighbor_indices(I_t, l, L)
resampled = d_s.index_select(dim, indices)
return resampled
# FILTER BASED
def resample_filter_2d_separable(
tensor: Tensor,
new_lens: tuple[int, int],
radius: int,
filters: tuple[Filter, Filter],
dims: tuple[int, int],
) -> Tensor:
_check([
"_valid_dims(tensor, dims)",
"_positive(new_lens)",
"radius > 0",
])
D_0 = _filter_1d(tensor, new_lens[0], radius, filters[0], dims[0])
D_1 = _filter_1d(D_0, new_lens[1], radius, filters[1], dims[1])
resampled = D_1.to(tensor.dtype)
return resampled
def resample_filter_2d(
tensor: Tensor,
new_lens: tuple[int, int],
radius: int,
filter: Filter,
dims: tuple[int, int],
) -> Tensor:
_check([
"_valid_dims(tensor, dims)",
"_positive(new_lens)",
"radius > 0",
])
resampled = _filter_2d(tensor, new_lens, radius, filter, dims).to(tensor.dtype)
return resampled
def _window_lengths(
l: int,
L: int,
radius: int,
) -> int:
if radius == 0:
w, W = 1, 1
elif L <= l:
W = 2 * radius
w = -((-l * W) // L)
else:
w = 2 * radius
W = -((-L * w) // l)
return (w, W)
def _padding_1d(
i_snn: Tensor,
I_t: Tensor,
l: int,
L: int,
W: int,
w: int,
) -> Tensor:
if w == 1 and W == 1:
p = i_snn * 0
elif L <= l:
p = (W * l + L * (2 * i_snn + 1) - l * (2 * I_t + 1)) // (2 * L)
else:
c = l * (2 * I_t + 1) > L * (2 * i_snn + 1)
p = (w // 2) - c.to(torch.int)
return p
def _x_values_1d(
i_snn: Tensor,
i_w: Tensor,
I_t: Tensor,
l: int,
L: int,
p: Tensor
) -> Tensor:
X_w_n = _outer_sum(
(2 * (i_snn - p) + 1) * L - (2 * I_t + 1) * l, (2 * L) * i_w
)
if L <= l:
x_values = X_w_n / (2 * l)
else:
# breaks notation convention
x_values = X_w_n / (2 * L)
return x_values
def _data_values_1d(
d_p: Tensor,
i_snn: Tensor,
i_w: Tensor,
p: Tensor,
p0: int,
L: int,
w: int,
dim: int = 0
) -> Tensor:
o = i_snn - p + p0
i_2d = _outer_sum(o, i_w)
i_1d = i_2d.flatten()
d_1d = d_p.index_select(dim, i_1d)
shape = list(d_p.shape)
shape = shape[:dim] + [L, w] + shape[dim+1:]
d_2d = d_1d.reshape(*shape)
return d_2d
def _filter_1d(
d_s: Tensor,
L: int,
radius: int,
filter: Filter,
dim: int = 0,
) -> Tensor:
l = int(d_s.shape[dim])
(w, W) = _window_lengths(l, L, radius)
I_t = torch.arange(L)
i_snn = _nearest_neighbor_indices(I_t, l, L)
p = _padding_1d(i_snn, I_t, l, L, W, w)
i_w = torch.arange(w)
x_values = _x_values_1d(i_snn, i_w, I_t, l, L, p)
p_0 = int(p[0])
d_p = transform_pad_dim_reflect(d_s, dim, (p_0, p_0 + 1))
d_2d = _data_values_1d(d_p, i_snn, i_w, p, p_0, L, w, dim)
f = filter(x_values)
f_n = util_sum_normalize(f, (1,))
shape = [1] * d_2d.dim()
shape[dim] = L
shape[dim+1] = w
d_f = d_2d * f_n.reshape(*shape)
d_r = d_f.sum(dim+1)
return d_r
def _x_values_2d(
X_w: tuple[Tensor, Tensor],
L: tuple[int, int],
w: tuple[int, int],
) -> Tensor:
X_w_sqr = (X_w[0] * X_w[0], X_w[1] * X_w[1])
X_w2_sqr = X_w_sqr[0].reshape(L[0], 1, w[0], 1) + \
X_w_sqr[1].reshape(1, L[1], 1, w[1])
X_w2 = torch.sqrt(X_w2_sqr)
return X_w2
def _data_values_2d(
d_p: Tensor,
i_snn: tuple[Tensor, Tensor],
p: tuple[Tensor, Tensor],
i_w: tuple[Tensor, Tensor],
p0: tuple[int, int],
w: tuple[int, int],
L: tuple[int, int],
l_p: tuple[int, int],
dim: tuple[int, int],
) -> Tensor:
o = (i_snn[0] - p[0] + p0[0], i_snn[1] - p[1] + p0[1])
i_2d = (_outer_sum(o[0], i_w[0]), _outer_sum(o[1], i_w[1]))
i_4d = (i_2d[0] * l_p[1]).reshape(L[0], 1, w[0], 1) + \
i_2d[1].reshape(1, L[1], 1, w[1])
i_1d = i_4d.flatten()
d_p = d_p.transpose(dim[1], -1)
d_p = d_p.transpose(dim[0], -2)
d_1d = d_p.flatten(start_dim=-2, end_dim=-1).index_select(-1, i_1d)
shape = list(d_1d.shape)[:-1]
d_4d = d_1d.reshape(*shape, L[0], L[1], w[0], w[1])
d_4d = d_4d.transpose(dim[0], -4)
d_4d = d_4d.transpose(dim[1], -3)
return d_4d
def _filter_2d(
d_s: Tensor,
L: tuple[int, int],
radius: int,
filter: Filter,
dim: tuple[int, int],
) -> Tensor:
l = (int(d_s.shape[dim[0]]), int(d_s.shape[dim[1]]))
(w, W) = tuple(zip(
_window_lengths(l[0], L[0], radius),
_window_lengths(l[1], L[1], radius)
))
I_t = (torch.arange(L[0]), torch.arange(L[1]))
i_snn = (
_nearest_neighbor_indices(I_t[0], l[0], L[0]),
_nearest_neighbor_indices(I_t[1], l[1], L[1])
)
p = (
_padding_1d(i_snn[0], I_t[0], l[0], L[0], W[0], w[0]),
_padding_1d(i_snn[1], I_t[1], l[1], L[1], W[1], w[0])
)
i_w = (torch.arange(w[0]), torch.arange(w[1]))
X_w = (
_x_values_1d(i_snn[0], i_w[0], I_t[0], l[0], L[0], p[0]),
_x_values_1d(i_snn[1], i_w[1], I_t[1], l[1], L[1], p[1])
)
X_w2 = _x_values_2d(X_w, L, w)
p0 = (int(p[0][0]), int(p[1][0]))
d_p = transform_pad_dim_reflect(d_s, dim[0], (p0[0], p0[0] + 1))
d_p = transform_pad_dim_reflect(d_p, dim[1], (p0[1], p0[1] + 1))
l_p = (int(d_p.shape[dim[0]]), int(d_p.shape[dim[1]]))
d_4d = _data_values_2d(d_p, i_snn, p, i_w, p0, w, L, l_p, dim)
f = filter(X_w2)
f_n = util_sum_normalize(f, (2, 3))
shape = [1] * d_4d.dim()
shape[dim[0]] = L[0]
shape[dim[1]] = L[1]
shape[-2] = w[0]
shape[-1] = w[1]
d_f = d_4d * f_n.reshape(*shape)
d_r = d_f.sum(-1).sum(-1)
return d_r
# HELPERS
def _outer_sum(lhs: Tensor, rhs: Tensor) -> Tensor:
ret = lhs.unsqueeze(1) + rhs.unsqueeze(0)
return ret
# #
# === SPECIAL FUNCTIONS === #
# #
# FUNCTIONS
def special_jinc(x: Tensor) -> Tensor:
_check([
"x.is_floating_point()",
])
p0: Tensor = (2 / pi) * torch.special.bessel_j1(pi * x) / x
j = p0.where(x != 0, 1)
return j
def special_gaussian(x: Tensor, sigma: float) -> Tensor:
_check([
"x.is_floating_point()",
"sigma > 0",
])
coef = 1 / (sigma * sqrt(2 * pi))
g = coef * torch.exp(x**2 / (-2 * sigma**2))
return g
def special_derivative_of_gaussian(x: Tensor, sigma: float) -> Tensor:
_check([
"x.is_floating_point()",
"sigma > 0",
])
coef = -1 / (sigma**3 * sqrt(2 * pi))
dog = coef * x * torch.exp(x**2 / (-2 * sigma**2))
return dog
# HELPERS
def special_gaussian_area_radius(area: float, sigma: float) -> float:
_check([
"sigma > 0",
"0 < area < 1",
])
radius = sqrt(2) * sigma * erfinv(area)
return radius
def special_derivative_of_gaussian_area_radius(area: float, sigma: float) -> float:
_check([
"sigma > 0",
"0 < area < 1",
])
radius = sqrt(2) * sigma * sqrt(log(1 / (1 - area)))
return radius
# #
# === TONEMAPPING === #
# #
# All based on https://64.github.io/tonemapping/
def tonemap_reinhard(tensor: Tensor) -> Tensor:
_check([
"tensor.is_floating_point()",
])
tonemapped = tensor / (1 + tensor)
return tonemapped
def tonemap_reinhard_luminance(tensor: Tensor, luminance: Tensor) -> Tensor:
_check([
"tensor.is_floating_point()",
"luminance.is_floating_point()",
"_is_broadcastable_to(luminance, tensor)",
])
tonemapped = tensor / (1 + luminance)
return tonemapped
def tonemap_reinhard_extended(tensor: Tensor, whitepoint: Tensor | float) -> Tensor:
_check([
"tensor.is_floating_point()",
"type(whitepoint) == float or whitepoint.is_floating_point()",
"_is_broadcastable_to(whitepoint, tensor)",
])
tonemapped = (tensor * (1 + (tensor / (whitepoint ** 2)))) / (1 + tensor)
return tonemapped
def tonemap_reinhard_extended_luminance(tensor: Tensor, luminance: Tensor, whitepoint: Tensor | float) -> Tensor:
_check([
"tensor.is_floating_point()",
"luminance.is_floating_point()",
"_is_broadcastable_to(luminance, tensor)",
"type(whitepoint) == float or whitepoint.is_floating_point()",
"_is_broadcastable_to(whitepoint, tensor)",
])
tonemapped = (tensor * (1 + (luminance / (whitepoint ** 2)))) / (1 + luminance)
return tonemapped
def tonemap_reinhard_jodie(tensor: Tensor, luminance: Tensor) -> Tensor:
_check([
"tensor.is_floating_point()",
"luminance.is_floating_point()",
"_is_broadcastable_to(luminance, tensor)",
])
t_reinhard = tonemap_reinhard(tensor)
t_reinhard_luminance = tonemap_reinhard_luminance(tensor, luminance)
lerped = torch.lerp(t_reinhard_luminance, t_reinhard, t_reinhard)
return lerped
def tonemap_reinhard_jodie_extended(tensor: Tensor, luminance: Tensor, whitepoint: Tensor | float) -> Tensor:
_check([
"tensor.is_floating_point()",
"luminance.is_floating_point()",
"_is_broadcastable_to(luminance, tensor)",
"type(whitepoint) == float or whitepoint.is_floating_point()",
"_is_broadcastable_to(whitepoint, tensor)",
])
t_reinhard = tonemap_reinhard_extended(tensor, whitepoint)
t_reinhard_luminance = tonemap_reinhard_extended_luminance(tensor, luminance, whitepoint)
lerped = torch.lerp(t_reinhard_luminance, t_reinhard, t_reinhard)
return lerped
def tonemap_uncharted_2(tensor: Tensor) -> Tensor:
_check([
"tensor.is_floating_point()",
])
def tonemap_partial(t: Tensor | float) -> Tensor | float:
A, B, C, D, E, F = 0.15, 0.5, 0.1, 0.2, 0.02, 0.3
partial = ((t*(A*t+C*B)+D*E)/(t*(A*t+B)+D*F))-E/F
return partial
EXPOSURE_BIAS = 2.0
W = 11.2
partial = tonemap_partial(tensor * EXPOSURE_BIAS)
white_scale = 1 / tonemap_partial(W)
tonemapped = partial * white_scale
return tonemapped
def tonemap_aces(tensor: Tensor, channel_dim: int) -> Tensor:
_check([
"tensor.is_floating_point()",
"_valid_dim(tensor, channel_dim)",
"tensor.shape[channel_dim] == 3",
])
INPUT_MAT = torch.tensor((
(0.59719, 0.35458, 0.04823),
(0.07600, 0.90834, 0.01566),
(0.02840, 0.13383, 0.83777),
))
OUTPUT_MAT = torch.tensor((
(+1.60475, -0.53108, -0.07367),
(-0.10208, +1.10813, -0.00605),
(-0.00327, -0.07276, +1.07602),
))
permuted = utils_dims_to_end(tensor, (channel_dim, ))
input = (permuted.unsqueeze(-2) * INPUT_MAT).sum(-1)
a = input * (input + 0.0245786) - 0.000090537
b = input * (0.983729 * input + 0.4329510) + 0.238081
rtt_odt_fit = a / b
tonemapped = (rtt_odt_fit.unsqueeze(-2) * OUTPUT_MAT).sum(-1).clamp(0, 1)
unpermuted = utils_dims_from_end(tonemapped, (channel_dim, ))
return unpermuted
# #
# === TRANSFORMS === #
# #
def transform_crop_dim(tensor: Tensor, dim: int, crop: tuple[int, int]) -> Tensor:
_check([
"_valid_dim(tensor, dim)",
"_nonnegative(crop)",
])
lower = crop[0]
upper = crop[1] if crop[1] != 0 else None
cropped = tensor.clone()[util_slice_dim(dim, lower, upper)]
return cropped
def transform_pad_dim_reflect(tensor: Tensor, dim: int, pad: tuple[int, int]) -> Tensor:
_check([
"_valid_dim(tensor, dim)",
"_nonnegative(pad)",
"tensor.shape[dim] >= max(pad) + 1",
])
length = tensor.shape[dim]
start = tensor[util_slice_dim(dim, 1, pad[0] + 1)].flip(dim)
end = tensor[util_slice_dim(dim, length - (pad[1] + 1), -1)].flip(dim)
padded = torch.cat((start, tensor, end), dim)
return padded
def transform_pad_dim2_reflect(tensor: Tensor, dims: tuple[int, int], pad: tuple[tuple[int, int], tuple[int, int]]) -> Tensor:
_check([
"_valid_dims(tensor, dims)",
"_nonnegative(pad[0]) and _nonnegative(pad[1])",
])
p0 = tensor
p1 = transform_pad_dim_reflect(p0, dims[0], pad[0])
p2 = transform_pad_dim_reflect(p1, dims[1], pad[1])
return p2
def transform_pad_dim_zero(tensor: Tensor, dim: int, pad: tuple[int, int]) -> Tensor:
_check([
"_valid_dim(tensor, dim)",
"_nonnegative(pad)",
])
dims = tensor.dim()
pad_list = [0, 0] * (dims - dim - 1) + list(pad)
padded = F.pad(tensor, pad_list)
return padded
def transform_pad_dim2_zero(tensor: Tensor, dims: tuple[int, int], pad: tuple[tuple[int, int], tuple[int, int]]) -> Tensor:
_check([
"_valid_dims(tensor, dims)",
"_nonnegative(pad[0]) and _nonnegative(pad[1])",
])
p0 = tensor
p1 = transform_pad_dim_zero(p0, dims[0], pad[0])
p2 = transform_pad_dim_zero(p1, dims[1], pad[1])
return p2
def transform_uncrop_from_bbox(tensor: Tensor, bbox: BBox, dim_h: int, dim_w: int) -> Tensor:
_check([
"bbox.valid",
"_valid_dims(tensor, (dim_h, dim_w))",
"tensor.shape[dim_h] == bbox.height",
"tensor.shape[dim_w] == bbox.width",
])
uncropped_h = transform_pad_dim_zero(tensor, dim_h, (bbox.up, bbox.down))
uncropped = transform_pad_dim_zero(uncropped_h, dim_w, (bbox.left, bbox.right))
return uncropped
def transform_crop_to_bbox(tensor: Tensor, bbox: BBox, dim_h: int, dim_w: int) -> Tensor:
_check([
"bbox.valid",
"_valid_dims(tensor, (dim_h, dim_w))",
"tensor.shape[dim_h] == bbox.total_height",
"tensor.shape[dim_w] == bbox.total_width",
])
c0 = tensor
c1 = c0[util_slice_dim(dim_h, bbox.up, bbox.down_offset)]
c2 = c1[util_slice_dim(dim_w, bbox.left, bbox.right_offset)]