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

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