ValueError:输入0与图层不兼容:预期形状=(无,48,187,621,64),找到形状=(48,187,621,64)

时间:2020-08-19 04:19:35

标签: python tensorflow keras

我正在张量流中训练一个模型,其中输入不是批处理的,而是形状的单个输入:

(48,187,621,64)

当我在模型l_regularization内部传递此输入时,如下所示:

make_regularization(l_cost_volume)

我得到了错误:

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)

如何解决此问题?是否可以通过“无”扩展单个图像的尺寸,或者让模型接受“找到的形状”尺寸? 顺便说一句,扩展“找到的形状”的尺寸也不起作用。它给出:

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 个答案:

答案 0 :(得分:0)

要添加批次尺寸,您可以轻松使用tf.expand_dims()或使用None进行特殊索引。第一种方法是

l_cost_volume = tf.expand_dims(l_cost_volume)

另一个是

l_cost_volume = l_cost_volume[None]

两者的输出形状均为(1, 48, 187, 621, 64)