You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# set the mixed precisionfromtensorflow.kerasimportmixed_precisionmixed_precision.set_global_policy('mixed_float16')
# https://shap.readthedocs.io/en/latest/example_notebooks/image_examples/image_classification/Explain%20ResNet50%20using%20the%20Partition%20explainer.htmlimportjsonfromtensorflow.keras.applications.resnet50importResNet50, preprocess_inputimportshap# load pre-trained model and datamodel=ResNet50(weights="imagenet")
X, y=shap.datasets.imagenet50()
# getting ImageNet 1000 class namesurl="https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json"withopen(shap.datasets.cache(url)) asfile:
class_names= [v[1] forvinjson.load(file).values()]
# print("Number of ImageNet classes:", len(class_names))# print("Class names:", class_names)# python function to get model output; replace this function with your own model function.deff(x):
tmp=x.copy()
preprocess_input(tmp)
returnmodel(tmp)
# define a masker that is used to mask out partitions of the input image.masker=shap.maskers.Image("inpaint_telea", X[0].shape)
# create an explainer with model and image maskerexplainer=shap.Explainer(f, masker, output_names=class_names)
# here we explain two images using 500 evaluations of the underlying model to estimate the SHAP valuesshap_values=explainer(
X[1:3], max_evals=100, batch_size=50, outputs=shap.Explanation.argsort.flip[:4]
)
# output with shap valuesshap.image_plot(shap_values)
Traceback
NotImplementedError: Failed in nopython mode pipeline (step: native lowering)
float16
Expected Behavior
Shap works as it does when not used with a model trained with float16 mixed precision.
Bug report checklist
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest release of shap.
I have confirmed this bug exists on the master branch of shap.
I'd be interested in making a PR to fix this bug
Installed Versions
0.45.1
The text was updated successfully, but these errors were encountered:
Seems like njit does not support f16, so the solution is to cast to these to f32. I created a PR that fixes your issue. Would be great if you could test it and report your findings back.
Thanks
Issue Description
When trying to use shap with a model that was trained using
float16
(mixed precision), I get the following error:Minimal Reproducible Example
Traceback
NotImplementedError: Failed in nopython mode pipeline (step: native lowering) float16
Expected Behavior
Shap works as it does when not used with a model trained with
float16
mixed precision.Bug report checklist
Installed Versions
0.45.1
The text was updated successfully, but these errors were encountered: