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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.engine as de
  21. import mindspore.dataset.vision.c_transforms as vision
  22. from mindspore import log as logger
  23. from util import visualize_list, save_and_check_md5, config_get_set_seed, config_get_set_num_parallel_workers, \
  24. visualize_one_channel_dataset
  25. GENERATE_GOLDEN = False
  26. MNIST_DATA_DIR = "../data/dataset/testMnistData"
  27. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  28. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  29. def test_random_solarize_op(threshold=(10, 150), plot=False, run_golden=True):
  30. """
  31. Test RandomSolarize
  32. """
  33. logger.info("Test RandomSolarize")
  34. # First dataset
  35. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  36. decode_op = vision.Decode()
  37. original_seed = config_get_set_seed(0)
  38. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  39. if threshold is None:
  40. solarize_op = vision.RandomSolarize()
  41. else:
  42. solarize_op = vision.RandomSolarize(threshold)
  43. data1 = data1.map(operations=decode_op, input_columns=["image"])
  44. data1 = data1.map(operations=solarize_op, input_columns=["image"])
  45. # Second dataset
  46. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  47. data2 = data2.map(operations=decode_op, input_columns=["image"])
  48. if run_golden:
  49. filename = "random_solarize_01_result.npz"
  50. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  51. image_solarized = []
  52. image = []
  53. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  54. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  55. image_solarized.append(item1["image"].copy())
  56. image.append(item2["image"].copy())
  57. if plot:
  58. visualize_list(image, image_solarized)
  59. ds.config.set_seed(original_seed)
  60. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  61. def test_random_solarize_mnist(plot=False, run_golden=True):
  62. """
  63. Test RandomSolarize op with MNIST dataset (Grayscale images)
  64. """
  65. mnist_1 = de.MnistDataset(dataset_dir=MNIST_DATA_DIR, num_samples=2, shuffle=False)
  66. mnist_2 = de.MnistDataset(dataset_dir=MNIST_DATA_DIR, num_samples=2, shuffle=False)
  67. mnist_2 = mnist_2.map(operations=vision.RandomSolarize((0, 255)), input_columns="image")
  68. images = []
  69. images_trans = []
  70. labels = []
  71. for _, (data_orig, data_trans) in enumerate(zip(mnist_1, mnist_2)):
  72. image_orig, label_orig = data_orig
  73. image_trans, _ = data_trans
  74. images.append(image_orig.asnumpy())
  75. labels.append(label_orig.asnumpy())
  76. images_trans.append(image_trans.asnumpy())
  77. if plot:
  78. visualize_one_channel_dataset(images, images_trans, labels)
  79. if run_golden:
  80. filename = "random_solarize_02_result.npz"
  81. save_and_check_md5(mnist_2, filename, generate_golden=GENERATE_GOLDEN)
  82. def test_random_solarize_errors():
  83. """
  84. Test that RandomSolarize errors with bad input
  85. """
  86. with pytest.raises(ValueError) as error_info:
  87. vision.RandomSolarize((12, 1))
  88. assert "threshold must be in min max format numbers" in str(error_info.value)
  89. with pytest.raises(ValueError) as error_info:
  90. vision.RandomSolarize((12, 1000))
  91. assert "Input is not within the required interval of (0 to 255)." in str(error_info.value)
  92. with pytest.raises(TypeError) as error_info:
  93. vision.RandomSolarize((122.1, 140))
  94. assert "Argument threshold[0] with value 122.1 is not of type (<class 'int'>,)." in str(error_info.value)
  95. with pytest.raises(ValueError) as error_info:
  96. vision.RandomSolarize((122, 100, 30))
  97. assert "threshold must be a sequence of two numbers" in str(error_info.value)
  98. with pytest.raises(ValueError) as error_info:
  99. vision.RandomSolarize((120,))
  100. assert "threshold must be a sequence of two numbers" in str(error_info.value)
  101. if __name__ == "__main__":
  102. test_random_solarize_op((10, 150), plot=True, run_golden=True)
  103. test_random_solarize_op((12, 120), plot=True, run_golden=False)
  104. test_random_solarize_op(plot=True, run_golden=False)
  105. test_random_solarize_mnist(plot=True, run_golden=True)
  106. test_random_solarize_errors()