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_confusion_matrix_metric.py 3.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright 2021 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. # """test_confusion_matrix_metric"""
  16. import numpy as np
  17. import pytest
  18. from mindspore import Tensor
  19. from mindspore.nn.metrics import ConfusionMatrixMetric
  20. def test_confusion_matrix_metric():
  21. """test_confusion_matrix_metric"""
  22. metric = ConfusionMatrixMetric(skip_channel=True, metric_name="tpr", calculation_method=False)
  23. metric.clear()
  24. x = Tensor(np.array([[[0], [1]], [[1], [0]]]))
  25. y = Tensor(np.array([[[0], [1]], [[0], [1]]]))
  26. metric.update(x, y)
  27. x = Tensor(np.array([[[0], [1]], [[1], [0]]]))
  28. y = Tensor(np.array([[[0], [1]], [[1], [0]]]))
  29. metric.update(x, y)
  30. output = metric.eval()
  31. assert np.allclose(output, np.array([0.75]))
  32. def test_confusion_matrix_metric_update_len():
  33. x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]]))
  34. metric = ConfusionMatrixMetric(skip_channel=True, metric_name="ppv", calculation_method=True)
  35. metric.clear()
  36. with pytest.raises(ValueError):
  37. metric.update(x)
  38. def test_confusion_matrix_metric_update_dim():
  39. x = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.2], [0.9, 0.6, 0.5]]))
  40. y = Tensor(np.array([1, 0]))
  41. metric = ConfusionMatrixMetric(skip_channel=True, metric_name="tnr", calculation_method=True)
  42. metric.clear()
  43. with pytest.raises(ValueError):
  44. metric.update(y, x)
  45. def test_confusion_matrix_metric_init_skip_channel():
  46. with pytest.raises(TypeError):
  47. ConfusionMatrixMetric(skip_channel=1)
  48. def test_confusion_matrix_metric_init_compute_sample():
  49. with pytest.raises(TypeError):
  50. ConfusionMatrixMetric(calculation_method=1)
  51. def test_confusion_matrix_metric_init_metric_name_type():
  52. with pytest.raises(TypeError):
  53. metric = ConfusionMatrixMetric(skip_channel=True, metric_name=1, calculation_method=False)
  54. x = Tensor(np.array([[[0], [1]], [[1], [0]]]))
  55. y = Tensor(np.array([[[0], [1]], [[1], [0]]]))
  56. metric.update(x, y)
  57. output = metric.eval()
  58. assert np.allclose(output, np.array([0.75]))
  59. def test_confusion_matrix_metric_init_metric_name_str():
  60. with pytest.raises(NotImplementedError):
  61. metric = ConfusionMatrixMetric(skip_channel=True, metric_name="wwwww", calculation_method=False)
  62. x = Tensor(np.array([[[0], [1]], [[1], [0]]]))
  63. y = Tensor(np.array([[[0], [1]], [[1], [0]]]))
  64. metric.update(x, y)
  65. output = metric.eval()
  66. assert np.allclose(output, np.array([0.75]))
  67. def test_confusion_matrix_metric_runtime():
  68. metric = ConfusionMatrixMetric(skip_channel=True, metric_name="tnr", calculation_method=True)
  69. metric.clear()
  70. with pytest.raises(RuntimeError):
  71. metric.eval()