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

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from util import diff_mse
  21. import mindspore.dataset as ds
  22. import mindspore.dataset.transforms.vision.c_transforms as vision
  23. from mindspore import log as logger
  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 test_decode_op():
  27. """
  28. Test Decode op
  29. """
  30. logger.info("test_decode_op")
  31. # Decode with rgb format set to True
  32. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  33. # Serialize and Load dataset requires using vision.Decode instead of vision.Decode().
  34. data1 = data1.map(input_columns=["image"], operations=[vision.Decode(True)])
  35. # Second dataset
  36. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  37. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  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. diff = actual - expected
  43. mse = np.sum(np.power(diff, 2))
  44. assert mse == 0
  45. def test_decode_op_tf_file_dataset():
  46. """
  47. Test Decode op with tf_file dataset
  48. """
  49. logger.info("test_decode_op_tf_file_dataset")
  50. # Decode with rgb format set to True
  51. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.FILES)
  52. data1 = data1.map(input_columns=["image"], operations=vision.Decode(True))
  53. for item in data1.create_dict_iterator():
  54. logger.info('decode == {}'.format(item['image']))
  55. # Second dataset
  56. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  57. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  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. diff = actual - expected
  63. mse = np.sum(np.power(diff, 2))
  64. assert mse == 0
  65. if __name__ == "__main__":
  66. test_decode_op()
  67. test_decode_op_tf_file_dataset()