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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.transforms.vision.c_transforms as vision
  20. import numpy as np
  21. import mindspore.dataset as ds
  22. from mindspore import log as logger
  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 diff_mse(in1, in2):
  26. mse = (np.square(in1.astype(float) / 255 - in2.astype(float) / 255)).mean()
  27. return mse * 100
  28. def test_decode_op():
  29. """
  30. Test Decode op
  31. """
  32. logger.info("test_decode_op")
  33. # Decode with rgb format set to True
  34. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  35. # Serialize and Load dataset requires using vision.Decode instead of vision.Decode().
  36. data1 = data1.map(input_columns=["image"], operations=[vision.Decode(True)])
  37. # Second dataset
  38. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  39. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  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. diff = actual - expected
  45. mse = np.sum(np.power(diff, 2))
  46. assert mse == 0
  47. def test_decode_op_tf_file_dataset():
  48. """
  49. Test Decode op with tf_file dataset
  50. """
  51. logger.info("test_decode_op_tf_file_dataset")
  52. # Decode with rgb format set to True
  53. data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=ds.Shuffle.FILES)
  54. data1 = data1.map(input_columns=["image"], operations=vision.Decode(True))
  55. for item in data1.create_dict_iterator():
  56. logger.info('decode == {}'.format(item['image']))
  57. # Second dataset
  58. data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
  59. for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
  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. diff = actual - expected
  65. mse = np.sum(np.power(diff, 2))
  66. assert mse == 0
  67. if __name__ == "__main__":
  68. test_decode_op()
  69. test_decode_op_tf_file_dataset()