Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mollie9523 opened this issue Apr 25, 2023 · 3 comments

Comments

@mollie9523
Copy link

mollie9523 commented Apr 25, 2023

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
Copy link

oskwys commented Jul 19, 2023

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
Copy link

Do you know the solution to this error?

@lukassoukup
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants