<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 KFServing Model Explainability
Install the necessary libraries
Step1:  Get endpoint, host headers, and load the image from a file or from the MNIST dataset.
Step2:  Display the input image to be used.
Step3:  Send the image to the inferenceservice.
Step4:  Unwrap the response from the inferenceservice and display the explanations.
<ASSISTANT_TASK:>
Python Code:
import os
import sys
import requests
import json
from matplotlib import pyplot as plt
import numpy as np
from aix360.datasets import MNISTDataset
from keras.applications import inception_v3 as inc_net
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions
import time
from skimage.color import gray2rgb, rgb2gray, label2rgb # since the code wants color images
Explanation: KFServing Model Explainability
Install the necessary libraries
End of explanation
print('************************************************************')
print('************************************************************')
print('************************************************************')
print("starting query")
if len(sys.argv) < 3:
    raise Exception("No endpoint specified. ")
endpoint = sys.argv[1]
headers = {
    'Host': sys.argv[2]
}
test_num = 1002
is_file = False
if len(sys.argv) > 3:
    try:
        test_num = int(sys.argv[2])
    except:
        is_file = True
if is_file:
    inputs = open(sys.argv[2])
    inputs = json.load(inputs)
    actual = "unk"
else:
    data = MNISTDataset()
    inputs = data.test_data[test_num]
    labels = data.test_labels[test_num]
    actual = 0
    for x in range(1, len(labels)):
        if labels[x] != 0:
            actual = x
    inputs = gray2rgb(inputs.reshape((-1, 28, 28)))
    inputs = np.reshape(inputs, (28,28,3))
input_image = {"instances": [inputs.tolist()]}
Explanation: Get endpoint, host headers, and load the image from a file or from the MNIST dataset.
End of explanation
fig0 = (inputs[:,:,0] + 0.5)*255
f, axarr = plt.subplots(1, 1, figsize=(10,10))
axarr.set_title("Original Image")
axarr.imshow(fig0, cmap="gray")
plt.show()
Explanation: Display the input image to be used.
End of explanation
print("Sending Explain Query")
x = time.time()
res = requests.post(endpoint, json=input_image, headers=headers)
print("TIME TAKEN: ", time.time() - x)
Explanation: Send the image to the inferenceservice.
End of explanation
print(res)
if not res.ok:
    res.raise_for_status()
res_json = res.json()
temp = np.array(res_json["explanations"]["temp"])
masks = np.array(res_json["explanations"]["masks"])
top_labels = np.array(res_json["explanations"]["top_labels"])
fig, m_axs = plt.subplots(2,5, figsize = (12,6))
for i, c_ax in enumerate(m_axs.flatten()):
    mask = masks[i]
    c_ax.imshow(label2rgb(mask, temp, bg_label = 0), interpolation = 'nearest')
    c_ax.set_title('Positive for {}\nActual {}'.format(top_labels[i], actual))
    c_ax.axis('off')
plt.show()
Explanation: Unwrap the response from the inferenceservice and display the explanations.
End of explanation
<END_TASK>