preprocessing.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- utf-8 -*-
  2. import matplotlib.pyplot as plt
  3. import tensorflow as tf
  4. import numpy as np
  5. def img_endecoding():
  6. image_raw_data = tf.gfile.FastGFile("backGround.jpg", 'rb').read()
  7. img_data = tf.image.decode_jpeg(image_raw_data)
  8. print(type(img_data.eval()))
  9. print(img_data.eval().ndim)
  10. print(img_data.eval().dtype)
  11. print(img_data.eval().size)
  12. print(img_data.eval().shape)
  13. # plt.imshow(img_data.eval())
  14. # plt.show()
  15. encoded_image = tf.image.encode_jpeg(img_data)
  16. with tf.gfile.GFile("backGround2.jpg", 'wb') as f:
  17. f.write(encoded_image.eval())
  18. return img_data
  19. def img_proc(img_data):
  20. # sugggest to convert img to real number domain 0.0-1.0, so as not to lose too much accuracy
  21. img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
  22. # change size
  23. resized = tf.image.resize_images(img_data, [300, 300], method=0)
  24. croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 500)
  25. padded = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
  26. central_cropped = tf.image.central_crop(img_data, 0.5)
  27. # flip
  28. flipped_ud = tf.image.flip_up_down(img_data)
  29. flipped_lr = tf.image.flip_left_right(img_data)
  30. transpose = tf.image.transpose_image(img_data)
  31. flipped_rndup = tf.image.random_flip_up_down(img_data)
  32. flipped_rndlr = tf.image.random_flip_left_right(img_data)
  33. # brightness
  34. bright_adjusted = tf.image.adjust_brightness(img_data, -0.5)
  35. bright_adjusted = tf.image.random_brightness(img_data, 0.5) # -0.5 - 0.5
  36. bright_adjusted_clip = tf.clip_by_value(bright_adjusted, 0.0, 1.0)
  37. # contrast
  38. contrast_adjusted = tf.image.adjust_contrast(img_data, 0.5)
  39. contrast_adjusted = tf.image.random_contrast(img_data, 0.5, 5)
  40. # hue 色相
  41. hue_adjusted = tf.image.adjust_hue(img_data, 0.1)
  42. hue_adjusted = tf.image.random_hue(img_data, 0.5) # 0-0.5
  43. # saturation 饱和度
  44. saturation_adjusted = tf.image.adjust_saturation(img_data, -5)
  45. saturation_adjusted = tf.image.random_saturation(img_data, 0, 5)
  46. # standardization, N~(0,1)
  47. adjusted = tf.image.per_image_standardization(img_data)
  48. # labelling, 输入四维矩阵
  49. batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, dtype=tf.float32), 0)
  50. print(batched.eval().ndim)
  51. # Ymin, Xmin, Ymax, Xmax
  52. boxes = tf.constant([[[0.1, 0.5, 0.85, 0.8], [0.35, 0.47, 0.5, 0.56]]])
  53. result = tf.image.draw_bounding_boxes(batched, boxes)
  54. # clip by boxes, 0.4 means at least contain 40% area
  55. begin, size, box = tf.image.sample_distorted_bounding_box(
  56. tf.shape(img_data), bounding_boxes=boxes,
  57. min_object_covered=0.4
  58. )
  59. image_with_box = tf.image.draw_bounding_boxes(batched, box)
  60. distorted_img = tf.slice(img_data, begin, size)
  61. plt.imshow(result[0].eval())
  62. plt.show()
  63. img_data = tf.image.convert_image_dtype(resized, dtype=tf.uint8)
  64. return img_data
  65. def distort_color(image, color_ordering=0):
  66. if color_ordering == 0:
  67. image = tf.image.random_brightness(image, max_delta=32. / 255.)
  68. image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
  69. image = tf.image.random_hue(image, max_delta=0.2)
  70. image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
  71. elif color_ordering == 1:
  72. image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
  73. image = tf.image.random_brightness(image, max_delta=32. / 255.)
  74. image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
  75. image = tf.image.random_hue(image, max_delta=0.2)
  76. elif color_ordering == 2:
  77. image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
  78. image = tf.image.random_hue(image, max_delta=0.2)
  79. image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
  80. image = tf.image.random_brightness(image, max_delta=32. / 255.)
  81. return tf.clip_by_value(image, 0.0, 1.0)
  82. def process_for_train(image, height, width, bbox, channels=3):
  83. if bbox is None:
  84. bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
  85. if image.dtype != tf.float32:
  86. image = tf.image.convert_image_dtype(image, dtype=tf.float32)
  87. bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
  88. tf.shape(image), bounding_boxes=bbox, min_object_covered=0.1
  89. )
  90. distorted_img = tf.slice(image, bbox_begin, bbox_size)
  91. # resize input image for train, all kinds of interpolation
  92. distorted_img = tf.image.resize_images(
  93. distorted_img, [height, width], method=np.random.randint(4)
  94. )
  95. # filp img
  96. distorted_img = tf.image.random_flip_left_right(distorted_img)
  97. distorted_img = tf.image.random_flip_up_down(distorted_img)
  98. if channels == 3:
  99. distorted_img = distort_color(distorted_img, np.random.randint(3))
  100. # distorted_img = tf.image.convert_image_dtype(distorted_img, dtype=tf.uint8)
  101. # print(distorted_img.shape)
  102. distorted_img.set_shape([height, width, channels])
  103. return distorted_img
  104. def main():
  105. # with tf.device('/cpu:0'):
  106. with tf.Session() as sess:
  107. img_data = img_endecoding()
  108. boxes = tf.constant([[[0.1, 0.5, 0.85, 0.8]]])
  109. # img_data = img_proc(img_data)
  110. for i in range(6):
  111. plt.figure(i)
  112. result = process_for_train(img_data, 500, 300, boxes)
  113. plt.imshow(result.eval())
  114. plt.show()
  115. # print(img_data)
  116. # plt.imshow(trans.eval())
  117. # plt.show()
  118. if __name__ == '__main__':
  119. main()