Skip to content

Commit

Permalink
Fix target type (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
joannasendorek committed Oct 18, 2023
1 parent 0fe28c6 commit 11c627e
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 205 deletions.
4 changes: 2 additions & 2 deletions foxai/explainer/base_explainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import torch

from foxai.types import AttributionsType, ModelType, TargetType
from foxai.types import AttributionsType, ModelType


class Explainer(ABC):
Expand All @@ -16,7 +16,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor, # TODO: add more generic way of passing model inputs # pylint: disable = (fixme)
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
**kwargs,
) -> AttributionsType:
"""Calculate features of given explainer.
Expand Down
22 changes: 2 additions & 20 deletions foxai/explainer/computer_vision/algorithm/conductance.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
get_last_conv_model_layer,
preprocess_baselines,
)
from foxai.types import AttributionsType, LayerType, ModelType, TargetType
from foxai.types import AttributionsType, LayerType, ModelType


class LayerConductanceCVExplainer(Explainer):
Expand Down Expand Up @@ -49,7 +49,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
baselines: Optional[BaselineType] = None,
additional_forward_args: Any = None,
n_steps: int = 50,
Expand All @@ -72,24 +72,6 @@ def calculate_features(
this is usually the target class).
If the network returns a scalar value per example,
no target index is necessary.
For general 2D outputs, targets can be either:
- a single integer or a tensor containing a single
integer, which is applied to all input examples
- a list of integers or a 1D tensor, with length matching
the number of examples in inputs (dim 0). Each integer
is applied as the target for the corresponding example.
For outputs with > 2 dimensions, targets can be either:
- A single tuple, which contains #output_dims - 1
elements. This target index is applied to all examples.
- A list of tuples with length equal to the number of
examples in inputs (dim 0), and each tuple containing
#output_dims - 1 elements. Each tuple is applied as the
target for the corresponding example.
Default: None
baselines:
Baselines define the starting point from which integral
Expand Down
22 changes: 2 additions & 20 deletions foxai/explainer/computer_vision/algorithm/deconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from foxai.array_utils import validate_result
from foxai.explainer.base_explainer import Explainer
from foxai.explainer.computer_vision.model_utils import modify_modules
from foxai.types import AttributionsType, ModelType, TargetType
from foxai.types import AttributionsType, ModelType


class BaseDeconvolutionCVExplainer(Explainer):
Expand All @@ -37,7 +37,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
additional_forward_args: Any = None,
**kwargs,
) -> AttributionsType:
Expand All @@ -54,24 +54,6 @@ def calculate_features(
this is usually the target class).
If the network returns a scalar value per example,
no target index is necessary.
For general 2D outputs, targets can be either:
- a single integer or a tensor containing a single
integer, which is applied to all input examples
- a list of integers or a 1D tensor, with length matching
the number of examples in inputs (dim 0). Each integer
is applied as the target for the corresponding example.
For outputs with > 2 dimensions, targets can be either:
- A single tuple, which contains #output_dims - 1
elements. This target index is applied to all examples.
- A list of tuples with length equal to the number of
examples in inputs (dim 0), and each tuple containing
#output_dims - 1 elements. Each tuple is applied as the
target for the corresponding example.
Default: None
additional_forward_args: If the forward function
requires additional arguments other than the inputs for
Expand Down
3 changes: 1 addition & 2 deletions foxai/explainer/computer_vision/algorithm/deeplift.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
CustomAttributionFuncType,
LayerType,
ModelType,
TargetType,
)


Expand Down Expand Up @@ -66,7 +65,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
baselines: Optional[BaselineType] = None,
additional_forward_args: Any = None,
custom_attribution_func: Optional[CustomAttributionFuncType] = None,
Expand Down
3 changes: 1 addition & 2 deletions foxai/explainer/computer_vision/algorithm/deeplift_shap.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
CustomAttributionFuncType,
LayerType,
ModelType,
TargetType,
)


Expand Down Expand Up @@ -66,7 +65,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
baselines: Optional[BaselineType] = None,
additional_forward_args: Any = None,
custom_attribution_func: Optional[CustomAttributionFuncType] = None,
Expand Down
28 changes: 4 additions & 24 deletions foxai/explainer/computer_vision/algorithm/gradcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
BaseObjectDetector,
)
from foxai.explainer.computer_vision.object_detection.types import ObjectDetectionOutput
from foxai.types import AttributionsType, LayerType, ModelType, TargetType
from foxai.types import AttributionsType, LayerType, ModelType


class LayerBaseGradCAM:
Expand Down Expand Up @@ -168,7 +168,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
**kwargs,
) -> AttributionsType:
"""Generate features image with GradCAM algorithm explainer.
Expand Down Expand Up @@ -251,7 +251,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
additional_forward_args: Any = None,
attribute_to_layer_input: bool = False,
interpolate_mode: str = "nearest",
Expand Down Expand Up @@ -394,9 +394,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[
TargetType
] = None, # pylint: disable = (unused-argument)
pred_label_idx: Optional[int] = None, # pylint: disable = (unused-argument)
layer: Optional[LayerType] = None,
**kwargs,
) -> AttributionsType:
Expand All @@ -418,24 +416,6 @@ def calculate_features(
this is usually the target class).
If the network returns a scalar value per example,
no target index is necessary.
For general 2D outputs, targets can be either:
- a single integer or a tensor containing a single
integer, which is applied to all input examples
- a list of integers or a 1D tensor, with length matching
the number of examples in inputs (dim 0). Each integer
is applied as the target for the corresponding example.
For outputs with > 2 dimensions, targets can be either:
- A single tuple, which contains #output_dims - 1
elements. This target index is applied to all examples.
- A list of tuples with length equal to the number of
examples in inputs (dim 0), and each tuple containing
#output_dims - 1 elements. Each tuple is applied as the
target for the corresponding example.
Default: None
layer: Layer for which attributions are computed.
If None provided, last convolutional layer from the model
Expand Down
23 changes: 2 additions & 21 deletions foxai/explainer/computer_vision/algorithm/gradient_shap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
get_last_conv_model_layer,
preprocess_baselines,
)
from foxai.types import AttributionsType, LayerType, ModelType, StdevsType, TargetType
from foxai.types import AttributionsType, LayerType, ModelType, StdevsType


