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_random_choice.py 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright 2020 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 RandomChoice op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.py_transforms as py_transforms
  21. import mindspore.dataset.vision.py_transforms as py_vision
  22. from mindspore import log as logger
  23. from util import visualize_list, diff_mse
  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. def test_random_choice_op(plot=False):
  27. """
  28. Test RandomChoice in python transformations
  29. """
  30. logger.info("test_random_choice_op")
  31. # define map operations
  32. transforms_list = [py_vision.CenterCrop(64), py_vision.RandomRotation(30)]
  33. transforms1 = [
  34. py_vision.Decode(),
  35. py_transforms.RandomChoice(transforms_list),
  36. py_vision.ToTensor()
  37. ]
  38. transform1 = py_transforms.Compose(transforms1)
  39. transforms2 = [
  40. py_vision.Decode(),
  41. py_vision.ToTensor()
  42. ]
  43. transform2 = py_transforms.Compose(transforms2)
  44. # First dataset
  45. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  46. data1 = data1.map(operations=transform1, input_columns=["image"])
  47. # Second dataset
  48. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  49. data2 = data2.map(operations=transform2, input_columns=["image"])
  50. image_choice = []
  51. image_original = []
  52. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1), data2.create_dict_iterator(num_epochs=1)):
  53. image1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  54. image2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  55. image_choice.append(image1)
  56. image_original.append(image2)
  57. if plot:
  58. visualize_list(image_original, image_choice)
  59. def test_random_choice_comp(plot=False):
  60. """
  61. Test RandomChoice and compare with single CenterCrop results
  62. """
  63. logger.info("test_random_choice_comp")
  64. # define map operations
  65. transforms_list = [py_vision.CenterCrop(64)]
  66. transforms1 = [
  67. py_vision.Decode(),
  68. py_transforms.RandomChoice(transforms_list),
  69. py_vision.ToTensor()
  70. ]
  71. transform1 = py_transforms.Compose(transforms1)
  72. transforms2 = [
  73. py_vision.Decode(),
  74. py_vision.CenterCrop(64),
  75. py_vision.ToTensor()
  76. ]
  77. transform2 = py_transforms.Compose(transforms2)
  78. # First dataset
  79. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  80. data1 = data1.map(operations=transform1, input_columns=["image"])
  81. # Second dataset
  82. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  83. data2 = data2.map(operations=transform2, input_columns=["image"])
  84. image_choice = []
  85. image_original = []
  86. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1), data2.create_dict_iterator(num_epochs=1)):
  87. image1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  88. image2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  89. image_choice.append(image1)
  90. image_original.append(image2)
  91. mse = diff_mse(image1, image2)
  92. assert mse == 0
  93. if plot:
  94. visualize_list(image_original, image_choice)
  95. def test_random_choice_exception_random_crop_badinput():
  96. """
  97. Test RandomChoice: hit error in RandomCrop with greater crop size,
  98. expected to raise error
  99. """
  100. logger.info("test_random_choice_exception_random_crop_badinput")
  101. # define map operations
  102. # note: crop size[5000, 5000] > image size[4032, 2268]
  103. transforms_list = [py_vision.RandomCrop(5000)]
  104. transforms = [
  105. py_vision.Decode(),
  106. py_transforms.RandomChoice(transforms_list),
  107. py_vision.ToTensor()
  108. ]
  109. transform = py_transforms.Compose(transforms)
  110. # Generate dataset
  111. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  112. data = data.map(operations=transform, input_columns=["image"])
  113. try:
  114. _ = data.create_dict_iterator(num_epochs=1).get_next()
  115. except RuntimeError as e:
  116. logger.info("Got an exception in DE: {}".format(str(e)))
  117. assert "Crop size" in str(e)
  118. if __name__ == '__main__':
  119. test_random_choice_op(plot=True)
  120. test_random_choice_comp(plot=True)
  121. test_random_choice_exception_random_crop_badinput()