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.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, output_numpy=True),
  53. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  54. image1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  55. image2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  56. image_choice.append(image1)
  57. image_original.append(image2)
  58. if plot:
  59. visualize_list(image_original, image_choice)
  60. def test_random_choice_comp(plot=False):
  61. """
  62. Test RandomChoice and compare with single CenterCrop results
  63. """
  64. logger.info("test_random_choice_comp")
  65. # define map operations
  66. transforms_list = [py_vision.CenterCrop(64)]
  67. transforms1 = [
  68. py_vision.Decode(),
  69. py_transforms.RandomChoice(transforms_list),
  70. py_vision.ToTensor()
  71. ]
  72. transform1 = py_transforms.Compose(transforms1)
  73. transforms2 = [
  74. py_vision.Decode(),
  75. py_vision.CenterCrop(64),
  76. py_vision.ToTensor()
  77. ]
  78. transform2 = py_transforms.Compose(transforms2)
  79. # First dataset
  80. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  81. data1 = data1.map(operations=transform1, input_columns=["image"])
  82. # Second dataset
  83. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  84. data2 = data2.map(operations=transform2, input_columns=["image"])
  85. image_choice = []
  86. image_original = []
  87. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  88. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  89. image1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  90. image2 = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  91. image_choice.append(image1)
  92. image_original.append(image2)
  93. mse = diff_mse(image1, image2)
  94. assert mse == 0
  95. if plot:
  96. visualize_list(image_original, image_choice)
  97. def test_random_choice_exception_random_crop_badinput():
  98. """
  99. Test RandomChoice: hit error in RandomCrop with greater crop size,
  100. expected to raise error
  101. """
  102. logger.info("test_random_choice_exception_random_crop_badinput")
  103. # define map operations
  104. # note: crop size[5000, 5000] > image size[4032, 2268]
  105. transforms_list = [py_vision.RandomCrop(5000)]
  106. transforms = [
  107. py_vision.Decode(),
  108. py_transforms.RandomChoice(transforms_list),
  109. py_vision.ToTensor()
  110. ]
  111. transform = py_transforms.Compose(transforms)
  112. # Generate dataset
  113. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  114. data = data.map(operations=transform, input_columns=["image"])
  115. try:
  116. _ = data.create_dict_iterator(num_epochs=1).get_next()
  117. except RuntimeError as e:
  118. logger.info("Got an exception in DE: {}".format(str(e)))
  119. assert "Crop size" in str(e)
  120. if __name__ == '__main__':
  121. test_random_choice_op(plot=True)
  122. test_random_choice_comp(plot=True)
  123. test_random_choice_exception_random_crop_badinput()