<h3 dir="auto">System information</h3>
<ul dir="auto">
<li>
<p dir="auto"><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>:<br>
YES</p>
</li>
<li>
<p dir="auto"><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>:<br>
Linux localhost.localdomain 3.10.0-514.el7.x86_64 <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="115886302" data-permission-text="Title is private" data-url="https://github.com/tensorflow/tensorflow/issues/1" data-hovercard-type="issue" data-hovercard-url="/tensorflow/tensorflow/issues/1/hovercard" href="https://github.com/tensorflow/tensorflow/issues/1">#1</a> SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux<br>
VERSION="7 (Core)"<br>
VERSION_ID="7"<br>
CENTOS_MANTISBT_PROJECT_VERSION="7"<br>
REDHAT_SUPPORT_PRODUCT_VERSION="7"</p>
</li>
<li>
<p dir="auto"><strong>TensorFlow installed from (source or binary)</strong>:<br>
Source</p>
</li>
<li>
<p dir="auto"><strong>TensorFlow version (use command below)</strong>:<br>
tf.VERSION = 1.2.1<br>
tf.GIT_VERSION = v1.2.1-0-gb4957ff<br>
tf.COMPILER_VERSION = v1.2.1-0-gb4957ff<br>
Sanity check: array([1], dtype=int32)</p>
</li>
<li>
<p dir="auto"><strong>Python version</strong>:<br>
2.7</p>
</li>
<li>
<p dir="auto"><strong>Bazel version (if compiling from source)</strong>:<br>
0.4.5</p>
</li>
<li>
<p dir="auto"><strong>CUDA/cuDNN version</strong>:<br>
CUDA8.0/CUDNN6.0</p>
</li>
<li>
<p dir="auto"><strong>GPU model and memory</strong>:<br>
GeForce GTX 1080  8113MiB</p>
</li>
<li>
<p dir="auto"><strong>Exact command to reproduce</strong>:<br>
See Source code</p>
</li>
</ul>
<h3 dir="auto">Describe the problem</h3>
<p dir="auto">As the title states, tf.image.resize_images returns different values compared to imresize in scipy or PIL. This is important because we expect the same behaviour for migrating code originally written using scipy.misc.imresize or PIL.</p>
<h3 dir="auto">Source code / logs</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import tensorflow as tf
from scipy.misc import imresize

image = (255 * np.random.rand(127, 127, 3)).astype(np.uint8)
resize_size = [255, 255]

image_resized = tf.image.resize_images(image, resize_size, method=tf.image.ResizeMethod.BILINEAR)
with tf.Session() as sess:
    image_resized_tf = sess.run(image_resized)

image_resized_np = imresize(image, resize_size, interp='bilinear')

diff = image_resized_np.astype(np.float32) - image_resized_tf.astype(np.float32)
print('resized image diff: {}'.format(np.mean(np.abs(diff))))
# resized image diff: 31.6155033112"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">tensorflow</span> <span class="pl-k">as</span> <span class="pl-s1">tf</span>
<span class="pl-k">from</span> <span class="pl-s1">scipy</span>.<span class="pl-s1">misc</span> <span class="pl-k">import</span> <span class="pl-s1">imresize</span>

<span class="pl-s1">image</span> <span class="pl-c1">=</span> (<span class="pl-c1">255</span> <span class="pl-c1">*</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">rand</span>(<span class="pl-c1">127</span>, <span class="pl-c1">127</span>, <span class="pl-c1">3</span>)).<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">uint8</span>)
<span class="pl-s1">resize_size</span> <span class="pl-c1">=</span> [<span class="pl-c1">255</span>, <span class="pl-c1">255</span>]

<span class="pl-s1">image_resized</span> <span class="pl-c1">=</span> <span class="pl-s1">tf</span>.<span class="pl-s1">image</span>.<span class="pl-en">resize_images</span>(<span class="pl-s1">image</span>, <span class="pl-s1">resize_size</span>, <span class="pl-s1">method</span><span class="pl-c1">=</span><span class="pl-s1">tf</span>.<span class="pl-s1">image</span>.<span class="pl-v">ResizeMethod</span>.<span class="pl-v">BILINEAR</span>)
<span class="pl-k">with</span> <span class="pl-s1">tf</span>.<span class="pl-v">Session</span>() <span class="pl-k">as</span> <span class="pl-s1">sess</span>:
    <span class="pl-s1">image_resized_tf</span> <span class="pl-c1">=</span> <span class="pl-s1">sess</span>.<span class="pl-en">run</span>(<span class="pl-s1">image_resized</span>)

