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

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