mindspore.nn.OcclusionSensitivity

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

Calculates the occlusion sensitivity of the model for a given image. It illustrates which parts of an image are most important for a network’s classification.

Occlusion sensitivity refers to how the predicted probability changes with the change of the occluded part of an image. The higher the value in the output image is, the greater the decline of certainty, indicating that the occluded area is more important in the decision-making process.

Parameters
  • pad_val (float) – The padding value of the occluded part in an image. 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. 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

Examples

>>> 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.

Parameters

inputsy_pred and label are a Tensor, list or numpy.ndarray. y_pred: a batch of images to test, which could be 2D or 3D. label: classification labels to check for changes. label is normally the true label, but doesn’t have to be. model is the neural network.

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

  • RuntimeError – If y_pred.shape[0] is not 1.

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