cdpierse / transformers-interpret

Model explainability that works seamlessly with 🤗 transformers. Explain your transformers model in just 2 lines of code.
Apache License 2.0
1.27k stars 96 forks source link

ImageClassificationExplainer: AttributeError: ndim when trying to visualize. #132

Open mollie9523 opened 1 year ago

mollie9523 commented 1 year ago

Hi,

I am trying to run the notebook that you provided but when it comes to visualizing, there is an attribute error.

image

How can I solve this?

oskwys commented 1 year ago

I have the same problem when playing with the exemplary code

`from transformers import AutoFeatureExtractor, AutoModelForImageClassification from transformers_interpret import ImageClassificationExplainer from PIL import Image import requests

model_name = "google/vit-base-patch16-224" model = AutoModelForImageClassification.from_pretrained(model_name) feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)

image_link = "https://imagesvc.meredithcorp.io/v3/mm/image?url=https%3A%2F%2Fstatic.onecms.io%2Fwp-content%2Fuploads%2Fsites%2F47%2F2020%2F08%2F16%2Fgolden-retriever-177213599-2000.jpg"

image = Image.open(requests.get(image_link, stream=True).raw)

image_classification_explainer = ImageClassificationExplainer(model=model, feature_extractor=feature_extractor)

image_attributions = image_classification_explainer( image )

print(image_attributions.shape)

image_classification_explainer.visualize( method="heatmap", side_by_side=True, outlier_threshold=0.03

)`

MohieEldinMuhammad commented 11 months ago

Do you know the solution to this error?

lukassoukup commented 7 months ago

Hi, I encountered the same error, it seems that transformers library doesn't work with PIL. Changing line 69 in _transformers_interpret/explainers/vision/imageclassification.py to this: self.feature_extractor.resize(np.array(self._image), size=(attributions_t.shape[0], attributions_t.shape[1])) worked in my case.