<span class="pl-s1">image_resized_np</span> <span class="pl-c1">=</span> <span class="pl-en">imresize</span>(<span class="pl-s1">image</span>, <span class="pl-s1">resize_size</span>, <span class="pl-s1">interp</span><span class="pl-c1">=</span><span class="pl-s">'bilinear'</span>)

<span class="pl-s1">diff</span> <span class="pl-c1">=</span> <span class="pl-s1">image_resized_np</span>.<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">float32</span>) <span class="pl-c1">-</span> <span class="pl-s1">image_resized_tf</span>.<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-s1">float32</span>)
<span class="pl-en">print</span>(<span class="pl-s">'resized image diff: {}'</span>.<span class="pl-en">format</span>(<span class="pl-s1">np</span>.<span class="pl-en">mean</span>(<span class="pl-s1">np</span>.<span class="pl-en">abs</span>(<span class="pl-s1">diff</span>))))
<span class="pl-c"># resized image diff: 31.6155033112</span></pre></div>
<p dir="auto">The tf.image.resize_images() seems to use a strange padding option, which one is not clear to me at the moment. I tried to replicate the bilinear interpolation with various padding options in for example skimage, but cant replicate the behaviour.</p>
<p dir="auto">It would be nice to be able to set the padding option used in tf.images.resize_images(), or document what is used at least.</p>
<p dir="auto">Example code for comparing the results of  tf.images.resize_images() and skimage transform:<br>
Looks like  tf.images.resize_images() does some weird unsymmetrical padding!?<br>
Using tensorflow 0.12.1:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import tensorflow as tf
import tensorlayer as tl
import numpy as np
import skimage
from scipy.misc import imread, imresize, imsave

sess = tf.InteractiveSession()

#create simple test image
imsize = 3
xa, ya = np.ogrid[:imsize, :imsize]
img = np.repeat((xa + ya)[..., np.newaxis], 3, 2) / float(imsize + imsize)

x = tf.placeholder(tf.float32, [1, imsize, imsize, 3])
y = tf.image.resize_images(x,(imsize*3, imsize*3))

sess.run(tf.global_variables_initializer())

upsampled_tf_result = sess.run(y, feed_dict={x: [img]})
upsampled_skimage_result = skimage.transform.rescale(img,
                                     3,
                                     mode='symmetric',
                                     cval=0,
                                     order=1,
                                     preserve_range=False)

print(np.allclose(upsampled_tf_result, upsampled_skimage_result))

imsave('upsampled_tf_result.png', np.squeeze(upsampled_tf_result))
imsave('upsampled_skimage_result.png', upsampled_skimage_result)"><pre class="notranslate"><code class="notranslate">import tensorflow as tf
import tensorlayer as tl
import numpy as np
import skimage
from scipy.misc import imread, imresize, imsave

sess = tf.InteractiveSession()

#create simple test image
imsize = 3
xa, ya = np.ogrid[:imsize, :imsize]
img = np.repeat((xa + ya)[..., np.newaxis], 3, 2) / float(imsize + imsize)

x = tf.placeholder(tf.float32, [1, imsize, imsize, 3])
y = tf.image.resize_images(x,(imsize*3, imsize*3))

sess.run(tf.global_variables_initializer())

upsampled_tf_result = sess.run(y, feed_dict={x: [img]})
upsampled_skimage_result = skimage.transform.rescale(img,
                                     3,
                                     mode='symmetric',
                                     cval=0,
                                     order=1,
                                     preserve_range=False)

print(np.allclose(upsampled_tf_result, upsampled_skimage_result))

imsave('upsampled_tf_result.png', np.squeeze(upsampled_tf_result))
imsave('upsampled_skimage_result.png', upsampled_skimage_result)
</code></pre></div>