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 2.5 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_update1():
  40. """test_occlusion_sensitivity_update1"""
  41. test_data = np.array([[5, 8], [3, 2], [4, 2]])
  42. metric = OcclusionSensitivity()
  43. metric.clear()
  44. with pytest.raises(ValueError):
  45. metric.update(test_data)
  46. def test_occlusion_sensitivity_init1():
  47. """test_occlusion_sensitivity_init1"""
  48. with pytest.raises(TypeError):
  49. OcclusionSensitivity(pad_val=False, margin=2, n_batch=128, b_box=None)
  50. def test_occlusion_sensitivity_init2():
  51. """test_occlusion_sensitivity_init2"""
  52. with pytest.raises(TypeError):
  53. OcclusionSensitivity(pad_val=0.0, margin=True, n_batch=128, b_box=None)
  54. def test_occlusion_sensitivity_runtime():
  55. """test_occlusion_sensitivity_runtime"""
  56. metric = OcclusionSensitivity()
  57. metric.clear()
  58. with pytest.raises(RuntimeError):
  59. metric.eval()