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
238 lines (184 loc) · 7.08 KB
/
Copy pathpadding.py
File metadata and controls
238 lines (184 loc) · 7.08 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
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
#! /usr/bin/python
# -*- coding: utf-8 -*-
import tensorflow as tf
import tensorlayer as tl
from tensorlayer import logging
from tensorlayer.decorators import deprecated_alias
from tensorlayer.layers.core import Layer
__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/versions/r2.0/api_docs/python/tf/pad>`__ for usage.
Parameters
----------
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 : None or str
A unique layer name.
Examples
--------
With TensorLayer
>>> net = tl.layers.Input([None, 224, 224, 3], name='input')
>>> padlayer = tl.layers.PadLayer([[0, 0], [3, 3], [3, 3], [0, 0]], "REFLECT", name='inpad')(net)
>>> print(padlayer)
>>> output shape : (None, 106, 106, 3)
"""
def __init__(
self,
padding=None,
mode='CONSTANT',
name=None, # 'pad_layer',
):
super().__init__(name)
self.padding = padding
self.mode = mode
logging.info("PadLayer %s: padding: %s mode: %s" % (self.name, list(self.padding), self.mode))
if self.padding is None:
raise Exception(
"padding should be a Tensor of type int32. see https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/pad"
)
self.build()
self._built = True
def __repr__(self):
s = '{classname}(padding={padding}, mode={mode}'
if self.name is not None:
s += ', name=\'{name}\''
s += ')'
return s.format(classname=self.__class__.__name__, **self.__dict__)
def build(self, inputs_shape=None):
pass
def forward(self, inputs):
outputs = tf.pad(tensor=inputs, paddings=self.padding, mode=self.mode, name=self.name)
return outputs
class ZeroPad1d(Layer):
"""
The :class:`ZeroPad1d` class is a 1D padding layer for signal [batch, length, channel].
Parameters
----------
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 : None or str
A unique layer name.
Examples
--------
With TensorLayer
>>> net = tl.layers.Input([None, 100, 1], name='input')
>>> pad1d = tl.layers.ZeroPad1d(padding=(2, 3))(net)
>>> print(pad1d)
>>> output shape : (None, 106, 1)
"""
def __init__(
self,
padding,
name=None, # 'zeropad1d',
):
super().__init__(name)
self.padding = padding
logging.info("ZeroPad1d %s: padding: %s" % (self.name, str(padding)))
if not isinstance(self.padding, (int, tuple, dict)):
raise AssertionError()
self.build()
self._built = True
def __repr__(self):
s = '{classname}(padding={padding}'
if self.name is not None:
s += ', name=\'{name}\''
s += ')'
return s.format(classname=self.__class__.__name__, **self.__dict__)
def build(self, inputs_shape=None):
self.layer = tf.keras.layers.ZeroPadding1D(padding=self.padding, name=self.name)
def forward(self, inputs):
outputs = self.layer(inputs)
return outputs
class ZeroPad2d(Layer):
"""
The :class:`ZeroPad2d` class is a 2D padding layer for image [batch, height, width, channel].
Parameters
----------
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 : None or str
A unique layer name.
Examples
--------
With TensorLayer
>>> net = tl.layers.Input([None, 100, 100, 3], name='input')
>>> pad2d = tl.layers.ZeroPad2d(padding=((3, 3), (4, 4)))(net)
>>> print(pad2d)
>>> output shape : (None, 106, 108, 3)
"""
def __init__(
self,
padding,
name=None, # 'zeropad2d',
):
super().__init__(name)
self.padding = padding
logging.info("ZeroPad2d %s: padding: %s" % (self.name, str(self.padding)))
if not isinstance(self.padding, (int, tuple)):
raise AssertionError("Padding should be of type `int` or `tuple`")
self.build()
self._built = True
def __repr__(self):
s = '{classname}(padding={padding}'
if self.name is not None:
s += ', name=\'{name}\''
s += ')'
return s.format(classname=self.__class__.__name__, **self.__dict__)
def build(self, inputs_shape=None):
self.layer = tf.keras.layers.ZeroPadding2D(padding=self.padding, name=self.name)
def forward(self, inputs):
outputs = self.layer(inputs)
return outputs
class ZeroPad3d(Layer):
"""
The :class:`ZeroPad3d` class is a 3D padding layer for volume [batch, depth, height, width, channel].
Parameters
----------
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 : None or str
A unique layer name.
Examples
--------
With TensorLayer
>>> net = tl.layers.Input([None, 100, 100, 100, 3], name='input')
>>> pad3d = tl.layers.ZeroPad3d(padding=((3, 3), (4, 4), (5, 5)))(net)
>>> print(pad3d)
>>> output shape : (None, 106, 108, 110, 3)
"""
def __init__(
self,
padding,
name=None, # 'zeropad3d',
):
super().__init__(name)
self.padding = padding
logging.info("ZeroPad3d %s: padding: %s" % (self.name, str(self.padding)))
if not isinstance(self.padding, (int, tuple)):
raise AssertionError()
self.build()
self._built = True
def __repr__(self):
s = '{classname}(padding={padding}'
if self.name is not None:
s += ', name=\'{name}\''
s += ')'
return s.format(classname=self.__class__.__name__, **self.__dict__)
def build(self, inputs_shape=None):
self.layer = tf.keras.layers.ZeroPadding3D(padding=self.padding, name=self.name)
def forward(self, inputs):
outputs = self.layer(inputs)
return outputs