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_map_offload.py 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright 2021 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. import mindspore.dataset.vision.c_transforms as C
  19. DATA_DIR = "../data/dataset/testPK/data"
  20. def test_offload():
  21. """
  22. Feature: test map offload flag.
  23. Description: Input is image dataset.
  24. Expectation: Output should be same with activated or deactivated offload.
  25. """
  26. # Dataset with offload activated.
  27. dataset_0 = ds.ImageFolderDataset(DATA_DIR)
  28. dataset_0 = dataset_0.map(operations=[C.Decode()], input_columns="image")
  29. dataset_0 = dataset_0.map(operations=[C.HWC2CHW()], input_columns="image", offload=True)
  30. dataset_0 = dataset_0.batch(8, drop_remainder=True)
  31. # Dataset with offload not activated.
  32. dataset_1 = ds.ImageFolderDataset(DATA_DIR)
  33. dataset_1 = dataset_1.map(operations=[C.Decode()], input_columns="image")
  34. dataset_1 = dataset_1.map(operations=[C.HWC2CHW()], input_columns="image")
  35. dataset_1 = dataset_1.batch(8, drop_remainder=True)
  36. for (img_0, _), (img_1, _) in zip(dataset_0.create_tuple_iterator(num_epochs=1, output_numpy=True),
  37. dataset_1.create_tuple_iterator(num_epochs=1, output_numpy=True)):
  38. np.testing.assert_array_equal(img_0, img_1)
  39. def test_auto_offload():
  40. """
  41. Feature: Test auto_offload config option.
  42. Description: Input is image dataset.
  43. Expectation: Output should same with auto_offload activated and deactivated.
  44. """
  45. trans = [C.Decode(), C.HWC2CHW()]
  46. # Dataset with config.auto_offload not activated
  47. dataset_auto_disabled = ds.ImageFolderDataset(DATA_DIR)
  48. dataset_auto_disabled = dataset_auto_disabled.map(operations=trans, input_columns="image")
  49. dataset_auto_disabled = dataset_auto_disabled.batch(8, drop_remainder=True)
  50. # Dataset with config.auto_offload activated
  51. ds.config.set_auto_offload(True)
  52. dataset_auto_enabled = ds.ImageFolderDataset(DATA_DIR)
  53. dataset_auto_enabled = dataset_auto_enabled.map(operations=trans, input_columns="image")
  54. dataset_auto_enabled = dataset_auto_enabled.batch(8, drop_remainder=True)
  55. for (img_0, _), (img_1, _) in zip(dataset_auto_disabled.create_tuple_iterator(num_epochs=1, output_numpy=True),
  56. dataset_auto_enabled.create_tuple_iterator(num_epochs=1, output_numpy=True)):
  57. np.testing.assert_array_equal(img_0, img_1)
  58. # Need to turn off here or subsequent test cases will fail.
  59. ds.config.set_auto_offload(False)
  60. def test_offload_concat_dataset_1():
  61. """
  62. Feature: test map offload flag for concatenated dataset.
  63. Description: Input is image dataset.
  64. Expectation: Should raise RuntimeError.
  65. """
  66. # Dataset with offload activated.
  67. dataset_0 = ds.ImageFolderDataset(DATA_DIR)
  68. dataset_0 = dataset_0.map(operations=[C.Decode()], input_columns="image")
  69. dataset_0 = dataset_0.map(operations=[C.HWC2CHW()], input_columns="image", offload=True)
  70. dataset_0 = dataset_0.batch(8, drop_remainder=True)
  71. # Dataset with offload not activated.
  72. dataset_1 = ds.ImageFolderDataset(DATA_DIR)
  73. dataset_1 = dataset_1.map(operations=[C.Decode()], input_columns="image")
  74. dataset_1 = dataset_1.map(operations=[C.HWC2CHW()], input_columns="image")
  75. dataset_1 = dataset_1.batch(8, drop_remainder=True)
  76. dataset_concat = dataset_0 + dataset_1
  77. error_msg = "Offload module currently does not support concatenated or zipped datasets."
  78. with pytest.raises(RuntimeError, match=error_msg):
  79. for (_, _) in dataset_concat.create_tuple_iterator(num_epochs=1, output_numpy=True):
  80. continue
  81. def test_offload_concat_dataset_2():
  82. """
  83. Feature: test map offload flag for concatenated dataset.
  84. Description: Input is image dataset.
  85. Expectation: Should raise RuntimeError.
  86. """
  87. # Dataset with offload activated.
  88. dataset_0 = ds.ImageFolderDataset(DATA_DIR)
  89. dataset_0 = dataset_0.map(operations=[C.Decode()], input_columns="image")
  90. dataset_0 = dataset_0.map(operations=[C.HWC2CHW()], input_columns="image", offload=True)
  91. # Dataset with offload not activated.
  92. dataset_1 = ds.ImageFolderDataset(DATA_DIR)
  93. dataset_1 = dataset_1.map(operations=[C.Decode()], input_columns="image")
  94. dataset_1 = dataset_1.map(operations=[C.HWC2CHW()], input_columns="image")
  95. dataset_concat = dataset_0 + dataset_1
  96. dataset_concat = dataset_concat.batch(8, drop_remainder=True)
  97. error_msg = "Offload module currently does not support concatenated or zipped datasets."
  98. with pytest.raises(RuntimeError, match=error_msg):
  99. for (_, _) in dataset_concat.create_tuple_iterator(num_epochs=1, output_numpy=True):
  100. continue
  101. if __name__ == "__main__":
  102. test_offload()
  103. test_auto_offload()
  104. test_offload_concat_dataset_1()
  105. test_offload_concat_dataset_2()