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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32):
  24. """
  25. create a train or eval dataset
  26. Args:
  27. dataset_path(string): the path of dataset.
  28. do_train(bool): whether dataset is used for train or eval.
  29. repeat_num(int): the repeat times of dataset. Default: 1
  30. batch_size(int): the batch size of dataset. Default: 32
  31. Returns:
  32. dataset
  33. """
  34. device_num = int(os.getenv("DEVICE_NUM"))
  35. rank_id = int(os.getenv("RANK_ID"))
  36. if device_num == 1:
  37. ds = de.ImageFolderDatasetV2(dataset_path, num_parallel_workers=8, shuffle=True)
  38. else:
  39. ds = de.ImageFolderDatasetV2(dataset_path, num_parallel_workers=8, shuffle=True,
  40. num_shards=device_num, shard_id=rank_id)
  41. image_size = 224
  42. mean = [0.485 * 255, 0.456 * 255, 0.406 * 255]
  43. std = [0.229 * 255, 0.224 * 255, 0.225 * 255]
  44. # define map operations
  45. if do_train:
  46. trans = [
  47. C.RandomCropDecodeResize(image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)),
  48. C.RandomHorizontalFlip(prob=0.5),
  49. C.Normalize(mean=mean, std=std),
  50. C.HWC2CHW()
  51. ]
  52. else:
  53. trans = [
  54. C.Decode(),
  55. C.Resize((256, 256)),
  56. C.CenterCrop(image_size),
  57. C.Normalize(mean=mean, std=std),
  58. C.HWC2CHW()
  59. ]
  60. type_cast_op = C2.TypeCast(mstype.int32)
  61. ds = ds.map(input_columns="image", num_parallel_workers=8, operations=trans)
  62. ds = ds.map(input_columns="label", num_parallel_workers=8, operations=type_cast_op)
  63. # apply batch operations
  64. ds = ds.batch(batch_size, drop_remainder=True)
  65. # apply dataset repeat operation
  66. ds = ds.repeat(repeat_num)
  67. return ds