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.

pyfuncmap.py 4.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 as ds
  16. DATA_DIR = ["./data.data"]
  17. SCHEMA_DIR = "./schema.json"
  18. def test_case_0():
  19. """
  20. Test PyFunc
  21. """
  22. print("Test 1-1 PyFunc : lambda x : x + x")
  23. col = "col0"
  24. # apply dataset operations
  25. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  26. ds1 = ds1.map(input_columns=col, output_columns="out", operations=(lambda x: x + x))
  27. print("************** Output Tensor *****************")
  28. for data in ds1.create_dict_iterator(): # each data is a dictionary
  29. # in this example, each dictionary has keys "image" and "label"
  30. print(data["out"])
  31. print("************** Output Tensor *****************")
  32. def test_case_1():
  33. """
  34. Test PyFunc
  35. """
  36. print("Test 1-n PyFunc : (lambda x : (x , x + x)) ")
  37. col = "col0"
  38. # apply dataset operations
  39. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  40. ds1 = ds1.map(input_columns=col, output_columns=["out0", "out1"], operations=(lambda x: (x, x + x)))
  41. print("************** Output Tensor *****************")
  42. for data in ds1.create_dict_iterator(): # each data is a dictionary
  43. # in this example, each dictionary has keys "image" and "label"
  44. print("out0")
  45. print(data["out0"])
  46. print("out1")
  47. print(data["out1"])
  48. print("************** Output Tensor *****************")
  49. def test_case_2():
  50. """
  51. Test PyFunc
  52. """
  53. print("Test n-1 PyFunc : (lambda x, y : x + y) ")
  54. col = ["col0", "col1"]
  55. # apply dataset operations
  56. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  57. ds1 = ds1.map(input_columns=col, output_columns="out", operations=(lambda x, y: x + y))
  58. print("************** Output Tensor *****************")
  59. for data in ds1.create_dict_iterator(): # each data is a dictionary
  60. # in this example, each dictionary has keys "image" and "label"
  61. print(data["out"])
  62. print("************** Output Tensor *****************")
  63. def test_case_3():
  64. """
  65. Test PyFunc
  66. """
  67. print("Test n-m PyFunc : (lambda x, y : (x , x + 1, x + y)")
  68. col = ["col0", "col1"]
  69. # apply dataset operations
  70. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  71. ds1 = ds1.map(input_columns=col, output_columns=["out0", "out1", "out2"],
  72. operations=(lambda x, y: (x, x + y, x + x + y)))
  73. print("************** Output Tensor *****************")
  74. for data in ds1.create_dict_iterator(): # each data is a dictionary
  75. # in this example, each dictionary has keys "image" and "label"
  76. print("out0")
  77. print(data["out0"])
  78. print("out1")
  79. print(data["out1"])
  80. print("out2")
  81. print(data["out2"])
  82. print("************** Output Tensor *****************")
  83. def test_case_4():
  84. """
  85. Test PyFunc
  86. """
  87. print("Test Parallel n-m PyFunc : (lambda x, y : (x , x + 1, x + y)")
  88. col = ["col0", "col1"]
  89. # apply dataset operations
  90. ds1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  91. ds1 = ds1.map(input_columns=col, output_columns=["out0", "out1", "out2"], num_parallel_workers=4,
  92. operations=(lambda x, y: (x, x + y, x + x + y)))
  93. print("************** Output Tensor *****************")
  94. for data in ds1.create_dict_iterator(): # each data is a dictionary
  95. # in this example, each dictionary has keys "image" and "label"
  96. print("out0")
  97. print(data["out0"])
  98. print("out1")
  99. print(data["out1"])
  100. print("out2")
  101. print(data["out2"])
  102. print("************** Output Tensor *****************")
  103. if __name__ == "__main__":
  104. test_case_0()
  105. # test_case_1()
  106. # test_case_2()
  107. # test_case_3()
  108. # test_case_4()