【问题标题】:Performing Conv2D on 4D data in Keras在 Keras 中对 4D 数据执行 Conv2D
【发布时间】:2019-08-14 06:48:23
【问题描述】:

我有维度为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

【问题讨论】:

    标签: python keras conv-neural-network


    【解决方案1】:

    你在这行有错字:

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

    改成:

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

    【讨论】:

      【解决方案2】:

      回答为时已晚,但对于有相同问题的人来说...
      我认为您可以将其传递给 Conv 层(也许我错了!)。下面的代码是一个来自 keras 的例子:link

      >>> # With extended batch shape [4, 7]:  
      >>> input_shape = (4, 7, 28, 28, 3)
      >>> x = tf.random.normal(input_shape)
      >>> y = tf.keras.layers.Conv2D(
      ... 2, 3, activation='relu', input_shape=input_shape[2:])(x)
      >>> print(y.shape)
      (4, 7, 26, 26, 2)
      

      或者另一种方式是使用TimeDistributed 层。看this link

      model = Sequential()
      model.add(TimeDistributed(Conv2D(5, (3,3), padding='same'), input_shape=(10, 100, 100, 3)))
      
      model.summary()
      

      模型总结:

      Layer (type)                 Output Shape              Param #   
      =================================================================
      time_distributed_2 (TimeDist (None, 10, 100, 100, 5)   140       
      =================================================================
      Total params: 140
      Trainable params: 140
      Non-trainable params: 0
      _________________________________________________________________
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-16
        • 1970-01-01
        • 2019-09-19
        • 2019-01-03
        • 2018-08-31
        • 1970-01-01
        • 2020-03-10
        • 1970-01-01
        相关资源
        最近更新 更多