Error when implementing SHAP on CNN model

So I am trying to implement SHAP to a CNN model I trained on an image dataset on Kaggle. But I keep getting the following Attribute Error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[9], line 1
----> 1 explainer = shap.DeepExplainer(s_model, val_data)
2 sp_values = explainer.shap_values(val_data)
4 shap.summary_plot(sp_values, val_data)
File /opt/conda/lib/python3.10/site-packages/shap/explainers/_deep/__init__.py:84, in DeepExplainer.__init__(self, model, data, session, learning_phase_flags)
81 framework = 'tensorflow'
83 if framework == 'tensorflow':
---> 84 self.explainer = TFDeep(model, data, session, learning_phase_flags)
85 elif framework == 'pytorch':
86 self.explainer = PyTorchDeep(model, data)
File /opt/conda/lib/python3.10/site-packages/shap/explainers/_deep/deep_tf.py:155, in TFDeep.__init__(self, model, data, session, learning_phase_flags)
153 self.expected_value = None
154 else:
--> 155 if self.data[0].shape[0] > 5000:
156 warnings.warn("You have provided over 5k background samples! For better performance consider using smaller random sample.")
157 if not tf.executing_eagerly():
AttributeError: '_PrefetchDataset' object has no attribute 'shape'
</code>
<code>--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[9], line 1 ----> 1 explainer = shap.DeepExplainer(s_model, val_data) 2 sp_values = explainer.shap_values(val_data) 4 shap.summary_plot(sp_values, val_data) File /opt/conda/lib/python3.10/site-packages/shap/explainers/_deep/__init__.py:84, in DeepExplainer.__init__(self, model, data, session, learning_phase_flags) 81 framework = 'tensorflow' 83 if framework == 'tensorflow': ---> 84 self.explainer = TFDeep(model, data, session, learning_phase_flags) 85 elif framework == 'pytorch': 86 self.explainer = PyTorchDeep(model, data) File /opt/conda/lib/python3.10/site-packages/shap/explainers/_deep/deep_tf.py:155, in TFDeep.__init__(self, model, data, session, learning_phase_flags) 153 self.expected_value = None 154 else: --> 155 if self.data[0].shape[0] > 5000: 156 warnings.warn("You have provided over 5k background samples! For better performance consider using smaller random sample.") 157 if not tf.executing_eagerly(): AttributeError: '_PrefetchDataset' object has no attribute 'shape' </code>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[9], line 1
----> 1 explainer = shap.DeepExplainer(s_model, val_data)
      2 sp_values = explainer.shap_values(val_data)
      4 shap.summary_plot(sp_values, val_data)

File /opt/conda/lib/python3.10/site-packages/shap/explainers/_deep/__init__.py:84, in DeepExplainer.__init__(self, model, data, session, learning_phase_flags)
     81         framework = 'tensorflow'
     83 if framework == 'tensorflow':
---> 84     self.explainer = TFDeep(model, data, session, learning_phase_flags)
     85 elif framework == 'pytorch':
     86     self.explainer = PyTorchDeep(model, data)

File /opt/conda/lib/python3.10/site-packages/shap/explainers/_deep/deep_tf.py:155, in TFDeep.__init__(self, model, data, session, learning_phase_flags)
    153     self.expected_value = None
    154 else:
--> 155     if self.data[0].shape[0] > 5000:
    156         warnings.warn("You have provided over 5k background samples! For better performance consider using smaller random sample.")
    157     if not tf.executing_eagerly():

AttributeError: '_PrefetchDataset' object has no attribute 'shape'

I am using the following code to split the dataset:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from tensorflow.keras.utils import image_dataset_from_directory
batch_size = 32
img_height = 224
img_width = 224
train_data = image_dataset_from_directory(
data_dir,
validation_split=0.3,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
val_data = image_dataset_from_directory(data_dir,
validation_split=0.3,
subset="validation",
seed=123,
image_size=(img_height,img_width),
batch_size=batch_size)
</code>
<code>from tensorflow.keras.utils import image_dataset_from_directory batch_size = 32 img_height = 224 img_width = 224 train_data = image_dataset_from_directory( data_dir, validation_split=0.3, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size) val_data = image_dataset_from_directory(data_dir, validation_split=0.3, subset="validation", seed=123, image_size=(img_height,img_width), batch_size=batch_size) </code>
from tensorflow.keras.utils import image_dataset_from_directory

batch_size = 32
img_height = 224
img_width = 224

train_data = image_dataset_from_directory(
                  data_dir,
                  validation_split=0.3,
                  subset="training",
                  seed=123,
                  image_size=(img_height, img_width),
                  batch_size=batch_size)


val_data = image_dataset_from_directory(data_dir,
                                        validation_split=0.3,
                                        subset="validation",
                                        seed=123,
                                        image_size=(img_height,img_width),
                                        batch_size=batch_size) 

After loading a previously trained model, I use the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>explainer = shap.KernelExplainer(s_model, val_data)
sp_values = explainer.shap_values(val_data)
shap.summary_plot(sp_values, val_data)
</code>
<code>explainer = shap.KernelExplainer(s_model, val_data) sp_values = explainer.shap_values(val_data) shap.summary_plot(sp_values, val_data) </code>
explainer = shap.KernelExplainer(s_model, val_data)
sp_values = explainer.shap_values(val_data)

shap.summary_plot(sp_values, val_data)

This is where I get the error.

I am using Tensorflow and Keras for the code. Can anyone help me solve this? I want to implement SHAP, LIME and any other interpretability method on some image models.

New contributor

B.RATH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật