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 3.5 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, config, platform, 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. if platform == "Ascend":
  35. rank_size = int(os.getenv("RANK_SIZE"))
  36. rank_id = int(os.getenv("RANK_ID"))
  37. if rank_size == 1:
  38. ds = de.ImageFolderDatasetV2(dataset_path, num_parallel_workers=8, shuffle=True)
  39. else:
  40. ds = de.ImageFolderDatasetV2(dataset_path, num_parallel_workers=8, shuffle=True,
  41. num_shards=rank_size, shard_id=rank_id)
  42. elif platform == "GPU":
  43. if do_train:
  44. from mindspore.communication.management import get_rank, get_group_size
  45. ds = de.ImageFolderDatasetV2(dataset_path, num_parallel_workers=8, shuffle=True,
  46. num_shards=get_group_size(), shard_id=get_rank())
  47. else:
  48. ds = de.ImageFolderDatasetV2(dataset_path, num_parallel_workers=8, shuffle=True)
  49. else:
  50. raise ValueError("Unsupport platform.")
  51. resize_height = config.image_height
  52. resize_width = config.image_width
  53. buffer_size = 1000
  54. # define map operations
  55. decode_op = C.Decode()
  56. resize_crop_op = C.RandomCropDecodeResize(resize_height, scale=(0.08, 1.0), ratio=(0.75, 1.333))
  57. horizontal_flip_op = C.RandomHorizontalFlip(prob=0.5)
  58. resize_op = C.Resize((256, 256))
  59. center_crop = C.CenterCrop(resize_width)
  60. rescale_op = C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4)
  61. normalize_op = C.Normalize(mean=[0.485*255, 0.456*255, 0.406*255], std=[0.229*255, 0.224*255, 0.225*255])
  62. change_swap_op = C.HWC2CHW()
  63. if do_train:
  64. trans = [resize_crop_op, horizontal_flip_op, rescale_op, normalize_op, change_swap_op]
  65. else:
  66. trans = [decode_op, resize_op, center_crop, normalize_op, change_swap_op]
  67. type_cast_op = C2.TypeCast(mstype.int32)
  68. ds = ds.map(input_columns="image", operations=trans, num_parallel_workers=8)
  69. ds = ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=8)
  70. # apply shuffle operations
  71. ds = ds.shuffle(buffer_size=buffer_size)
  72. # apply batch operations
  73. ds = ds.batch(batch_size, drop_remainder=True)
  74. # apply dataset repeat operation
  75. ds = ds.repeat(repeat_num)
  76. return ds