1

I'm trying to get shap values for my pret-trained EfficientNetB0 model.

import shap
import numpy as np
import tensorflow as tf
import cv2

model = tf.keras.applications.EfficientNetB0(weights='imagenet')

def get_shap_values(model,train_data,sample_images):
    explainer = shap.DeepExplainer(model, train_data)
    shap_values = explainer.shap_values(sample_images)

    shap_numpy = [np.swapaxes(np.swapaxes(s, 1, -1), 1, 2) for s in shap_values]
    test_numpy = np.swapaxes(np.swapaxes(sample_images, 1, -1), 1, 2)
    shap.image_plot(shap_numpy, -test_numpy)

def data_resize(data): 
    data_resized = np.zeros((data.shape[0], 224, 224, 3))
    for i in range(data.shape[0]):
        img = cv2.resize(data[i], (224, 224))
        # img = np.expand_dims(img, axis=-1)
        # img = np.concatenate([img, img, img], axis=-1)
        data_resized[i] = img
    return data_resized

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.cifar10.load_data()
train_images = data_resize(train_images[:100])
test_images = data_resize(test_images[:10])

get_shap_values(model,train_images,train_images)

When I run this I get the error LookupError: gradient registry has no entry for: shap_FusedBatchNormV3.

I tried this solution but it just breaks the model. Couldn't find any solutions, any help would be appreciated.

Nandana
  • 11
  • 2

0 Answers0