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

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