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