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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 matplotlib.pyplot as plt
  18. import numpy as np
  19. import pytest
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.vision.py_transforms as vision
  22. from mindspore import log as logger
  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. def visualize(image_1, image_2):
  26. """
  27. visualizes the image using FiveCrop
  28. """
  29. plt.subplot(161)
  30. plt.imshow(image_1)
  31. plt.title("Original")
  32. for i, image in enumerate(image_2):
  33. image = (image.transpose(1, 2, 0) * 255).astype(np.uint8)
  34. plt.subplot(162 + i)
  35. plt.imshow(image)
  36. plt.title("image {} in FiveCrop".format(i + 1))
  37. plt.show()
  38. def skip_test_five_crop_op():
  39. """
  40. Test FiveCrop
  41. """
  42. logger.info("test_five_crop")
  43. # First dataset
  44. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  45. transforms_1 = [
  46. vision.Decode(),
  47. vision.ToTensor(),
  48. ]
  49. transform_1 = vision.ComposeOp(transforms_1)
  50. data1 = data1.map(input_columns=["image"], operations=transform_1())
  51. # Second dataset
  52. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  53. transforms_2 = [
  54. vision.Decode(),
  55. vision.FiveCrop(200),
  56. lambda images: np.stack([vision.ToTensor()(image) for image in images]) # 4D stack of 5 images
  57. ]
  58. transform_2 = vision.ComposeOp(transforms_2)
  59. data2 = data2.map(input_columns=["image"], operations=transform_2())
  60. num_iter = 0
  61. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  62. num_iter += 1
  63. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  64. image_2 = item2["image"]
  65. logger.info("shape of image_1: {}".format(image_1.shape))
  66. logger.info("shape of image_2: {}".format(image_2.shape))
  67. logger.info("dtype of image_1: {}".format(image_1.dtype))
  68. logger.info("dtype of image_2: {}".format(image_2.dtype))
  69. # visualize(image_1, image_2)
  70. # The output data should be of a 4D tensor shape, a stack of 5 images.
  71. assert len(image_2.shape) == 4
  72. assert image_2.shape[0] == 5
  73. def test_five_crop_error_msg():
  74. """
  75. Test FiveCrop error message.
  76. """
  77. logger.info("test_five_crop_error_msg")
  78. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  79. transforms = [
  80. vision.Decode(),
  81. vision.FiveCrop(200),
  82. vision.ToTensor()
  83. ]
  84. transform = vision.ComposeOp(transforms)
  85. data = data.map(input_columns=["image"], operations=transform())
  86. with pytest.raises(RuntimeError) as info:
  87. data.create_tuple_iterator().get_next()
  88. error_msg = "TypeError: img should be PIL Image or Numpy array. Got <class 'tuple'>"
  89. # error msg comes from ToTensor()
  90. assert error_msg in str(info.value)
  91. if __name__ == "__main__":
  92. test_five_crop_op()
  93. test_five_crop_error_msg()