ValueError:“连续”层要求输入的形状与连续轴一致,但形状必须匹配。得到了输入形状:[(无,523,523,32),等等

时间:2019-08-18 08:37:44

标签: python tensorflow keras deep-learning

我正在尝试使用下面的代码使用tensorflow来连接层,但是遇到了意外错误。我是tensorflow的新手

inp = Input(shape=(1050,1050,3))
x1= layers.Conv2D(16 ,(3,3), activation='relu')(inp)
x1= layers.Conv2D(32,(3,3), activation='relu')(x1)
x1= layers.MaxPooling2D(2,2)(x1)
x2= layers.Conv2D(32,(3,3), activation='relu')(x1)
x2= layers.Conv2D(64,(3,3), activation='relu')(x2)
x2= layers.MaxPooling2D(3,3)(x2)
x3= layers.Conv2D(64,(3,3), activation='relu')(x2)
x3= layers.Conv2D(64,(2,2), activation='relu')(x3)
x3= layers.Conv2D(64,(3,3), activation='relu')(x3)
x3= layers.Dropout(0.2)(x3)
x3= layers.MaxPooling2D(2,2)(x3)
x4= layers.Conv2D(64,(3,3), activation='relu')(x3)
x4= layers.MaxPooling2D(2,2)(x4)
x = layers.Dropout(0.2)(x4)
o = layers.Concatenate(axis=3)([x1, x2, x3, x4, x])
y = layers.Flatten()(o)
y = layers.Dense(1024, activation='relu')(y)
y = layers.Dense(5, activation='softmax')(y) 

model = Model(inp, y)
model.summary()
model.compile(loss='sparse_categorical_crossentropy',optimizer=RMSprop(lr=0.001),metrics=['accuracy'])

主要错误可以在标题中看到 但是我提供了回溯错误以供参考 错误是

ValueError                                Traceback (most recent call last)
<ipython-input-12-31a1fcec98a4> in <module>
     14 x4= layers.MaxPooling2D(2,2)(x4)
     15 x = layers.Dropout(0.2)(x4)
---> 16 o = layers.Concatenate(axis=3)([x1, x2, x3, x4, x])
     17 y = layers.Flatten()(o)
     18 y = layers.Dense(1024, activation='relu')(y)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    589           # Build layer if applicable (if the `build` method has been
    590           # overridden).
--> 591           self._maybe_build(inputs)
    592 
    593           # Wrapping `call` function in autograph to allow for dynamic control

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   1879       # operations.
   1880       with tf_utils.maybe_init_scope(self):
-> 1881         self.build(input_shapes)
   1882     # We must set self.built since user defined build functions are not
   1883     # constrained to set self.built.

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/utils/tf_utils.py in wrapper(instance, input_shape)
    293     if input_shape is not None:
    294       input_shape = convert_shapes(input_shape, to_tuples=True)
--> 295     output_shape = fn(instance, input_shape)
    296     # Return shapes from `fn` as TensorShapes.
    297     if output_shape is not None:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape)
    389                        'inputs with matching shapes '
    390                        'except for the concat axis. '
--> 391                        'Got inputs shapes: %s' % (input_shape))
    392 
    393   def _merge_function(self, inputs):

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 523, 523, 32), (None, 173, 173, 64), (None, 84, 84, 64), (None, 41, 41, 64), (None, 41, 41, 64)]

我已经使用tensorflow.keras导入了运行代码所需的所有必需文件

2 个答案:

答案 0 :(得分:1)

如果在添加图层时使用model.summary(),则会看到您要连接的图层的输出具有非常不同的形状。准确地说,实际上是错误中描述的形状。如错误所示,您需要对此进行改善。操作差异是造成这种情况的原因,您可以通过上述命令检查输出形状来快速发现。除了运算方差外,相对于输入形状的卷积核方差还会在其中投入额外的扳手,为此层参数padding='same'可以提供帮助。 为了清楚起见(由于代码中包含model.summary),我的意思是添加层,检查摘要,添加更多层并再次检查。您会看到形状问题的出现。

答案 1 :(得分:1)

您不能执行不同尺寸(即高度和宽度)的输入的串联操作。根据您的情况,您尝试执行此操作layers.Concatenate(axis=3)([x1, x2, x3, x4, x]),其中

x1 has dimension = (None, 523, 523, 32)
x2 has dimension = (None, 173, 173, 64)
x3 has dimension = (None, 84, 84, 64)
x4 has dimension = (None, 41, 41, 64)
and x has dimension = (None, 41, 41, 64)

发生此错误是因为所有输入尺寸(即要连接的高度和宽度)都不相同。要解决该错误,您必须将所有输入都设置为相同的尺寸,即相同的高度和宽度,这可以通过将图层采样到固定尺寸来实现。根据您的用例,您可以向下采样或向上采样以达到所需的尺寸。

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 523, 523, 32), (None, 173, 173, 64), (None, 84, 84, 64), (None, 41, 41, 64), (None, 41, 41, 64)]

错误状态为layer requires inputs with matching shapes,这只不过是输入的高度和宽度。