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.6 kB

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