-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix dimension mismatch in "Visualizing what convnets learn" example #2302
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,7 +108,7 @@ center-cropping it, and restricting it to the [0, 255] range. | |
|
|
||
| def initialize_image(): | ||
| # We start from a gray image with some random noise | ||
| img = tf.random.uniform((1, img_width, img_height, 3)) | ||
| img = tf.random.uniform((1, img_height, img_width, 3)) | ||
| # ResNet50V2 expects inputs in the range [-1, +1]. | ||
| # Here we scale our random inputs to [-0.125, +0.125] | ||
| return (img - 0.5) * 0.25 | ||
|
|
@@ -196,16 +196,16 @@ cropped_width = img_width - 25 * 2 | |
| cropped_height = img_height - 25 * 2 | ||
| width = n * cropped_width + (n - 1) * margin | ||
| height = n * cropped_height + (n - 1) * margin | ||
| stitched_filters = np.zeros((width, height, 3)) | ||
| stitched_filters = np.zeros((height, width, 3)) | ||
|
|
||
| # Fill the picture with our saved filters | ||
| for i in range(n): | ||
| for j in range(n): | ||
| img = all_imgs[i * n + j] | ||
| stitched_filters[ | ||
| (cropped_width + margin) * i : (cropped_width + margin) * i + cropped_width, | ||
| (cropped_height + margin) * j : (cropped_height + margin) * j | ||
| + cropped_height, | ||
| (cropped_height + margin) * i : (cropped_height + margin) * i + cropped_height, | ||
| (cropped_width + margin) * j : (cropped_width + margin) * j | ||
| + cropped_width, | ||
|
Comment on lines
+206
to
+208
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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, :] = imgThis change would make the code's intent more explicit. |
||
| :, | ||
| ] = img | ||
| keras.utils.save_img("stiched_filters.png", stitched_filters) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,7 +98,7 @@ def gradient_ascent_step(img, filter_index, learning_rate): | |
|
|
||
| def initialize_image(): | ||
| # We start from a gray image with some random noise | ||
| img = tf.random.uniform((1, img_width, img_height, 3)) | ||
| img = tf.random.uniform((1, img_height, img_width, 3)) | ||
| # ResNet50V2 expects inputs in the range [-1, +1]. | ||
| # Here we scale our random inputs to [-0.125, +0.125] | ||
| return (img - 0.5) * 0.25 | ||
|
|
@@ -176,16 +176,16 @@ def deprocess_image(img): | |
| cropped_height = img_height - 25 * 2 | ||
| width = n * cropped_width + (n - 1) * margin | ||
| height = n * cropped_height + (n - 1) * margin | ||
| stitched_filters = np.zeros((width, height, 3)) | ||
| stitched_filters = np.zeros((height, width, 3)) | ||
|
|
||
| # Fill the picture with our saved filters | ||
| for i in range(n): | ||
| for j in range(n): | ||
| img = all_imgs[i * n + j] | ||
| stitched_filters[ | ||
| (cropped_width + margin) * i : (cropped_width + margin) * i + cropped_width, | ||
| (cropped_height + margin) * j : (cropped_height + margin) * j | ||
| + cropped_height, | ||
| (cropped_height + margin) * i : (cropped_height + margin) * i + cropped_height, | ||
| (cropped_width + margin) * j : (cropped_width + margin) * j | ||
| + cropped_width, | ||
|
Comment on lines
+186
to
+188
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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, :] = imgThis change would make the code's intent more explicit. |
||
| :, | ||
| ] = img | ||
| keras.utils.save_img("stiched_filters.png", stitched_filters) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
This change would make the code's intent more explicit.