-
Notifications
You must be signed in to change notification settings - Fork 5
/
limee.py
77 lines (60 loc) · 2.36 KB
/
limee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Implementation of a wrapper around LIME.
Handles implementation of the LIME (Local Interpretable Model-Agnostic Explanations)
wrapper for the analysis of how important various parts of input are. Current implementation
supports unimodal explanations with specified type for one sample, keeping other
inputs constant.
"""
from typing import Callable, Dict
import numpy as np
from lime.lime_image import LimeImageExplainer
from lime.lime_text import LimeTextExplainer
class Lime:
"""Implementation of wrapper around LIME (Local Interpretable Model-Agnostic Explanations)."""
@classmethod
def explain_image_instance(
cls,
predictor_fn: Callable,
input_arr: np.ndarray,
init_params: Dict = {},
explanation_params: Dict = {},
):
"""Explain image instance.
Args:
predictor_fn: Predictor function which takes in a batch of inputs (not single input).
input_arr: Input image as an array without a batch dimension.
init_params: Initialization parameters for LIME Explainer.
explanation_params: Parameters for LIME Explainer.
Returns:
lime.lime_image.ImageExplanation:
Explanation of the image with given arguments
"""
# Create LIME explainer
lime_explainer = LimeImageExplainer(**init_params)
# Run LIME explainer
exp = lime_explainer.explain_instance(
input_arr, predictor_fn, **explanation_params
)
return exp
@classmethod
def explain_text_instance(
cls,
predictor_fn: Callable,
text: str,
init_params: Dict = {},
explanation_params: Dict = {},
):
"""Explain text instance.
Args:
predictor_fn: Predictor function which takes in a batch of inputs (not single input).
text: Input text as a string.
init_params: Initialization parameters for LIME Explainer.
explanation_params: Parameters for LIME Explainer.
Returns:
lime.lime_text.TextExplanation:
Explanation of the text with given arguments
"""
# Create LIME explainer
lime_explainer = LimeTextExplainer(**init_params)
# Run LIME explainer
exp = lime_explainer.explain_instance(text, predictor_fn, **explanation_params)
return exp