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_solarize_op.py 4.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. """
  16. Testing RandomSolarizeOp op in DE
  17. """
  18. import pytest
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.vision.c_transforms as vision
  21. from mindspore import log as logger
  22. from util import visualize_list, save_and_check_md5, config_get_set_seed, config_get_set_num_parallel_workers
  23. GENERATE_GOLDEN = False
  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_solarize_op(threshold=None, plot=False):
  27. """
  28. Test RandomSolarize
  29. """
  30. logger.info("Test RandomSolarize")
  31. # First dataset
  32. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  33. decode_op = vision.Decode()
  34. if threshold is None:
  35. solarize_op = vision.RandomSolarize()
  36. else:
  37. solarize_op = vision.RandomSolarize(threshold)
  38. data1 = data1.map(input_columns=["image"], operations=decode_op)
  39. data1 = data1.map(input_columns=["image"], operations=solarize_op)
  40. # Second dataset
  41. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  42. data2 = data2.map(input_columns=["image"], operations=decode_op)
  43. image_solarized = []
  44. image = []
  45. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  46. image_solarized.append(item1["image"].copy())
  47. image.append(item2["image"].copy())
  48. if plot:
  49. visualize_list(image, image_solarized)
  50. def test_random_solarize_md5():
  51. """
  52. Test RandomSolarize
  53. """
  54. logger.info("Test RandomSolarize")
  55. original_seed = config_get_set_seed(0)
  56. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  57. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  58. decode_op = vision.Decode()
  59. random_solarize_op = vision.RandomSolarize((10, 150))
  60. data1 = data1.map(input_columns=["image"], operations=decode_op)
  61. data1 = data1.map(input_columns=["image"], operations=random_solarize_op)
  62. # Compare with expected md5 from images
  63. filename = "random_solarize_01_result.npz"
  64. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  65. # Restore config setting
  66. ds.config.set_seed(original_seed)
  67. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  68. def test_random_solarize_errors():
  69. """
  70. Test that RandomSolarize errors with bad input
  71. """
  72. with pytest.raises(ValueError) as error_info:
  73. vision.RandomSolarize((12, 1))
  74. assert "threshold must be in min max format numbers" in str(error_info.value)
  75. with pytest.raises(ValueError) as error_info:
  76. vision.RandomSolarize((12, 1000))
  77. assert "Input is not within the required interval of (0 to 255)." in str(error_info.value)
  78. with pytest.raises(TypeError) as error_info:
  79. vision.RandomSolarize((122.1, 140))
  80. assert "Argument threshold[0] with value 122.1 is not of type (<class 'int'>,)." in str(error_info.value)
  81. with pytest.raises(ValueError) as error_info:
  82. vision.RandomSolarize((122, 100, 30))
  83. assert "threshold must be a sequence of two numbers" in str(error_info.value)
  84. with pytest.raises(ValueError) as error_info:
  85. vision.RandomSolarize((120,))
  86. assert "threshold must be a sequence of two numbers" in str(error_info.value)
  87. if __name__ == "__main__":
  88. test_random_solarize_op((100, 100), plot=True)
  89. test_random_solarize_op((12, 120), plot=True)
  90. test_random_solarize_op(plot=True)
  91. test_random_solarize_errors()
  92. test_random_solarize_md5()