Ejemplo n.º 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
Ejemplo n.º 2
0
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
Ejemplo n.º 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
Ejemplo n.º 4
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