Example #1
0
def test_anchor_images():
    os.environ.clear()
    alibi_model = os.path.join(
        kfserving.Storage.download(IMAGENET_EXPLAINER_URI), EXPLAINER_FILENAME)
    with open(alibi_model, "rb") as f:
        model = InceptionV3(weights="imagenet")
        predictor = lambda x: model.predict(x)  # pylint:disable=unnecessary-lambda
        alibi_model = dill.load(f)
        anchor_images = AnchorImages(predictor,
                                     alibi_model,
                                     batch_size=25,
                                     stop_on_first=True)
        category = "Persian cat"
        image_shape = (299, 299, 3)
        data, _ = fetch_imagenet(category,
                                 nb_images=10,
                                 target_size=image_shape[:2],
                                 seed=2,
                                 return_X_y=True)
        images = preprocess_input(data)
        print(images.shape)
        np.random.seed(0)
        explanation = anchor_images.explain(images[0:1])
        exp_json = json.loads(explanation.to_json())
        assert exp_json["data"]["precision"] > 0.9
def test_cifar10_images():  # pylint: disable-msg=too-many-locals
    alibi_model = make_anchor_image()
    anchor_images = AnchorImages(alibi_model)

    _, test = tf.keras.datasets.cifar10.load_data()
    X_test, _ = test
    X_test = X_test.astype("float32") / 255
    idx = 12
    test_example = X_test[idx : idx + 1]

    np.random.seed(0)
    explanation = anchor_images.explain(test_example)
    exp_json = json.loads(explanation.to_json())
    assert exp_json["data"]["precision"] > 0.9
Example #3
0
def test_anchor_images():
    os.environ.clear()
    alibi_model = os.path.join(
        kfserving.Storage.download(IMAGENET_EXPLAINER_URI), EXPLAINER_FILENAME)
    with open(alibi_model, "rb") as f:
        model = InceptionV3(weights="imagenet")
        predictor = lambda x: model.predict(x)  # pylint:disable=unnecessary-lambda
        alibi_model = dill.load(f)
        anchor_images = AnchorImages(predictor,
                                     alibi_model,
                                     batch_size=25,
                                     stop_on_first=True)
        category = "Persian cat"
        image_shape = (299, 299, 3)
        # the image downloader comes from seldonio/alibi
        # https://github.com/SeldonIO/alibi/blob/76e6192b6d78848dd47c11ba6f6348ca94c424c6/alibi/datasets.py#L104-L125
        img_urls = json.load(open('alibiexplainer/tests/persian_cat.json'))
        seed = 2
        random.seed(seed)
        random.shuffle(img_urls)
        data = []
        nb = 0
        nb_images = 10
        target_size = image_shape[:2]
        min_std = 10.
        for img_url in img_urls:
            try:
                resp = requests.get(img_url, timeout=2)
                resp.raise_for_status()
            except RequestException:
                continue
            try:
                image = PIL.Image.open(BytesIO(resp.content)).convert('RGB')
            except OSError:
                continue
            image = np.expand_dims(image.resize(target_size), axis=0)
            if np.std(image) < min_std:  # do not include empty images
                continue
            data.append(image)
            nb += 1
            if nb == nb_images:
                break
        data = np.concatenate(data, axis=0)
        images = preprocess_input(data)
        print(images.shape)
        np.random.seed(0)
        explanation = anchor_images.explain(images[0:1])
        exp_json = json.loads(explanation.to_json())
        assert exp_json["data"]["precision"] > 0.9
Example #4
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
Example #5
0
def test_cifar10_images():  # pylint: disable-msg=too-many-locals

    alibi_model = os.path.join(
        kfserving.Storage.download(CIFAR10_EXPLAINER_URI), EXPLAINER_FILENAME
    )
    with open(alibi_model, "rb") as f:
        alibi_model = dill.load(f)
        url = "https://storage.googleapis.com/seldon-models/alibi-detect/classifier/"
        path_model = os.path.join(url, "cifar10", "resnet32", "model.h5")
        save_path = tf.keras.utils.get_file("resnet32", path_model)
        model = tf.keras.models.load_model(save_path)
        _, test = tf.keras.datasets.cifar10.load_data()
        X_test, _ = test
        X_test = X_test.astype("float32") / 255
        idx = 12
        test_example = X_test[idx: idx + 1]
        anchor_images = AnchorImages(
            lambda x: model.predict(x), alibi_model)  # pylint: disable-msg=unnecessary-lambda
        np.random.seed(0)
        explanation = anchor_images.explain(test_example)
        exp_json = json.loads(explanation.to_json())
        assert exp_json["data"]["precision"] > 0.9
Example #6
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
Example #7
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