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.

test_equalize.py 8.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """
  16. Testing Equalize op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset.engine as de
  20. import mindspore.dataset.transforms.vision.c_transforms as C
  21. import mindspore.dataset.transforms.vision.py_transforms as F
  22. from mindspore import log as logger
  23. from util import visualize_list, diff_mse, save_and_check_md5
  24. DATA_DIR = "../data/dataset/testImageNetData/train/"
  25. GENERATE_GOLDEN = False
  26. def test_equalize_py(plot=False):
  27. """
  28. Test Equalize py op
  29. """
  30. logger.info("Test Equalize")
  31. # Original Images
  32. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  33. transforms_original = F.ComposeOp([F.Decode(),
  34. F.Resize((224, 224)),
  35. F.ToTensor()])
  36. ds_original = ds.map(input_columns="image",
  37. operations=transforms_original())
  38. ds_original = ds_original.batch(512)
  39. for idx, (image, _) in enumerate(ds_original):
  40. if idx == 0:
  41. images_original = np.transpose(image, (0, 2, 3, 1))
  42. else:
  43. images_original = np.append(images_original,
  44. np.transpose(image, (0, 2, 3, 1)),
  45. axis=0)
  46. # Color Equalized Images
  47. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  48. transforms_equalize = F.ComposeOp([F.Decode(),
  49. F.Resize((224, 224)),
  50. F.Equalize(),
  51. F.ToTensor()])
  52. ds_equalize = ds.map(input_columns="image",
  53. operations=transforms_equalize())
  54. ds_equalize = ds_equalize.batch(512)
  55. for idx, (image, _) in enumerate(ds_equalize):
  56. if idx == 0:
  57. images_equalize = np.transpose(image, (0, 2, 3, 1))
  58. else:
  59. images_equalize = np.append(images_equalize,
  60. np.transpose(image, (0, 2, 3, 1)),
  61. axis=0)
  62. num_samples = images_original.shape[0]
  63. mse = np.zeros(num_samples)
  64. for i in range(num_samples):
  65. mse[i] = diff_mse(images_equalize[i], images_original[i])
  66. logger.info("MSE= {}".format(str(np.mean(mse))))
  67. if plot:
  68. visualize_list(images_original, images_equalize)
  69. def test_equalize_c(plot=False):
  70. """
  71. Test Equalize Cpp op
  72. """
  73. logger.info("Test Equalize cpp op")
  74. # Original Images
  75. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  76. transforms_original = [C.Decode(), C.Resize(size=[224, 224])]
  77. ds_original = ds.map(input_columns="image",
  78. operations=transforms_original)
  79. ds_original = ds_original.batch(512)
  80. for idx, (image, _) in enumerate(ds_original):
  81. if idx == 0:
  82. images_original = image
  83. else:
  84. images_original = np.append(images_original,
  85. image,
  86. axis=0)
  87. # Equalize Images
  88. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  89. transform_equalize = [C.Decode(), C.Resize(size=[224, 224]),
  90. C.Equalize()]
  91. ds_equalize = ds.map(input_columns="image",
  92. operations=transform_equalize)
  93. ds_equalize = ds_equalize.batch(512)
  94. for idx, (image, _) in enumerate(ds_equalize):
  95. if idx == 0:
  96. images_equalize = image
  97. else:
  98. images_equalize = np.append(images_equalize,
  99. image,
  100. axis=0)
  101. if plot:
  102. visualize_list(images_original, images_equalize)
  103. num_samples = images_original.shape[0]
  104. mse = np.zeros(num_samples)
  105. for i in range(num_samples):
  106. mse[i] = diff_mse(images_equalize[i], images_original[i])
  107. logger.info("MSE= {}".format(str(np.mean(mse))))
  108. def test_equalize_py_c(plot=False):
  109. """
  110. Test Equalize Cpp op and python op
  111. """
  112. logger.info("Test Equalize cpp and python op")
  113. # equalize Images in cpp
  114. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  115. ds = ds.map(input_columns=["image"],
  116. operations=[C.Decode(), C.Resize((224, 224))])
  117. ds_c_equalize = ds.map(input_columns="image",
  118. operations=C.Equalize())
  119. ds_c_equalize = ds_c_equalize.batch(512)
  120. for idx, (image, _) in enumerate(ds_c_equalize):
  121. if idx == 0:
  122. images_c_equalize = image
  123. else:
  124. images_c_equalize = np.append(images_c_equalize,
  125. image,
  126. axis=0)
  127. # Equalize images in python
  128. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  129. ds = ds.map(input_columns=["image"],
  130. operations=[C.Decode(), C.Resize((224, 224))])
  131. transforms_p_equalize = F.ComposeOp([lambda img: img.astype(np.uint8),
  132. F.ToPIL(),
  133. F.Equalize(),
  134. np.array])
  135. ds_p_equalize = ds.map(input_columns="image",
  136. operations=transforms_p_equalize())
  137. ds_p_equalize = ds_p_equalize.batch(512)
  138. for idx, (image, _) in enumerate(ds_p_equalize):
  139. if idx == 0:
  140. images_p_equalize = image
  141. else:
  142. images_p_equalize = np.append(images_p_equalize,
  143. image,
  144. axis=0)
  145. num_samples = images_c_equalize.shape[0]
  146. mse = np.zeros(num_samples)
  147. for i in range(num_samples):
  148. mse[i] = diff_mse(images_p_equalize[i], images_c_equalize[i])
  149. logger.info("MSE= {}".format(str(np.mean(mse))))
  150. if plot:
  151. visualize_list(images_c_equalize, images_p_equalize, visualize_mode=2)
  152. def test_equalize_one_channel():
  153. """
  154. Test Equalize cpp op with one channel image
  155. """
  156. logger.info("Test Equalize C Op With One Channel Images")
  157. c_op = C.Equalize()
  158. try:
  159. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  160. ds = ds.map(input_columns=["image"],
  161. operations=[C.Decode(),
  162. C.Resize((224, 224)),
  163. lambda img: np.array(img[:, :, 0])])
  164. ds.map(input_columns="image",
  165. operations=c_op)
  166. except RuntimeError as e:
  167. logger.info("Got an exception in DE: {}".format(str(e)))
  168. assert "The shape" in str(e)
  169. def test_equalize_md5_py():
  170. """
  171. Test Equalize py op with md5 check
  172. """
  173. logger.info("Test Equalize")
  174. # First dataset
  175. data1 = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  176. transforms = F.ComposeOp([F.Decode(),
  177. F.Equalize(),
  178. F.ToTensor()])
  179. data1 = data1.map(input_columns="image", operations=transforms())
  180. # Compare with expected md5 from images
  181. filename = "equalize_01_result.npz"
  182. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  183. def test_equalize_md5_c():
  184. """
  185. Test Equalize cpp op with md5 check
  186. """
  187. logger.info("Test Equalize cpp op with md5 check")
  188. # Generate dataset
  189. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  190. transforms_equalize = [C.Decode(),
  191. C.Resize(size=[224, 224]),
  192. C.Equalize(),
  193. F.ToTensor()]
  194. data = ds.map(input_columns="image", operations=transforms_equalize)
  195. # Compare with expected md5 from images
  196. filename = "equalize_01_result_c.npz"
  197. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  198. if __name__ == "__main__":
  199. test_equalize_py(plot=False)
  200. test_equalize_c(plot=False)
  201. test_equalize_py_c(plot=False)
  202. test_equalize_one_channel()
  203. test_equalize_md5_py()
  204. test_equalize_md5_c()