在Keras中对4D数据执行2D卷积

hiz5n14c  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(204)

我有尺寸24*64*64*10的数据(不包括批量)。
我想将输入拆分为24个维度为64*64*10的输入,对每个输入执行Conv2D,然后将它们连接起来,再次获得4D数据以供进一步处理。
任何有关实施的帮助都将是有益的。我正在与Keras合作。
编辑:我尝试使用下面的代码来执行2D卷积

num_ch= 24
input= Input(shape=(64,64,10,num_ch))
print(input.shape)
branch_out= []
for i in range(num_ch):
    out= Lambda(lambda x: x[:,:,:,:,i] )(input)
    print(out.shape)
    out= Conv2D(10, kernel_size=(3,3),strides= (1,1), padding='same', data_format= 'channels_last')(input)
    branch_out.append(out)

出现以下错误:

(?, 64, 64, 10, 24)
(?, 64, 64, 10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-83-51977f4edbba> in <module>
      7     out= Lambda(lambda x: x[:,:,:,:,i] )(input)
      8     print(out.shape)
----> 9     out= Conv2D(10, kernel_size=(3,3),strides= (1,1), padding='same', data_format= 'channels_last')(input)
     10     branch_out.append(out)

~/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    412                 # Raise exceptions in case the input is not compatible
    413                 # with the input_spec specified in the layer constructor.
--> 414                 self.assert_input_compatibility(inputs)
    415 
    416                 # Collect input shapes to build layer.

~/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
    309                                      self.name + ': expected ndim=' +
    310                                      str(spec.ndim) + ', found ndim=' +
--> 311                                      str(K.ndim(x)))
    312             if spec.max_ndim is not None:
    313                 ndim = K.ndim(x)

ValueError: Input 0 is incompatible with layer conv2d_25: expected ndim=4, found ndim=5

我不明白为什么它仍然显示维数为5。

6kkfgxo0

6kkfgxo01#

2D卷积的输出格式是4DTensor,其形式为
(批次大小、高度、宽度、通道)(默认情况下,数据格式=“通道最后”)。
因此,如果将32x32x3图像输入提供给conv2D图层,其中滤波器数量= 8,核大小=(1X1)(2D conv图层将具有2D核矩阵),则输出Tensor将为(无,32,32,8)要了解2D核如何在3D图像上工作,请参阅Understanding the output shape of conv2d layer in keras
对于您的代码,只需将input替换为(64,64,10)

input= Input(shape=(64,64,10))
out= Conv2D(10, kernel_size=(3,3),strides= (1,1), padding='same', data_format= 'channels_last')(input)

相关问题