-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_pool.py
82 lines (63 loc) · 2.16 KB
/
global_pool.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
from tensorflow.keras import backend as KerasBackend # noqa
from tensorflow.keras.layers import Dense, Layer # noqa
from gns.config.settings import settings_fabric
settings = settings_fabric()
class GlobalPoolLayer(Layer):
"""
The base class of the layer for GlobalPool.
Notes:
Parameters of type pooling_op, batch_pooling_op must be redefined in
specific implementations of the class before calling the `call()` method.
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.supports_masking = True
self.pooling_op = None
self.batch_pooling_op = None
self.data_mode = settings.models.disjoint
def build(self, input_shape):
"""
Build layer
Args:
input_shape: input shape
Returns:
"""
if isinstance(input_shape, list) and len(input_shape) == 2:
self.data_mode = settings.models.disjoint
else:
if len(input_shape) == 2:
self.data_mode = settings.models.single
else:
self.data_mode = settings.models.batch
super().build(input_shape)
def call(self, inputs):
if self.data_mode == settings.models.disjoint:
X = inputs[0]
I = inputs[1]
if KerasBackend.ndim(I) == 2:
I = I[:, 0]
else:
X = inputs
if self.data_mode == settings.models.disjoint:
return self.pooling_op(X, I)
else:
return self.batch_pooling_op(
X,
axis=-2,
keepdims=(self.data_mode == settings.models.single)
)
def compute_output_shape(self, input_shape):
"""
Calculate the output form.
Args:
input_shape: input form
Returns:
"""
if self.data_mode == settings.models.single:
return (1,) + input_shape[-1:]
elif self.data_mode == settings.models.batch:
return input_shape[:-2] + input_shape[-1:]
else:
return input_shape[0]
def global_pool_layer_fabric(**kwargs):
return GlobalPoolLayer(**kwargs)