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