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

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 os
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.c_transforms as C
  21. import mindspore.dataset.transforms.vision.c_transforms as vision
  22. import mindspore.common.dtype as mstype
  23. from config import cifar_cfg as cfg
  24. def create_dataset(data_home, repeat_num=1, training=True):
  25. """Data operations."""
  26. ds.config.set_seed(1)
  27. data_dir = os.path.join(data_home, "cifar-10-batches-bin")
  28. if not training:
  29. data_dir = os.path.join(data_home, "cifar-10-verify-bin")
  30. data_set = ds.Cifar10Dataset(data_dir)
  31. resize_height = cfg.image_height
  32. resize_width = cfg.image_width
  33. rescale = 1.0 / 255.0
  34. shift = 0.0
  35. # define map operations
  36. random_crop_op = vision.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT
  37. random_horizontal_op = vision.RandomHorizontalFlip()
  38. resize_op = vision.Resize((resize_height, resize_width)) # interpolation default BILINEAR
  39. rescale_op = vision.Rescale(rescale, shift)
  40. normalize_op = vision.Normalize((0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023))
  41. changeswap_op = vision.HWC2CHW()
  42. type_cast_op = C.TypeCast(mstype.int32)
  43. c_trans = []
  44. if training:
  45. c_trans = [random_crop_op, random_horizontal_op]
  46. c_trans += [resize_op, rescale_op, normalize_op,
  47. changeswap_op]
  48. # apply map operations on images
  49. data_set = data_set.map(input_columns="label", operations=type_cast_op)
  50. data_set = data_set.map(input_columns="image", operations=c_trans)
  51. # apply repeat operations
  52. data_set = data_set.repeat(repeat_num)
  53. # apply shuffle operations
  54. data_set = data_set.shuffle(buffer_size=10)
  55. # apply batch operations
  56. data_set = data_set.batch(batch_size=cfg.batch_size, drop_remainder=True)
  57. return data_set

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.