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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. Data operations, will be used in train.py and eval.py
  17. """
  18. import mindspore.common.dtype as mstype
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.c_transforms as C2
  21. import mindspore.dataset.vision.c_transforms as C
  22. def create_dataset(dataset_path, do_train, batch_size=16, device_num=1, rank=0):
  23. """
  24. create a train or eval dataset
  25. Args:
  26. dataset_path(string): the path of dataset.
  27. do_train(bool): whether dataset is used for train or eval.
  28. batch_size(int): the batch size of dataset. Default: 16.
  29. device_num (int): Number of shards that the dataset should be divided into (default=1).
  30. rank (int): The shard ID within num_shards (default=0).
  31. Returns:
  32. dataset
  33. """
  34. if device_num == 1:
  35. data_set = ds.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True)
  36. else:
  37. data_set = ds.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True,
  38. num_shards=device_num, shard_id=rank)
  39. # define map operations
  40. if do_train:
  41. trans = [
  42. C.RandomCropDecodeResize(299),
  43. C.RandomHorizontalFlip(prob=0.5),
  44. C.RandomColorAdjust(brightness=0.4, contrast=0.4, saturation=0.4)
  45. ]
  46. else:
  47. trans = [
  48. C.Decode(),
  49. C.Resize(320),
  50. C.CenterCrop(299)
  51. ]
  52. trans += [
  53. C.Normalize(mean=[127.5, 127.5, 127.5], std=[127.5, 127.5, 127.5]),
  54. C.HWC2CHW(),
  55. C2.TypeCast(mstype.float32)
  56. ]
  57. type_cast_op = C2.TypeCast(mstype.int32)
  58. data_set = data_set.map(input_columns="image", operations=trans, num_parallel_workers=8)
  59. data_set = data_set.map(input_columns="label", operations=type_cast_op, num_parallel_workers=8)
  60. # apply batch operations
  61. data_set = data_set.batch(batch_size, drop_remainder=True)
  62. return data_set