-
Notifications
You must be signed in to change notification settings - Fork 599
/
normalization.py
381 lines (314 loc) · 12.3 KB
/
normalization.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
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
# Copyright (c) 2020, Apple Inc. All rights reserved.
#
# Use of this source code is governed by a BSD-3-clause license that can be
# found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
import numpy as np
from coremltools.converters.mil.mil import (DefaultInputs, InputSpec,
Operation, TensorInputType,
precondition, types)
from coremltools.converters.mil.mil.operation import VALUE
from coremltools.converters.mil.mil.ops.defs._op_reqs import register_op
from coremltools.converters.mil.mil.types.symbolic import any_symbolic
@register_op
class batch_norm(Operation):
"""
Normalize input tensor ``x`` by ``mean`` and ``variance``, and optionally apply a
scale ``gamma`` and an offset ``beta``:
.. math::
y_i = \\gamma_i \\dfrac{ (x_i - mean_i)}{\\sqrt{variance_i + epsilon}} + beta_i \\;,\\;i=1,....,C
The ``mean``, ``variance``, ``gamma``, and ``beta``
must be 1-D tensors whose lengths are equal to the second axis (the "depth"
or "channel" dimension) of ``x``.
Parameters
----------
x: tensor<[n,C,*D], T> (Required)
* ``3 <= rank <= 5``.
* ``*D`` refers to the spatial dimensions, ``1 <= rank(*D) <= 3``.
* ``n`` is the batch dimension.
mean: const tensor<[C], T> (Required)
variance: const tensor<[C], T> (Required)
gamma: const tensor<[C], T> (Optional)
* Optional scale applied to normalized tensor.
* Default is all ones.
beta: const tensor<[C], T> (Optional)
* Optional offset applied to normalized tensor.
* Default is all zeros.
epsilon: const T (Optional)
* Default is ``1e-5``.
Returns
-------
tensor<[n,C,*D], T>
* Output tensor has the same shape and type as the input ``x``.
Attributes
----------
T: fp16, fp32
"""
input_spec = InputSpec(
x=TensorInputType(type_domain="T"),
mean=TensorInputType(const=True, type_domain="T"),
variance=TensorInputType(const=True, type_domain="T"),
gamma=TensorInputType(const=True, optional=True, type_domain="T"),
beta=TensorInputType(const=True, optional=True, type_domain="T"),
epsilon=TensorInputType(const=True, optional=True, type_domain="T"),
)
type_domains = {
"T": (types.fp16, types.fp32),
}
def default_inputs(self):
return DefaultInputs(
gamma=None,
beta=None,
epsilon=1e-5,
)
def type_inference(self):
x_shape = self.x.shape
return types.tensor(self.x.dtype, tuple(x_shape))
@register_op
class instance_norm(Operation):
"""
Apply instance normalization to the n-dimensional input tensor.
Parameters
----------
x: tensor<[n,C,*D], T> (Required)
* ``3 <= rank(x) <= 4``.
* ``*D`` refers to the spatial dimensions, ``1 <= rank(*D) <= 2``.
* ``n`` is the batch dimension.
gamma: const tensor<[C], T> (Optional)
* Optional scale applied to normalized tensor.
* Default to all ones.
beta: const tensor<[C], T> (Optional)
* Optional offset applied to normalized tensor.
* Default to all zeros.
epsilon: const f32 (Optional)
* Default to ``1e-5``.
Returns
-------
tensor<[n,C,*D], T>
* Output tensor has the same shape and type as the input ``x``.
Attributes
----------
T: fp16, fp32
"""
input_spec = InputSpec(
x=TensorInputType(type_domain="T"),
gamma=TensorInputType(const=True, optional=True, type_domain="T"),
beta=TensorInputType(const=True, optional=True, type_domain="T"),
epsilon=TensorInputType(const=True, optional=True, type_domain="T"),
)
type_domains = {
"T": (types.fp16, types.fp32),
}
def default_inputs(self):
return DefaultInputs(
gamma=None,
beta=None,
epsilon=1e-5,
)
def type_inference(self):
x_shape = self.x.shape
return types.tensor(self.x.dtype, tuple(x_shape))
@register_op
class l2_norm(Operation):
"""
Apply L2 normalization to the n-dimensional input tensor. That is, divide the input
tensor by the square root of the sum of squares of all elements of the input.
.. math::
x_i \\leftarrow \\dfrac{x_i}{\\sqrt{\\sum{x_i^2} + \\epsilon}}
Parameters
----------
x: tensor<[\*B, \*D], T> (Required)
* Input tensor, ``rank(x) >= 3``.
* ``*B`` refers to the leading dimensions.
* ``*D`` refers to the spatial dimensions to be normalized. Must be rank 3: ``rank(*D) == 3``.
* When ``rank(x) == 3``, in which ``rank(*B) == 0 and rank(*D) == 3``, the input is divided by
the square root of the sum of squares of all elements.
* For ranks greater than 3, in which ``rank(*B) >= 1 and rank(*D) == 3``,
the leading dimensions \*B, starting from ``0`` to ``-4`` (inclusive),
are all treated as batch. The L2 normalization are done batch-wise.
epsilon: const T (Optional)
* Small constant to avoid division by ``0``.
* Optional, defaults to ``1e-6``.
Returns
-------
tensor<[\*B, \*D], T>
* Same type and shape as the input tensor ``x``.
Attributes
----------
T: fp16, fp32
"""
input_spec = InputSpec(
x=TensorInputType(type_domain="T"),
epsilon=TensorInputType(const=True, optional=True, type_domain="T"),
)
type_domains = {
"T": (types.fp16, types.fp32),
}
def default_inputs(self):
return DefaultInputs(
epsilon=1e-6,
)
def type_inference(self):
if self.x.rank < 3:
msg = "Input rank of l2_norm must be at least 3. Got {}".format(self.x.rank)
raise ValueError(msg)
x_shape = self.x.shape
return types.tensor(self.x.dtype, tuple(x_shape))
@precondition(allow=VALUE)
def value_inference(self):
val = self.x.val
eps = self.epsilon.val
shape = self.x.shape
rank = self.x.rank
batch_dims = rank - 3
if batch_dims == 0:
square_sum = np.sum(val**2)
output = val/np.power(square_sum + eps, 0.5)
else:
batch_dim_prod = np.prod(shape[:batch_dims])
reshape_val = np.reshape(val, (batch_dim_prod, -1))
square_sum = np.sum(reshape_val * reshape_val, axis=1, keepdims=True) + eps
output = reshape_val/np.power(square_sum, 0.5)
output = np.reshape(output, shape)
return output
@register_op
class layer_norm(Operation):
"""
Apply layer normalization to the n-dimensional input tensor:
.. math::
out = gamma * (input - E[x]) / sqrt(Var[x] + epsilon) + beta
Parameters
----------
x: tensor<\*?, T> (Required)
* Input tensor.
axes: const<[K], i32> (Optional)
* Dimensions to perform layer normalization.
* Default is ``None`` (all dimensions).
gamma: const tensor<\*?, T>, T> (Optional)
* if provided, the shape must be be ``x.shape[axes]``. For instance, if
input ``x`` with shape ``(3,4,5,6)`` and ``axes = [2,3]``, gamma must have
shape ``(5,6)``.
* Default is all ones.
beta: const tensor<\*?, T>, T> (Optional)
* Same shape as gamma.
* Default is all zeros.
epsilon: const T (Optional)
* Small constant to avoid division by ``0``.
* Default is ``1e-5``.
Returns
-------
tensor<\*?, T>:
* Tensor with same shape and type as the input tensor ``x``.
Attributes
----------
T: fp16, fp32
"""
input_spec = InputSpec(
x=TensorInputType(type_domain="T"),
axes=TensorInputType(const=True, optional=True, type_domain=types.int32),
gamma=TensorInputType(const=True, optional=True, type_domain="T"),
beta=TensorInputType(const=True, optional=True, type_domain="T"),
epsilon=TensorInputType(const=True, optional=True, type_domain="T"),
)
type_domains = {
"T": (types.fp16, types.fp32),
}
def default_inputs(self):
return DefaultInputs(
axes=range(self.x.rank),
gamma=None,
beta=None,
epsilon=1e-5,
)
@staticmethod
def _is_compatible_shape(shapea, shapeb):
if not len(shapea) == len(shapeb):
return False
for a, b in zip(shapea, shapeb):
if any_symbolic([a, b]):
continue
if a != b:
return False
return True
def type_inference(self):
rank = self.x.rank
# check valid axes
positive_axes = [axis + rank if axis < 0 else axis for axis in self.axes.val]
if not all([axis >= 0 and axis < rank for axis in positive_axes]):
raise ValueError("axes must in the range of [-x.rank, x.rank-1].")
# check shape of gamma and beta
normalized_shape = [self.x.shape[i] for i in range(rank) if i in positive_axes]
if self.gamma is not None and not layer_norm._is_compatible_shape(list(self.gamma.shape), normalized_shape):
raise ValueError("Expect shape {} for gamma, but get shape {} instead".format(normalized_shape, self.gamma.shape))
if self.beta is not None and not layer_norm._is_compatible_shape(list(self.gamma.shape), normalized_shape):
raise ValueError("Expect shape {} for beta, but get shape {} instead".format(normalized_shape, self.beta.shape))
x_shape = self.x.shape
return types.tensor(self.x.dtype, tuple(x_shape))
@precondition(allow=VALUE)
def value_inference(self):
def np_layer_norm(x, axes, gamma, beta, epsilon=1e-5):
rank = len(x.shape)
axes = [axis + rank if axis < 0 else axis for axis in axes]
normalized_shape = [x.shape[i] if i in axes else 1 for i in range(rank)]
gamma = np.ones(shape=normalized_shape) if gamma is None else np.reshape(gamma, normalized_shape)
beta = np.zeros(shape=normalized_shape) if beta is None else np.reshape(beta, normalized_shape)
num = x - np.mean(x, axis=tuple(axes), keepdims=True)
dem = np.sqrt(
np.sum(np.square(num), axis=tuple(axes), keepdims=True)
/ np.prod(normalized_shape)
+ epsilon
)
return num / dem * gamma + beta
_axes = self.x.shape if self.axes is None else self.axes.val
_gamma = None if self.gamma is None else self.gamma.val
_beta = None if self.beta is None else self.beta.val
return np_layer_norm(self.x.val, _axes, _gamma, _beta, self.epsilon.val)
@register_op
class local_response_norm(Operation):
"""
Apply local response normalization to the n-dimensional input tensor:
.. math::
x_i \\leftarrow \\dfrac{x_i}{\\left ( k + \\dfrac{\\alpha}{\\text{size}} \\sum_j x_j^2 \\right )^\\beta}
Parameters
----------
x: tensor<[n,C,*D], T> (Required)
* Input tensor, ``3 <= rank(x) <= 4``.
* ``*D`` refers to the spatial dimensions, ``1 <= rank(*D) <= 2``.
* ``n`` is the batch dimension.
size: const i32 (Required)
* Amount of neighboring channels to normalize.
alpha: const T (Optional)
* Scale factor.
* Default is ``1e-4``.
beta: const T (Optional)
* An exponent.
* Default is ``0.75``.
k: const T (Optional)
* Additive factor.
* Default is ``1.0``.
Returns
-------
tensor<[n,C,*D], T>
* Same type and shape as the input tensor ``x``.
Attributes
----------
T: fp16, fp32
"""
input_spec = InputSpec(
x=TensorInputType(type_domain="T"),
size=TensorInputType(const=True, type_domain=types.int32),
alpha=TensorInputType(const=True, optional=True, type_domain="T"),
beta=TensorInputType(const=True, optional=True, type_domain="T"),
k=TensorInputType(const=True, optional=True, type_domain="T"),
)
type_domains = {
"T": (types.fp16, types.fp32),
}
def default_inputs(self):
return DefaultInputs(
alpha=1e-4,
beta=0.75,
k=1.,
)
def type_inference(self):
x_shape = self.x.shape
return types.tensor(self.x.dtype, tuple(x_shape))