Exemple #1
0
    def __init__(self,
                 name: str,
                 predictor_host: str,
                 method: ExplainerMethod,
                 config: Mapping,
                 explainer: object = None,
                 protocol: Protocol = Protocol.seldon_grpc,
                 tf_data_type: str = None,
                 keras_model: keras.Model = None):
        super().__init__(name)
        self.predictor_host = predictor_host
        logging.info("Predict URL set to %s", self.predictor_host)
        self.method = method
        self.protocol = protocol
        self.tf_data_type = tf_data_type
        logging.info("Protocol is %s", str(self.protocol))

        # Add type for first value to help pass mypy type checks
        if self.method is ExplainerMethod.anchor_tabular:
            self.wrapper: ExplainerWrapper = AnchorTabular(
                self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.anchor_images:
            self.wrapper = AnchorImages(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.anchor_text:
            self.wrapper = AnchorText(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.kernel_shap:
            self.wrapper = KernelShap(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.integrated_gradients:
            self.wrapper = IntegratedGradients(keras_model, **config)
        elif self.method is ExplainerMethod.tree_shap:
            self.wrapper = TreeShap(explainer, **config)
        else:
            raise NotImplementedError
Exemple #2
0
def test_anchor_tabular():
    skmodel = SKLearnServer(IRIS_MODEL_URI)
    skmodel.load()

    with tempfile.TemporaryDirectory() as alibi_model_dir:
        make_anchor_tabular(alibi_model_dir)
        alibi_model = load_explainer(predictor=skmodel.predict,
                                     path=alibi_model_dir)
        anchor_tabular = AnchorTabular(alibi_model)

    test_data = np.array([[5.964, 4.006, 2.081, 1.031]])
    explanation = anchor_tabular.explain(test_data)
    explanation_json = json.loads(explanation.to_json())
    assert explanation_json["meta"]["name"] == "AnchorTabular"
def test_anchor_tabular():

    alibi_model = os.path.join(
        kfserving.Storage.download(ADULT_EXPLAINER_URI), EXPLAINER_FILENAME
    )
    with open(alibi_model, "rb") as f:
        skmodel = SKLearnServer(ADULT_MODEL_URI)
        skmodel.load()
        alibi_model = dill.load(f)
        anchor_tabular = AnchorTabular(skmodel.predict, alibi_model)
        adult = fetch_adult()
        X_test = adult.data[30001:, :]
        np.random.seed(0)
        explanation = anchor_tabular.explain(X_test[0:1].tolist())
        exp_json = json.loads(explanation.to_json())
        assert exp_json["data"]["anchor"][0] == "Marital Status = Never-Married"
Exemple #4
0
    def __init__(self,
                 name: str,
                 predict_url: str,
                 protocol: Protocol,
                 method: ExplainerMethod,
                 config: Mapping,
                 explainer: object = None):
        super().__init__(name)
        self.predict_url = predict_url
        self.protocol = protocol
        self.method = method

        if self.method is ExplainerMethod.anchor_tabular:
            self.wrapper = AnchorTabular(self._predict_fn,explainer,**config)
        else:
            raise NotImplementedError
Exemple #5
0
 def __init__(self,
              name: str,
              predict_url: str,
              protocol: Protocol,
              method: ExplainerMethod,
              training_data_url: str = None):
     super().__init__(name)
     self.predict_url = predict_url
     self.protocol = protocol
     self.method = method
     self.training_data_url = training_data_url
     if self.method is ExplainerMethod.anchor_tabular:
         self.explainer: ExplainerMethodImpl = AnchorTabular(
             self._predict_fn)
         self.explainer.validate(self.training_data_url)
     else:
         raise NotImplementedError
def test_anchor_tabular():
    os.environ.clear()
    alibi_model = os.path.join(kserve.Storage.download(ADULT_EXPLAINER_URI),
                               EXPLAINER_FILENAME)
    with open(alibi_model, "rb") as f:
        skmodel = SKLearnModel("adult", ADULT_MODEL_URI)
        skmodel.load()
        predictor = Predictor(skmodel)
        alibi_model = dill.load(f)
        anchor_tabular = AnchorTabular(predictor.predict_fn, alibi_model)
        adult = fetch_adult()
        X_test = adult.data[30001:, :]
        np.random.seed(0)
        explanation = anchor_tabular.explain(X_test[0:1].tolist())
        exp_json = json.loads(explanation.to_json())
        assert exp_json["data"]["anchor"][0] == "Relationship = Own-child" or \
               exp_json["data"]["anchor"][0] == "Age <= 28.00"
Exemple #7
0
    def __init__(  # pylint:disable=too-many-arguments
        self,
        name: str,
        predictor_host: str,
        method: ExplainerMethod,
        config: Mapping,
        explainer: object = None,
    ):
        super().__init__(name)
        self.predictor_host = predictor_host
        logging.info("Predict URL set to %s", self.predictor_host)
        self.method = method

        if self.method is ExplainerMethod.anchor_tabular:
            self.wrapper: ExplainerWrapper = AnchorTabular(
                self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.anchor_images:
            self.wrapper = AnchorImages(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.anchor_text:
            self.wrapper = AnchorText(self._predict_fn, explainer, **config)
        else:
            raise NotImplementedError
Exemple #8
0
    def __init__(self,
                 name: str,
                 predictor_host: str,
                 protocol: Protocol,
                 method: ExplainerMethod,
                 config: Mapping,
                 explainer: object = None):
        super().__init__(name)
        self.protocol = protocol
        if self.protocol == Protocol.tensorflow_http:
            self.predict_url = PREDICTOR_URL_FORMAT.format(predictor_host, name)
        else:
            self.predict_url = SELDON_PREDICTOR_URL_FORMAT.format(predictor_host)
        logging.info("Predict URL set to %s", self.predict_url)
        self.method = method

        if self.method is ExplainerMethod.anchor_tabular:
            self.wrapper = AnchorTabular(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.anchor_images:
            self.wrapper = AnchorImages(self._predict_fn, explainer, **config)
        elif self.method is ExplainerMethod.anchor_text:
            self.wrapper = AnchorText(self._predict_fn, explainer, **config)
        else:
            raise NotImplementedError