|
| 1 | +""" |
| 2 | +Lazy Captum class resolution for optional interpretation support. |
| 3 | +
|
| 4 | +Why this file exists |
| 5 | +-------------------- |
| 6 | +Captum is an optional dependency of edge2torch. This module imports Captum |
| 7 | +classes only when interpretation is actually executed. |
| 8 | +
|
| 9 | +Role in the package |
| 10 | +------------------- |
| 11 | +This is an internal dependency bridge. It maps validated Captum method names to |
| 12 | +Captum attribution classes. It should not validate public inputs, execute |
| 13 | +attribution, prepare input data, or map attribution results. |
| 14 | +""" |
| 15 | + |
| 16 | +from typing import Any |
| 17 | + |
| 18 | +from ..utils.errors import Edge2TorchError |
| 19 | + |
| 20 | + |
| 21 | +def get_captum_class(method: str) -> type[Any]: |
| 22 | + """ |
| 23 | + Return the Captum attribution class for a supported method name. |
| 24 | + """ |
| 25 | + try: |
| 26 | + from captum.attr import ( |
| 27 | + LRP, |
| 28 | + Deconvolution, |
| 29 | + DeepLift, |
| 30 | + DeepLiftShap, |
| 31 | + FeatureAblation, |
| 32 | + FeaturePermutation, |
| 33 | + GradientShap, |
| 34 | + GuidedBackprop, |
| 35 | + InputXGradient, |
| 36 | + IntegratedGradients, |
| 37 | + InternalInfluence, |
| 38 | + KernelShap, |
| 39 | + LayerActivation, |
| 40 | + LayerConductance, |
| 41 | + LayerDeepLift, |
| 42 | + LayerDeepLiftShap, |
| 43 | + LayerFeatureAblation, |
| 44 | + LayerFeaturePermutation, |
| 45 | + LayerGradientShap, |
| 46 | + LayerGradientXActivation, |
| 47 | + LayerIntegratedGradients, |
| 48 | + LayerLRP, |
| 49 | + Lime, |
| 50 | + Occlusion, |
| 51 | + Saliency, |
| 52 | + ShapleyValues, |
| 53 | + ShapleyValueSampling, |
| 54 | + ) |
| 55 | + except ImportError as exc: |
| 56 | + raise Edge2TorchError( |
| 57 | + "interpret_model() requires Captum, which is an optional " |
| 58 | + "dependency. Install interpretation support with " |
| 59 | + "'pip install \"edge2torch[interpret]\"'." |
| 60 | + ) from exc |
| 61 | + |
| 62 | + classes: dict[str, type[Any]] = { |
| 63 | + "IntegratedGradients": IntegratedGradients, |
| 64 | + "DeepLift": DeepLift, |
| 65 | + "DeepLiftShap": DeepLiftShap, |
| 66 | + "GradientShap": GradientShap, |
| 67 | + "Saliency": Saliency, |
| 68 | + "InputXGradient": InputXGradient, |
| 69 | + "GuidedBackprop": GuidedBackprop, |
| 70 | + "Deconvolution": Deconvolution, |
| 71 | + "FeatureAblation": FeatureAblation, |
| 72 | + "Occlusion": Occlusion, |
| 73 | + "FeaturePermutation": FeaturePermutation, |
| 74 | + "ShapleyValueSampling": ShapleyValueSampling, |
| 75 | + "ShapleyValues": ShapleyValues, |
| 76 | + "Lime": Lime, |
| 77 | + "KernelShap": KernelShap, |
| 78 | + "LRP": LRP, |
| 79 | + "LayerActivation": LayerActivation, |
| 80 | + "LayerConductance": LayerConductance, |
| 81 | + "InternalInfluence": InternalInfluence, |
| 82 | + "LayerGradientXActivation": LayerGradientXActivation, |
| 83 | + "LayerDeepLift": LayerDeepLift, |
| 84 | + "LayerDeepLiftShap": LayerDeepLiftShap, |
| 85 | + "LayerGradientShap": LayerGradientShap, |
| 86 | + "LayerIntegratedGradients": LayerIntegratedGradients, |
| 87 | + "LayerFeatureAblation": LayerFeatureAblation, |
| 88 | + "LayerFeaturePermutation": LayerFeaturePermutation, |
| 89 | + "LayerLRP": LayerLRP, |
| 90 | + } |
| 91 | + |
| 92 | + return classes[method] |
0 commit comments