forked from tensorlayer/TensorLayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpadding.py
More file actions
165 lines (128 loc) · 5.39 KB
/
Copy pathpadding.py
File metadata and controls
165 lines (128 loc) · 5.39 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
# -*- coding: utf-8 -*-
import tensorflow as tf
from .. import _logging as logging
from .core import *
from ..deprecation import deprecated_alias
__all__ = [
'PadLayer',
'ZeroPad1d',
'ZeroPad2d',
'ZeroPad3d',
]
class PadLayer(Layer):
"""The :class:`PadLayer` class is a padding layer for any mode and dimension.
Please see `tf.pad <https://www.tensorflow.org/api_docs/python/tf/pad>`__ for usage.
Parameters
----------
prev_layer : :class:`Layer`
The previous layer.
padding : list of lists of 2 ints, or a Tensor of type int32.
The int32 values to pad.
mode : str
"CONSTANT", "REFLECT", or "SYMMETRIC" (case-insensitive).
name : str
A unique layer name.
Examples
--------
>>> net = InputLayer(image, name='in')
>>> net = PadLayer(net, [[0, 0], [3, 3], [3, 3], [0, 0]], "REFLECT", name='inpad')
"""
@deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release
def __init__(
self,
prev_layer,
padding=None,
mode='CONSTANT',
name='pad_layer',
):
super(PadLayer, self).__init__(prev_layer=prev_layer, name=name)
logging.info("PadLayer %s: padding:%s mode:%s" % (name, list(padding), mode))
self.inputs = prev_layer.outputs
if padding is None:
raise Exception(
"padding should be a Tensor of type int32. see https://www.tensorflow.org/api_docs/python/tf/pad"
)
self.outputs = tf.pad(self.inputs, paddings=padding, mode=mode, name=name)
self.all_layers.append(self.outputs)
class ZeroPad1d(Layer):
"""
The :class:`ZeroPad1d` class is a 1D padding layer for signal [batch, length, channel].
Parameters
----------
prev_layer : :class:`Layer`
The previous layer.
padding : int, or tuple of 2 ints
- If int, zeros to add at the beginning and end of the padding dimension (axis 1).
- If tuple of 2 ints, zeros to add at the beginning and at the end of the padding dimension.
name : str
A unique layer name.
"""
@deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release
def __init__(
self,
prev_layer,
padding,
name='zeropad1d',
):
super(ZeroPad1d, self).__init__(prev_layer=prev_layer, name=name)
logging.info("ZeroPad1d %s: padding:%s" % (name, str(padding)))
self.inputs = prev_layer.outputs
if not isinstance(padding, (int, tuple, dict)):
raise AssertionError()
self.outputs = tf.keras.layers.ZeroPadding1D(padding=padding, name=name)(self.inputs)
self.all_layers.append(self.outputs)
class ZeroPad2d(Layer):
"""
The :class:`ZeroPad2d` class is a 2D padding layer for image [batch, height, width, channel].
Parameters
----------
prev_layer : :class:`Layer`
The previous layer.
padding : int, or tuple of 2 ints, or tuple of 2 tuples of 2 ints.
- If int, the same symmetric padding is applied to width and height.
- If tuple of 2 ints, interpreted as two different symmetric padding values for height and width as ``(symmetric_height_pad, symmetric_width_pad)``.
- If tuple of 2 tuples of 2 ints, interpreted as ``((top_pad, bottom_pad), (left_pad, right_pad))``.
name : str
A unique layer name.
"""
@deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release
def __init__(
self,
prev_layer,
padding,
name='zeropad2d',
):
super(ZeroPad2d, self).__init__(prev_layer=prev_layer, name=name)
logging.info("ZeroPad2d %s: padding:%s" % (name, str(padding)))
self.inputs = prev_layer.outputs
if not isinstance(padding, (int, tuple)):
raise AssertionError()
self.outputs = tf.keras.layers.ZeroPadding2D(padding=padding, name=name)(self.inputs)
self.all_layers.append(self.outputs)
class ZeroPad3d(Layer):
"""
The :class:`ZeroPad3d` class is a 3D padding layer for volume [batch, depth, height, width, channel].
Parameters
----------
prev_layer : :class:`Layer`
The previous layer.
padding : int, or tuple of 2 ints, or tuple of 2 tuples of 2 ints.
- If int, the same symmetric padding is applied to width and height.
- If tuple of 2 ints, interpreted as two different symmetric padding values for height and width as ``(symmetric_dim1_pad, symmetric_dim2_pad, symmetric_dim3_pad)``.
- If tuple of 2 tuples of 2 ints, interpreted as ``((left_dim1_pad, right_dim1_pad), (left_dim2_pad, right_dim2_pad), (left_dim3_pad, right_dim3_pad))``.
name : str
A unique layer name.
"""
def __init__(
self,
prev_layer,
padding,
name='zeropad3d',
):
super(ZeroPad3d, self).__init__(prev_layer=prev_layer, name=name)
logging.info("ZeroPad3d %s: padding:%s" % (name, str(padding)))
self.inputs = prev_layer.outputs
if not isinstance(padding, (int, tuple)):
raise AssertionError()
self.outputs = tf.keras.layers.ZeroPadding3D(padding=padding, name=name)(self.inputs)
self.all_layers.append(self.outputs)