-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCoordsEncodingLayer.py
156 lines (134 loc) · 5.1 KB
/
CCoordsEncodingLayer.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
import tensorflow as tf
import math
# Learnable encoding of coordinates
class CCoordsEncodingLayer(tf.keras.layers.Layer):
def __init__(self,
N, raw=True, useShifts=False,
hiddenN=1.0,
scaling='pow', # 'pow' or 'linear'
maxFrequency=1e+4,
useLowBands=True, useHighBands=True,
finalDropout=0.0, bandsDropout=False,
sharedTransformation=False,
**kwargs
):
super().__init__(**kwargs)
self._bottleneck = tf.keras.layers.Dense(N, use_bias=False, activation=None, name=self.name + '/_bottleneck')
N = int(N * hiddenN)
self._N = N
self._raw = raw
self._sharedTransformation = sharedTransformation
if useShifts:
self._shifts = tf.Variable(
initial_value=tf.random_normal_initializer()(shape=(1, 1, 1, N), dtype="float32"),
trainable=True, dtype="float32",
name=self.name + '/CEL_shifts'
)
else:
self._shifts = tf.constant(0.0, dtype=tf.float32)
maxN = 1 + N // 2 if useHighBands and useLowBands else N
freq = self._createBands(scaling, maxN)
bands = []
if useLowBands: bands.append(1.0 / freq[::-1])
if useHighBands: bands.append(freq)
self._baseFreq = tf.concat([(x[1:] + x[:-1])[:maxN] / 2.0 for x in bands], axis=-1)
self._freqRange = tf.concat([(x[1:] - x[:-1])[:maxN] / 2.0 for x in bands], axis=-1)
self._freqDeltas = tf.Variable(
initial_value=tf.random_normal_initializer()(shape=(N,), dtype="float32"),
trainable=True, dtype="float32",
name=self.name + '/CEL_freqDeltas'
)
self._freq = tf.Variable(
initial_value=[maxFrequency],
trainable=True, dtype="float32",
name=self.name + '/CEL_freq'
)
self._dropout = lambda x, **_: x
if 0.0 < finalDropout:
if bandsDropout:
self._dropout = self._bandsDropoutMake(finalDropout)
else:
self._dropout = tf.keras.layers.Dropout(finalDropout)
return
def build(self, input_shape):
M = input_shape[1]
shapePrefix = [1, ] * (len(input_shape) - 2)
P = 1 if self._sharedTransformation else self._N
F = self._transform(tf.zeros([1, M, input_shape[-1]])).shape[-1]
self._fussionW = tf.Variable(
initial_value=tf.random_normal_initializer()(shape=shapePrefix+[P, F], dtype="float32"),
trainable=True, dtype="float32",
name=self.name + '/CEL_fussionW'
)
self._fussionB = tf.Variable(
initial_value=tf.random_normal_initializer()(shape=shapePrefix+[P,], dtype="float32"),
trainable=True, dtype="float32",
name=self.name + '/CEL_fussionB'
)
self._gates = tf.Variable(
initial_value=tf.zeros(shape=(1, 1, self._N), dtype="float32"),
trainable=True, dtype="float32",
name=self.name + '/CEL_gates'
)
return
def _fussion(self, x):
res = tf.reduce_sum(x * self._fussionW, axis=-1) + self._fussionB
return res
def _transform(self, x):
B, M, P, N = tf.shape(x)[0], x.shape[1], x.shape[2], self._N
data = []
tX = (x[..., None] * self.coefs) + self.shifts # (B, M, 2, N)
tX = tX * (2.0 * math.pi)
tX = tf.transpose(tX, (0, 1, 3, 2))[..., None] # (B, M, N, 2, 1)
for F in [tf.sin, tf.cos]:
fX = F(tX)
data.append(fX)
continue
res = tf.concat(data, axis=-1)
tf.assert_equal(tf.shape(res)[:-1], (B, M, N, P))
return tf.reshape(res, (B, M, N, res.shape[-1] * P))
def call(self, x, training=None):
tf.assert_rank(x, 3, "Input must be (B, M, P)")
# x is (B, M, P)
# output is (B, M, N)
B, M, P, N = tf.shape(x)[0], x.shape[1], x.shape[2], self._N
transformed = self._transform(x) # (B, M, N, P * F)
res = self._fussion(transformed) * self.gates # (B, M, N)
tf.assert_equal(tf.shape(res), (B, M, N))
res = self._dropout(res, training=training)
if self._raw:
res = tf.concat([x, res], axis=-1)
return self._bottleneck(res)
@property
def coefs(self):
coefs = self._baseFreq + tf.nn.tanh(self._freqDeltas) * self._freqRange
freq = tf.nn.softplus(self._freq)
return coefs[None, None, None] * freq[None, None, None]
@property
def shifts(self):
return self._shifts
@property
def gates(self):
return tf.nn.tanh(self._gates)
def _bandsDropoutMake(self, maxRate):
def apply(x):
normed = tf.abs(self.gates)
normed = tf.math.divide_no_nan(normed, tf.reduce_max(normed, axis=-1, keepdims=True))
normed = (1.0 - normed) * maxRate
noise = tf.random.uniform(tf.shape(x), minval=0.0, maxval=1.0)
mask = tf.cast(normed < noise, x.dtype) / (1.0 - normed)
return x * tf.stop_gradient(mask)
def F(x, training):
training = tf.keras.backend.learning_phase()
training = tf.cast(training, tf.bool)
return tf.cond(training, lambda: apply(x), lambda: tf.identity(x))
return F
def _createBands(self, scaling, N):
if 'pow' == scaling:
# 1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32, ... 1 / 2^N
maxFrequency = 2.0 ** min((N, 32))
base = math.pow(maxFrequency, 1.0 / float(N))
return tf.pow(base, tf.cast(tf.range(N), tf.float32))
if 'linear' == scaling:
return tf.linspace(1.0, float(N), N)
return