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.

utils.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. """ Utils """
  16. from PIL import Image
  17. import numpy as np
  18. from mindspore.common import dtype as mstype
  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.dataset.transforms.vision import Inter
  23. def create_dataset(data_path, batch_size=32, repeat_size=1,
  24. num_parallel_workers=1):
  25. """ create dataset for train or test
  26. Args:
  27. data_path: Data path
  28. batch_size: The number of data records in each group
  29. repeat_size: The number of replicated data records
  30. num_parallel_workers: The number of parallel workers
  31. """
  32. # define dataset
  33. mnist_ds = ds.MnistDataset(data_path)
  34. #mnist_ds = ds.MnistDataset(data_path,num_samples=32)
  35. # define operation parameters
  36. resize_height, resize_width = 32, 32
  37. rescale = 1.0 / 255.0
  38. shift = 0.0
  39. # define map operations
  40. # resize images to (32, 32)
  41. resize_op = CV.Resize((resize_height, resize_width),
  42. interpolation=Inter.LINEAR)
  43. rescale_op = CV.Rescale(rescale, shift) # rescale images
  44. # change shape from (height, width, channel) to (channel, height, width) to fit network.
  45. hwc2chw_op = CV.HWC2CHW()
  46. # change data type of label to int32 to fit network
  47. type_cast_op = C.TypeCast(mstype.int32)
  48. # apply map operations on images
  49. mnist_ds = mnist_ds.map(input_columns="label", operations=type_cast_op,
  50. num_parallel_workers=num_parallel_workers)
  51. mnist_ds = mnist_ds.map(input_columns="image", operations=resize_op,
  52. num_parallel_workers=num_parallel_workers)
  53. mnist_ds = mnist_ds.map(input_columns="image", operations=rescale_op,
  54. num_parallel_workers=num_parallel_workers)
  55. mnist_ds = mnist_ds.map(input_columns="image", operations=hwc2chw_op,
  56. num_parallel_workers=num_parallel_workers)
  57. # apply DatasetOps
  58. buffer_size = 10000
  59. # 10000 as in LeNet train script
  60. mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size)
  61. mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)
  62. mnist_ds = mnist_ds.repeat(repeat_size)
  63. return mnist_ds
  64. def save_img(data, name, size=32, num=32):
  65. """
  66. Visualize data and save to target files
  67. Args:
  68. data: nparray of size (num, size, size)
  69. name: output file name
  70. size: image size
  71. num: number of images
  72. """
  73. col = int(num / 8)
  74. row = 8
  75. imgs = Image.new('L', (size*col, size*row))
  76. for i in range(num):
  77. j = i/8
  78. img_data = data[i]
  79. img_data = np.resize(img_data, (size, size))
  80. img_data = img_data * 255
  81. img_data = img_data.astype(np.uint8)
  82. im = Image.fromarray(img_data, 'L')
  83. imgs.paste(im, (int(j) * size, (i % 8) * size))
  84. imgs.save(name)