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.

test_onehot_op.py 3.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright 2019 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. Testing the OneHot Op
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.transforms.c_transforms as data_trans
  21. import mindspore.dataset.vision.c_transforms as c_vision
  22. from mindspore import log as logger
  23. from util import dataset_equal_with_function
  24. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  25. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  26. def one_hot(index, depth):
  27. """
  28. Apply the one_hot
  29. """
  30. arr = np.zeros([1, depth], dtype=np.int32)
  31. arr[0, index] = 1
  32. return arr
  33. def test_one_hot():
  34. """
  35. Test OneHot Tensor Operator
  36. """
  37. logger.info("test_one_hot")
  38. depth = 10
  39. # First dataset
  40. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  41. one_hot_op = data_trans.OneHot(num_classes=depth)
  42. data1 = data1.map(operations=one_hot_op, input_columns=["label"], column_order=["label"])
  43. # Second dataset
  44. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["label"], shuffle=False)
  45. assert dataset_equal_with_function(data1, data2, 0, one_hot, depth)
  46. def test_one_hot_post_aug():
  47. """
  48. Test One Hot Encoding after Multiple Data Augmentation Operators
  49. """
  50. logger.info("test_one_hot_post_aug")
  51. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  52. # Define data augmentation parameters
  53. rescale = 1.0 / 255.0
  54. shift = 0.0
  55. resize_height, resize_width = 224, 224
  56. # Define map operations
  57. decode_op = c_vision.Decode()
  58. rescale_op = c_vision.Rescale(rescale, shift)
  59. resize_op = c_vision.Resize((resize_height, resize_width))
  60. # Apply map operations on images
  61. data1 = data1.map(operations=decode_op, input_columns=["image"])
  62. data1 = data1.map(operations=rescale_op, input_columns=["image"])
  63. data1 = data1.map(operations=resize_op, input_columns=["image"])
  64. # Apply one-hot encoding on labels
  65. depth = 4
  66. one_hot_encode = data_trans.OneHot(depth)
  67. data1 = data1.map(operations=one_hot_encode, input_columns=["label"])
  68. # Apply datasets ops
  69. buffer_size = 100
  70. seed = 10
  71. batch_size = 2
  72. ds.config.set_seed(seed)
  73. data1 = data1.shuffle(buffer_size=buffer_size)
  74. data1 = data1.batch(batch_size, drop_remainder=True)
  75. num_iter = 0
  76. for item in data1.create_dict_iterator(num_epochs=1):
  77. logger.info("image is: {}".format(item["image"]))
  78. logger.info("label is: {}".format(item["label"]))
  79. num_iter += 1
  80. assert num_iter == 1
  81. if __name__ == "__main__":
  82. test_one_hot()
  83. test_one_hot_post_aug()