Henk
Henk

Reputation: 73

Keras MaxPooling3D not allowed

I'm trying to build a CNN and got stuck with MaxPooling3D layers not working. Both layers get an input shape of (1, 5, 32) and I'd like to max-pool over the depth using poolsize (1, 1, 32) so the output becomes of shape (1, 5, 1). However this throws the error:

ValueError: Input 0 of layer max_pooling3d is incompatible with the
 layer: expected ndim=5, found ndim=4. Full shape received: [None, 1,
 5, 32]

I don't understand why a dimension of 5 is expected/required. If I instead use MaxPooling2D layers with poolsize (1,1) everything compiles correctly and I get the model below.

> Model: "functional_1"
> __________________________________________________________________________________________________ 
Layer (type)                    Output Shape         Param #    Connected to                     
> ================================================================================================== 
input_1 (InputLayer)            [(None, 5, 5, 1)]    0                
 __________________________________________________________________________________________________ 
conv2d_1 (Conv2D)               (None, 5, 1, 32)     192         input_1[0][0]                    
 __________________________________________________________________________________________________ 
conv2d (Conv2D)                 (None, 1, 5, 32)     192         input_1[0][0]                    
 __________________________________________________________________________________________________ 
reshape (Reshape)               (None, 1, 5, 32)     0           conv2d_1[0][0]                   
 __________________________________________________________________________________________________ 
max_pooling2d (MaxPooling2D)    (None, 1, 5, 32)     0           conv2d[0][0]                     
 __________________________________________________________________________________________________ 
max_pooling2d_1 (MaxPooling2D)  (None, 1, 5, 32)     0           reshape[0][0]                    
 __________________________________________________________________________________________________ 
concatenate (Concatenate)       (None, 1, 5, 64)     0           max_pooling2d[0][0]              
                                                                max_pooling2d_1[0][0]            
 ================================================================================================== 
Total params: 384 Trainable params: 384 Non-trainable params: 0
 __________________________________________________________________________________________________
 
 Process finished with exit code 0

The code I used to build this:

    n=5
    inp_similarity = Input(shape=(n, n, 1)) 
    conv11 = Conv2D(32, (n, 1))(inp_similarity) 
    conv12 = Conv2D(32, (1, n))(inp_similarity) 
    reshape1 = Reshape((1,5,32))(conv12) 

    maxpl11 = MaxPooling2D((1, 1))(conv11) 
    maxpl12 = MaxPooling2D((1, 1))(reshape1) 

    merge1 = Concatenate()([maxpl11, maxpl12])

    model = Model(inp_similarity, merge1)
    model.compile(optimizer='adam',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    model.summary()

Upvotes: 1

Views: 396

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

your aim is to operate a 'pooling' on the the feature dimensionality... this is not the scope of the pooling layer... they operate pooling only on the spatial dimensionalities. you need something more simple

n=5
inp_similarity = Input(shape=(n, n, 1)) 
conv11 = Conv2D(32, (n, 1))(inp_similarity) 
conv12 = Conv2D(32, (1, n))(inp_similarity) 
reshape1 = Reshape((1,5,32))(conv12) 

maxpl11 = Lambda(lambda x: tf.reduce_max(x, axis=-1, keepdims=True))(conv11)
maxpl12 = Lambda(lambda x: tf.reduce_max(x, axis=-1, keepdims=True))(reshape1)

merge1 = Concatenate()([maxpl11, maxpl12])

model = Model(inp_similarity, merge1)
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

model.summary()

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_4 (InputLayer)            [(None, 5, 5, 1)]    0                                            
__________________________________________________________________________________________________
conv2d_35 (Conv2D)              (None, 5, 1, 32)     192         input_4[0][0]                    
__________________________________________________________________________________________________
conv2d_34 (Conv2D)              (None, 1, 5, 32)     192         input_4[0][0]                    
__________________________________________________________________________________________________
reshape_2 (Reshape)             (None, 1, 5, 32)     0           conv2d_35[0][0]                  
__________________________________________________________________________________________________
lambda_6 (Lambda)               (None, 1, 5, 1)      0           conv2d_34[0][0]                  
__________________________________________________________________________________________________
lambda_7 (Lambda)               (None, 1, 5, 1)      0           reshape_2[0][0]                  
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 1, 5, 2)      0           lambda_6[0][0]                   
                                                                 lambda_7[0][0]                   
==================================================================================================

Upvotes: 2

Related Questions