Skip to content

Commit 69250f9

Browse files
Fix dimension mismatch in "Visualizing what convnets learn" example (#2302)
* fixing dimensions * fixing dimensions
1 parent 1b37b22 commit 69250f9

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

examples/vision/ipynb/visualizing_what_convnets_learn.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
"source": [
175175
"def initialize_image():\n",
176176
" # We start from a gray image with some random noise\n",
177-
" img = tf.random.uniform((1, img_width, img_height, 3))\n",
177+
" img = tf.random.uniform((1, img_height, img_width, 3))\n",
178178
" # ResNet50V2 expects inputs in the range [-1, +1].\n",
179179
" # Here we scale our random inputs to [-0.125, +0.125]\n",
180180
" return (img - 0.5) * 0.25\n",
@@ -293,16 +293,16 @@
293293
"cropped_height = img_height - 25 * 2\n",
294294
"width = n * cropped_width + (n - 1) * margin\n",
295295
"height = n * cropped_height + (n - 1) * margin\n",
296-
"stitched_filters = np.zeros((width, height, 3))\n",
296+
"stitched_filters = np.zeros((height, width, 3))\n",
297297
"\n",
298298
"# Fill the picture with our saved filters\n",
299299
"for i in range(n):\n",
300300
" for j in range(n):\n",
301301
" img = all_imgs[i * n + j]\n",
302302
" stitched_filters[\n",
303-
" (cropped_width + margin) * i : (cropped_width + margin) * i + cropped_width,\n",
304-
" (cropped_height + margin) * j : (cropped_height + margin) * j\n",
305-
" + cropped_height,\n",
303+
" (cropped_height + margin) * i : (cropped_height + margin) * i + cropped_height,\n",
304+
" (cropped_width + margin) * j : (cropped_width + margin) * j\n",
305+
" + cropped_width,\n",
306306
" :,\n",
307307
" ] = img\n",
308308
"keras.utils.save_img(\"stiched_filters.png\", stitched_filters)\n",

examples/vision/md/visualizing_what_convnets_learn.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ center-cropping it, and restricting it to the [0, 255] range.
108108

109109
def initialize_image():
110110
# We start from a gray image with some random noise
111-
img = tf.random.uniform((1, img_width, img_height, 3))
111+
img = tf.random.uniform((1, img_height, img_width, 3))
112112
# ResNet50V2 expects inputs in the range [-1, +1].
113113
# Here we scale our random inputs to [-0.125, +0.125]
114114
return (img - 0.5) * 0.25
@@ -196,16 +196,16 @@ cropped_width = img_width - 25 * 2
196196
cropped_height = img_height - 25 * 2
197197
width = n * cropped_width + (n - 1) * margin
198198
height = n * cropped_height + (n - 1) * margin
199-
stitched_filters = np.zeros((width, height, 3))
199+
stitched_filters = np.zeros((height, width, 3))
200200

201201
# Fill the picture with our saved filters
202202
for i in range(n):
203203
for j in range(n):
204204
img = all_imgs[i * n + j]
205205
stitched_filters[
206-
(cropped_width + margin) * i : (cropped_width + margin) * i + cropped_width,
207-
(cropped_height + margin) * j : (cropped_height + margin) * j
208-
+ cropped_height,
206+
(cropped_height + margin) * i : (cropped_height + margin) * i + cropped_height,
207+
(cropped_width + margin) * j : (cropped_width + margin) * j
208+
+ cropped_width,
209209
:,
210210
] = img
211211
keras.utils.save_img("stiched_filters.png", stitched_filters)

examples/vision/visualizing_what_convnets_learn.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def gradient_ascent_step(img, filter_index, learning_rate):
9898

9999
def initialize_image():
100100
# We start from a gray image with some random noise
101-
img = tf.random.uniform((1, img_width, img_height, 3))
101+
img = tf.random.uniform((1, img_height, img_width, 3))
102102
# ResNet50V2 expects inputs in the range [-1, +1].
103103
# Here we scale our random inputs to [-0.125, +0.125]
104104
return (img - 0.5) * 0.25
@@ -176,16 +176,16 @@ def deprocess_image(img):
176176
cropped_height = img_height - 25 * 2
177177
width = n * cropped_width + (n - 1) * margin
178178
height = n * cropped_height + (n - 1) * margin
179-
stitched_filters = np.zeros((width, height, 3))
179+
stitched_filters = np.zeros((height, width, 3))
180180

181181
# Fill the picture with our saved filters
182182
for i in range(n):
183183
for j in range(n):
184184
img = all_imgs[i * n + j]
185185
stitched_filters[
186-
(cropped_width + margin) * i : (cropped_width + margin) * i + cropped_width,
187-
(cropped_height + margin) * j : (cropped_height + margin) * j
188-
+ cropped_height,
186+
(cropped_height + margin) * i : (cropped_height + margin) * i + cropped_height,
187+
(cropped_width + margin) * j : (cropped_width + margin) * j
188+
+ cropped_width,
189189
:,
190190
] = img
191191
keras.utils.save_img("stiched_filters.png", stitched_filters)

0 commit comments

Comments
 (0)