This page was generated from notebooks/explainer_examples.ipynb.

Example Model Explanations with Seldon

Setup Seldon Core

Follow the instructions to Setup Cluster with Ambassador Ingress and Install Seldon Core.

Running this notebook

For the ImageNet Model you will need:

This should install the required package dependencies, if not please also install: - Pillow package (pip install Pillow) - matplotlib package (pip install matplotlib) - tensorflow package (pip install tensorflow)

You will also need to start Jupyter with settings to allow for large payloads, for example:

jupyter notebook --NotebookApp.iopub_data_rate_limit=1000000000

Income Prediction Model

[3]:
!pygmentize resources/income_explainer.yaml
apiVersion: machinelearning.seldon.io/v1alpha2
kind: SeldonDeployment
metadata:
  name: income
spec:
  name: income
  predictors:
  - graph:
      children: []
      implementation: SKLEARN_SERVER
      modelUri: gs://seldon-models/sklearn/income/model
      name: classifier
    explainer:
      type: AnchorTabular
      modelUri: gs://seldon-models/sklearn/income/explainer
    name: default
    replicas: 1
[ ]:
!kubectl apply -f resources/income_explainer.yaml
[ ]:
!kubectl rollout status deploy/income-default-4903e3c
[ ]:
!kubectl rollout status deploy/income-default-explainer
[ ]:
from seldon_core.seldon_client import SeldonClient
import numpy as np
sc = SeldonClient(deployment_name="income",namespace="seldon")
[ ]:
data = np.array([[39, 7, 1, 1, 1, 1, 4, 1, 2174, 0, 40, 9]])
r = sc.predict(gateway="ambassador",transport="rest",data=data)
print(r)
[ ]:
data = np.array([[39, 7, 1, 1, 1, 1, 4, 1, 2174, 0, 40, 9]])
explanation = sc.explain(deployment_name="income",gateway="ambassador",transport="rest",data=data)
print(explanation["names"])
[ ]:
!kubectl delete -f resources/income_explainer.yaml

Movie Sentiment Model

[2]:
!pygmentize resources/moviesentiment_explainer.yaml
apiVersion: machinelearning.seldon.io/v1alpha2
kind: SeldonDeployment
metadata:
  name: movie
spec:
  name: movie
  predictors:
  - graph:
      children: []
      implementation: SKLEARN_SERVER
      modelUri: gs://seldon-models/sklearn/moviesentiment
      name: classifier
    explainer:
      type: AnchorText
    name: default
    replicas: 1
[ ]:
!kubectl apply -f resources/moviesentiment_explainer.yaml
[ ]:
!kubectl rollout status deploy/movie-default-4903e3c
[ ]:
!kubectl rollout status deploy/movie-default-explainer
[ ]:
from seldon_core.seldon_client import SeldonClient
import numpy as np
sc = SeldonClient(deployment_name="movie",namespace="seldon")
[ ]:
data = np.array(['this film has great actors'])
r = sc.predict(gateway="ambassador",transport="rest",data=data,payload_type='ndarray')
print(r)
[ ]:
data = np.array(['this film has great actors'])
explanation = sc.explain(deployment_name="movie",gateway="ambassador",transport="rest",data=data,payload_type='ndarray')
print(explanation)
[ ]:
!kubectl delete -f resources/moviesentiment_explainer.yaml

Imagenet Model

[1]:
!pygmentize resources/imagenet_explainer_grpc.yaml
apiVersion: machinelearning.seldon.io/v1alpha2
kind: SeldonDeployment
metadata:
  name: image
spec:
  annotations:
    seldon.io/rest-read-timeout: "10000000"
    seldon.io/grpc-read-timeout: "10000000"
    seldon.io/grpc-max-message-size: "1000000000"
    seldon.io/engine-java-opts: "-Xmx10G"
  name: image
  predictors:
  - componentSpecs:
    - spec:
        containers:
        - image: docker.io/seldonio/imagenet-transformer:0.1
          name: transformer
    graph:
      name: transformer
      type: TRANSFORMER
      endpoint:
        type: GRPC
      children:
      - implementation: TENSORFLOW_SERVER
        modelUri: gs://seldon-models/tfserving/imagenet/model
        name: classifier
        endpoint:
          type: GRPC
        parameters:
          - name: model_name
            type: STRING
            value: classifier
          - name: model_input
            type: STRING
            value: input_image
          - name: model_output
            type: STRING
            value: predictions/Softmax:0
    svcOrchSpec:
      resources:
        requests:
          memory: 10Gi
        limits:
          memory: 10Gi
      env:
      - name: SELDON_LOG_LEVEL
        value: DEBUG
    explainer:
      type: AnchorImages
      modelUri: gs://seldon-models/tfserving/imagenet/explainer
      config:
        batch_size: "100"
      endpoint:
        type: GRPC
    name: default
    replicas: 1
[ ]:
!kubectl apply -f resources/imagenet_explainer_grpc.yaml
[ ]:
!kubectl rollout status deploy/image-default-5d14729
[ ]:
!kubectl rollout status deploy/image-default-explainer
[ ]:
from PIL import Image
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
from tensorflow.keras.applications.inception_v3 import InceptionV3, decode_predictions
import alibi
from alibi.datasets import fetch_imagenet
import numpy as np

def get_image_data():
    data = []
    image_shape = (299, 299, 3)
    target_size = image_shape[:2]
    image = Image.open("cat-raw.jpg").convert('RGB')
    image = np.expand_dims(image.resize(target_size), axis=0)
    data.append(image)
    data = np.concatenate(data, axis=0)
    return data

data = get_image_data()
[ ]:
from seldon_core.seldon_client import SeldonClient
import numpy as np
sc = SeldonClient(deployment_name="image",namespace="seldon",grpc_max_send_message_length= 27 * 1024 * 1024,grpc_max_receive_message_length= 27 * 1024 * 1024)
[ ]:
import tensorflow as tf
data = get_image_data()
req = data[0:1]
r = sc.predict(gateway="ambassador",transport="grpc",data=req,payload_type='tftensor')
preds = tf.make_ndarray(r.response.data.tftensor)

label = decode_predictions(preds, top=1)
plt.title(label[0])
plt.imshow(data[0])
[ ]:
req = np.expand_dims(data[0], axis=0)
explanation = sc.explain(deployment_name="image",gateway="ambassador",transport="rest",data=req)
exp_arr = np.array(explanation['anchor'])

f, axarr = plt.subplots(1, 2)
axarr[0].imshow(data[0])
axarr[1].imshow(explanation['anchor'])
plt.show()

[ ]:
!kubectl delete -f resources/imagenet_explainer_grpc.yaml
[ ]:
import json
with open('cat.json') as json_file:
    j = json.load(json_file)
[ ]:
r = sc.predict(gateway="ambassador",transport="grpc",data=req,payload_type='tftensor')
preds = tf.make_ndarray(r.response.data.tftensor)

label = decode_predictions(preds, top=1)
plt.title(label[0])