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_iterator.py 3.8 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 numpy as np
  16. import pytest
  17. import mindspore.dataset as ds
  18. from mindspore.dataset.engine.iterators import ITERATORS_LIST, _cleanup
  19. DATA_DIR = ["../data/dataset/testTFTestAllTypes/test.data"]
  20. SCHEMA_DIR = "../data/dataset/testTFTestAllTypes/datasetSchema.json"
  21. COLUMNS = ["col_1d", "col_2d", "col_3d", "col_binary", "col_float",
  22. "col_sint16", "col_sint32", "col_sint64"]
  23. def check(project_columns):
  24. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=COLUMNS, shuffle=False)
  25. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=project_columns, shuffle=False)
  26. for data_actual, data_expected in zip(data1.create_tuple_iterator(project_columns, num_epochs=1),
  27. data2.create_tuple_iterator(num_epochs=1)):
  28. assert len(data_actual) == len(data_expected)
  29. assert all([np.array_equal(d1, d2) for d1, d2 in zip(data_actual, data_expected)])
  30. def test_iterator_create_tuple():
  31. """
  32. Test creating tuple iterator
  33. """
  34. check(COLUMNS)
  35. check(COLUMNS[0:1])
  36. check(COLUMNS[0:2])
  37. check(COLUMNS[0:7])
  38. check(COLUMNS[7:8])
  39. check(COLUMNS[0:2:8])
  40. def test_iterator_weak_ref():
  41. ITERATORS_LIST.clear()
  42. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR)
  43. itr1 = data.create_tuple_iterator(num_epochs=1)
  44. itr2 = data.create_tuple_iterator(num_epochs=1)
  45. itr3 = data.create_tuple_iterator(num_epochs=1)
  46. assert len(ITERATORS_LIST) == 3
  47. assert sum(itr() is not None for itr in ITERATORS_LIST) == 3
  48. del itr1
  49. assert len(ITERATORS_LIST) == 3
  50. assert sum(itr() is not None for itr in ITERATORS_LIST) == 2
  51. del itr2
  52. assert len(ITERATORS_LIST) == 3
  53. assert sum(itr() is not None for itr in ITERATORS_LIST) == 1
  54. del itr3
  55. assert len(ITERATORS_LIST) == 3
  56. assert sum(itr() is not None for itr in ITERATORS_LIST) == 0
  57. itr1 = data.create_tuple_iterator(num_epochs=1)
  58. itr2 = data.create_tuple_iterator(num_epochs=1)
  59. itr3 = data.create_tuple_iterator(num_epochs=1)
  60. _cleanup()
  61. with pytest.raises(AttributeError) as info:
  62. itr2.__next__()
  63. assert "object has no attribute 'depipeline'" in str(info.value)
  64. del itr1
  65. assert len(ITERATORS_LIST) == 6
  66. assert sum(itr() is not None for itr in ITERATORS_LIST) == 2
  67. _cleanup()
  68. class MyDict(dict):
  69. def __getattr__(self, key):
  70. return self[key]
  71. def __setattr__(self, key, value):
  72. self[key] = value
  73. def __call__(self, t):
  74. return t
  75. def test_tree_copy():
  76. """
  77. Testing copying the tree with a pyfunc that cannot be pickled
  78. """
  79. data = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=COLUMNS)
  80. data1 = data.map(operations=[MyDict()])
  81. itr = data1.create_tuple_iterator(num_epochs=1)
  82. assert id(data1) != id(itr.dataset)
  83. assert id(data) != id(itr.dataset.children[0])
  84. assert id(data1.operations[0]) == id(itr.dataset.operations[0])
  85. itr.release()
  86. if __name__ == '__main__':
  87. test_iterator_create_tuple()
  88. test_iterator_weak_ref()
  89. test_tree_copy()