Reputation: 7994
I'm training a binary classifier for grayscale images 129x107 pixels. Here's the model setup, with Conv2d commented out.
model = tf.keras.Sequential([
keras.Input(name="MBInput", shape=[129, 107, 1]),
keras.layers.Conv2D(3, kernel_size=3, input_shape=(129,107,1)), # Without this line it all works
keras.layers.Flatten(),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy')
When I include the convolution step tensorflow complains
Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 129, 107)
I'm probably missing something obvious. What's the right code for 3x3 convolutional filter in this context?
Upvotes: 0
Views: 161
Reputation:
If you specify input_shape
in Conv2D
layer then there is no need of explicitly add Input
layer to the model and don't mix keras
and tf.keras
Working code as shown below
import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(3, kernel_size=3, input_shape=(129,107,1)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(100, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy')
model.summary()
Output:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 127, 105, 3) 30
_________________________________________________________________
flatten (Flatten) (None, 40005) 0
_________________________________________________________________
dense (Dense) (None, 100) 4000600
_________________________________________________________________
dense_1 (Dense) (None, 1) 101
=================================================================
Total params: 4,000,731
Trainable params: 4,000,731
Non-trainable params: 0
_________________________________________________________________
If you would like to use Input
layer, then you can remove input_shape
from Conv2D
layer
import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.Input(name="MBInput", shape=[129, 107, 1]))
model.add(tf.keras.layers.Conv2D(3, kernel_size=3))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(100, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy')
model.summary()
Output:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 127, 105, 3) 30
_________________________________________________________________
flatten_1 (Flatten) (None, 40005) 0
_________________________________________________________________
dense_2 (Dense) (None, 100) 4000600
_________________________________________________________________
dense_3 (Dense) (None, 1) 101
=================================================================
Total params: 4,000,731
Trainable params: 4,000,731
Non-trainable params: 0
_________________________________________________________________
Here both models are same.
Upvotes: 1