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

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