test_functions.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  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. import copy
  15. import numpy as np
  16. import paddlers.transforms as T
  17. from testing_utils import CpuCommonTest
  18. from data import build_input_from_file
  19. __all__ = ['TestMatchHistograms', 'TestMatchByRegression']
  20. def calc_err(a, b):
  21. a = a.astype('float64')
  22. b = b.astype('float64')
  23. return np.abs(a - b).mean()
  24. class TestMatchHistograms(CpuCommonTest):
  25. def setUp(self):
  26. self.inputs = [
  27. build_input_from_file(
  28. "data/ssmt/test_mixed_binary.txt", prefix="./data/ssmt")
  29. ]
  30. def test_output_shape(self):
  31. decoder = T.DecodeImg()
  32. for input in copy.deepcopy(self.inputs):
  33. for sample in input:
  34. sample = decoder(sample)
  35. im_out = T.functions.match_histograms(sample['image2'],
  36. sample['image'])
  37. self.check_output_equal(im_out.shape, sample['image2'].shape)
  38. self.assertEqual(im_out.dtype, sample['image2'].dtype)
  39. im_out = T.functions.match_histograms(sample['image'],
  40. sample['image2'])
  41. self.check_output_equal(im_out.shape, sample['image'].shape)
  42. self.assertEqual(im_out.dtype, sample['image'].dtype)
  43. class TestMatchByRegression(CpuCommonTest):
  44. def setUp(self):
  45. self.inputs = [
  46. build_input_from_file(
  47. "data/ssmt/test_mixed_binary.txt", prefix="./data/ssmt")
  48. ]
  49. def test_output_shape(self):
  50. decoder = T.DecodeImg()
  51. for input in copy.deepcopy(self.inputs):
  52. for sample in input:
  53. sample = decoder(sample)
  54. im_out = T.functions.match_by_regression(sample['image2'],
  55. sample['image'])
  56. self.check_output_equal(im_out.shape, sample['image2'].shape)
  57. self.assertEqual(im_out.dtype, sample['image2'].dtype)
  58. err1 = calc_err(sample['image'], sample['image2'])
  59. err2 = calc_err(sample['image'], im_out)
  60. self.assertLessEqual(err2, err1)
  61. im_out = T.functions.match_by_regression(sample['image'],
  62. sample['image2'])
  63. self.check_output_equal(im_out.shape, sample['image'].shape)
  64. self.assertEqual(im_out.dtype, sample['image'].dtype)
  65. err1 = calc_err(sample['image'], sample['image2'])
  66. err2 = calc_err(im_out, sample['image2'])
  67. self.assertLessEqual(err2, err1)