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

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 os
  19. import mindspore.common.dtype as mstype
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.c_transforms as C
  22. import mindspore.dataset.transforms.vision.c_transforms as vision
  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. rank_size = int(os.environ.get("RANK_SIZE")) if os.environ.get("RANK_SIZE") else None
  31. rank_id = int(os.environ.get("RANK_ID")) if os.environ.get("RANK_ID") else None
  32. data_set = ds.Cifar10Dataset(data_dir, num_shards=rank_size, shard_id=rank_id)
  33. resize_height = cfg.image_height
  34. resize_width = cfg.image_width
  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. normalize_op = vision.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
  40. changeswap_op = vision.HWC2CHW()
  41. type_cast_op = C.TypeCast(mstype.int32)
  42. c_trans = []
  43. if training:
  44. c_trans = [random_crop_op, random_horizontal_op]
  45. c_trans += [resize_op, normalize_op, changeswap_op]
  46. # apply map operations on images
  47. data_set = data_set.map(input_columns="label", operations=type_cast_op)
  48. data_set = data_set.map(input_columns="image", operations=c_trans)
  49. # apply repeat operations
  50. data_set = data_set.repeat(repeat_num)
  51. # apply shuffle operations
  52. data_set = data_set.shuffle(buffer_size=10)
  53. # apply batch operations
  54. data_set = data_set.batch(batch_size=cfg.batch_size, drop_remainder=True)
  55. return data_set