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

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. """ create train dataset. """
  16. from functools import partial
  17. import mindspore.common.dtype as mstype
  18. import mindspore.dataset as ds
  19. import mindspore.dataset.transforms.c_transforms as C2
  20. import mindspore.dataset.vision.c_transforms as C
  21. def create_dataset(dataset_path, config, repeat_num=1, batch_size=32):
  22. """
  23. create a train dataset
  24. Args:
  25. dataset_path(string): the path of dataset.
  26. config(EasyDict):the basic config for training
  27. repeat_num(int): the repeat times of dataset. Default: 1.
  28. batch_size(int): the batch size of dataset. Default: 32.
  29. Returns:
  30. dataset
  31. """
  32. load_func = partial(ds.Cifar10Dataset, dataset_path)
  33. data_set = load_func(num_parallel_workers=8, shuffle=False)
  34. resize_height = config.image_height
  35. resize_width = config.image_width
  36. mean = [0.485 * 255, 0.456 * 255, 0.406 * 255]
  37. std = [0.229 * 255, 0.224 * 255, 0.225 * 255]
  38. # define map operations
  39. resize_op = C.Resize((resize_height, resize_width))
  40. normalize_op = C.Normalize(mean=mean, std=std)
  41. changeswap_op = C.HWC2CHW()
  42. c_trans = [resize_op, normalize_op, changeswap_op]
  43. type_cast_op = C2.TypeCast(mstype.int32)
  44. data_set = data_set.map(operations=c_trans, input_columns="image",
  45. num_parallel_workers=8)
  46. data_set = data_set.map(operations=type_cast_op,
  47. input_columns="label", num_parallel_workers=8)
  48. # apply batch operations
  49. data_set = data_set.batch(batch_size, drop_remainder=True)
  50. # apply dataset repeat operation
  51. data_set = data_set.repeat(repeat_num)
  52. return data_set