class BaseGradientSHAPCVExplainer(Explainer):
Expand Down Expand Up @@ -57,7 +57,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
baselines: Optional[BaselineType] = None,
n_samples: int = 5,
stdevs: StdevsType = 0.0,
Expand All @@ -79,25 +79,6 @@ def calculate_features(
this is usually the target class).
If the network returns a scalar value per example,
no target index is necessary.
For general 2D outputs, targets can be either:
- a single integer or a tensor containing a single
integer, which is applied to all input examples
- a list of integers or a 1D tensor, with length matching
the number of examples in inputs (dim 0). Each integer
is applied as the target for the corresponding example.
For outputs with > 2 dimensions, targets can be either:
- A single tuple, which contains #output_dims - 1
elements. This target index is applied to all examples.
- A list of tuples with length equal to the number of
examples in inputs (dim 0), and each tuple containing
#output_dims - 1 elements. Each tuple is applied as the
target for the corresponding example.
Default: None
baselines:
Baselines define the starting point from which expectation
Expand Down
23 changes: 2 additions & 21 deletions foxai/explainer/computer_vision/algorithm/guided_backprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from foxai.array_utils import validate_result
from foxai.explainer.base_explainer import Explainer
from foxai.explainer.computer_vision.model_utils import modify_modules
from foxai.types import AttributionsType, ModelType, TargetType
from foxai.types import AttributionsType, ModelType


