Exemple #1
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"
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 #4
0
class AlibiExplainer(kfserving.KFModel):
    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

    def load(self):
        pass

    def _predict_fn(self, arr: np.ndarray) -> np.ndarray:
        if self.protocol == Protocol.seldon_http:
            payload = seldon.create_request(arr, seldon.SeldonPayload.NDARRAY)
            response_raw = requests.post(self.predict_url, json=payload)
            if response_raw.status_code == 200:
                rh = SeldonRequestHandler(response_raw.json())
                response_list = rh.extract_request()
                return np.array(response_list)
            else:
                raise Exception("Failed to get response from model return_code:%d" % response_raw.status_code)
        else:
            raise NotImplementedError

    def explain(self, inputs: List) -> Any:
        if self.method is ExplainerMethod.anchor_tabular:
            explaination = self.wrapper.explain(inputs)
            return json.loads(json.dumps(explaination, cls=NumpyEncoder))
        else:
            raise NotImplementedError