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_resize.py 5.9 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
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 Resize op in DE
  17. """
  18. import pytest
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.vision.c_transforms as vision
  21. import mindspore.dataset.vision.py_transforms as py_vision
  22. from mindspore.dataset.vision.utils import Inter
  23. from mindspore import log as logger
  24. from util import visualize_list, save_and_check_md5, \
  25. config_get_set_seed, config_get_set_num_parallel_workers
  26. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  27. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  28. GENERATE_GOLDEN = False
  29. def test_resize_op(plot=False):
  30. def test_resize_op_parameters(test_name, size, plot):
  31. """
  32. Test resize_op
  33. """
  34. logger.info("Test resize: {0}".format(test_name))
  35. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  36. # define map operations
  37. decode_op = vision.Decode()
  38. resize_op = vision.Resize(size)
  39. # apply map operations on images
  40. data1 = data1.map(operations=decode_op, input_columns=["image"])
  41. data2 = data1.map(operations=resize_op, input_columns=["image"])
  42. image_original = []
  43. image_resized = []
  44. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  45. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  46. image_1 = item1["image"]
  47. image_2 = item2["image"]
  48. image_original.append(image_1)
  49. image_resized.append(image_2)
  50. if plot:
  51. visualize_list(image_original, image_resized)
  52. test_resize_op_parameters("Test single int for size", 10, plot=False)
  53. test_resize_op_parameters("Test tuple for size", (10, 15), plot=False)
  54. def test_resize_op_ANTIALIAS():
  55. """
  56. Test resize_op
  57. """
  58. logger.info("Test resize for ANTIALIAS")
  59. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  60. # define map operations
  61. decode_op = py_vision.Decode()
  62. resize_op = py_vision.Resize(20, Inter.ANTIALIAS)
  63. # apply map operations on images
  64. data1 = data1.map(operations=[decode_op, resize_op, py_vision.ToTensor()], input_columns=["image"])
  65. num_iter = 0
  66. for _ in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
  67. num_iter += 1
  68. logger.info("use Resize by Inter.ANTIALIAS process {} images.".format(num_iter))
  69. def test_resize_md5(plot=False):
  70. def test_resize_md5_parameters(test_name, size, filename, seed, plot):
  71. """
  72. Test Resize with md5 check
  73. """
  74. logger.info("Test Resize with md5 check: {0}".format(test_name))
  75. original_seed = config_get_set_seed(seed)
  76. original_num_parallel_workers = config_get_set_num_parallel_workers(1)
  77. # Generate dataset
  78. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  79. decode_op = vision.Decode()
  80. resize_op = vision.Resize(size)
  81. data1 = data1.map(operations=decode_op, input_columns=["image"])
  82. data2 = data1.map(operations=resize_op, input_columns=["image"])
  83. image_original = []
  84. image_resized = []
  85. # Compare with expected md5 from images
  86. save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
  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. image_1 = item1["image"]
  90. image_2 = item2["image"]
  91. image_original.append(image_1)
  92. image_resized.append(image_2)
  93. if plot:
  94. visualize_list(image_original, image_resized)
  95. # Restore configuration
  96. ds.config.set_seed(original_seed)
  97. ds.config.set_num_parallel_workers(original_num_parallel_workers)
  98. test_resize_md5_parameters("Test single int for size", 5, "resize_01_result.npz", 5, plot)
  99. test_resize_md5_parameters("Test tuple for size", (5, 7), "resize_02_result.npz", 7, plot)
  100. def test_resize_op_invalid_input():
  101. def test_invalid_input(test_name, size, interpolation, error, error_msg):
  102. logger.info("Test Resize with bad input: {0}".format(test_name))
  103. with pytest.raises(error) as error_info:
  104. vision.Resize(size, interpolation)
  105. assert error_msg in str(error_info.value)
  106. test_invalid_input("invalid size parameter type as a single number", 4.5, Inter.LINEAR, TypeError,
  107. "Size should be a single integer or a list/tuple (h, w) of length 2.")
  108. test_invalid_input("invalid size parameter shape", (2, 3, 4), Inter.LINEAR, TypeError,
  109. "Size should be a single integer or a list/tuple (h, w) of length 2.")
  110. test_invalid_input("invalid size parameter type in a tuple", (2.3, 3), Inter.LINEAR, TypeError,
  111. "Argument size at dim 0 with value 2.3 is not of type (<class 'int'>,)")
  112. test_invalid_input("invalid Interpolation value", (2.3, 3), None, KeyError, "None")
  113. if __name__ == "__main__":
  114. test_resize_op(plot=True)
  115. test_resize_op_ANTIALIAS()
  116. test_resize_md5(plot=True)
  117. test_resize_op_invalid_input()