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_imagenet.py 2.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.c_transforms as C2
  22. import mindspore.dataset.transforms.vision.c_transforms as V_C
  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("RANK_SIZE"))
  35. rank_id = int(os.getenv("RANK_ID"))
  36. if device_num == 1:
  37. ds = de.ImageFolderDatasetV2(dataset_path, num_parallel_workers=8, shuffle=False)
  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. if do_train:
  45. transform_img = [
  46. V_C.RandomCropDecodeResize(image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)),
  47. V_C.RandomHorizontalFlip(prob=0.5),
  48. V_C.Normalize(mean=mean, std=std),
  49. V_C.HWC2CHW()
  50. ]
  51. else:
  52. transform_img = [
  53. V_C.Decode(),
  54. V_C.Resize((256, 256)),
  55. V_C.CenterCrop(image_size),
  56. V_C.Normalize(mean=mean, std=std),
  57. V_C.HWC2CHW()
  58. ]
  59. # type_cast_op = C2.TypeCast(mstype.float16)
  60. type_cast_op = C2.TypeCast(mstype.int32)
  61. ds = ds.map(input_columns="image", operations=transform_img, num_parallel_workers=8)
  62. ds = ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=8)
  63. # apply shuffle operations
  64. # ds = ds.shuffle(buffer_size=config.buffer_size)
  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