1
|
|
|
import math |
2
|
|
|
|
3
|
|
|
import tensorflow as tf |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def rectangular_kernel1d(kernel_size: int) -> tf.Tensor: |
7
|
|
|
""" |
8
|
|
|
Return a the 1D rectangular kernel for LocalNormalizedCrossCorrelation. |
9
|
|
|
|
10
|
|
|
:param kernel_size: scalar, size of the 1-D kernel |
11
|
|
|
:return: kernel_weights, of shape (kernel_size, ) |
12
|
|
|
""" |
13
|
|
|
|
14
|
|
|
kernel = tf.ones(shape=(kernel_size,), dtype=tf.float32) |
15
|
|
|
return kernel |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
def triangular_kernel1d(kernel_size: int) -> tf.Tensor: |
19
|
|
|
""" |
20
|
|
|
Return a the 1D triangular kernel for LocalNormalizedCrossCorrelation. |
21
|
|
|
|
22
|
|
|
Assume kernel_size is odd, it will be a smoothed from |
23
|
|
|
a kernel which center part is zero. |
24
|
|
|
Then length of the ones will be around half kernel_size. |
25
|
|
|
The weight scale of the kernel does not matter as LNCC will normalize it. |
26
|
|
|
|
27
|
|
|
:param kernel_size: scalar, size of the 1-D kernel |
28
|
|
|
:return: kernel_weights, of shape (kernel_size, ) |
29
|
|
|
""" |
30
|
|
|
assert kernel_size >= 3 |
31
|
|
|
assert kernel_size % 2 != 0 |
32
|
|
|
|
33
|
|
|
padding = kernel_size // 2 |
34
|
|
|
kernel = tf.constant( |
35
|
|
|
[0] * math.ceil(padding / 2) |
36
|
|
|
+ [1] * (kernel_size - padding) |
37
|
|
|
+ [0] * math.floor(padding / 2), |
38
|
|
|
dtype=tf.float32, |
39
|
|
|
) |
40
|
|
|
|
41
|
|
|
# (padding*2, ) |
42
|
|
|
filters = tf.ones(shape=(kernel_size - padding, 1, 1), dtype=tf.float32) |
43
|
|
|
|
44
|
|
|
# (kernel_size, 1, 1) |
45
|
|
|
kernel = tf.nn.conv1d( |
46
|
|
|
kernel[None, :, None], filters=filters, stride=[1, 1, 1], padding="SAME" |
47
|
|
|
) |
48
|
|
|
|
49
|
|
|
return kernel[0, :, 0] |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def gaussian_kernel1d_size(kernel_size: int) -> tf.Tensor: |
53
|
|
|
""" |
54
|
|
|
Return a the 1D Gaussian kernel for LocalNormalizedCrossCorrelation. |
55
|
|
|
|
56
|
|
|
:param kernel_size: scalar, size of the 1-D kernel |
57
|
|
|
:return: filters, of shape (kernel_size, ) |
58
|
|
|
""" |
59
|
|
|
mean = (kernel_size - 1) / 2.0 |
60
|
|
|
sigma = kernel_size / 3 |
61
|
|
|
|
62
|
|
|
grid = tf.range(0, kernel_size, dtype=tf.float32) |
63
|
|
|
filters = tf.exp(-tf.square(grid - mean) / (2 * sigma ** 2)) |
64
|
|
|
|
65
|
|
|
return filters |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def gaussian_kernel1d_sigma(sigma: int) -> tf.Tensor: |
69
|
|
|
""" |
70
|
|
|
Calculate a gaussian kernel. |
71
|
|
|
|
72
|
|
|
:param sigma: number defining standard deviation for |
73
|
|
|
gaussian kernel. |
74
|
|
|
:return: shape = (dim, ) |
75
|
|
|
""" |
76
|
|
|
assert sigma > 0 |
77
|
|
|
tail = int(sigma * 3) |
78
|
|
|
kernel = tf.exp([-0.5 * x ** 2 / sigma ** 2 for x in range(-tail, tail + 1)]) |
79
|
|
|
kernel = kernel / tf.reduce_sum(kernel) |
80
|
|
|
return kernel |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
def cauchy_kernel1d(sigma: int) -> tf.Tensor: |
84
|
|
|
""" |
85
|
|
|
Approximating cauchy kernel in 1d. |
86
|
|
|
|
87
|
|
|
:param sigma: int, defining standard deviation of kernel. |
88
|
|
|
:return: shape = (dim, ) |
89
|
|
|
""" |
90
|
|
|
assert sigma > 0 |
91
|
|
|
tail = int(sigma * 5) |
92
|
|
|
k = tf.math.reciprocal([((x / sigma) ** 2 + 1) for x in range(-tail, tail + 1)]) |
93
|
|
|
k = k / tf.reduce_sum(k) |
94
|
|
|
return k |
95
|
|
|
|