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.

dataset.py 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2020 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. """
  16. create train or eval dataset.
  17. """
  18. import os
  19. import mindspore.common.dtype as mstype
  20. import mindspore.dataset.engine as de
  21. import mindspore.dataset.transforms.vision.c_transforms as C
  22. import mindspore.dataset.transforms.c_transforms as C2
  23. from mindspore.communication.management import init, get_rank, get_group_size
  24. from config import config
  25. def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32, target="Ascend"):
  26. """
  27. create a train or eval dataset
  28. Args:
  29. dataset_path(string): the path of dataset.
  30. do_train(bool): whether dataset is used for train or eval.
  31. repeat_num(int): the repeat times of dataset. Default: 1
  32. batch_size(int): the batch size of dataset. Default: 32
  33. target(str): the device target. Default: Ascend
  34. Returns:
  35. dataset
  36. """
  37. if target == "Ascend":
  38. device_num = int(os.getenv("DEVICE_NUM"))
  39. rank_id = int(os.getenv("RANK_ID"))
  40. else:
  41. init("nccl")
  42. rank_id = get_rank()
  43. device_num = get_group_size()
  44. if device_num == 1:
  45. ds = de.Cifar10Dataset(dataset_path, num_parallel_workers=8, shuffle=True)
  46. else:
  47. ds = de.Cifar10Dataset(dataset_path, num_parallel_workers=8, shuffle=True,
  48. num_shards=device_num, shard_id=rank_id)
  49. # define map operations
  50. trans = []
  51. if do_train:
  52. trans += [
  53. C.RandomCrop((32, 32), (4, 4, 4, 4)),
  54. C.RandomHorizontalFlip(prob=0.5)
  55. ]
  56. trans += [
  57. C.Resize((config.image_height, config.image_width)),
  58. C.Rescale(1.0 / 255.0, 0.0),
  59. C.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),
  60. C.HWC2CHW()
  61. ]
  62. type_cast_op = C2.TypeCast(mstype.int32)
  63. ds = ds.map(input_columns="label", num_parallel_workers=8, operations=type_cast_op)
  64. ds = ds.map(input_columns="image", num_parallel_workers=8, operations=trans)
  65. # apply batch operations
  66. ds = ds.batch(batch_size, drop_remainder=True)
  67. # apply dataset repeat operation
  68. ds = ds.repeat(repeat_num)
  69. return ds