ValueError: Input 0 is incompatible with layer: expected shape=(None, 48, 187, 621, 64), found shape=(48, 187, 621, 64)

Viewed 2121

I'm training a model in tensorflow, where the input isn't batched but is a single input of shape:

(48, 187, 621, 64)

When I pass this input inside the model l_regularization, like so:

make_regularization(l_cost_volume)

I get the error:

Traceback (most recent call last):
  File "train.py", line 300, in <module>
    train(ds, epochs)
  File "train.py", line 278, in train
    x_train_right, y_train_right_noc)
  File "train.py", line 242, in train_step
    l_regularization = make_regularization(l_cost_volume)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 977, in __call__
    input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
  File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py", line 274, in assert_input_compatibility
    ', found shape=' + display_shape(x.shape))
ValueError: Input 0 is incompatible with layer gc-net-part1: expected shape=(None, 48, 187, 621, 64), found shape=(48, 187, 621, 64)

How to fix this? Is there a way to extend the dimension of the single image by None or alternatively have the model accept the "found shape" dimension? By the way, extending the dimension of the "found shape" doesn't work either. It gives:

ValueError: Input 0 is incompatible with layer gc-net-part1: expected shape=(None, 48, 187, 621, 64), found shape=(1, 48, 187, 621, 64)
1 Answers

To add a batch dimension, you can easily make use of tf.expand_dims() or by special indexing using None. The first method is

l_cost_volume = tf.expand_dims(l_cost_volume)

The other is

l_cost_volume = l_cost_volume[None]

The output shape of both will be (1, 48, 187, 621, 64)

Related