[SOLVED] Layer up_sampling2d_1 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>

Issue

I am testing how the UpSampling2D layer works in Keras, so I am just trying to pass different inputs to check the output. While checking the example, given in the official documentation here, I can not reproduce the example, and it is giving me the error.

Code in documentation:

>>> input_shape = (2, 2, 1, 3)
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
>>> print(x)
[[[[ 0  1  2]]
  [[ 3  4  5]]]
 [[[ 6  7  8]]
  [[ 9 10 11]]]]
>>> y = tf.keras.layers.UpSampling2D(size=(1, 2))(x)
>>> print(y)
tf.Tensor(
  [[[[ 0  1  2]
     [ 0  1  2]]
    [[ 3  4  5]
     [ 3  4  5]]]
   [[[ 6  7  8]
     [ 6  7  8]]
    [[ 9 10 11]
     [ 9 10 11]]]], shape=(2, 2, 2, 3), dtype=int64)

Error:

ValueError                                Traceback (most recent call last)
~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
    441             try:
--> 442                 K.is_keras_tensor(x)
    443             except ValueError:

~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
    467                           tf.SparseTensor)):
--> 468         raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. '
    469                          'Expected a symbolic tensor instance.')

ValueError: Unexpectedly found an instance of type `<class 'numpy.ndarray'>`. Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-9-73f9365e21ac> in <module>
      6 
      7 
----> 8 y = keras.layers.UpSampling2D(size=(1, 2))(x)
      9 print(y)

~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
    573                 # Raise exceptions in case the input is not compatible
    574                 # with the input_spec specified in the layer constructor.
--> 575                 self.assert_input_compatibility(inputs)
    576 
    577                 # Collect input shapes to build layer.

~/anaconda3/envs/TF_YOLO/lib/python3.5/site-packages/keras/engine/topology.py in assert_input_compatibility(self, inputs)
    446                                  'Received type: ' +
    447                                  str(type(x)) + '. Full input: ' +
--> 448                                  str(inputs) + '. All inputs to the layer '
    449                                  'should be tensors.')
    450 

ValueError: Layer up_sampling2d_2 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>. Full input: [array([[[[ 0,  1,  2]],

        [[ 3,  4,  5]]],


       [[[ 6,  7,  8]],

        [[ 9, 10, 11]]]])]. All inputs to the layer should be tensors.

How should I be able to test this layer without creating a model and testing it, just like given in the documentation.

Solution

Your error came from inputing numpy.ndarray instead of tensor, you can try this:

a = tf.constant([2,2,1,3])
x = tf.reshape(tf.range(tf.math.reduce_prod(a, 0)) , a.numpy())

y = tf.keras.layers.UpSampling2D(size=(1, 2))(x)
print(y)

Output:

tf.Tensor(
[[[[ 0  1  2]
   [ 0  1  2]]

  [[ 3  4  5]
   [ 3  4  5]]]


 [[[ 6  7  8]
   [ 6  7  8]]

  [[ 9 10 11]
   [ 9 10 11]]]], shape=(2, 2, 2, 3), dtype=int32)

Answered By – I'mahdi

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *