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

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. import mindspore.dataset.transforms.text.utils as nlp
  24. def flat_map_func(x):
  25. data_dir = x[0].item().decode('utf8')
  26. d = ds.ImageFolderDatasetV2(data_dir)
  27. return d
  28. data = ds.TextFileDataset(DATA_FILE)
  29. data = data.flat_map(flat_map_func)
  30. count = 0
  31. for d in data:
  32. assert isinstance(d[0], np.ndarray)
  33. count += 1
  34. assert count == 52
  35. def test_flat_map_2():
  36. '''
  37. Flatten 3D structure data
  38. '''
  39. import mindspore.dataset.transforms.text.utils as nlp
  40. def flat_map_func_1(x):
  41. data_dir = x[0].item().decode('utf8')
  42. d = ds.ImageFolderDatasetV2(data_dir)
  43. return d
  44. def flat_map_func_2(x):
  45. text_file = x[0].item().decode('utf8')
  46. d = ds.TextFileDataset(text_file)
  47. d = d.flat_map(flat_map_func_1)
  48. return d
  49. data = ds.TextFileDataset(INDEX_FILE)
  50. data = data.flat_map(flat_map_func_2)
  51. count = 0
  52. for d in data:
  53. assert isinstance(d[0], np.ndarray)
  54. count += 1
  55. assert count == 104
  56. if __name__ == "__main__":
  57. test_flat_map_1()
  58. test_flat_map_2()