You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

padding.py 7.2 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import tensorlayer as tl
  4. from tensorlayer import logging
  5. from tensorlayer.layers.core import Module
  6. __all__ = [
  7. 'PadLayer',
  8. 'ZeroPad1d',
  9. 'ZeroPad2d',
  10. 'ZeroPad3d',
  11. ]
  12. class PadLayer(Module):
  13. """The :class:`PadLayer` class is a padding layer for any mode and dimension.
  14. Please see `tf.pad <https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/pad>`__ for usage.
  15. Parameters
  16. ----------
  17. padding : list of lists of 2 ints, or a Tensor of type int32.
  18. The int32 values to pad.
  19. mode : str
  20. "CONSTANT", "REFLECT", or "SYMMETRIC" (case-insensitive).
  21. name : None or str
  22. A unique layer name.
  23. Examples
  24. --------
  25. With TensorLayer
  26. >>> net = tl.layers.Input([10, 224, 224, 3], name='input')
  27. >>> padlayer = tl.layers.PadLayer([[0, 0], [3, 3], [3, 3], [0, 0]], "REFLECT", name='inpad')(net)
  28. >>> print(padlayer)
  29. >>> output shape : (10, 230, 230, 3)
  30. """
  31. def __init__(
  32. self,
  33. padding=None,
  34. mode='CONSTANT',
  35. constant_values=0,
  36. name=None, # 'pad_layer',
  37. ):
  38. super().__init__(name)
  39. self.padding = padding
  40. self.mode = mode
  41. self.constant_values = constant_values
  42. logging.info("PadLayer %s: padding: %s mode: %s" % (self.name, self.padding, self.mode))
  43. if self.padding is None:
  44. raise Exception(
  45. "padding should be a Tensor of type int32. see https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/pad"
  46. )
  47. self.build()
  48. self._built = True
  49. def __repr__(self):
  50. s = '{classname}(padding={padding}, mode={mode}'
  51. if self.name is not None:
  52. s += ', name=\'{name}\''
  53. s += ')'
  54. return s.format(classname=self.__class__.__name__, **self.__dict__)
  55. def build(self, inputs_shape=None):
  56. self.pad = tl.ops.Pad(paddings=self.padding, mode=self.mode, constant_values=self.constant_values)
  57. def forward(self, inputs):
  58. outputs = self.pad(inputs)
  59. return outputs
  60. class ZeroPad1d(Module):
  61. """
  62. The :class:`ZeroPad1d` class is a 1D padding layer for signal [batch, length, channel].
  63. Parameters
  64. ----------
  65. padding : int, or tuple of 2 ints
  66. - If int, zeros to add at the beginning and end of the padding dimension (axis 1).
  67. - If tuple of 2 ints, zeros to add at the beginning and at the end of the padding dimension.
  68. name : None or str
  69. A unique layer name.
  70. Examples
  71. --------
  72. With TensorLayer
  73. >>> net = tl.layers.Input([10, 100, 1], name='input')
  74. >>> pad1d = tl.layers.ZeroPad1d(padding=(3, 3))(net)
  75. >>> print(pad1d)
  76. >>> output shape : (10, 106, 1)
  77. """
  78. def __init__(
  79. self,
  80. padding,
  81. name=None, # 'zeropad1d',
  82. ):
  83. super().__init__(name)
  84. self.padding = padding
  85. logging.info("ZeroPad1d %s: padding: %s" % (self.name, str(padding)))
  86. if not isinstance(self.padding, (int, tuple, dict)):
  87. raise AssertionError()
  88. self.build()
  89. self._built = True
  90. def __repr__(self):
  91. s = '{classname}(padding={padding}'
  92. if self.name is not None:
  93. s += ', name=\'{name}\''
  94. s += ')'
  95. return s.format(classname=self.__class__.__name__, **self.__dict__)
  96. def build(self, inputs_shape=None):
  97. self.layer = tl.ops.ZeroPadding1D(padding=self.padding)
  98. def forward(self, inputs):
  99. outputs = self.layer(inputs)
  100. return outputs
  101. class ZeroPad2d(Module):
  102. """
  103. The :class:`ZeroPad2d` class is a 2D padding layer for image [batch, height, width, channel].
  104. Parameters
  105. ----------
  106. padding : tuple of 2 ints or int, or tuple of 2 tuples of 2 ints.
  107. - If int, the same symmetric padding is applied to width and height.
  108. - If tuple of 2 ints, interpreted as two different symmetric padding values for height and width as ``(symmetric_height_pad, symmetric_width_pad)``.
  109. - If tuple of 2 tuples of 2 ints, interpreted as ``((top_pad, bottom_pad), (left_pad, right_pad))``.
  110. name : None or str
  111. A unique layer name.
  112. Examples
  113. --------
  114. With TensorLayer
  115. >>> net = tl.layers.Input([10, 100, 100, 3], name='input')
  116. >>> pad2d = tl.layers.ZeroPad2d(padding=((3, 3), (4, 4)))(net)
  117. >>> print(pad2d)
  118. >>> output shape : (10, 106, 108, 3)
  119. """
  120. def __init__(
  121. self,
  122. padding,
  123. name=None, # 'zeropad2d',
  124. ):
  125. super().__init__(name)
  126. self.padding = padding
  127. logging.info("ZeroPad2d %s: padding: %s" % (self.name, str(self.padding)))
  128. if not isinstance(self.padding, (int, tuple)):
  129. raise AssertionError("Padding should be of type `int` or `tuple`")
  130. self.build()
  131. self._built = True
  132. def __repr__(self):
  133. s = '{classname}(padding={padding}'
  134. if self.name is not None:
  135. s += ', name=\'{name}\''
  136. s += ')'
  137. return s.format(classname=self.__class__.__name__, **self.__dict__)
  138. def build(self, inputs_shape=None):
  139. self.layer = tl.ops.ZeroPadding2D(padding=self.padding)
  140. def forward(self, inputs):
  141. outputs = self.layer(inputs)
  142. return outputs
  143. class ZeroPad3d(Module):
  144. """
  145. The :class:`ZeroPad3d` class is a 3D padding layer for volume [batch, depth, height, width, channel].
  146. Parameters
  147. ----------
  148. padding : int, or tuple of 2 ints, or tuple of 2 tuples of 2 ints.
  149. - If int, the same symmetric padding is applied to width and height.
  150. - 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)``.
  151. - If tuple of 2 tuples of 2 ints, interpreted as
  152. ``((left_dim1_pad, right_dim1_pad), (left_dim2_pad, right_dim2_pad), (left_dim3_pad, right_dim3_pad))``.
  153. name : None or str
  154. A unique layer name.
  155. Examples
  156. --------
  157. With TensorLayer
  158. >>> net = tl.layers.Input([10, 100, 100, 100, 3], name='input')
  159. >>> pad3d = tl.layers.ZeroPad3d(padding=((3, 3), (4, 4), (5, 5)))(net)
  160. >>> print(pad3d)
  161. >>> output shape : (10, 106, 108, 110, 3)
  162. """
  163. def __init__(
  164. self,
  165. padding,
  166. name=None, # 'zeropad3d',
  167. ):
  168. super().__init__(name)
  169. self.padding = padding
  170. logging.info("ZeroPad3d %s: padding: %s" % (self.name, str(self.padding)))
  171. if not isinstance(self.padding, (int, tuple)):
  172. raise AssertionError()
  173. self.build()
  174. self._built = True
  175. def __repr__(self):
  176. s = '{classname}(padding={padding}'
  177. if self.name is not None:
  178. s += ', name=\'{name}\''
  179. s += ')'
  180. return s.format(classname=self.__class__.__name__, **self.__dict__)
  181. def build(self, inputs_shape=None):
  182. self.layer = tl.ops.ZeroPadding3D(padding=self.padding)
  183. def forward(self, inputs):
  184. outputs = self.layer(inputs)
  185. return outputs

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.