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 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. def test_offload_normalize_op():
  102. """
  103. Feature: test map offload Normalize op.
  104. Description: Input is image dataset.
  105. Expectation: Output should be same with activated or deactivated offload for Normalize op.
  106. """
  107. mean = [0.485 * 255, 0.456 * 255, 0.406 * 255]
  108. std = [0.229 * 255, 0.224 * 255, 0.225 * 255]
  109. # Dataset with offload activated.
  110. dataset_0 = ds.ImageFolderDataset(DATA_DIR)
  111. dataset_0 = dataset_0.map(operations=[C.Decode()], input_columns="image")
  112. dataset_0 = dataset_0.map(operations=[C.Normalize(mean=mean, std=std)], input_columns="image", offload=True)
  113. dataset_0 = dataset_0.map(operations=[C.HWC2CHW()], input_columns="image", offload=True)
  114. dataset_0 = dataset_0.batch(8, drop_remainder=True)
  115. # Dataset with offload not activated.
  116. dataset_1 = ds.ImageFolderDataset(DATA_DIR)
  117. dataset_1 = dataset_1.map(operations=[C.Decode()], input_columns="image")
  118. dataset_1 = dataset_1.map(operations=[C.Normalize(mean=mean, std=std)], input_columns="image")
  119. dataset_1 = dataset_1.map(operations=[C.HWC2CHW()], input_columns="image")
  120. dataset_1 = dataset_1.batch(8, drop_remainder=True)
  121. for (img_0, _), (img_1, _) in zip(dataset_0.create_tuple_iterator(num_epochs=1, output_numpy=True),
  122. dataset_1.create_tuple_iterator(num_epochs=1, output_numpy=True)):
  123. np.testing.assert_almost_equal(img_0, img_1, decimal=6)
  124. def test_offload_rescale_op():
  125. """
  126. Feature: test map offload Rescale op.
  127. Description: Input is image dataset.
  128. Expectation: Output should be same with activated or deactivated offload for Rescale op.
  129. """
  130. rescale = 1.0 / 255.0
  131. shift = 0.0
  132. # Dataset with offload activated.
  133. dataset_0 = ds.ImageFolderDataset(DATA_DIR)
  134. dataset_0 = dataset_0.map(operations=[C.Decode()], input_columns="image")
  135. dataset_0 = dataset_0.map(operations=[C.Rescale(rescale, shift)], input_columns="image", offload=True)
  136. dataset_0 = dataset_0.map(operations=[C.HWC2CHW()], input_columns="image", offload=True)
  137. dataset_0 = dataset_0.batch(8, drop_remainder=True)
  138. # Dataset with offload not activated.
  139. dataset_1 = ds.ImageFolderDataset(DATA_DIR)
  140. dataset_1 = dataset_1.map(operations=[C.Decode()], input_columns="image")
  141. dataset_1 = dataset_1.map(operations=[C.Rescale(rescale, shift)], input_columns="image")
  142. dataset_1 = dataset_1.map(operations=[C.HWC2CHW()], input_columns="image")
  143. dataset_1 = dataset_1.batch(8, drop_remainder=True)
  144. for (img_0, _), (img_1, _) in zip(dataset_0.create_tuple_iterator(num_epochs=1, output_numpy=True),
  145. dataset_1.create_tuple_iterator(num_epochs=1, output_numpy=True)):
  146. np.testing.assert_almost_equal(img_0, img_1, decimal=6)
  147. if __name__ == "__main__":
  148. test_offload()
  149. test_auto_offload()
  150. test_offload_concat_dataset_1()
  151. test_offload_concat_dataset_2()
  152. test_offload_normalize_op()
  153. test_offload_rescale_op()