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_decode.py 4.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 Decode op in DE
  17. """
  18. import cv2
  19. import numpy as np
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.vision.c_transforms as vision
  22. import mindspore.dataset.vision.py_transforms as py_vision
  23. from mindspore import log as logger
  24. from util import diff_mse
  25. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  26. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  27. def test_decode_op():
  28. """
  29. Test Decode op
  30. """
  31. logger.info("test_decode_op")
  32. # Decode with rgb format set to True
  33. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  34. # Serialize and Load dataset requires using vision.Decode instead of vision.Decode().
  35. data1 = data1.map(operations=[vision.Decode(True)], input_columns=["image"])
  36. # Second dataset
  37. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  38. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  39. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  40. actual = item1["image"]
  41. expected = cv2.imdecode(item2["image"], cv2.IMREAD_COLOR)
  42. expected = cv2.cvtColor(expected, cv2.COLOR_BGR2RGB)
  43. assert actual.shape == expected.shape
  44. mse = diff_mse(actual, expected)
  45. assert mse == 0
  46. def test_decode_op_tf_file_dataset():
  47. """
  48. Test Decode op with tf_file dataset
  49. """
  50. logger.info("test_decode_op_tf_file_dataset")
  51. # Decode with rgb format set to True
  52. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.FILES)
  53. data1 = data1.map(operations=vision.Decode(True), input_columns=["image"])
  54. for item in data1.create_dict_iterator(num_epochs=1):
  55. logger.info('decode == {}'.format(item['image']))
  56. # Second dataset
  57. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  58. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  59. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  60. actual = item1["image"]
  61. expected = cv2.imdecode(item2["image"], cv2.IMREAD_COLOR)
  62. expected = cv2.cvtColor(expected, cv2.COLOR_BGR2RGB)
  63. assert actual.shape == expected.shape
  64. mse = diff_mse(actual, expected)
  65. assert mse == 0
  66. class ImageDataset:
  67. def __init__(self, data_path, data_type="numpy"):
  68. self.data = [data_path]
  69. self.label = np.random.sample((1, 1))
  70. self.data_type = data_type
  71. def __getitem__(self, index):
  72. # use file open and read method
  73. f = open(self.data[index], 'rb')
  74. img_bytes = f.read()
  75. f.close()
  76. if self.data_type == "numpy":
  77. img_bytes = np.frombuffer(img_bytes, dtype=np.uint8)
  78. # return bytes directly
  79. return (img_bytes, self.label[index])
  80. def __len__(self):
  81. return len(self.data)
  82. def test_read_image_decode_op():
  83. data_path = "../data/dataset/testPK/data/class1/0.jpg"
  84. dataset1 = ds.GeneratorDataset(ImageDataset(data_path, data_type="numpy"), ["data", "label"])
  85. dataset2 = ds.GeneratorDataset(ImageDataset(data_path, data_type="bytes"), ["data", "label"])
  86. decode_op = py_vision.Decode()
  87. to_tensor = py_vision.ToTensor(output_type=np.int32)
  88. dataset1 = dataset1.map(operations=[decode_op, to_tensor], input_columns=["data"])
  89. dataset2 = dataset2.map(operations=[decode_op, to_tensor], input_columns=["data"])
  90. for item1, item2 in zip(dataset1, dataset2):
  91. assert np.count_nonzero(item1[0].asnumpy() - item2[0].asnumpy()) == 0
  92. if __name__ == "__main__":
  93. test_decode_op()
  94. test_decode_op_tf_file_dataset()
  95. test_read_image_decode_op()