简体   繁体   中英

Convolutional Neural Network Layers

I want to detect certain patterns using a CNN, however, the last two layers of my CNN get an error when I try to run them. I have commented those layers in the code below. *Every Conv2D layer is repeated before the MaxPooling layer.

  inputs = tf.keras.layers.Input(shape=(256, 256, 27), name='input_layer')
        lambda_layer = tf.keras.layers.Lambda(lambda value: value / 255)(inputs)
        xp = tf.keras.layers.Conv2D(64, 3, padding='same', activation=tf.nn.relu)(lambda_layer)
        xp = tf.keras.layers.MaxPooling2D()(xp)  # by default uses 2,2
        xp = tf.keras.layers.BatchNormalization()(xp)
        xp = tf.keras.layers.Conv2D(94, 3, padding='same', activation=tf.nn.relu)(xp)
        xp = tf.keras.layers.MaxPooling2D()(xp)
        xp = tf.keras.layers.BatchNormalization()(xp)
        xp = tf.keras.layers.Conv2D(128, 3, padding='same', activation=tf.nn.relu)(xp)
        xp = tf.keras.layers.MaxPooling2D()(xp)
        xp = tf.keras.layers.BatchNormalization()(xp)
        xp = tf.keras.layers.Conv2D(156, 3, padding='valid', activation=tf.nn.relu)(xp)
        xp = tf.keras.layers.MaxPooling2D()(xp)
        xp = tf.keras.layers.BatchNormalization()(xp)
        xp = tf.keras.layers.Conv2D(256, 3, padding='same', activation=tf.nn.relu)(xp)
        xp = tf.keras.layers.MaxPooling2D()(xp)
        xp = tf.keras.layers.BatchNormalization()(xp)
        xp = tf.keras.layers.Conv2D(394, 3, padding='same', activation=tf.nn.relu)(xp)
        xp = tf.keras.layers.MaxPooling2D()(xp)
        xp = tf.keras.layers.BatchNormalization()(xp)
        xp = tf.keras.layers.Conv2D(458, 3, padding='same', activation=tf.nn.relu)(xp)
        xp = tf.keras.layers.MaxPooling2D()(xp)
        xp = tf.keras.layers.BatchNormalization()(xp)
    #    xp = tf.keras.layers.Conv2D(516, 3, padding='same', activation=tf.nn.relu)(xp)
     #   xp = tf.keras.layers.Conv2D(516, 3, padding='same', activation=tf.nn.relu)(xp)
     #   xp = tf.keras.layers.MaxPooling2D()(xp)
    #    xp = tf.keras.layers.BatchNormalization()(xp)
        xp = tf.keras.layers.Dropout(0.25)(xp)
        xp = tf.keras.layers.Flatten()(xp)
        xp = tf.keras.layers.Dense(1024, activation=tf.nn.relu)(xp)
        xp = tf.keras.layers.BatchNormalization()(xp)
        xp = tf.keras.layers.Dropout(0.25)(xp)
        xp = tf.keras.layers.Dense(512, activation=tf.nn.relu)(xp)
        xp = tf.keras.layers.Dropout(0.25)(xp)

I get an error


Call arguments received:
  • inputs=tf.Tensor(shape=(None, 2, 2, 394), dtype=float32)

Meaning I might have convolved the image too much so it's getting an error. What can I do to solve this?

Yes, the problem is your input shape is not enough to utilize pooling this much. Convolutional layers are okay since you are using padding="same" which means input shape and output shape of layer are identical. However everytime you use MaxPooling2D with a kernel of (2, 2) x and y dimensions of the input gets divided by 2. That's why after a while there is nothing to pool.

As you can see here, after a certain time you ran out of data to feed into you layers because you compressed it via pooling too much.

在此处输入图像描述

Possible solutions:

  1. You can either change you input shape

  2. Decrease the amount of MaxPooling2D layers.

  3. You can learn to use stride and padding parameters to have more control over the output shape after pooling.

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