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_random_dataset.py 2.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.common.dtype as mstype
  16. import mindspore.dataset as ds
  17. from mindspore import log as logger
  18. # just a basic test with parallel random data op
  19. def test_randomdataset_basic1():
  20. logger.info("Test randomdataset basic")
  21. schema = ds.Schema()
  22. schema.add_column('image', de_type=mstype.uint8, shape=[2])
  23. schema.add_column('label', de_type=mstype.uint8, shape=[1])
  24. # apply dataset operations
  25. ds1 = ds.RandomDataset(schema=schema, num_samples=50, num_parallel_workers=4)
  26. ds1 = ds1.repeat(4)
  27. num_iter = 0
  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. logger.info("{} image: {}".format(num_iter, data["image"]))
  31. logger.info("{} label: {}".format(num_iter, data["label"]))
  32. num_iter += 1
  33. logger.info("Number of data in ds1: ", num_iter)
  34. assert num_iter == 200
  35. # Another simple test
  36. def test_randomdataset_basic2():
  37. logger.info("Test randomdataset basic 2")
  38. schema = ds.Schema()
  39. schema.add_column('image', de_type=mstype.uint8,
  40. shape=[640, 480, 3]) # 921600 bytes (a bit less than 1 MB per image)
  41. schema.add_column('label', de_type=mstype.uint8, shape=[1])
  42. # Make up about 10 samples
  43. ds1 = ds.RandomDataset(schema=schema, num_samples=10, num_parallel_workers=1)
  44. # cache size allows for about 4 images since each image just a bit less than 1MB, after that we will have to spill
  45. ds1 = ds1.repeat(4)
  46. num_iter = 0
  47. for data in ds1.create_dict_iterator(): # each data is a dictionary
  48. # in this example, each dictionary has keys "image" and "label"
  49. # logger.info(data["image"])
  50. logger.info("printing the label: {}".format(data["label"]))
  51. num_iter += 1
  52. logger.info("Number of data in ds1: ", num_iter)
  53. assert num_iter == 40
  54. if __name__ == '__main__':
  55. test_randomdataset_basic1()
  56. test_randomdataset_basic2()
  57. logger.info('test_randomdataset_basic Ended.\n')