class BaseGuidedBackpropCVExplainer(Explainer):
Expand All @@ -34,7 +34,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
additional_forward_args: Any = None,
**kwargs,
) -> AttributionsType:
Expand All @@ -57,25 +57,6 @@ def calculate_features(
this is usually the target class).
If the network returns a scalar value per example,
no target index is necessary.
For general 2D outputs, targets can be either:
- a single integer or a tensor containing a single
integer, which is applied to all input examples
- a list of integers or a 1D tensor, with length matching
the number of examples in inputs (dim 0). Each integer
is applied as the target for the corresponding example.
For outputs with > 2 dimensions, targets can be either:
- A single tuple, which contains #output_dims - 1
elements. This target index is applied to all examples.
- A list of tuples with length equal to the number of
examples in inputs (dim 0), and each tuple containing
#output_dims - 1 elements. Each tuple is applied as the
target for the corresponding example.
Default: None
additional_forward_args: If the forward function
requires additional arguments other than the inputs for
Expand Down
23 changes: 2 additions & 21 deletions foxai/explainer/computer_vision/algorithm/integrated_gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
get_last_conv_model_layer,
preprocess_baselines,
)
from foxai.types import AttributionsType, LayerType, ModelType, TargetType
from foxai.types import AttributionsType, LayerType, ModelType


class BaseIntegratedGradientsCVExplainer(Explainer):
Expand Down Expand Up @@ -61,7 +61,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
baselines: Optional[BaselineType] = None,
additional_forward_args: Any = None,
n_steps: int = 50,
Expand All @@ -84,25 +84,6 @@ def calculate_features(
this is usually the target class).
If the network returns a scalar value per example,
no target index is necessary.
For general 2D outputs, targets can be either:
- a single integer or a tensor containing a single
integer, which is applied to all input examples
- a list of integers or a 1D tensor, with length matching
the number of examples in inputs (dim 0). Each integer
is applied as the target for the corresponding example.
For outputs with > 2 dimensions, targets can be either:
- A single tuple, which contains #output_dims - 1
elements. This target index is applied to all examples.
- A list of tuples with length equal to the number of
examples in inputs (dim 0), and each tuple containing
#output_dims - 1 elements. Each tuple is applied as the
target for the corresponding example.
Default: None
baselines:
Baselines define the starting point from which integral
Expand Down
23 changes: 2 additions & 21 deletions foxai/explainer/computer_vision/algorithm/noise_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from foxai.array_utils import validate_result
from foxai.explainer.base_explainer import Explainer
from foxai.explainer.computer_vision.model_utils import get_last_conv_model_layer
from foxai.types import AttributionsType, LayerType, ModelType, StdevsType, TargetType
from foxai.types import AttributionsType, LayerType, ModelType, StdevsType


class BaseNoiseTunnelCVExplainer(Explainer):
Expand All @@ -34,7 +34,7 @@ def calculate_features(
self,
model: ModelType,
input_data: torch.Tensor,
pred_label_idx: Optional[TargetType] = None,
pred_label_idx: Optional[int] = None,
nt_type: str = "smoothgrad",
nt_samples: int = 5,
nt_samples_batch_size: Optional[int] = None,
Expand All @@ -56,25 +56,6 @@ def calculate_features(
this is usually the target class).
If the network returns a scalar value per example,
no target index is necessary.
For general 2D outputs, targets can be either:
- a single integer or a tensor containing a single
integer, which is applied to all input examples
- a list of integers or a 1D tensor, with length matching
the number of examples in inputs (dim 0). Each integer
is applied as the target for the corresponding example.
For outputs with > 2 dimensions, targets can be either:
- A single tuple, which contains #output_dims - 1
elements. This target index is applied to all examples.
- A list of tuples with length equal to the number of
examples in inputs (dim 0), and each tuple containing
#output_dims - 1 elements. Each tuple is applied as the
target for the corresponding example.
Default: None
nt_type: Smoothing type of the attributions.
`smoothgrad`, `smoothgrad_sq` or `vargrad`
Expand Down
Loading

0 comments on commit 11c627e

Please sign in to comment.