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.

tensorflow_image.py 6.2 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import tensorflow as tf
  2. import numpy as np
  3. from tensorflow.python.ops import math_ops
  4. from tensorflow.python.ops import array_ops
  5. from tensorflow.python.framework import ops
  6. from tensorflow.python.ops.image_ops_impl import _AssertAtLeast3DImage
  7. from tensorflow.python.framework import dtypes
  8. from tensorflow.python.ops.image_ops_impl import convert_image_dtype
  9. __all__ = [
  10. 'CentralCrop',
  11. 'HsvToRgb',
  12. 'AdjustBrightness',
  13. 'AdjustContrast',
  14. 'AdjustHue',
  15. 'AdjustSaturation',
  16. 'Crop',
  17. 'FlipHorizontal',
  18. 'FlipVertical',
  19. 'GrayToRgb',
  20. 'Standardization',
  21. ]
  22. def CentralCrop(image, central_fraction=None, size=None):
  23. '''
  24. Parameters
  25. ----------
  26. image :
  27. input Either a 3-D float Tensor of shape [height, width, depth],
  28. or a 4-D Tensor of shape [batch_size, height, width, depth].
  29. central_fraction :
  30. float (0, 1], fraction of size to crop
  31. size:
  32. size (Union[int, sequence]) – The output size of the cropped image. If size is an integer, a square crop of size (size, size) is returned.
  33. If size is a sequence of length 2, it should be (height, width).
  34. Returns :
  35. 3-D / 4-D float Tensor, as per the input.
  36. -------
  37. '''
  38. if size is None and central_fraction is None:
  39. raise ValueError('central_fraction and size can not be both None')
  40. if central_fraction is None:
  41. outshape = np.shape(image)
  42. if len(outshape) == 3:
  43. h_axis = 0
  44. w_axis = 1
  45. elif len(outshape) == 4:
  46. h_axis = 1
  47. w_axis = 2
  48. if isinstance(size, int):
  49. target_height = size
  50. target_width = size
  51. elif isinstance(size, tuple):
  52. target_height = size[0]
  53. target_width = size[1]
  54. central_fraction = max(target_height // outshape[h_axis], target_width // outshape[w_axis])
  55. return tf.image.central_crop(image, central_fraction)
  56. def HsvToRgb(image):
  57. return tf.image.hsv_to_rgb(image)
  58. def AdjustBrightness(image, factor):
  59. return tf.image.adjust_brightness(image, delta=factor)
  60. def AdjustContrast(image, factor):
  61. return tf.image.adjust_contrast(image, contrast_factor=factor)
  62. def AdjustHue(image, factor):
  63. return tf.image.adjust_hue(image, delta=factor)
  64. def AdjustSaturation(image, factor):
  65. return tf.image.adjust_saturation(image, saturation_factor=factor)
  66. def Crop(image, offset_height, offset_width, target_height, target_width):
  67. '''
  68. Parameters
  69. ----------
  70. image:
  71. A image or a batch of images
  72. offset_height:
  73. Vertical coordinate of the top-left corner of the result in the input.
  74. offset_width:
  75. Horizontal coordinate of the top-left corner of the result in the input.
  76. target_height:
  77. Height of the result.
  78. target_width:
  79. Width of the result.
  80. Returns:
  81. Output [batch, target_height, target_width, channels] or [target_height, target_width, channels]
  82. -------
  83. '''
  84. return tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)
  85. def FlipHorizontal(image):
  86. return tf.image.flip_left_right(image)
  87. def FlipVertical(image):
  88. return tf.image.flip_up_down(image)
  89. def GrayToRgb(image):
  90. return tf.image.grayscale_to_rgb(image)
  91. def RgbToGray(image):
  92. return tf.image.rgb_to_grayscale(image)
  93. def PadToBoundingBox(image, offset_height, offset_width, target_height, target_width):
  94. return tf.image.pad_to_bounding_box(image, offset_height, offset_width, target_height, target_width)
  95. def Standardization(image, mean=None, std=None, channel_mode=False):
  96. '''
  97. Parameters
  98. ----------
  99. image:
  100. An n-D Tensor with at least 3 dimensions, the last 3 of which are the dimensions of each image.
  101. mean:
  102. List or tuple of mean values for each channel, with respect to channel order.
  103. std:
  104. List or tuple of standard deviations for each channel.
  105. channel_mode:
  106. Decide to implement standardization on whole image or each channel of image.
  107. Returns:
  108. A Tensor with the same shape and dtype as image.
  109. -------
  110. '''
  111. with ops.name_scope(None, 'Standardization', [image]) as scope:
  112. image = ops.convert_to_tensor(image, name='image')
  113. image = _AssertAtLeast3DImage(image)
  114. orig_dtype = image.dtype
  115. if orig_dtype not in [dtypes.float16, dtypes.float32]:
  116. image = convert_image_dtype(image, dtypes.float32)
  117. if mean is not None and std is not None:
  118. mean = np.array(mean, dtype=np.float32)
  119. std = np.array(std, dtype=np.float32)
  120. image -= mean
  121. image = math_ops.divide(image, std, name=scope)
  122. return convert_image_dtype(image, orig_dtype, saturate=True)
  123. elif mean is None and std is None:
  124. if channel_mode:
  125. num_pixels = math_ops.reduce_prod(array_ops.shape(image)[-3:-1])
  126. #`num_pixels` is the number of elements in each channels of 'image'
  127. image_mean = math_ops.reduce_mean(image, axis=[-2, -3], keepdims=True)
  128. # `image_mean` is the mean of elements in each channels of 'image'
  129. stddev = math_ops.reduce_std(image, axis=[-2, -3], keepdims=True)
  130. min_stddev = math_ops.rsqrt(math_ops.cast(num_pixels, image.dtype))
  131. adjusted_sttdev = math_ops.maximum(stddev, min_stddev)
  132. image -= image_mean
  133. image = math_ops.divide(image, adjusted_sttdev, name=scope)
  134. return convert_image_dtype(image, orig_dtype, saturate=True)
  135. else:
  136. num_pixels = math_ops.reduce_prod(array_ops.shape(image)[-3:])
  137. #`num_pixels` is the number of elements in `image`
  138. image_mean = math_ops.reduce_mean(image, axis=[-1, -2, -3], keepdims=True)
  139. # Apply a minimum normalization that protects us against uniform images.
  140. stddev = math_ops.reduce_std(image, axis=[-1, -2, -3], keepdims=True)
  141. min_stddev = math_ops.rsqrt(math_ops.cast(num_pixels, image.dtype))
  142. adjusted_stddev = math_ops.maximum(stddev, min_stddev)
  143. image -= image_mean
  144. image = math_ops.divide(image, adjusted_stddev, name=scope)
  145. return convert_image_dtype(image, orig_dtype, saturate=True)
  146. else:
  147. raise ValueError('std and mean must both be None or not None')

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