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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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
  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 test_five_crop_op(plot=False):
  26. """
  27. Test FiveCrop
  28. """
  29. logger.info("test_five_crop")
  30. # First dataset
  31. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  32. transforms_1 = [
  33. vision.Decode(),
  34. vision.ToTensor(),
  35. ]
  36. transform_1 = vision.ComposeOp(transforms_1)
  37. data1 = data1.map(input_columns=["image"], operations=transform_1())
  38. # Second dataset
  39. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  40. transforms_2 = [
  41. vision.Decode(),
  42. vision.FiveCrop(200),
  43. lambda images: np.stack([vision.ToTensor()(image) for image in images]) # 4D stack of 5 images
  44. ]
  45. transform_2 = vision.ComposeOp(transforms_2)
  46. data2 = data2.map(input_columns=["image"], operations=transform_2())
  47. num_iter = 0
  48. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  49. num_iter += 1
  50. image_1 = (item1["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  51. image_2 = item2["image"]
  52. logger.info("shape of image_1: {}".format(image_1.shape))
  53. logger.info("shape of image_2: {}".format(image_2.shape))
  54. logger.info("dtype of image_1: {}".format(image_1.dtype))
  55. logger.info("dtype of image_2: {}".format(image_2.dtype))
  56. if plot:
  57. visualize_list(np.array([image_1]*10), (image_2 * 255).astype(np.uint8).transpose(0, 2, 3, 1))
  58. # The output data should be of a 4D tensor shape, a stack of 5 images.
  59. assert len(image_2.shape) == 4
  60. assert image_2.shape[0] == 5
  61. def test_five_crop_error_msg():
  62. """
  63. Test FiveCrop error message.
  64. """
  65. logger.info("test_five_crop_error_msg")
  66. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  67. transforms = [
  68. vision.Decode(),
  69. vision.FiveCrop(200),
  70. vision.ToTensor()
  71. ]
  72. transform = vision.ComposeOp(transforms)
  73. data = data.map(input_columns=["image"], operations=transform())
  74. with pytest.raises(RuntimeError) as info:
  75. data.create_tuple_iterator().get_next()
  76. error_msg = "TypeError: img should be PIL Image or Numpy array. Got <class 'tuple'>"
  77. # error msg comes from ToTensor()
  78. assert error_msg in str(info.value)
  79. if __name__ == "__main__":
  80. test_five_crop_op(plot=True)
  81. test_five_crop_error_msg()