votes up 7

'size' must be a 1-D Tensor of 2 elements: new_height, new_width

Package:
Exception Class:
ValueError

Raise code

 height, width, _ = images.get_shape().as_list()

    try:
      size = ops.convert_to_tensor(size, dtypes.int32, name='size')
    except (TypeError, ValueError):
      raise ValueError('\'size\' must be a 1-D int32 Tensor')
    if not size.get_shape().is_compatible_with([2]):
      raise ValueError('\'size\' must be a 1-D Tensor of 2 elements: '
                       'new_height, new_width')

    if preserve_aspect_ratio:
      # Get the current shapes of the image, even if dynamic.
      _, current_height, current_width, _ = _ImageDimensions(images, rank=4)

      # do the computation to find the right scale and height/width.
      
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 2 votes down

When resizing images, the output shape is specified using the size parameter. Its values represent the new shape of the image as (new_height, new_width). In other words it should be a 1D tensor with two elements.

Reproducing the error:

pipenv install tensorflow

import tensorflow as tf
image = tf.constant([[1,0,0,0,0],
                     [0,1,0,0,0],
                     [0,0,1,0,0],
                     [0,0,0,1,0],
                     [0,0,0,0,1]])


image = tf.reshape(image, [1,image.shape[0],image.shape[1], 1])
print(image.shape)
new_image =  tf.image.resize(image, [3,])
print(new_image.shape)

The output error:

(1, 5, 5, 1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-d54c951a4782> in <module>()
      8 image = tf.reshape(image, [1,image.shape[0],image.shape[1], 1])
      9 print(image.shape)
---> 10 new_image =  tf.image.resize(image, [3,])
     11 print(new_image.shape)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    204     """Call target, and fall back on dispatchers if there is a TypeError."""
    205     try:
--> 206       return target(*args, **kwargs)
    207     except (TypeError, ValueError):
    208       # Note: convert_to_eager_tensor currently raises a ValueError, not a

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/image_ops_impl.py in resize_images_v2(images, size, method, preserve_aspect_ratio, antialias, name)
   1721       preserve_aspect_ratio=preserve_aspect_ratio,
   1722       name=name,
-> 1723       skip_resize_if_same=False)
   1724 
   1725 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/image_ops_impl.py in _resize_images_common(images, resizer_fn, size, preserve_aspect_ratio, name, skip_resize_if_same)
   1403       raise ValueError('\'size\' must be a 1-D int32 Tensor')
   1404     if not size.get_shape().is_compatible_with([2]):
-> 1405       raise ValueError('\'size\' must be a 1-D Tensor of 2 elements: '
   1406                        'new_height, new_width')
   1407 

ValueError: 'size' must be a 1-D Tensor of 2 elements: new_height, new_width


Fix:

Make sure the size parameter is a list of two elements or a 1D tensor of two elements.

import tensorflow as tf
image = tf.constant([[1,0,0,0,0],
                     [0,1,0,0,0],
                     [0,0,1,0,0],
                     [0,0,0,1,0],
                     [0,0,0,0,1]])


image = tf.reshape(image, [1,image.shape[0],image.shape[1], 1])
print(image.shape)
new_image =  tf.image.resize(image, [3,10])
print(new_image.shape)

The output:

(1, 5, 5, 1)
(1, 3, 10, 1)

Jul 10, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix