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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) # resize images to (32, 32)
  41. rescale_op = CV.Rescale(rescale, shift) # rescale images
  42. hwc2chw_op = CV.HWC2CHW() # change shape from (height, width, channel) to (channel, height, width) to fit network.
  43. type_cast_op = C.TypeCast(mstype.int32) # change data type of label to int32 to fit network
  44. # apply map operations on images
  45. mnist_ds = mnist_ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=num_parallel_workers)
  46. mnist_ds = mnist_ds.map(input_columns="image", operations=resize_op, num_parallel_workers=num_parallel_workers)
  47. mnist_ds = mnist_ds.map(input_columns="image", operations=rescale_op, num_parallel_workers=num_parallel_workers)
  48. mnist_ds = mnist_ds.map(input_columns="image", operations=hwc2chw_op, num_parallel_workers=num_parallel_workers)
  49. # apply DatasetOps
  50. buffer_size = 10000
  51. mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size) # 10000 as in LeNet train script
  52. mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)
  53. mnist_ds = mnist_ds.repeat(repeat_size)
  54. return mnist_ds
  55. def save_img(data, name, size=32, num=32):
  56. """
  57. Visualize data and save to target files
  58. Args:
  59. data: nparray of size (num, size, size)
  60. name: ouput file name
  61. size: image size
  62. num: number of images
  63. """
  64. col = int(num / 8)
  65. row = 8
  66. imgs = Image.new('L', (size*col, size*row))
  67. for i in range(num):
  68. j = i/8
  69. img_data = data[i]
  70. img_data = np.resize(img_data, (size, size))
  71. img_data = img_data * 255
  72. img_data = img_data.astype(np.uint8)
  73. im = Image.fromarray(img_data, 'L')
  74. imgs.paste(im, (int(j) * size, (i % 8) * size))
  75. imgs.save(name)