| @@ -161,7 +161,7 @@ class ExplainRunner: | |||||
| Examples: | Examples: | ||||
| >>> from mindspore.explainer import ExplainRunner | >>> from mindspore.explainer import ExplainRunner | ||||
| >>> from mindspore.explainer.explanation import GuidedBackprop, Gradient | >>> from mindspore.explainer.explanation import GuidedBackprop, Gradient | ||||
| >>> from mindspore.nn import Sigmoid | |||||
| >>> from mindspore.nn import Softmax | |||||
| >>> from mindspore.train.serialization import load_checkpoint, load_param_into_net | >>> from mindspore.train.serialization import load_checkpoint, load_param_into_net | ||||
| >>> # Prepare the dataset for explaining and evaluation, e.g., Cifar10 | >>> # Prepare the dataset for explaining and evaluation, e.g., Cifar10 | ||||
| >>> dataset = get_dataset('/path/to/Cifar10_dataset') | >>> dataset = get_dataset('/path/to/Cifar10_dataset') | ||||
| @@ -172,9 +172,9 @@ class ExplainRunner: | |||||
| >>> load_param_into_net(net, param_dict) | >>> load_param_into_net(net, param_dict) | ||||
| >>> gbp = GuidedBackprop(net) | >>> gbp = GuidedBackprop(net) | ||||
| >>> gradient = Gradient(net) | >>> gradient = Gradient(net) | ||||
| >>> runner = ExplainRunner("./") | |||||
| >>> explainers = [gbp, gradient] | >>> explainers = [gbp, gradient] | ||||
| >>> runner.run((dataset, classes), explainers, activation_fn=Sigmoid()) | |||||
| >>> # runner is an ExplainRunner object | |||||
| >>> runner.run((dataset, classes), explainers, activation_fn=Softmax()) | |||||
| """ | """ | ||||
| check_value_type("dataset", dataset, tuple) | check_value_type("dataset", dataset, tuple) | ||||
| @@ -414,8 +414,10 @@ class Faithfulness(LabelSensitiveMetric): | |||||
| numpy.ndarray, 1D array of shape :math:`(N,)`, result of faithfulness evaluated on `explainer`. | numpy.ndarray, 1D array of shape :math:`(N,)`, result of faithfulness evaluated on `explainer`. | ||||
| Examples: | Examples: | ||||
| >>> # init an explainer, the network should contain the output activation function. | |||||
| >>> network = resnet50(20) | |||||
| >>> import numpy as np | |||||
| >>> import mindspore as ms | |||||
| >>> from mindspore.explainer.explanation import Gradient | |||||
| >>> # init an explainer with a trained network, e.g., resnet50 | |||||
| >>> gradient = Gradient(network) | >>> gradient = Gradient(network) | ||||
| >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | ||||
| >>> targets = 5 | >>> targets = 5 | ||||
| @@ -110,7 +110,10 @@ class Localization(LabelSensitiveMetric): | |||||
| numpy.ndarray, 1D array of shape :math:`(N,)`, result of localization evaluated on `explainer`. | numpy.ndarray, 1D array of shape :math:`(N,)`, result of localization evaluated on `explainer`. | ||||
| Examples: | Examples: | ||||
| >>> # init an explainer with a trained network | |||||
| >>> import numpy as np | |||||
| >>> import mindspore as ms | |||||
| >>> from mindspore.explainer.explanation import Gradient | |||||
| >>> # init an explainer with a trained network, e.g., resnet50 | |||||
| >>> gradient = Gradient(network) | >>> gradient = Gradient(network) | ||||
| >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | ||||
| >>> masks = np.zeros([1, 1, 224, 224]) | >>> masks = np.zeros([1, 1, 224, 224]) | ||||
| @@ -66,7 +66,8 @@ class GradCAM(IntermediateLayerAttribution): | |||||
| Examples: | Examples: | ||||
| >>> from mindspore.explainer.explanation import GradCAM | >>> from mindspore.explainer.explanation import GradCAM | ||||
| >>> net = resnet50(10) | |||||
| >>> from mindspore.train.serialization import load_checkpoint, load_param_into_net | |||||
| >>> network = resnet50(10) # please refer to model_zoo | |||||
| >>> param_dict = load_checkpoint("resnet50.ckpt") | >>> param_dict = load_checkpoint("resnet50.ckpt") | ||||
| >>> load_param_into_net(net, param_dict) | >>> load_param_into_net(net, param_dict) | ||||
| >>> # specify a layer name to generate explanation, usually the layer can be set as the last conv layer. | >>> # specify a layer name to generate explanation, usually the layer can be set as the last conv layer. | ||||
| @@ -111,6 +112,8 @@ class GradCAM(IntermediateLayerAttribution): | |||||
| Tensor, a 4D tensor of shape :math:`(N, 1, H, W)`. | Tensor, a 4D tensor of shape :math:`(N, 1, H, W)`. | ||||
| Examples: | Examples: | ||||
| >>> import mindspore as ms | |||||
| >>> import numpy as np | |||||
| >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | ||||
| >>> label = 5 | >>> label = 5 | ||||
| >>> # gradcam is a GradCAM object, parse data and the target label to be explained and get the attribution | >>> # gradcam is a GradCAM object, parse data and the target label to be explained and get the attribution | ||||
| @@ -61,7 +61,9 @@ class Gradient(Attribution): | |||||
| Examples: | Examples: | ||||
| >>> from mindspore.explainer.explanation import Gradient | >>> from mindspore.explainer.explanation import Gradient | ||||
| >>> net = resnet50(10) | |||||
| >>> from mindspore.train.serialization import load_checkpoint, load_param_into_net | |||||
| >>> # init Gradient with a trained network | |||||
| >>> net = resnet50(10) # please refer to model_zoo | |||||
| >>> param_dict = load_checkpoint("resnet50.ckpt") | >>> param_dict = load_checkpoint("resnet50.ckpt") | ||||
| >>> load_param_into_net(net, param_dict) | >>> load_param_into_net(net, param_dict) | ||||
| >>> gradient = Gradient(net) | >>> gradient = Gradient(net) | ||||
| @@ -89,6 +91,8 @@ class Gradient(Attribution): | |||||
| Tensor, a 4D tensor of shape :math:`(N, 1, H, W)`. | Tensor, a 4D tensor of shape :math:`(N, 1, H, W)`. | ||||
| Examples: | Examples: | ||||
| >>> import mindspore as ms | |||||
| >>> import numpy as np | |||||
| >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | ||||
| >>> label = 5 | >>> label = 5 | ||||
| >>> # gradient is a Gradient object, parse data and the target label to be explained and get the attribution | >>> # gradient is a Gradient object, parse data and the target label to be explained and get the attribution | ||||
| @@ -97,11 +97,14 @@ class Deconvolution(ModifiedReLU): | |||||
| network (Cell): The black-box model to be explained. | network (Cell): The black-box model to be explained. | ||||
| Examples: | Examples: | ||||
| >>> import numpy as np | |||||
| >>> import mindspore as ms | |||||
| >>> from mindspore.explainer.explanation import Deconvolution | >>> from mindspore.explainer.explanation import Deconvolution | ||||
| >>> net = resnet50(10) | |||||
| >>> from mindspore.train.serialization import load_checkpoint, load_param_into_net | |||||
| >>> net = resnet50(10) # please refer to model_zoo | |||||
| >>> param_dict = load_checkpoint("resnet50.ckpt") | >>> param_dict = load_checkpoint("resnet50.ckpt") | ||||
| >>> load_param_into_net(net, param_dict) | >>> load_param_into_net(net, param_dict) | ||||
| >>> # init Gradient with a trained network. | |||||
| >>> # init Deconvolution with a trained network. | |||||
| >>> deconvolution = Deconvolution(net) | >>> deconvolution = Deconvolution(net) | ||||
| >>> # parse data and the target label to be explained and get the saliency map | >>> # parse data and the target label to be explained and get the saliency map | ||||
| >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | ||||
| @@ -132,14 +135,17 @@ class GuidedBackprop(ModifiedReLU): | |||||
| network (Cell): The black-box model to be explained. | network (Cell): The black-box model to be explained. | ||||
| Examples: | Examples: | ||||
| >>> import numpy as np | |||||
| >>> import mindspore as ms | |||||
| >>> from mindspore.train.serialization import load_checkpoint, load_param_into_net | |||||
| >>> from mindspore.explainer.explanation import GuidedBackprop | >>> from mindspore.explainer.explanation import GuidedBackprop | ||||
| >>> net = resnet50(10) | |||||
| >>> net = resnet50(10) # please refer to model_zoo | |||||
| >>> param_dict = load_checkpoint("resnet50.ckpt") | >>> param_dict = load_checkpoint("resnet50.ckpt") | ||||
| >>> load_param_into_net(net, param_dict) | >>> load_param_into_net(net, param_dict) | ||||
| >>> # init Gradient with a trained network. | |||||
| >>> # init GuidedBackprop with a trained network. | |||||
| >>> gbp = GuidedBackprop(net) | >>> gbp = GuidedBackprop(net) | ||||
| >>> # parse data and the target label to be explained and get the saliency map | >>> # parse data and the target label to be explained and get the saliency map | ||||
| >>> inputs = ms.Tensor(np.random.rand([1, 3, 224, 224]), ms.float32) | |||||
| >>> inputs = ms.Tensor(np.random.rand(1, 3, 224, 224), ms.float32) | |||||
| >>> label = 5 | >>> label = 5 | ||||
| >>> saliency = gbp(inputs, label) | >>> saliency = gbp(inputs, label) | ||||
| """ | """ | ||||
| @@ -52,11 +52,14 @@ class RISE(PerturbationAttribution): | |||||
| Examples: | Examples: | ||||
| >>> from mindspore.explainer.explanation import RISE | >>> from mindspore.explainer.explanation import RISE | ||||
| >>> net = resnet50(10) | |||||
| >>> from mindspore.nn import Sigmoid | |||||
| >>> from mindspore.train.serialization import load_checkpoint, load_param_into_net | |||||
| >>> # init RISE with a trained network | |||||
| >>> net = resnet50(10) # please refer to model_zoo | |||||
| >>> param_dict = load_checkpoint("resnet50.ckpt") | >>> param_dict = load_checkpoint("resnet50.ckpt") | ||||
| >>> load_param_into_net(net, param_dict) | >>> load_param_into_net(net, param_dict) | ||||
| >>> # init RISE with specified activation function | >>> # init RISE with specified activation function | ||||
| >>> rise = RISE(net, activation_fn=nn.layer.Sigmoid()) | |||||
| >>> rise = RISE(net, activation_fn=Sigmoid()) | |||||
| """ | """ | ||||
| def __init__(self, | def __init__(self, | ||||
| @@ -113,6 +116,8 @@ class RISE(PerturbationAttribution): | |||||
| Tensor, a 4D tensor of shape :math:`(N, ?, H, W)` or :math:`(N, 1, H, W)`. | Tensor, a 4D tensor of shape :math:`(N, ?, H, W)` or :math:`(N, 1, H, W)`. | ||||
| Examples: | Examples: | ||||
| >>> import mindspore as ms | |||||
| >>> import numpy as np | |||||
| >>> # given an instance of RISE, saliency map can be generate | >>> # given an instance of RISE, saliency map can be generate | ||||
| >>> inputs = ms.Tensor(np.random.rand(2, 3, 224, 224), ms.float32) | >>> inputs = ms.Tensor(np.random.rand(2, 3, 224, 224), ms.float32) | ||||
| >>> # when `targets` is an integer | >>> # when `targets` is an integer | ||||