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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright 2019 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. import mindspore.dataset.transforms.vision.c_transforms as vision
  16. import mindspore.dataset.transforms.vision.py_transforms as py_vision
  17. import numpy as np
  18. import matplotlib.pyplot as plt
  19. import mindspore.dataset as ds
  20. from mindspore import log as logger
  21. from util import diff_mse, visualize, save_and_check_md5
  22. GENERATE_GOLDEN = False
  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_center_crop_op(height=375, width=375, plot=False):
  26. """
  27. Test random_vertical
  28. """
  29. logger.info("Test CenterCrop")
  30. # First dataset
  31. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  32. decode_op = vision.Decode()
  33. # 3 images [375, 500] [600, 500] [512, 512]
  34. center_crop_op = vision.CenterCrop([height, width])
  35. data1 = data1.map(input_columns=["image"], operations=decode_op)
  36. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  37. # Second dataset
  38. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  39. data2 = data2.map(input_columns=["image"], operations=decode_op)
  40. image_cropped = []
  41. image = []
  42. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  43. image_cropped.append(item1["image"].copy())
  44. image.append(item2["image"].copy())
  45. if plot:
  46. visualize(image, image_cropped)
  47. def test_center_crop_md5(height=375, width=375):
  48. """
  49. Test random_vertical
  50. """
  51. logger.info("Test CenterCrop")
  52. # First dataset
  53. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle =False)
  54. decode_op = vision.Decode()
  55. # 3 images [375, 500] [600, 500] [512, 512]
  56. center_crop_op = vision.CenterCrop([height, width])
  57. data1 = data1.map(input_columns=["image"], operations=decode_op)
  58. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  59. # expected md5 from images
  60. filename = "test_center_crop_01_result.npz"
  61. parameters = {"params": {}}
  62. save_and_check_md5(data1, parameters, filename, generate_golden=GENERATE_GOLDEN)
  63. def test_center_crop_comp(height=375, width=375, plot=False):
  64. """
  65. Test random_vertical between python and c image augmentation
  66. """
  67. logger.info("Test CenterCrop")
  68. # First dataset
  69. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  70. decode_op = vision.Decode()
  71. center_crop_op = vision.CenterCrop([height, width])
  72. data1 = data1.map(input_columns=["image"], operations=decode_op)
  73. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  74. # Second dataset
  75. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  76. transforms = [
  77. py_vision.Decode(),
  78. py_vision.CenterCrop([height, width]),
  79. py_vision.ToTensor()
  80. ]
  81. transform = py_vision.ComposeOp(transforms)
  82. data2 = data2.map(input_columns=["image"], operations=transform())
  83. image_cropped = []
  84. image = []
  85. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  86. c_image = item1["image"]
  87. py_image = (item2["image"].transpose(1, 2, 0) * 255).astype(np.uint8)
  88. # the images aren't exactly the same due to rouding error
  89. assert (diff_mse(py_image, c_image) < 0.001)
  90. image_cropped.append(item1["image"].copy())
  91. image.append(item2["image"].copy())
  92. if plot:
  93. visualize(image, image_cropped)
  94. if __name__ == "__main__":
  95. test_center_crop_op(600, 600)
  96. test_center_crop_op(300, 600)
  97. test_center_crop_op(600, 300)
  98. test_center_crop_md5(600, 600)
  99. test_center_crop_comp()