votes up 7

output_shape must be a 1-D Tensor of 2 elements: new_height, new_width, instead got (param1)

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

  if output_shape_value is not None:
          output_shape = output_shape_value

    output_shape = tf.convert_to_tensor(
        output_shape, tf.int32, name='output_shape')

    if not output_shape.get_shape().is_compatible_with([2]):
      raise ValueError('output_shape must be a 1-D Tensor of 2 elements: '
                       'new_height, new_width, instead got '
                       '{}'.format(output_shape))

    fill_value = tf.convert_to_tensor(
        fill_value, tf.float32, name='fill_value')

    return tf.raw_ops.ImageProjectiveTransformV3(
      
😲  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 4 votes down

The function image_preprocessing.transform applies the given transform(s) to the image(s). Output dimesion after the transform is specified using he parameteroutput_shape in the form of (height, width). In other words the output_shape

should be a list like object of two elements.

Reproducing the error:

pipenv install tensorflow

import numpy as np
from keras.layers.preprocessing import image_preprocessing
img = np.arange(15).reshape((1, 5, 3, 1)).astype(np.float32)
transform_matrix = np.asarray([[1., 0., 1., 0., 1., 0., 0., 0.]])
output = image_preprocessing.transform(img,
                                       transforms=transform_matrix,
                                       output_shape=(1,28,28) # this causes the error
                                       )
print(output.shape)

The error output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-4c9210782c6e> in <module>()
      5 output = image_preprocessing.transform(img,
      6                                        transforms=transform_matrix,
----> 7                                        output_shape=(1,28,28))
      8 print(output.shape)

/usr/local/lib/python3.7/dist-packages/keras/layers/preprocessing/image_preprocessing.py in transform(images, transforms, fill_mode, fill_value, interpolation, output_shape, name)
    658       raise ValueError('output_shape must be a 1-D Tensor of 2 elements: '
    659                        'new_height, new_width, instead got '
--> 660                        '{}'.format(output_shape))
    661 
    662     fill_value = tf.convert_to_tensor(

ValueError: output_shape must be a 1-D Tensor of 2 elements: new_height, new_width, instead got [ 1 28 28]

How to fix it:

The output_shape parameter should be 1D array of two elements or a tuple of two elements in the form of [new_height,new_width] or(new_height, new_width) .

import numpy as np
from keras.layers.preprocessing import image_preprocessing
img = np.arange(15).reshape((1, 5, 3, 1)).astype(np.float32)
transform_matrix = np.asarray([[1., 0., 1., 0., 1., 0., 0., 0.]])
output = image_preprocessing.transform(img,
                                       transforms=transform_matrix,
                                       output_shape=(28,28))
print(output.shape)


Output:

(1, 28, 28, 1)

Jul 16, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix