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

show_image method will return an error may be a bug #2247

Closed
COD1995 opened this issue Aug 12, 2022 · 3 comments · Fixed by #2248
Closed

show_image method will return an error may be a bug #2247

COD1995 opened this issue Aug 12, 2022 · 3 comments · Fixed by #2248
Assignees

Comments

@COD1995
Copy link

COD1995 commented Aug 12, 2022

In TensorFlow
TypeError: Invalid shape (28, 28, 1) for image data

This error will come up when running the code data.visualize() in the notebook.

@astonzhang
Copy link
Member

Thanks. Could you be more specific on which line of code in which notebook? Besides, please also share pip show tensorflow output so we know the version.

@COD1995
Copy link
Author

COD1995 commented Aug 12, 2022

The error showed up in sections 4.2 and 4.3, which used the visualize() method.

pip show tensorflow result
image

This issue only happens with TensorFlow version, the PyTorch works just fine
image

@AnirudhDagar
Copy link
Member

Thanks @COD1995 for reporting the problem and filing the issue. It really helps us improve the book.

Locally

I'm using tensorflow==2.9.1 as suggested in our requirements, and everything works perfectly for that.

Just to be double sure, I also installed tensorflow==2.8.2 to mimic your environment, and the code seems fine.

Colab

I was able to reproduce the problem when running on colab. Initially it seemed to be the version difference. But after further investigation, I found the root cause. colab is using python 3.7 and installs fairly old libraries numpy==1.21.6, matplotlib==3.2.2 whereas locally, I had the latest version of libraries (same as we set for d2l requirements).


Digging further, I found that a silent squeeze was introduced if the third dimension is 1 in the following PR:

matplotlib/matplotlib#15090 (corresponding issue discussion matplotlib/matplotlib#15089).

This is precisely the point of difference. This change was released in v3.3.0 (See release notes.). I'm surprised that the matplotlib devs did not put this change in the release highlights.


Anyway, I'll send a fix for this now in the master branch. @COD1995 till then you could:

Manually do the squeeze for the fix:
d2l.show_images(tf.squeeze(X), nrows, ncols, titles=labels)

or

Upgrade your matplotlib version: pip install --upgrade matplotlib>=3.3

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

Successfully merging a pull request may close this issue.

3 participants