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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 mindspore.dataset as ds
  20. import mindspore.dataset.vision.c_transforms as vision
  21. from mindspore import log as logger
  22. from util import diff_mse
  23. DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
  24. SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
  25. def test_decode_op():
  26. """
  27. Test Decode op
  28. """
  29. logger.info("test_decode_op")
  30. # Decode with rgb format set to True
  31. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  32. # Serialize and Load dataset requires using vision.Decode instead of vision.Decode().
  33. data1 = data1.map(operations=[vision.Decode(True)], input_columns=["image"])
  34. # Second dataset
  35. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  36. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  37. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  38. actual = item1["image"]
  39. expected = cv2.imdecode(item2["image"], cv2.IMREAD_COLOR)
  40. expected = cv2.cvtColor(expected, cv2.COLOR_BGR2RGB)
  41. assert actual.shape == expected.shape
  42. mse = diff_mse(actual, expected)
  43. assert mse == 0
  44. def test_decode_op_tf_file_dataset():
  45. """
  46. Test Decode op with tf_file dataset
  47. """
  48. logger.info("test_decode_op_tf_file_dataset")
  49. # Decode with rgb format set to True
  50. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.FILES)
  51. data1 = data1.map(operations=vision.Decode(True), input_columns=["image"])
  52. for item in data1.create_dict_iterator(num_epochs=1):
  53. logger.info('decode == {}'.format(item['image']))
  54. # Second dataset
  55. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  56. for item1, item2 in zip(data1.create_dict_iterator(num_epochs=1, output_numpy=True),
  57. data2.create_dict_iterator(num_epochs=1, output_numpy=True)):
  58. actual = item1["image"]
  59. expected = cv2.imdecode(item2["image"], cv2.IMREAD_COLOR)
  60. expected = cv2.cvtColor(expected, cv2.COLOR_BGR2RGB)
  61. assert actual.shape == expected.shape
  62. mse = diff_mse(actual, expected)
  63. assert mse == 0
  64. if __name__ == "__main__":
  65. test_decode_op()
  66. test_decode_op_tf_file_dataset()