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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.transforms.vision.c_transforms as c_vision
  22. from mindspore import log as logger
  23. from util import diff_mse
  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(input_columns=["label"], operations=one_hot_op, columns_order=["label"])
  43. # Second dataset
  44. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["label"], shuffle=False)
  45. num_iter = 0
  46. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  47. assert len(item1) == len(item2)
  48. label1 = item1["label"]
  49. label2 = one_hot(item2["label"][0], depth)
  50. mse = diff_mse(label1, label2)
  51. logger.info("DE one_hot: {}, Numpy one_hot: {}, diff: {}".format(label1, label2, mse))
  52. assert mse == 0
  53. num_iter += 1
  54. assert num_iter == 3
  55. def test_one_hot_post_aug():
  56. """
  57. Test One Hot Encoding after Multiple Data Augmentation Operators
  58. """
  59. logger.info("test_one_hot_post_aug")
  60. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, shuffle=False)
  61. # Define data augmentation parameters
  62. rescale = 1.0 / 255.0
  63. shift = 0.0
  64. resize_height, resize_width = 224, 224
  65. # Define map operations
  66. decode_op = c_vision.Decode()
  67. rescale_op = c_vision.Rescale(rescale, shift)
  68. resize_op = c_vision.Resize((resize_height, resize_width))
  69. # Apply map operations on images
  70. data1 = data1.map(input_columns=["image"], operations=decode_op)
  71. data1 = data1.map(input_columns=["image"], operations=rescale_op)
  72. data1 = data1.map(input_columns=["image"], operations=resize_op)
  73. # Apply one-hot encoding on labels
  74. depth = 4
  75. one_hot_encode = data_trans.OneHot(depth)
  76. data1 = data1.map(input_columns=["label"], operations=one_hot_encode)
  77. # Apply datasets ops
  78. buffer_size = 100
  79. seed = 10
  80. batch_size = 2
  81. ds.config.set_seed(seed)
  82. data1 = data1.shuffle(buffer_size=buffer_size)
  83. data1 = data1.batch(batch_size, drop_remainder=True)
  84. num_iter = 0
  85. for item in data1.create_dict_iterator():
  86. logger.info("image is: {}".format(item["image"]))
  87. logger.info("label is: {}".format(item["label"]))
  88. num_iter += 1
  89. assert num_iter == 1
  90. if __name__ == "__main__":
  91. test_one_hot()
  92. test_one_hot_post_aug()