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

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