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.

image_resampling.py 5.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 'UpSampling2d',
  8. 'DownSampling2d',
  9. ]
  10. class UpSampling2d(Module):
  11. """The :class:`UpSampling2d` class is a up-sampling 2D layer.
  12. See `tf.image.resize_images <https://www.tensorflow.org/api_docs/python/tf/image/resize_images>`__.
  13. Parameters
  14. ----------
  15. scale : int/float or tuple of int/float
  16. (height, width) scale factor.
  17. method : str
  18. The resize method selected through the given string. Default 'bilinear'.
  19. - 'bilinear', Bilinear interpolation.
  20. - 'nearest', Nearest neighbor interpolation.
  21. - 'bicubic', Bicubic interpolation.
  22. - 'area', Area interpolation.
  23. antialias : boolean
  24. Whether to use an anti-aliasing filter when downsampling an image.
  25. data_format : str
  26. channels_last 'channel_last' (default) or channels_first.
  27. name : None or str
  28. A unique layer name.
  29. Examples
  30. ---------
  31. With TensorLayer
  32. >>> ni = tl.layers.Input([10, 50, 50, 32], name='input')
  33. >>> ni = tl.layers.UpSampling2d(scale=(2, 2))(ni)
  34. >>> output shape : [10, 100, 100, 32]
  35. """
  36. def __init__(self, scale, method='bilinear', antialias=False, data_format='channels_last', name=None, ksize=None):
  37. super(UpSampling2d, self).__init__(name)
  38. self.method = method
  39. self.antialias = antialias
  40. self.data_format = data_format
  41. self.ksize = ksize
  42. logging.info(
  43. "UpSampling2d %s: scale: %s method: %s antialias: %s" % (self.name, scale, self.method, self.antialias)
  44. )
  45. if isinstance(scale, (list, tuple)) and len(scale) != 2:
  46. raise ValueError("scale must be int or tuple/list of length 2")
  47. self.scale = (scale, scale) if isinstance(scale, int) else scale
  48. self.build(None)
  49. self._built = True
  50. def __repr__(self):
  51. s = '{classname}(scale={scale}, method={method}'
  52. if self.name is not None:
  53. s += ', name=\'{name}\''
  54. s += ')'
  55. return s.format(classname=self.__class__.__name__, scale=self.scale, method=self.method, name=self.name)
  56. def build(self, inputs_shape):
  57. self.resize = tl.ops.Resize(
  58. scale=self.scale, method=self.method, antialias=self.antialias, data_format=self.data_format,
  59. ksize=self.ksize
  60. )
  61. def forward(self, inputs):
  62. """
  63. Parameters
  64. ------------
  65. inputs : :class:`Tensor`
  66. Inputs tensors with 4-D Tensor of the shape (batch, height, width, channels)
  67. """
  68. outputs = self.resize(inputs)
  69. return outputs
  70. class DownSampling2d(Module):
  71. """The :class:`DownSampling2d` class is down-sampling 2D layer.
  72. See `tf.image.resize_images <https://www.tensorflow.org/versions/master/api_docs/python/image/resizing#resize_images>`__.
  73. Parameters
  74. ----------
  75. scale : int/float or tuple of int/float
  76. (height, width) scale factor.
  77. method : str
  78. The resize method selected through the given string. Default 'bilinear'.
  79. - 'bilinear', Bilinear interpolation.
  80. - 'nearest', Nearest neighbor interpolation.
  81. - 'bicubic', Bicubic interpolation.
  82. - 'area', Area interpolation.
  83. antialias : boolean
  84. Whether to use an anti-aliasing filter when downsampling an image.
  85. data_format : str
  86. channels_last 'channel_last' (default) or channels_first.
  87. name : None or str
  88. A unique layer name.
  89. Examples
  90. ---------
  91. With TensorLayer
  92. >>> ni = tl.layers.Input([10, 50, 50, 32], name='input')
  93. >>> ni = tl.layers.DownSampling2d(scale=(2, 2))(ni)
  94. >>> output shape : [10, 25, 25, 32]
  95. """
  96. def __init__(self, scale, method='bilinear', antialias=False, data_format='channels_last', name=None, ksize=None):
  97. super(DownSampling2d, self).__init__(name)
  98. self.method = method
  99. self.antialias = antialias
  100. self.data_format = data_format
  101. self.ksize = ksize
  102. logging.info(
  103. "DownSampling2d %s: scale: %s method: %s antialias: %s" % (self.name, scale, self.method, self.antialias)
  104. )
  105. if isinstance(scale, (list, tuple)) and len(scale) != 2:
  106. raise ValueError("scale must be int or tuple/list of length 2")
  107. self.scale = (scale, scale) if isinstance(scale, int) else scale
  108. self.build(None)
  109. self._built = True
  110. def __repr__(self):
  111. s = '{classname}(scale={scale}, method={method}'
  112. if self.name is not None:
  113. s += ', name=\'{name}\''
  114. s += ')'
  115. return s.format(classname=self.__class__.__name__, scale=self.scale, method=self.method, name=self.name)
  116. def build(self, inputs_shape):
  117. scale = [1.0 / self.scale[0], 1.0 / self.scale[1]]
  118. self.resize = tl.ops.Resize(
  119. scale=scale, method=self.method, antialias=self.antialias, data_format=self.data_format, ksize=self.ksize
  120. )
  121. def forward(self, inputs):
  122. """
  123. Parameters
  124. ------------
  125. inputs : :class:`Tensor`
  126. Inputs tensors with 4-D Tensor of the shape (batch, height, width, channels)
  127. """
  128. outputs = self.resize(inputs)
  129. return outputs

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