I am doing XAI for the input occluded image using a pretrained ArcFace model to find the occluded area of the image and plot the PLQ map.
Here for XAI, I have used shap library in python. I have using GradientExplainer and it expects 50 as default batch size. But while running it exit with the code "code=3221225477". What does it refers to. I provide the complete code, please check the code and guide me whether it have any error.
import time
import shap
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import tensorflow.keras.backend as K
import os
import random
model = load_model("model_kerasarc_v3.h5")
def preprocess_image(img_path, img_size=(112, 112)):
img = image.load_img(img_path, target_size=img_size)
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) / 255.0
return img_array, img
def load_background_images(dataset_path, num_images=50):
image_files = random.sample(os.listdir(dataset_path), num_images)
images = [preprocess_image(os.path.join(dataset_path, img))[0] for img in image_files]
return np.vstack(images)
def get_shap_explanation(model, img_array, background):
print("1")
start_time = time.time()
explainer = shap.GradientExplainer(model, background)
shap_values = explainer.shap_values(img_array,nsamples=50)
print("done")
return np.array(shap_values)
def plot_shap_explanations(shap_values, img_array):
shap_values = np.array(shap_values)
shap.summary_plot(shap_values, img_array)
def main(image_path, dataset_path):
img_array, original_image = preprocess_image(image_path)
print("Image array shape : ",img_array.shape)
background = load_background_images(dataset_path, num_images=50)
print("Background shape : ", background.shape)
#background = tf.convert_to_tensor(background, dtype=tf.float32)
img_array_batch = np.repeat(img_array, 50, axis=0)
print("Batch shape : ", img_array_batch.shape)
print("Model input shape: ", model.input.shape)
img_array_batch = img_array_batch.astype(np.float32) # Ensure correct type
background = np.array(background, dtype=np.float32)
shap_values = get_shap_explanation(model, img_array_batch, background)
print("shap_values shape : ", shap_values.shape)
plot_shap_explanations(shap_values, img_array_batch)
if __name__ == "__main__":
main(
"C:/Users/Saran Babu/OneDrive/Desktop/Project/XAI/test_images/cap.png",
"C:/Users/Saran Babu/OneDrive/Desktop/train"
)