1
votes
import keras.layers as KL
input_image = KL.Input([None, None, 3], name = 'input_image')
x = KL.Conv2D(64, (3,3), padding='same')(input_image)

after Conv, I want to add a dense as below:

KL.Dense(2)(KL.Flatten()(x))

but there will be an error:

ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 64). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.

So if I want a model contained conv followed by dense which can accept any size of input, how should I do?

3

3 Answers

2
votes

Although it's not the same as flattening your input you could use Global Max Pooling:

 x = KL.GlobalMaxPooling2D()(x)

This will change your dimension from (None, None, None 64) to (None, 64) (including batch dimension). Global Max Pooling is a common way to close up convultional Networks and feed the output into a Dense Neural Network.

2
votes

To build a CNN model you should use a pooling layer and then a flatten one, as you can see in the example below.

The pooling layer will reduce the number of data to be analysed in the convolutional network, and then we use Flatten to have the data as a "normal" input to a Dense layer. Moreover, after a convolutional layer, we always add a pooling one.

The example below is for 1D CNN but has the same structure as the 2D ones. Again, Flatten() changes the shape of the output to use properly in the last Dense layer.

    model = Sequential()
    model.add(Conv1D(num_filters_to_use, (filters_size_tuple), input_shape=features_array_shape, activation='relu'))
    model.add(MaxPooling1D(pool_size=2))
    model.add(Flatten())
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))
0
votes

Neural networks don't work with variable sized inputs. Unless you are dealing with recurrent neural networks.

With a network with variable sized input, what would the weights of the network look like?

Typically, you will pick a size for your input layer and resize or pad your input to match that size.