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_normalizeOp.py 12 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. # Copyright 2019 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 Normalize op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.vision.c_transforms as c_vision
  21. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  22. from mindspore import log as logger
  23. from util import diff_mse, save_and_check_md5, visualize_image
  24. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  25. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  26. GENERATE_GOLDEN = False
  27. def normalize_np(image, mean, std):
  28. """
  29. Apply the normalization
  30. """
  31. # DE decodes the image in RGB by deafult, hence
  32. # the values here are in RGB
  33. image = np.array(image, np.float32)
  34. image = image - np.array(mean)
  35. image = image * (1.0 / np.array(std))
  36. return image
  37. def util_test_normalize(mean, std, op_type):
  38. """
  39. Utility function for testing Normalize. Input arguments are given by other tests
  40. """
  41. if op_type == "cpp":
  42. # define map operations
  43. decode_op = c_vision.Decode()
  44. normalize_op = c_vision.Normalize(mean, std)
  45. # Generate dataset
  46. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  47. data = data.map(input_columns=["image"], operations=decode_op)
  48. data = data.map(input_columns=["image"], operations=normalize_op)
  49. elif op_type == "python":
  50. # define map operations
  51. transforms = [
  52. py_vision.Decode(),
  53. py_vision.ToTensor(),
  54. py_vision.Normalize(mean, std)
  55. ]
  56. transform = py_vision.ComposeOp(transforms)
  57. # Generate dataset
  58. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  59. data = data.map(input_columns=["image"], operations=transform())
  60. else:
  61. raise ValueError("Wrong parameter value")
  62. return data
  63. def util_test_normalize_grayscale(num_output_channels, mean, std):
  64. """
  65. Utility function for testing Normalize. Input arguments are given by other tests
  66. """
  67. transforms = [
  68. py_vision.Decode(),
  69. py_vision.Grayscale(num_output_channels),
  70. py_vision.ToTensor(),
  71. py_vision.Normalize(mean, std)
  72. ]
  73. transform = py_vision.ComposeOp(transforms)
  74. # Generate dataset
  75. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  76. data = data.map(input_columns=["image"], operations=transform())
  77. return data
  78. def test_normalize_op_c(plot=False):
  79. """
  80. Test Normalize in cpp transformations
  81. """
  82. logger.info("Test Normalize in cpp")
  83. mean = [121.0, 115.0, 100.0]
  84. std = [70.0, 68.0, 71.0]
  85. # define map operations
  86. decode_op = c_vision.Decode()
  87. normalize_op = c_vision.Normalize(mean, std)
  88. # First dataset
  89. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  90. data1 = data1.map(input_columns=["image"], operations=decode_op)
  91. data1 = data1.map(input_columns=["image"], operations=normalize_op)
  92. # Second dataset
  93. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  94. data2 = data2.map(input_columns=["image"], operations=decode_op)
  95. num_iter = 0
  96. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  97. image_de_normalized = item1["image"]
  98. image_original = item2["image"]
  99. image_np_normalized = normalize_np(image_original, mean, std)
  100. mse = diff_mse(image_de_normalized, image_np_normalized)
  101. logger.info("image_{}, mse: {}".format(num_iter + 1, mse))
  102. assert mse < 0.01
  103. if plot:
  104. visualize_image(image_original, image_de_normalized, mse, image_np_normalized)
  105. num_iter += 1
  106. def test_normalize_op_py(plot=False):
  107. """
  108. Test Normalize in python transformations
  109. """
  110. logger.info("Test Normalize in python")
  111. mean = [0.475, 0.45, 0.392]
  112. std = [0.275, 0.267, 0.278]
  113. # define map operations
  114. transforms = [
  115. py_vision.Decode(),
  116. py_vision.ToTensor()
  117. ]
  118. transform = py_vision.ComposeOp(transforms)
  119. normalize_op = py_vision.Normalize(mean, std)
  120. # First dataset
  121. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  122. data1 = data1.map(input_columns=["image"], operations=transform())
  123. data1 = data1.map(input_columns=["image"], operations=normalize_op)
  124. # Second dataset
  125. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  126. data2 = data2.map(input_columns=["image"], operations=transform())
  127. num_iter = 0
  128. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  129. image_de_normalized = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  130. image_np_normalized = (normalize_np(item2["image"].transpose(1, 2, 0), mean, std) * 255).astype(np.uint8)
  131. image_original = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  132. mse = diff_mse(image_de_normalized, image_np_normalized)
  133. logger.info("image_{}, mse: {}".format(num_iter + 1, mse))
  134. assert mse < 0.01
  135. if plot:
  136. visualize_image(image_original, image_de_normalized, mse, image_np_normalized)
  137. num_iter += 1
  138. def test_decode_op():
  139. """
  140. Test Decode op
  141. """
  142. logger.info("Test Decode")
  143. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image", "label"], num_parallel_workers=1,
  144. shuffle=False)
  145. # define map operations
  146. decode_op = c_vision.Decode()
  147. # apply map operations on images
  148. data1 = data1.map(input_columns=["image"], operations=decode_op)
  149. num_iter = 0
  150. for item in data1.create_dict_iterator():
  151. logger.info("Looping inside iterator {}".format(num_iter))
  152. _ = item["image"]
  153. num_iter += 1
  154. def test_decode_normalize_op():
  155. """
  156. Test Decode op followed by Normalize op
  157. """
  158. logger.info("Test [Decode, Normalize] in one Map")
  159. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image", "label"], num_parallel_workers=1,
  160. shuffle=False)
  161. # define map operations
  162. decode_op = c_vision.Decode()
  163. normalize_op = c_vision.Normalize([121.0, 115.0, 100.0], [70.0, 68.0, 71.0])
  164. # apply map operations on images
  165. data1 = data1.map(input_columns=["image"], operations=[decode_op, normalize_op])
  166. num_iter = 0
  167. for item in data1.create_dict_iterator():
  168. logger.info("Looping inside iterator {}".format(num_iter))
  169. _ = item["image"]
  170. num_iter += 1
  171. def test_normalize_md5_01():
  172. """
  173. Test Normalize with md5 check: valid mean and std
  174. expected to pass
  175. """
  176. logger.info("test_normalize_md5_01")
  177. data_c = util_test_normalize([121.0, 115.0, 100.0], [70.0, 68.0, 71.0], "cpp")
  178. data_py = util_test_normalize([0.475, 0.45, 0.392], [0.275, 0.267, 0.278], "python")
  179. # check results with md5 comparison
  180. filename1 = "normalize_01_c_result.npz"
  181. filename2 = "normalize_01_py_result.npz"
  182. save_and_check_md5(data_c, filename1, generate_golden=GENERATE_GOLDEN)
  183. save_and_check_md5(data_py, filename2, generate_golden=GENERATE_GOLDEN)
  184. def test_normalize_md5_02():
  185. """
  186. Test Normalize with md5 check: len(mean)=len(std)=1 with RGB images
  187. expected to pass
  188. """
  189. logger.info("test_normalize_md5_02")
  190. data_py = util_test_normalize([0.475], [0.275], "python")
  191. # check results with md5 comparison
  192. filename2 = "normalize_02_py_result.npz"
  193. save_and_check_md5(data_py, filename2, generate_golden=GENERATE_GOLDEN)
  194. def test_normalize_exception_unequal_size_c():
  195. """
  196. Test Normalize in c transformation: len(mean) != len(std)
  197. expected to raise ValueError
  198. """
  199. logger.info("test_normalize_exception_unequal_size_c")
  200. try:
  201. _ = c_vision.Normalize([100, 250, 125], [50, 50, 75, 75])
  202. except ValueError as e:
  203. logger.info("Got an exception in DE: {}".format(str(e)))
  204. assert str(e) == "Length of mean and std must be equal"
  205. def test_normalize_exception_unequal_size_py():
  206. """
  207. Test Normalize in python transformation: len(mean) != len(std)
  208. expected to raise ValueError
  209. """
  210. logger.info("test_normalize_exception_unequal_size_py")
  211. try:
  212. _ = py_vision.Normalize([0.50, 0.30, 0.75], [0.18, 0.32, 0.71, 0.72])
  213. except ValueError as e:
  214. logger.info("Got an exception in DE: {}".format(str(e)))
  215. assert str(e) == "Length of mean and std must be equal"
  216. def test_normalize_exception_invalid_size_py():
  217. """
  218. Test Normalize in python transformation: len(mean)=len(std)=2
  219. expected to raise RuntimeError
  220. """
  221. logger.info("test_normalize_exception_invalid_size_py")
  222. data = util_test_normalize([0.75, 0.25], [0.18, 0.32], "python")
  223. try:
  224. _ = data.create_dict_iterator().get_next()
  225. except RuntimeError as e:
  226. logger.info("Got an exception in DE: {}".format(str(e)))
  227. assert "Length of mean and std must both be 1 or" in str(e)
  228. def test_normalize_exception_invalid_range_py():
  229. """
  230. Test Normalize in python transformation: value is not in range [0,1]
  231. expected to raise ValueError
  232. """
  233. logger.info("test_normalize_exception_invalid_range_py")
  234. try:
  235. _ = py_vision.Normalize([0.75, 1.25, 0.5], [0.1, 0.18, 1.32])
  236. except ValueError as e:
  237. logger.info("Got an exception in DE: {}".format(str(e)))
  238. assert "Input is not within the required range" in str(e)
  239. def test_normalize_grayscale_md5_01():
  240. """
  241. Test Normalize with md5 check: len(mean)=len(std)=1 with 1 channel grayscale images
  242. expected to pass
  243. """
  244. logger.info("test_normalize_grayscale_md5_01")
  245. data = util_test_normalize_grayscale(1, [0.5], [0.175])
  246. # check results with md5 comparison
  247. filename = "normalize_03_py_result.npz"
  248. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  249. def test_normalize_grayscale_md5_02():
  250. """
  251. Test Normalize with md5 check: len(mean)=len(std)=3 with 3 channel grayscale images
  252. expected to pass
  253. """
  254. logger.info("test_normalize_grayscale_md5_02")
  255. data = util_test_normalize_grayscale(3, [0.5, 0.5, 0.5], [0.175, 0.235, 0.512])
  256. # check results with md5 comparison
  257. filename = "normalize_04_py_result.npz"
  258. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  259. def test_normalize_grayscale_exception():
  260. """
  261. Test Normalize: len(mean)=len(std)=3 with 1 channel grayscale images
  262. expected to raise RuntimeError
  263. """
  264. logger.info("test_normalize_grayscale_exception")
  265. try:
  266. _ = util_test_normalize_grayscale(1, [0.5, 0.5, 0.5], [0.175, 0.235, 0.512])
  267. except RuntimeError as e:
  268. logger.info("Got an exception in DE: {}".format(str(e)))
  269. assert "Input is not within the required range" in str(e)
  270. if __name__ == "__main__":
  271. test_decode_op()
  272. test_decode_normalize_op()
  273. test_normalize_op_c(plot=True)
  274. test_normalize_op_py(plot=True)
  275. test_normalize_md5_01()
  276. test_normalize_md5_02()
  277. test_normalize_exception_unequal_size_c()
  278. test_normalize_exception_unequal_size_py()
  279. test_normalize_exception_invalid_size_py()
  280. test_normalize_exception_invalid_range_py()
  281. test_normalize_grayscale_md5_01()
  282. test_normalize_grayscale_md5_02()
  283. test_normalize_grayscale_exception()