mindspore.nn.OcclusionSensitivity

class mindspore.nn.OcclusionSensitivity(pad_val=0.0, margin=2, n_batch=128, b_box=None)[source]

This function is used to calculate the occlusion sensitivity of the model for a given image. Occlusion sensitivity refers to how the probability of a given prediction changes with the change of the occluded part of the image.

For a given result, the output probability is the probability of a region.

The higher the value in the output image, the greater the decline of certainty, indicating that the occluded area is more important in the decision-making process.

Parameters
  • pad_val (float) – What values need to be entered in the image when a part of the image is occluded. Default: 0.0.

  • margin (Union[int, Sequence]) – Create a cuboid / cube around the voxel you want to occlude. Default: 2.

  • n_batch (int) – number of images in a batch before inference. Default: 128.

  • b_box (Sequence) – Bounding box on which to perform the analysis. The output image will also match in size. There should be a minimum and maximum for all dimensions except batch: [min1, max1, min2, max2,...]. If no bounding box is supplied, this will be the same size as the input image. If a bounding box is used, the output image will be cropped to this size. Default: None.

Supported Platforms:

Ascend GPU CPU

Example

>>> import numpy as np
>>> from mindspore import nn, Tensor
>>>
>>> class DenseNet(nn.Cell):
...     def __init__(self):
...         super(DenseNet, self).__init__()
...         w = np.array([[0.1, 0.8, 0.1, 0.1],[1, 1, 1, 1]]).astype(np.float32)
...         b = np.array([0.3, 0.6]).astype(np.float32)
...         self.dense = nn.Dense(4, 2, weight_init=Tensor(w), bias_init=Tensor(b))
...
...     def construct(self, x):
...         return self.dense(x)
>>>
>>> model = DenseNet()
>>> test_data = np.array([[0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
>>> label = np.array(1).astype(np.int32)
>>> metric = nn.OcclusionSensitivity()
>>> metric.clear()
>>> metric.update(model, test_data, label)
>>> score = metric.eval()
>>> print(score)
[0.29999995    0.6    1    0.9]
clear()[source]

Clears the internal evaluation result.

eval()[source]

Computes the occlusion_sensitivity.

Returns

A numpy ndarray.

Raises

RuntimeError – If the update method is not called first, an error will be reported.

update(*inputs)[source]

Updates input, including model, y_pred and label.

Inputs:
  • model (nn.Cell) - classification model to use for inference.

  • y_pred (Union[Tensor, list, np.ndarray]) - image to test. Should be tensor consisting of 1 batch, can be 2- or 3D.

  • label (Union[int, Tensor]) - classification label to check for changes (normally the true label, but doesn’t have to be

Raises
  • ValueError – If the number of input is not 3.

  • RuntimeError – If the batch size is not 1.

  • RuntimeError – If the number of labels is different from the number of batches.