如何解决"ValueError:"Concatenate"层需要除concat轴以外具有匹配形状的输入"? [英] How to resolve "ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis"?
本文介绍了如何解决"ValueError:"Concatenate"层需要除concat轴以外具有匹配形状的输入"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现3D CNN,但是由于输入形状不匹配,我收到了一条值错误消息.我要怎么做才能得到此错误,以及如何解决此类问题?
I am trying to implement a 3D CNN, but I am getting a value error message because the input shapes do not match. What am I doing wrong to get this error and how do I fix this kind of issue?
回溯中引用的部分代码如下:
The part of the code referenced in the traceback is as follows:
x = conv3d_bn(img_input, 32, 3, 3, 3, strides=(2, 2, 2), padding='same', name='Conv3d_1b_3x3')
x = conv3d_bn(img_input, 64, 3, 3, 3, strides=(2, 2, 2), padding='same', name='Conv3d_1c_3x3')
branch_0 = MaxPooling3D((1, 3, 3), strides=(1, 2, 2), padding='same', name='MaxPool2d_2a_3x3')(x)
branch_1 = conv3d_bn(branch_0, 96, 3, 3, 3, padding='same', name='Conv3d_2b_3x3')
x = layers.concatenate([branch_0, branch_1], axis=channel_axis, name='mixed_2a')
branch_0 = conv3d_bn(x, 64, 1, 1, 1, padding='same', name='Conv3d_3_0a_1x1')
branch_0 = conv3d_bn(branch_0, 96, 3, 3, 3, padding='same', name='Conv3d_3c_0b_3x3')
branch_1 = conv3d_bn(x, 64, 1, 1, 1, padding='same', name='Conv3d_3_1a_1x1')
branch_1 = conv3d_bn(branch_1, 64, 7, 1, 1, padding='same', name='Conv3d_3c_1b_3x3')
branch_1 = conv3d_bn(branch_1, 64, 1, 7, 7, padding='same', name='Conv3d_3c_1c_3x3')
branch_1 = conv3d_bn(branch_1, 96, 3, 3, 3, padding='same', name='Conv3d_3c_1d_3x3')
x = layers.concatenate([branch_0, branch_1], axis=channel_axis, name='mixed_3a')
branch_0 = conv3d_bn(x, 192, 1, 1, 1, padding='same', name='Conv3d_3_0a_1x1')
branch_1 = MaxPooling3D((1, 3, 3), strides=(1, 2, 2), padding='same', name='MaxPool2d_0b_3x3')(x)
x = layers.concatenate([branch_0, branch_1], axis=channel_axis, name='mixed_4a')
这是回溯:
Traceback (most recent call last):
File "train.py", line 292, in <module>
main(**vars(p.parse_args()))
File "train.py", line 155, in main
400, spatial_squeeze=True, endpoint_logit='Logits')
File "/home/larry/Documents/Projekt/i3dv2.py", line 241, in InceptionI3DV2
x = layers.concatenate([branch_0, branch_1], axis=channel_axis, name='mixed_4a')
File "/home/larry/anaconda3/lib/python3.7/site-packages/keras/layers/merge.py", line 649, in concatenate
return Concatenate(axis=axis, **kwargs)(inputs)
File "/home/larry/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py", line 463, in __call__
self.build(unpack_singleton(input_shapes))
File "/home/larry/anaconda3/lib/python3.7/site-packages/keras/layers/merge.py", line 362, in build
'Got inputs shapes: %s' % (input_shape))
ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 32, 75, 75, 192), (None, 32, 38, 38, 192)]
我尝试过
branch_0 = conv3d_bn(x, 192, 1, 3, 3, padding='same', name='Conv3d_3_0a_1x1')
branch_1 = MaxPooling3D((1, 3, 3), strides=(1, 2, 2), padding='same', name='MaxPool2d_0b_3x3')(x)
x = layers.concatenate([branch_0, branch_1], axis=channel_axis, name='mixed_4a')
仍然出现相同的错误.
期待您的评论.
推荐答案
基本上是这样.
考虑串联轴,例如追加更多行.其余部分必须具有相同的形状.在我的图中,串联轴是垂直箭头.
Think of concatenation axis like appending more rows. The rest must be of equal shape. In my drawing, the concatenation axis are the vertical arrows.
这篇关于如何解决"ValueError:"Concatenate"层需要除concat轴以外具有匹配形状的输入"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文