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