简体   繁体   中英

Keras MaxPooling2D layer does not reduce the shape

I am building a neural network, more specifically a CNN to classify the locations of various proteins in human cell organelles. I have 512 x 512 images in 4 channels R, G, B, and Y:

def ModelMaker(intTuple):

    model = Sequential()

    model.add(Conv2D(8, (7, 7), strides=(2, 2), kernel_initializer='he_normal', bias_initializer='zeros',
                     kernel_regularizer='l2', padding='valid', data_format="channels_last", input_shape=intTuple))

    model.add(Conv2D(16, (3, 3), strides=(1, 1), padding='same', kernel_regularizer='l2'))
    model.add(PReLU(alpha_initializer=VarianceScaling(scale=1.0, mode='fan_in', distribution='normal', seed=None)))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'))

    model.add(Conv2D(32, (3, 3), strides=(1, 1), padding='same', kernel_regularizer='l2'))
    model.add(PReLU(alpha_initializer=VarianceScaling(scale=1.0, mode='fan_in', distribution='normal', seed=None)))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'))

    model.add(Conv2D(64, (3, 3), strides=(1, 1), padding='same', kernel_regularizer='l2'))
    model.add(PReLU(alpha_initializer=VarianceScaling(scale=1.0, mode='fan_in', distribution='normal', seed=None)))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'))

    model.add(Conv2D(128, (3, 3), strides=(1, 1), padding='same', kernel_regularizer='l2'))
    model.add(PReLU(alpha_initializer=VarianceScaling(scale=1.0, mode='fan_in', distribution='normal', seed=None)))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'))

    model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', kernel_regularizer='l2'))
    model.add(PReLU(alpha_initializer=VarianceScaling(scale=1.0, mode='fan_in', distribution='normal', seed=None)))
    model.add(AveragePooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'))

    model.add(Flatten())

    model.add(Dense(28))
    model.add(Activation('softmax'))

    return model

model = ModelMaker((512, 512, 4))
model.summary()

Everything is good, but when I create a model and run a model.summary() , and go through the layers, something is wierd. I have the following sequence of layers for each convolution: Conv2D ---> PReLU ---> MaxPooling2D/AveragePooling2D . However, the model summary looks like this:

Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_6 (Conv2D)            (None, 253, 253, 8)       1576      
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 253, 253, 16)      1168      
_________________________________________________________________
p_re_lu_5 (PReLU)            (None, 253, 253, 16)      1024144   
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 253, 253, 16)      0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 253, 253, 32)      4640      
_________________________________________________________________
p_re_lu_6 (PReLU)            (None, 253, 253, 32)      2048288   
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 253, 253, 32)      0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 253, 253, 64)      18496     
_________________________________________________________________
p_re_lu_7 (PReLU)            (None, 253, 253, 64)      4096576   
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 253, 253, 64)      0         
_________________________________________________________________
conv2d_10 (Conv2D)           (None, 253, 253, 128)     73856     
_________________________________________________________________
p_re_lu_8 (PReLU)            (None, 253, 253, 128)     8193152   
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 253, 253, 128)     0         
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 253, 253, 256)     295168    
_________________________________________________________________
p_re_lu_9 (PReLU)            (None, 253, 253, 256)     16386304  
_________________________________________________________________
average_pooling2d_1 (Average (None, 253, 253, 256)     0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 16386304)          0         
_________________________________________________________________
dense_1 (Dense)              (None, 28)                458816540 
_________________________________________________________________
activation_1 (Activation)    (None, 28)                0         
=================================================================
Total params: 490,959,908
Trainable params: 490,959,908
Non-trainable params: 0
_________________________________________________________________

Something seems to be wrong with the dimensionality reduction. The size of the tensors is all the same for the first two dimensions (253, 253, XX). It stays constant at 253... only the channel sizes increase, which is normal. I removed my last two dense layer stacks as the model won't even define itself as the runtime crashes as the ram is used up. I am guessing this is due to the MONSTROUS size of the dense layers when the shape (253, 253, 256) is flattened and passed. The entire model won't fit on the RAM. Help!

Use strides=(2, 2) or leave them blank. Then, the max_pooling2d will work. The number of trainable parameters will decrease significantly. If unspecified, max_pooling2d will default to pool_size . See the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM