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

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.transforms.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(input_columns=["image"], operations=[vision.Decode(True)])
  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(), data2.create_dict_iterator()):
  37. actual = item1["image"]
  38. expected = cv2.imdecode(item2["image"], cv2.IMREAD_COLOR)
  39. expected = cv2.cvtColor(expected, cv2.COLOR_BGR2RGB)
  40. assert actual.shape == expected.shape
  41. mse = diff_mse(actual, expected)
  42. assert mse == 0
  43. def test_decode_op_tf_file_dataset():
  44. """
  45. Test Decode op with tf_file dataset
  46. """
  47. logger.info("test_decode_op_tf_file_dataset")
  48. # Decode with rgb format set to True
  49. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.FILES)
  50. data1 = data1.map(input_columns=["image"], operations=vision.Decode(True))
  51. for item in data1.create_dict_iterator():
  52. logger.info('decode == {}'.format(item['image']))
  53. # Second dataset
  54. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  55. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  56. actual = item1["image"]
  57. expected = cv2.imdecode(item2["image"], cv2.IMREAD_COLOR)
  58. expected = cv2.cvtColor(expected, cv2.COLOR_BGR2RGB)
  59. assert actual.shape == expected.shape
  60. mse = diff_mse(actual, expected)
  61. assert mse == 0
  62. if __name__ == "__main__":
  63. test_decode_op()
  64. test_decode_op_tf_file_dataset()