I'm trying to interpret inference result of image classification model. I tried usign gcam but it wasn't much helpful as heatmap doesn't give pixel level information. I found this library called shap according to there documentation link it'll give pixel level interpretation I followed there one of the example for image classification and used it to replicate for my own problem as shown below
model = load_model(r'Model.h5', compile=False)
image = cv2.imread(r'img.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
test_img = [image]
test_img = [np.expand_dims(img, axis=0) for img in test_img]
test_img = tf.concat(test_img, axis=0)
test_img = tf.image.resize(test_img, [224, 224])
test_img = tf.cast(test_img, tf.float32) / 255.0
def f(X):
tmp = X.copy()
# preprocess_input(tmp)
return model(tmp)
masker = shap.maskers.Image("inpaint_telea", test_img.shape)
explainer = shap.Explainer(f, masker, output_names=class_label_mapping)
shap_values = explainer(test_img,outputs=shap.Explanation.argsort.flip[:1])
shap.image_plot(shap_values)
when I executed above code I got error as below
Output exceeds the size limit. Open the full output data in a text editor --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) c:\research\visualize_cnn\visualize_shap.ipynb Cell 8 in <cell line: 1>() ----> 1 shap_values = explainer(test_img,outputs=shap.Explanation.argsort.flip[:1]) 2 shap.image_plot(shap_values)
File ~\AppData\Roaming\Python\Python38\site-packages\shap\explainers_partition.py:136, in Partition.call(self, max_evals, fixed_context, main_effects, error_bounds, batch_size, outputs, silent, *args) 132 def call(self, *args, max_evals=500, fixed_context=None, main_effects=False, error_bounds=False, batch_size="auto", 133 outputs=None, silent=False): 134 """ Explain the output of the model on the given arguments. 135 """ --> 136 return super().call( 137 *args, max_evals=max_evals, fixed_context=fixed_context, main_effects=main_effects, error_bounds=error_bounds, batch_size=batch_size, 138 outputs=outputs, silent=silent 139 )
File ~\AppData\Roaming\Python\Python38\site-packages\shap\explainers_explainer.py:266, in Explainer.call(self, max_evals, main_effects, error_bounds, batch_size, outputs, silent, *args, **kwargs) 264 feature_names = [[] for _ in range(len(args))] 265 for row_args in show_progress(zip(*args), num_rows, self.class.name+" explainer", silent): --> 266 row_result = self.explain_row( 267 *row_args, max_evals=max_evals, main_effects=main_effects, error_bounds=error_bounds, 268 batch_size=batch_size, outputs=outputs, silent=silent, **kwargs 269 ) 270 values.append(row_result.get("values", None)) 271 output_indices.append(row_result.get("output_indices", None)) ... ---> 91 x = x.ravel() 93 # if mask is not given then we mask the whole image 94 if mask is None:
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'ravel'
The way I do inference is as below
image = cv2.imread('img.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
test_img = [image]
test_img = [np.expand_dims(img, axis=0) for img in test_img]
test_img = tf.concat(test_img, axis=0)
test_img = tf.image.resize(test_img, [224, 224])
test_img = tf.cast(test_img, tf.float32) / 255.0
y_pred_on_hot = model.predict(test_img) # , batch_size=1)
y_pred = np.argmax(y_pred_on_hot, axis=1)
print(y_pred)
print(class_label_mapping[y_pred[0]])
This will print the class label of testing image.
How can I interpret the testing image using shap library successfully without any error ? Any help or suggestion on this will be very helpful