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_five_crop.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. Testing FiveCrop in DE
  16. """
  17. import pytest
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.py_transforms
  21. import mindspore.dataset.vision.py_transforms as vision
  22. from mindspore import log as logger
  23. from util import visualize_list, save_and_check_md5
  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. GENERATE_GOLDEN = False
  27. def test_five_crop_op(plot=False):
  28. """
  29. Test FiveCrop
  30. """
  31. logger.info("test_five_crop")
  32. # First dataset
  33. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  34. transforms_1 = [
  35. vision.Decode(),
  36. vision.ToTensor(),
  37. ]
  38. transform_1 = mindspore.dataset.transforms.py_transforms.Compose(transforms_1)
  39. data1 = data1.map(operations=transform_1, input_columns=["image"])
  40. # Second dataset
  41. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  42. transforms_2 = [
  43. vision.Decode(),
  44. vision.FiveCrop(200),
  45. lambda *images: np.stack([vision.ToTensor()(image) for image in images]) # 4D stack of 5 images
  46. ]
  47. transform_2 = mindspore.dataset.transforms.py_transforms.Compose(transforms_2)
  48. data2 = data2.map(operations=transform_2, input_columns=["image"])
  49. num_iter = 0
  50. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  51. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  52. num_iter += 1
  53. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  54. image_2 = item2["image"]
  55. logger.info("shape of image_1: {}".format(image_1.shape))
  56. logger.info("shape of image_2: {}".format(image_2.shape))
  57. logger.info("dtype of image_1: {}".format(image_1.dtype))
  58. logger.info("dtype of image_2: {}".format(image_2.dtype))
  59. if plot:
  60. visualize_list(np.array([image_1]*5), (image_2 * 255).astype(np.uint8).transpose(0, 2, 3, 1))
  61. # The output data should be of a 4D tensor shape, a stack of 5 images.
  62. assert len(image_2.shape) == 4
  63. assert image_2.shape[0] == 5
  64. def test_five_crop_error_msg():
  65. """
  66. Test FiveCrop error message.
  67. """
  68. logger.info("test_five_crop_error_msg")
  69. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  70. transforms = [
  71. vision.Decode(),
  72. vision.FiveCrop(200),
  73. vision.ToTensor()
  74. ]
  75. transform = mindspore.dataset.transforms.py_transforms.Compose(transforms)
  76. data = data.map(operations=transform, input_columns=["image"])
  77. with pytest.raises(RuntimeError) as info:
  78. for _ in data:
  79. pass
  80. error_msg = "TypeError: __call__() takes 2 positional arguments but 6 were given"
  81. # error msg comes from ToTensor()
  82. assert error_msg in str(info.value)
  83. def test_five_crop_md5():
  84. """
  85. Test FiveCrop with md5 check
  86. """
  87. logger.info("test_five_crop_md5")
  88. # First dataset
  89. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  90. transforms = [
  91. vision.Decode(),
  92. vision.FiveCrop(100),
  93. lambda *images: np.stack([vision.ToTensor()(image) for image in images]) # 4D stack of 5 images
  94. ]
  95. transform = mindspore.dataset.transforms.py_transforms.Compose(transforms)
  96. data = data.map(operations=transform, input_columns=["image"])
  97. # Compare with expected md5 from images
  98. filename = "five_crop_01_result.npz"
  99. save_and_check_md5(data, filename, generate_golden=GENERATE_GOLDEN)
  100. if __name__ == "__main__":
  101. test_five_crop_op(plot=True)
  102. test_five_crop_error_msg()
  103. test_five_crop_md5()