Search code examples
pythontensorflowkerascomputer-visionkeras-layer

Pass the correct size of neurons on conv layer


I have started to develop the FCNet based on this Figure below: enter image description here

The image size of input layer is (500,500,3) and the first convLayer has (698,698,3). Writing the code to check I received the (498,498,3). How can I proceed with this?

Follow the part of my code implemented using keras. This is just the first block of convolution.

from keras.models import *
from keras.layers import *
from keras.optimizers import *

def network(input_size=(IMAGE_SIZE,IMAGE_SIZE,3)):
    inputs = Input(input_size)
    conv1 = Conv2D(64, 3,  kernel_initializer='he_normal', activation='relu',padding='valid')(inputs)
    conv1 = Conv2D(64, 3,  kernel_initializer='he_normal', activation='relu',padding='valid')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    model = Model(input=inputs, output=pool1)


    model.summary()

Here is the output of the model summary.

enter image description here


Solution

  • In this case, they are performing a zero padding in order to fit the convolution layer.

    Try this:

    IMAGE_SIZE=500
    
    def network(input_size=(IMAGE_SIZE,IMAGE_SIZE,3)):
        inputs = Input(input_size)
        zero = ZeroPadding2D(padding=(100, 100), data_format=None)(inputs)
        conv1 = Conv2D(64, 3,  kernel_initializer='he_normal', activation='relu')(zero)
        conv1 = Conv2D(64, 3,  kernel_initializer='he_normal', 
        activation='relu',padding='same')(conv1)
        pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
        model = Model(input=inputs, output=pool1)
        model.summary()
    

    so in the next layer you can use padding='same' again