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_repeat.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. from util import save_and_check
  17. import mindspore.dataset as ds
  18. from mindspore import log as logger
  19. DATA_DIR_TF = ["../data/dataset/testTFTestAllTypes/test.data"]
  20. SCHEMA_DIR_TF = "../data/dataset/testTFTestAllTypes/datasetSchema.json"
  21. COLUMNS_TF = ["col_1d", "col_2d", "col_3d", "col_binary", "col_float",
  22. "col_sint16", "col_sint32", "col_sint64"]
  23. GENERATE_GOLDEN = False
  24. # Data for CIFAR and MNIST are not part of build tree
  25. # They need to be downloaded directly
  26. # prep_data.py can be exuted or code below
  27. # import sys
  28. # sys.path.insert(0,"../../data")
  29. # import prep_data
  30. # prep_data.download_all_for_test("../../data")
  31. IMG_DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  32. IMG_SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  33. DATA_DIR_TF2 = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  34. SCHEMA_DIR_TF2 = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  35. def test_tf_repeat_01():
  36. """
  37. a simple repeat operation.
  38. """
  39. logger.info("Test Simple Repeat")
  40. # define parameters
  41. repeat_count = 2
  42. parameters = {"params": {'repeat_count': repeat_count}}
  43. # apply dataset operations
  44. data1 = ds.TFRecordDataset(DATA_DIR_TF, SCHEMA_DIR_TF, shuffle=False)
  45. data1 = data1.repeat(repeat_count)
  46. filename = "repeat_result.npz"
  47. save_and_check(data1, parameters, filename, generate_golden=GENERATE_GOLDEN)
  48. def test_tf_repeat_02():
  49. """
  50. a simple repeat operation to tes infinite
  51. """
  52. logger.info("Test Infinite Repeat")
  53. # define parameters
  54. repeat_count = -1
  55. # apply dataset operations
  56. data1 = ds.TFRecordDataset(DATA_DIR_TF, SCHEMA_DIR_TF, shuffle=False)
  57. data1 = data1.repeat(repeat_count)
  58. itr = 0
  59. for _ in data1:
  60. itr = itr + 1
  61. if itr == 100:
  62. break
  63. assert itr == 100
  64. def test_tf_repeat_03():
  65. '''repeat and batch '''
  66. data1 = ds.TFRecordDataset(DATA_DIR_TF2, SCHEMA_DIR_TF2, shuffle=False)
  67. batch_size = 32
  68. resize_height, resize_width = 32, 32
  69. decode_op = vision.Decode()
  70. resize_op = vision.Resize((resize_height, resize_width), interpolation=ds.transforms.vision.Inter.LINEAR)
  71. data1 = data1.map(input_columns=["image"], operations=decode_op)
  72. data1 = data1.map(input_columns=["image"], operations=resize_op)
  73. data1 = data1.repeat(22)
  74. data1 = data1.batch(batch_size, drop_remainder=True)
  75. num_iter = 0
  76. for item in data1.create_dict_iterator():
  77. num_iter += 1
  78. logger.info("Number of tf data in data1: {}".format(num_iter))
  79. assert num_iter == 2
  80. if __name__ == "__main__":
  81. logger.info("--------test tf repeat 01---------")
  82. # test_repeat_01()
  83. logger.info("--------test tf repeat 02---------")
  84. # test_repeat_02()
  85. logger.info("--------test tf repeat 03---------")
  86. test_tf_repeat_03()