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_occlusion_sensitivity.py 3.0 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_occlusion_sensitivity"""
  16. import pytest
  17. import numpy as np
  18. from mindspore import nn
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.nn.metrics import OcclusionSensitivity
  21. class DenseNet(nn.Cell):
  22. def __init__(self):
  23. super(DenseNet, self).__init__()
  24. w = np.array([[0.1, 0.8, 0.1, 0.1], [1, 1, 1, 1]]).astype(np.float32)
  25. b = np.array([0.3, 0.6]).astype(np.float32)
  26. self.dense = nn.Dense(4, 2, weight_init=Tensor(w), bias_init=Tensor(b))
  27. def construct(self, x):
  28. return self.dense(x)
  29. model = DenseNet()
  30. def test_occlusion_sensitivity():
  31. """test_occlusion_sensitivity"""
  32. test_data = np.array([[0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
  33. label = np.array(1).astype(np.int32)
  34. metric = OcclusionSensitivity()
  35. metric.clear()
  36. metric.update(model, test_data, label)
  37. score = metric.eval()
  38. assert np.allclose(score, np.array([0.2, 0.2, 0.2, 0.2]))
  39. def test_occlusion_sensitivity_indexes_awareness():
  40. """A indexes aware version of test_occlusion_sensitivity"""
  41. test_data = np.array([[0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
  42. test_data2 = np.array([[0.2, 0.3, 0.1, 0.4]]).astype(np.float32)
  43. label = np.array(1).astype(np.int32)
  44. metric = OcclusionSensitivity().set_indexes([0, 2, 3])
  45. metric.clear()
  46. metric.update(model, test_data, test_data2, label)
  47. score = metric.eval()
  48. assert np.allclose(score, np.array([0.3, 0.3, 0.3, 0.3]))
  49. def test_occlusion_sensitivity_update1():
  50. """test_occlusion_sensitivity_update1"""
  51. test_data = np.array([[5, 8], [3, 2], [4, 2]])
  52. metric = OcclusionSensitivity()
  53. metric.clear()
  54. with pytest.raises(ValueError):
  55. metric.update(test_data)
  56. def test_occlusion_sensitivity_init1():
  57. """test_occlusion_sensitivity_init1"""
  58. with pytest.raises(TypeError):
  59. OcclusionSensitivity(pad_val=False, margin=2, n_batch=128, b_box=None)
  60. def test_occlusion_sensitivity_init2():
  61. """test_occlusion_sensitivity_init2"""
  62. with pytest.raises(TypeError):
  63. OcclusionSensitivity(pad_val=0.0, margin=True, n_batch=128, b_box=None)
  64. def test_occlusion_sensitivity_runtime():
  65. """test_occlusion_sensitivity_runtime"""
  66. metric = OcclusionSensitivity()
  67. metric.clear()
  68. with pytest.raises(RuntimeError):
  69. metric.eval()