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_flat_map.py 2.0 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. import numpy as np
  16. import mindspore.dataset as ds
  17. DATA_FILE = "../data/dataset/test_flat_map/images1.txt"
  18. INDEX_FILE = "../data/dataset/test_flat_map/image_index.txt"
  19. def test_flat_map_1():
  20. '''
  21. DATA_FILE records the path of image folders, load the images from them.
  22. '''
  23. def flat_map_func(x):
  24. data_dir = x[0].item().decode('utf8')
  25. d = ds.ImageFolderDataset(data_dir)
  26. return d
  27. data = ds.TextFileDataset(DATA_FILE)
  28. data = data.flat_map(flat_map_func)
  29. count = 0
  30. for d in data.create_tuple_iterator(output_numpy=True):
  31. assert isinstance(d[0], np.ndarray)
  32. count += 1
  33. assert count == 52
  34. def test_flat_map_2():
  35. '''
  36. Flatten 3D structure data
  37. '''
  38. def flat_map_func_1(x):
  39. data_dir = x[0].item().decode('utf8')
  40. d = ds.ImageFolderDataset(data_dir)
  41. return d
  42. def flat_map_func_2(x):
  43. text_file = x[0].item().decode('utf8')
  44. d = ds.TextFileDataset(text_file)
  45. d = d.flat_map(flat_map_func_1)
  46. return d
  47. data = ds.TextFileDataset(INDEX_FILE)
  48. data = data.flat_map(flat_map_func_2)
  49. count = 0
  50. for d in data.create_tuple_iterator(output_numpy=True):
  51. assert isinstance(d[0], np.ndarray)
  52. count += 1
  53. assert count == 104
  54. if __name__ == "__main__":
  55. test_flat_map_1()
  56. test_flat_map_2()