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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. Produce the dataset
  17. """
  18. from config import alexnet_cfg as cfg
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.c_transforms as C
  21. import mindspore.dataset.transforms.vision.c_transforms as CV
  22. from mindspore.common import dtype as mstype
  23. def create_dataset(data_path, batch_size=32, repeat_size=1, status="train"):
  24. """
  25. create dataset for train or test
  26. """
  27. cifar_ds = ds.Cifar10Dataset(data_path)
  28. rescale = 1.0 / 255.0
  29. shift = 0.0
  30. resize_op = CV.Resize((cfg.image_height, cfg.image_width))
  31. rescale_op = CV.Rescale(rescale, shift)
  32. normalize_op = CV.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
  33. if status == "train":
  34. random_crop_op = CV.RandomCrop([32, 32], [4, 4, 4, 4])
  35. random_horizontal_op = CV.RandomHorizontalFlip()
  36. channel_swap_op = CV.HWC2CHW()
  37. typecast_op = C.TypeCast(mstype.int32)
  38. cifar_ds = cifar_ds.map(input_columns="label", operations=typecast_op)
  39. if status == "train":
  40. cifar_ds = cifar_ds.map(input_columns="image", operations=random_crop_op)
  41. cifar_ds = cifar_ds.map(input_columns="image", operations=random_horizontal_op)
  42. cifar_ds = cifar_ds.map(input_columns="image", operations=resize_op)
  43. cifar_ds = cifar_ds.map(input_columns="image", operations=rescale_op)
  44. cifar_ds = cifar_ds.map(input_columns="image", operations=normalize_op)
  45. cifar_ds = cifar_ds.map(input_columns="image", operations=channel_swap_op)
  46. cifar_ds = cifar_ds.shuffle(buffer_size=cfg.buffer_size)
  47. cifar_ds = cifar_ds.batch(batch_size, drop_remainder=True)
  48. cifar_ds = cifar_ds.repeat(repeat_size)
  49. return cifar_ds