Skip to content

Fix dimension mismatch in "Visualizing what convnets learn" example#2302

Merged
jeffcarp merged 2 commits intokeras-team:masterfrom
buildwithsuhana:bug
Mar 5, 2026
Merged

Fix dimension mismatch in "Visualizing what convnets learn" example#2302
jeffcarp merged 2 commits intokeras-team:masterfrom
buildwithsuhana:bug

Conversation

@buildwithsuhana
Copy link
Contributor

This PR corrects a dimension mismatch in the "Visualizing what convnets learn" vision example where images were being initialized and stitched using (width, height) ordering instead of the standard Keras/TensorFlow (height, width) convention.

This PR is a solution for the issue raised: #2297

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @buildwithsuhana, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses and resolves a critical dimension mismatch within the "Visualizing what convnets learn" example. The fix ensures that image initialization and filter stitching operations consistently adhere to the standard Keras/TensorFlow (height, width) convention, preventing incorrect image rendering and processing. This change directly resolves a reported issue, improving the example's correctness and reliability.

Highlights

  • Image Initialization Correction: Corrected the order of img_width and img_height when initializing images with tf.random.uniform to align with Keras/TensorFlow's standard (height, width) convention.
  • Filter Stitching Dimension Fix: Adjusted the dimensions of the stitched_filters array and the subsequent indexing logic during the filter stitching process to correctly use (height, width) ordering, resolving a dimension mismatch.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • examples/vision/ipynb/visualizing_what_convnets_learn.ipynb
    • Updated image initialization dimensions from (width, height) to (height, width).
    • Corrected filter stitching array dimensions and indexing to use (height, width) ordering.
  • examples/vision/md/visualizing_what_convnets_learn.md
    • Updated image initialization dimensions from (width, height) to (height, width).
    • Corrected filter stitching array dimensions and indexing to use (height, width) ordering.
  • examples/vision/visualizing_what_convnets_learn.py
    • Updated image initialization dimensions from (width, height) to (height, width).
    • Corrected filter stitching array dimensions and indexing to use (height, width) ordering.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses the dimension mismatch issue by swapping width and height in image initialization and stitching, aligning with the standard Keras/TensorFlow convention. The fix is applied consistently across the Python script, Jupyter notebook, and Markdown file.

I have one minor suggestion for improving code readability in the image stitching logic. Additionally, I noticed a small typo in a filename used across all files: stiched_filters.png should likely be stitched_filters.png. Since this is on a line not modified in this PR, I'm mentioning it here for your consideration.

Comment on lines +303 to +305
" (cropped_height + margin) * i : (cropped_height + margin) * i + cropped_height,\n",
" (cropped_width + margin) * j : (cropped_width + margin) * j\n",
" + cropped_width,\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this slicing logic is correct, it's a bit dense and can be hard to parse. For improved readability and maintainability, consider extracting the start and end coordinates into separate variables. This makes the logic clearer.

For example:

# Inside the loops
row_start = i * (cropped_height + margin)
row_end = row_start + cropped_height
col_start = j * (cropped_width + margin)
col_end = col_start + cropped_width
stitched_filters[row_start:row_end, col_start:col_end, :] = img

This change would make the code's intent more explicit.

Comment on lines +206 to +208
(cropped_height + margin) * i : (cropped_height + margin) * i + cropped_height,
(cropped_width + margin) * j : (cropped_width + margin) * j
+ cropped_width,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this slicing logic is correct, it's a bit dense and can be hard to parse. For improved readability and maintainability, consider extracting the start and end coordinates into separate variables. This makes the logic clearer.

For example:

# Inside the loops
row_start = i * (cropped_height + margin)
row_end = row_start + cropped_height
col_start = j * (cropped_width + margin)
col_end = col_start + cropped_width
stitched_filters[row_start:row_end, col_start:col_end, :] = img

This change would make the code's intent more explicit.

Comment on lines +186 to +188
(cropped_height + margin) * i : (cropped_height + margin) * i + cropped_height,
(cropped_width + margin) * j : (cropped_width + margin) * j
+ cropped_width,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this slicing logic is correct, it's a bit dense and can be hard to parse. For improved readability and maintainability, consider extracting the start and end coordinates into separate variables. This makes the logic clearer.

For example:

# Inside the loops
row_start = i * (cropped_height + margin)
row_end = row_start + cropped_height
col_start = j * (cropped_width + margin)
col_end = col_start + cropped_width
stitched_filters[row_start:row_end, col_start:col_end, :] = img

This change would make the code's intent more explicit.

Copy link
Member

@jeffcarp jeffcarp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jeffcarp jeffcarp merged commit 69250f9 into keras-team:master Mar 5, 2026
3 of 4 checks passed
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

Successfully merging this pull request may close these issues.

3 participants