votes up 7

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

Exception Class:

Raise code

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

      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.
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

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],

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

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/ 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/ 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)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/ 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')

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


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],

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

The output:

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

Jul 10, 2021 kellemnegasi answer
kellemnegasi 30.0k

Add a possible fix

Please authorize to post fix