votes up 8

'image' (shape %s) must have either 3 or 4 dimensions.

Exception Class:

Raise code

is_batch = False
      image = array_ops.expand_dims(image, 0)
    elif image_shape.ndims is None:
      is_batch = False
      image = array_ops.expand_dims(image, 0)
      image.set_shape([None] * 4)
    elif image_shape.ndims != 4:
      raise ValueError(
          '\'image\' (shape %s) must have either 3 or 4 dimensions.' %

    assert_ops = _CheckAtLeast3DImage(image, require_static=False)
    batch, height, width, depth = _ImageDimensions(image, rank=4)

    after_padding_width = target_width - offset_width - width

😲 Agile task management is now easier than calling a taxi. #Tracklify
🙏 Scream for help to Ukraine
Today, 3rd 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

The function pad_to_bounding_box pads an image with zeros to the specified height and width.

The image parameter should be 4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels]


output = tf.image.pad_to_bounding_box(image, 

Reproducing the error:

import tensorflow as tf

image = tf.random.normal((28,28))
padded_image = tf.image.pad_to_bounding_box(image, 433231)

The error:

ValueError                                Traceback (most recent call last)
<ipython-input-1-4474a7b62f1c> in <module>()
      3 image = tf.random.normal((28,28))
----> 4 padded_image = tf.image.pad_to_bounding_box(image, 4, 3, 32, 31)
      5 print(padded_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 pad_to_bounding_box(image, offset_height, offset_width, target_height, target_width)
   1063       raise ValueError(
   1064           '\'image\' (shape %s) must have either 3 or 4 dimensions.' %
-> 1065           image_shape)
   1067     assert_ops = _CheckAtLeast3DImage(image, require_static=False)

ValueError: 'image' (shape (28, 28)) must have either 3 or 4 dimensions.

Fixed version of the code:

import tensorflow as tf

image = tf.random.normal((28,28,3))
padded_image = tf.image.pad_to_bounding_box(image, 433231)


(32, 31, 3)

Jul 10, 2021 kellemnegasi answer
kellemnegasi 30.0k

Add a possible fix

Please authorize to post fix