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.dataset as ds
  18. import mindspore.common.dtype as mstype
  19. import mindspore.dataset.vision.c_transforms as C
  20. import mindspore.dataset.transforms.c_transforms as C2
  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. cifar_ds = load_func(num_parallel_workers=8, shuffle=False)
  34. resize_height = config.image_height
  35. resize_width = config.image_width
  36. rescale = 1.0 / 255.0
  37. shift = 0.0
  38. # define map operations
  39. # interpolation default BILINEAR
  40. resize_op = C.Resize((resize_height, resize_width))
  41. rescale_op = C.Rescale(rescale, shift)
  42. normalize_op = C.Normalize(
  43. (0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
  44. changeswap_op = C.HWC2CHW()
  45. type_cast_op = C2.TypeCast(mstype.int32)
  46. c_trans = [resize_op, rescale_op, normalize_op, changeswap_op]
  47. # apply map operations on images
  48. cifar_ds = cifar_ds.map(input_columns="label", operations=type_cast_op)
  49. cifar_ds = cifar_ds.map(input_columns="image", operations=c_trans)
  50. # apply batch operations
  51. cifar_ds = cifar_ds.batch(batch_size, drop_remainder=True)
  52. # apply dataset repeat operation
  53. cifar_ds = cifar_ds.repeat(repeat_num)
  54. return cifar_ds