<p dir="auto">Extended from <a href="http://stackoverflow.com/questions/37032251/tensorflow-image-resize-mess-up-image-on-unknown-image-size" rel="nofollow">Stackoverflow question</a></p>
<h3 dir="auto">Environment info</h3>
<p dir="auto">Operating System: Ubuntu 14.04 / OS X Yosemite</p>
<p dir="auto">Installed version of CUDA and cuDNN:<br>
CUDA Version : 7.5<br>
cuDnn: 4</p>
<p dir="auto">Tensorflow version: 0.8.0</p>
<p dir="auto">When resizing image with variable dimension (output from <code class="notranslate">decode_jpeg</code>), image tend to get messed up. However, if <code class="notranslate">reshape()</code> is applied prior to <code class="notranslate">resize()</code>, image will get resized properly. This issue does not affect <code class="notranslate">ResizeMethod.Nearest_Neighbour</code>.</p>
<h3 dir="auto">Steps to reproduce</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
import tensorflow as tf
import matplotlib.pyplot as plt

file_contents = tf.read_file('./2008_000002.jpg')
im = tf.image.decode_jpeg(file_contents)
im_bi = tf.image.resize_images(im, 256, 256, method=tf.image.ResizeMethod.BILINEAR)
im_nn = tf.image.resize_images(im, 256, 256, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
im_bic = tf.image.resize_images(im, 256, 256, method=tf.image.ResizeMethod.BICUBIC)
im_ar = tf.image.resize_images(im, 256, 256, method=tf.image.ResizeMethod.AREA)

# im = tf.reshape(im, shape=[256, 256, 3])

sess = tf.Session()
sess.run(tf.initialize_all_variables())

img_bi, img_nn, img_bic, img_ar = sess.run([im_bi, im_nn, im_bic, im_ar])

plt.imshow(img_bi)
plt.title(&quot;BILINEAR&quot;)
plt.figure()

plt.imshow(img_nn)
plt.title(&quot;NEAREST_NEIGHBOR&quot;)
plt.figure()

plt.imshow(img_bic)
plt.title(&quot;BICUBIC&quot;)
plt.figure()

plt.imshow(img_ar)
plt.title('AREA')
plt.show()"><pre class="notranslate"><code class="notranslate">
import tensorflow as tf
import matplotlib.pyplot as plt

file_contents = tf.read_file('./2008_000002.jpg')
im = tf.image.decode_jpeg(file_contents)
im_bi = tf.image.resize_images(im, 256, 256, method=tf.image.ResizeMethod.BILINEAR)
im_nn = tf.image.resize_images(im, 256, 256, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
im_bic = tf.image.resize_images(im, 256, 256, method=tf.image.ResizeMethod.BICUBIC)
im_ar = tf.image.resize_images(im, 256, 256, method=tf.image.ResizeMethod.AREA)

# im = tf.reshape(im, shape=[256, 256, 3])

sess = tf.Session()
sess.run(tf.initialize_all_variables())

img_bi, img_nn, img_bic, img_ar = sess.run([im_bi, im_nn, im_bic, im_ar])

plt.imshow(img_bi)
plt.title("BILINEAR")
plt.figure()

plt.imshow(img_nn)
plt.title("NEAREST_NEIGHBOR")
plt.figure()

plt.imshow(img_bic)
plt.title("BICUBIC")
plt.figure()

plt.imshow(img_ar)
plt.title('AREA')
plt.show()
</code></pre></div>
<p dir="auto"><a href="http://imgur.com/MskVWIV" rel="nofollow">Original image</a></p>
<p dir="auto">Result<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/650407/15034074/7b973208-12a4-11e6-943e-ea04e1389c01.png"><img src="https://cloud.githubusercontent.com/assets/650407/15034074/7b973208-12a4-11e6-943e-ea04e1389c01.png" alt="nearest Neighbour" style="max-width: 100%;"></a><br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/650407/15034080/88bfbc84-12a4-11e6-81be-b0fa632d7997.png"><img src="https://cloud.githubusercontent.com/assets/650407/15034080/88bfbc84-12a4-11e6-81be-b0fa632d7997.png" alt="bilinear" style="max-width: 100%;"></a><br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/650407/15034081/88c2a41c-12a4-11e6-9681-f008ec36ff0f.png"><img src="https://cloud.githubusercontent.com/assets/650407/15034081/88c2a41c-12a4-11e6-9681-f008ec36ff0f.png" alt="bicubic" style="max-width: 100%;"></a><br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/650407/15034079/88bb4604-12a4-11e6-9ff9-1a53c49a5ab3.png"><img src="https://cloud.githubusercontent.com/assets/650407/15034079/88bb4604-12a4-11e6-9ff9-1a53c49a5ab3.png" alt="area" style="max-width: 100%;"></a></p>
<p dir="auto">It looks like the op <code class="notranslate">tf.image.resize_images()</code> has no effect on the image shape when the parameters <code class="notranslate">new_height</code> and <code class="notranslate">new_width</code> are dynamically computed (this is possible since the fix of issue <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="131664218" data-permission-text="Title is private" data-url="https://github.com/tensorflow/tensorflow/issues/1001" data-hovercard-type="issue" data-hovercard-url="/tensorflow/tensorflow/issues/1001/hovercard" href="https://github.com/tensorflow/tensorflow/issues/1001">#1001</a>).</p>
<p dir="auto">For example, this piece of code</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_shorter_edge = tf.constant(400, dtype=tf.int32)

height_smaller_than_width = tf.less_equal(height, width)
new_height_and_width = tf.cond(
    height_smaller_than_width,
    lambda: (new_shorter_edge, _compute_longer_edge(height, width, new_shorter_edge)),
    lambda: (_compute_longer_edge(width, height, new_shorter_edge), new_shorter_edge)
)

image = tf.image.resize_images(image, new_height_and_width[0], new_height_and_width[1])
image = tf.Print(image, [tf.shape(image), height, width, new_height_and_width[0], new_height_and_width[1]])
return tf.image.random_crop(image, [224, 224])"><pre class="notranslate"><code class="notranslate">shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_shorter_edge = tf.constant(400, dtype=tf.int32)

height_smaller_than_width = tf.less_equal(height, width)
new_height_and_width = tf.cond(
    height_smaller_than_width,
    lambda: (new_shorter_edge, _compute_longer_edge(height, width, new_shorter_edge)),
    lambda: (_compute_longer_edge(width, height, new_shorter_edge), new_shorter_edge)
)

image = tf.image.resize_images(image, new_height_and_width[0], new_height_and_width[1])
image = tf.Print(image, [tf.shape(image), height, width, new_height_and_width[0], new_height_and_width[1]])
return tf.image.random_crop(image, [224, 224])
</code></pre></div>
<p dir="auto">throws the following exception:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="I tensorflow/core/kernels/logging_ops.cc:79] [122 160 3][122][160][400][524]
W tensorflow/core/common_runtime/executor.cc:1096] 0x7f9c08008490 Compute status: Failed precondition: width must be &gt;= target_width: width = 160, target_width = 224"><pre class="notranslate"><code class="notranslate">I tensorflow/core/kernels/logging_ops.cc:79] [122 160 3][122][160][400][524]
W tensorflow/core/common_runtime/executor.cc:1096] 0x7f9c08008490 Compute status: Failed precondition: width must be &gt;= target_width: width = 160, target_width = 224
</code></pre></div>
<p dir="auto">So as you can see in the first line of the printout, the shape of the image is not affected by <code class="notranslate">tf.image.resize_images</code>. Is this a bug or am I doing something wrong?</p>
<hr>
<p dir="auto">I also made up a workaround for this:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(image, tf.pack(new_height_and_width))
image = tf.squeeze(image, [0])
image = tf.Print(image, [tf.shape(image), height, width, new_height_and_width[0], new_height_and_width[1]])
return tf.image.random_crop(image, [224, 224])"><pre class="notranslate"><code class="notranslate">image = tf.expand_dims(image, 0)
image = tf.image.resize_bilinear(image, tf.pack(new_height_and_width))
image = tf.squeeze(image, [0])
image = tf.Print(image, [tf.shape(image), height, width, new_height_and_width[0], new_height_and_width[1]])
return tf.image.random_crop(image, [224, 224])
</code></pre></div>
<p dir="auto">for example prints</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="I tensorflow/core/kernels/logging_ops.cc:79] [400 711 3][281][500][400][711]"><pre class="notranslate"><code class="notranslate">I tensorflow/core/kernels/logging_ops.cc:79] [400 711 3][281][500][400][711]
</code></pre></div>