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 13 kB

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