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 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. """dataset base and LeNet."""
  16. import os
  17. from mindspore import dataset as ds
  18. from mindspore.common import dtype as mstype
  19. from mindspore.dataset.transforms import c_transforms as C
  20. from mindspore.dataset.vision import Inter
  21. from mindspore.dataset.vision import c_transforms as CV
  22. from mindspore import nn, Tensor
  23. from mindspore.common.initializer import Normal
  24. from mindspore.ops import operations as P
  25. def create_mnist_dataset(mode='train', num_samples=2, batch_size=2):
  26. """create dataset for train or test"""
  27. mnist_path = '/home/workspace/mindspore_dataset/mnist'
  28. num_parallel_workers = 1
  29. # define dataset
  30. mnist_ds = ds.MnistDataset(os.path.join(mnist_path, mode), num_samples=num_samples, shuffle=False)
  31. resize_height, resize_width = 32, 32
  32. # define map operations
  33. resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR) # Bilinear mode
  34. rescale_nml_op = CV.Rescale(1 / 0.3081, -1 * 0.1307 / 0.3081)
  35. rescale_op = CV.Rescale(1.0 / 255.0, shift=0.0)
  36. hwc2chw_op = CV.HWC2CHW()
  37. type_cast_op = C.TypeCast(mstype.int32)
  38. # apply map operations on images
  39. mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns="label", num_parallel_workers=num_parallel_workers)
  40. mnist_ds = mnist_ds.map(operations=resize_op, input_columns="image", num_parallel_workers=num_parallel_workers)
  41. mnist_ds = mnist_ds.map(operations=rescale_op, input_columns="image", num_parallel_workers=num_parallel_workers)
  42. mnist_ds = mnist_ds.map(operations=rescale_nml_op, input_columns="image", num_parallel_workers=num_parallel_workers)
  43. mnist_ds = mnist_ds.map(operations=hwc2chw_op, input_columns="image", num_parallel_workers=num_parallel_workers)
  44. # apply DatasetOps
  45. mnist_ds = mnist_ds.batch(batch_size=batch_size, drop_remainder=True)
  46. return mnist_ds
  47. class LeNet5(nn.Cell):
  48. """
  49. Lenet network
  50. Args:
  51. num_class (int): Number of classes. Default: 10.
  52. num_channel (int): Number of channels. Default: 1.
  53. Returns:
  54. Tensor, output tensor
  55. Examples:
  56. >>> LeNet(num_class=10)
  57. """
  58. def __init__(self, num_class=10, num_channel=1, include_top=True):
  59. super(LeNet5, self).__init__()
  60. self.conv1 = nn.Conv2d(num_channel, 6, 5, pad_mode='valid')
  61. self.conv2 = nn.Conv2d(6, 16, 5, pad_mode='valid')
  62. self.relu = nn.ReLU()
  63. self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
  64. self.include_top = include_top
  65. if self.include_top:
  66. self.flatten = nn.Flatten()
  67. self.fc1 = nn.Dense(16 * 5 * 5, 120, weight_init=Normal(0.02))
  68. self.fc2 = nn.Dense(120, 84, weight_init=Normal(0.02))
  69. self.fc3 = nn.Dense(84, num_class, weight_init=Normal(0.02))
  70. self.scalar_summary = P.ScalarSummary()
  71. self.image_summary = P.ImageSummary()
  72. self.histogram_summary = P.HistogramSummary()
  73. self.tensor_summary = P.TensorSummary()
  74. self.channel = Tensor(num_channel)
  75. def construct(self, x):
  76. """construct."""
  77. self.image_summary('image', x)
  78. x = self.conv1(x)
  79. self.histogram_summary('histogram', x)
  80. x = self.relu(x)
  81. self.tensor_summary('tensor', x)
  82. x = self.relu(x)
  83. x = self.max_pool2d(x)
  84. self.scalar_summary('scalar', self.channel)
  85. x = self.conv2(x)
  86. x = self.relu(x)
  87. x = self.max_pool2d(x)
  88. if not self.include_top:
  89. return x
  90. x = self.flatten(x)
  91. x = self.relu(self.fc1(x))
  92. x = self.relu(self.fc2(x))
  93. x = self.fc3(x)
  94. return x