This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load and pre-process cat image | |
IMAGE_PATH = './cat2.jpg' | |
img = tf.keras.preprocessing.image.load_img(IMAGE_PATH, target_size=(299, 299)) | |
img = tf.keras.preprocessing.image.img_to_array(img) | |
# view the image | |
plt.imshow(img/255.) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load dependencies | |
import numpy as np | |
import tensorflow as tf | |
import matplotlib.pyplot as plt | |
from tf_explain.core.activations import ExtractActivations | |
from tensorflow.keras.applications.xception import decode_predictions | |
%matplotlib inline | |
# load Xception pre-trained CNN model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# focus on 14th layer of the CNN model | |
print(model.layers[14].input) | |
Out [49]: <tf.Tensor 'block4_conv3_2/Relu:0' shape=(?, 28, 28, 512) dtype=float32> | |
# make model predictions | |
e = shap.GradientExplainer((model.layers[14].input, model.layers[-1].output), | |
map2layer(preprocess_input(X.copy()), 14)) | |
shap_values, indexes = e.shap_values(map2layer(to_predict, 14), ranked_outputs=2) | |
index_names = np.vectorize(lambda x: class_names[str(x)][1])(indexes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# utility function to pass inputs to specific model layers | |
def map2layer(x, layer): | |
feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())])) | |
return K.get_session().run(model.layers[layer].input, feed_dict) | |
# focus on the 7th layer of CNN model | |
print(model.layers[7].input) | |
Out [46]: <tf.Tensor 'block2_pool_2/MaxPool:0' shape=(?, 56, 56, 128) dtype=float32> | |
# make model predictions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get imagenet id to label name mappings | |
url = "https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json" | |
fname = shap.datasets.cache(url) | |
with open(fname) as f: | |
class_names = json.load(f) | |
# make model predictions | |
predictions = model.predict(preprocess_input(to_predict.copy())) | |
# get prediction labels |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load sample images | |
X, y = shap.datasets.imagenet50() | |
# load sample cat image for test | |
IMAGE_PATH = './cat2.jpg' | |
img = keras.preprocessing.image.load_img(IMAGE_PATH, target_size=(224, 224)) | |
img = keras.preprocessing.image.img_to_array(img) | |
# select 3 other sample images for test | |
import matplotlib.pyplot as plt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = VGG16(weights='imagenet', include_top=True) | |
model.summary() | |
# output | |
_________________________________________________________________ | |
Layer (type) Output Shape Param # | |
================================================================= | |
input_3 (InputLayer) (None, 224, 224, 3) 0 | |
_________________________________________________________________ | |
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
from keras.applications.vgg16 import VGG16 | |
from keras.applications.vgg16 import preprocess_input, decode_predictions | |
from matplotlib.colors import LinearSegmentedColormap | |
import numpy as np | |
import shap | |
import keras.backend as K | |
import json | |
shap.initjs() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hourly_avg_errors_sorted_df = (not_found_df | |
.groupBy(F.hour('time') | |
.alias('hour')) | |
.count() | |
.sort('hour')) | |
hourly_avg_errors_sorted_pd_df = hourly_avg_errors_sorted_df.toPandas() | |
c = sns.catplot(x='hour', y='count', | |
data=hourly_avg_errors_sorted_pd_df, | |
kind='bar', height=5, aspect=1.5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(errors_by_date_sorted_df | |
.sort("count", ascending=False) | |
.show(3)) |