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_invert.py 8.6 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
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 Invert op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset.engine as de
  20. import mindspore.dataset.transforms.vision.py_transforms as F
  21. import mindspore.dataset.transforms.vision.c_transforms as C
  22. from mindspore import log as logger
  23. from util import visualize_list, save_and_check_md5, diff_mse
  24. DATA_DIR = "../data/dataset/testImageNetData/train/"
  25. GENERATE_GOLDEN = False
  26. def test_invert_py(plot=False):
  27. """
  28. Test Invert python op
  29. """
  30. logger.info("Test Invert Python op")
  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 Inverted Images
  47. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  48. transforms_invert = F.ComposeOp([F.Decode(),
  49. F.Resize((224, 224)),
  50. F.Invert(),
  51. F.ToTensor()])
  52. ds_invert = ds.map(input_columns="image",
  53. operations=transforms_invert())
  54. ds_invert = ds_invert.batch(512)
  55. for idx, (image, _) in enumerate(ds_invert):
  56. if idx == 0:
  57. images_invert = np.transpose(image, (0, 2, 3, 1))
  58. else:
  59. images_invert = np.append(images_invert,
  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] = np.mean((images_invert[i] - images_original[i]) ** 2)
  66. logger.info("MSE= {}".format(str(np.mean(mse))))
  67. if plot:
  68. visualize_list(images_original, images_invert)
  69. def test_invert_c(plot=False):
  70. """
  71. Test Invert Cpp op
  72. """
  73. logger.info("Test Invert 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. # Invert Images
  88. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  89. transform_invert = [C.Decode(), C.Resize(size=[224, 224]),
  90. C.Invert()]
  91. ds_invert = ds.map(input_columns="image",
  92. operations=transform_invert)
  93. ds_invert = ds_invert.batch(512)
  94. for idx, (image, _) in enumerate(ds_invert):
  95. if idx == 0:
  96. images_invert = image
  97. else:
  98. images_invert = np.append(images_invert,
  99. image,
  100. axis=0)
  101. if plot:
  102. visualize_list(images_original, images_invert)
  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_invert[i], images_original[i])
  107. logger.info("MSE= {}".format(str(np.mean(mse))))
  108. def test_invert_py_c(plot=False):
  109. """
  110. Test Invert Cpp op and python op
  111. """
  112. logger.info("Test Invert cpp and python op")
  113. # Invert 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_invert = ds.map(input_columns="image",
  118. operations=C.Invert())
  119. ds_c_invert = ds_c_invert.batch(512)
  120. for idx, (image, _) in enumerate(ds_c_invert):
  121. if idx == 0:
  122. images_c_invert = image
  123. else:
  124. images_c_invert = np.append(images_c_invert,
  125. image,
  126. axis=0)
  127. # invert 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_invert = F.ComposeOp([lambda img: img.astype(np.uint8),
  132. F.ToPIL(),
  133. F.Invert(),
  134. np.array])
  135. ds_p_invert = ds.map(input_columns="image",
  136. operations=transforms_p_invert())
  137. ds_p_invert = ds_p_invert.batch(512)
  138. for idx, (image, _) in enumerate(ds_p_invert):
  139. if idx == 0:
  140. images_p_invert = image
  141. else:
  142. images_p_invert = np.append(images_p_invert,
  143. image,
  144. axis=0)
  145. num_samples = images_c_invert.shape[0]
  146. mse = np.zeros(num_samples)
  147. for i in range(num_samples):
  148. mse[i] = diff_mse(images_p_invert[i], images_c_invert[i])
  149. logger.info("MSE= {}".format(str(np.mean(mse))))
  150. if plot:
  151. visualize_list(images_c_invert, images_p_invert, visualize_mode=2)
  152. def test_invert_one_channel():
  153. """
  154. Test Invert cpp op with one channel image
  155. """
  156. logger.info("Test Invert C Op With One Channel Images")
  157. c_op = C.Invert()
  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_invert_md5_py():
  170. """
  171. Test Invert python op with md5 check
  172. """
  173. logger.info("Test Invert python op with md5 check")
  174. # Generate dataset
  175. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  176. transforms_invert = F.ComposeOp([F.Decode(),
  177. F.Invert(),
  178. F.ToTensor()])
  179. data = ds.map(input_columns="image", operations=transforms_invert())
  180. # Compare with expected md5 from images
  181. filename = "invert_01_result_py.npz"
  182. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  183. def test_invert_md5_c():
  184. """
  185. Test Invert cpp op with md5 check
  186. """
  187. logger.info("Test Invert cpp op with md5 check")
  188. # Generate dataset
  189. ds = de.ImageFolderDatasetV2(dataset_dir=DATA_DIR, shuffle=False)
  190. transforms_invert = [C.Decode(),
  191. C.Resize(size=[224, 224]),
  192. C.Invert(),
  193. F.ToTensor()]
  194. data = ds.map(input_columns="image", operations=transforms_invert)
  195. # Compare with expected md5 from images
  196. filename = "invert_01_result_c.npz"
  197. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  198. if __name__ == "__main__":
  199. test_invert_py(plot=False)
  200. test_invert_c(plot=False)
  201. test_invert_py_c(plot=False)
  202. test_invert_one_channel()
  203. test_invert_md5_py()
  204. test_invert_md5_c()