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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 numpy as np
  17. import matplotlib.pyplot as plt
  18. import mindspore.dataset as ds
  19. from mindspore import log as logger
  20. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  21. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  22. def visualize(image_original, image_cropped):
  23. """
  24. visualizes the image using DE op and Numpy op
  25. """
  26. num = len(image_cropped)
  27. for i in range(num):
  28. plt.subplot(2, num, i + 1)
  29. plt.imshow(image_original[i])
  30. plt.title("Original image")
  31. plt.subplot(2, num, i + num + 1)
  32. plt.imshow(image_cropped[i])
  33. plt.title("DE center_crop image")
  34. plt.show()
  35. def test_center_crop_op(height=375, width=375, plot=False):
  36. """
  37. Test random_vertical
  38. """
  39. logger.info("Test CenterCrop")
  40. # First dataset
  41. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  42. decode_op = vision.Decode()
  43. # 3 images [375, 500] [600, 500] [512, 512]
  44. center_crop_op = vision.CenterCrop(height, width)
  45. data1 = data1.map(input_columns=["image"], operations=decode_op)
  46. data1 = data1.map(input_columns=["image"], operations=center_crop_op)
  47. # Second dataset
  48. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
  49. data2 = data2.map(input_columns=["image"], operations=decode_op)
  50. image_cropped = []
  51. image = []
  52. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  53. image_cropped.append(item1["image"].copy())
  54. image.append(item2["image"].copy())
  55. if plot:
  56. visualize(image, image_cropped)
  57. if __name__ == "__main__":
  58. test_center_crop_op()
  59. test_center_crop_op(600, 600)
  60. test_center_crop_op(300, 600)
  61. test_center_crop_op(600, 300)