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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. import mindspore.dataset.transforms.vision.c_transforms as vision
  16. import numpy as np
  17. import matplotlib.pyplot as plt
  18. import mindspore.dataset as ds
  19. from mindspore import log as logger
  20. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  21. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  22. def normalize_np(image):
  23. """
  24. Apply the normalization
  25. """
  26. # DE decodes the image in RGB by deafult, hence
  27. # the values here are in RGB
  28. image = np.array(image, np.float32)
  29. image = image - np.array([121.0, 115.0, 100.0])
  30. image = image * (1.0 / np.array([70.0, 68.0, 71.0]))
  31. return image
  32. def get_normalized(image_id):
  33. """
  34. Reads the image using DE ops and then normalizes using Numpy
  35. """
  36. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  37. decode_op = vision.Decode()
  38. data1 = data1.map(input_columns=["image"], operations=decode_op)
  39. num_iter = 0
  40. for item in data1.create_dict_iterator():
  41. image = item["image"]
  42. if num_iter == image_id:
  43. return normalize_np(image)
  44. num_iter += 1
  45. def test_normalize_op():
  46. """
  47. Test Normalize
  48. """
  49. logger.info("Test Normalize")
  50. # define map operations
  51. decode_op = vision.Decode()
  52. normalize_op = vision.Normalize([121.0, 115.0, 100.0], [70.0, 68.0, 71.0])
  53. # First dataset
  54. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  55. data1 = data1.map(input_columns=["image"], operations=decode_op)
  56. data1 = data1.map(input_columns=["image"], operations=normalize_op)
  57. # Second dataset
  58. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  59. data2 = data2.map(input_columns=["image"], operations=decode_op)
  60. num_iter = 0
  61. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  62. image_de_normalized = item1["image"]
  63. image_np_normalized = normalize_np(item2["image"])
  64. diff = image_de_normalized - image_np_normalized
  65. mse = np.sum(np.power(diff, 2))
  66. logger.info("image_{}, mse: {}".format(num_iter + 1, mse))
  67. assert mse < 0.01
  68. # Uncomment these blocks to see visual results
  69. # plt.subplot(131)
  70. # plt.imshow(image_de_normalized)
  71. # plt.title("DE normalize image")
  72. #
  73. # plt.subplot(132)
  74. # plt.imshow(image_np_normalized)
  75. # plt.title("Numpy normalized image")
  76. #
  77. # plt.subplot(133)
  78. # plt.imshow(diff)
  79. # plt.title("Difference image, mse : {}".format(mse))
  80. #
  81. # plt.show()
  82. num_iter += 1
  83. def test_decode_op():
  84. logger.info("Test Decode")
  85. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image", "label"], num_parallel_workers=1,
  86. shuffle=False)
  87. # define map operations
  88. decode_op = vision.Decode()
  89. # apply map operations on images
  90. data1 = data1.map(input_columns=["image"], operations=decode_op)
  91. num_iter = 0
  92. image = None
  93. for item in data1.create_dict_iterator():
  94. logger.info("Looping inside iterator {}".format(num_iter))
  95. image = item["image"]
  96. # plt.subplot(131)
  97. # plt.imshow(image)
  98. # plt.title("DE image")
  99. # plt.show()
  100. num_iter += 1
  101. def test_decode_normalize_op():
  102. logger.info("Test [Decode, Normalize] in one Map")
  103. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image", "label"], num_parallel_workers=1,
  104. shuffle=False)
  105. # define map operations
  106. decode_op = vision.Decode()
  107. normalize_op = vision.Normalize([121.0, 115.0, 100.0], [70.0, 68.0, 71.0])
  108. # apply map operations on images
  109. data1 = data1.map(input_columns=["image"], operations=[decode_op, normalize_op])
  110. num_iter = 0
  111. image = None
  112. for item in data1.create_dict_iterator():
  113. logger.info("Looping inside iterator {}".format(num_iter))
  114. image = item["image"]
  115. # plt.subplot(131)
  116. # plt.imshow(image)
  117. # plt.title("DE image")
  118. # plt.show()
  119. num_iter += 1
  120. if __name__ == "__main__":
  121. test_decode_op()
  122. test_decode_normalize_op()
  123. test_normalize_op()