0

I want to implement a CNN model with Keras. My data has the shape NHW, respectivly (N, 40, 4). You can see it as an gray-scale Image of size 40x4. I want to stride the kernel which has size (3, 4) just over the Height-axis, so it should compute the result for 3 full rows. As you can see there are no Channels (which means I have a 3-Tensor), but Keras Conv2D layer requires a shape of NHWC (4-Tensor). I tried it anyhow by the following command, but it results obviously in an error:

tf.keras.layers.Conv2D(8, kernel_size=(3, 4), activation="relu", input_shape=(40, 4)

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (1, 40, 4)

Therefore, I tried to append a new axis to my input data, which shall represent the channel.

np.expand_dims(input_data, axis=3)

...

tf.keras.layers.Conv2D(1, kernel_size=(3, 4), activation="relu", input_shape=(40, 4, 1), data_format="channels_last")

This results in a different error message:

ValueError: Shape mismatch: The shape of labels (received (1,)) should equal the shape of logits except for the last dimension (received (40, 5)).

Do you know how I can implement such a CNN model with an input shape of NHW?

Globalhost
  • 13
  • 2
  • [This](https://stackoverflow.com/questions/43895750/keras-input-shape-for-conv2d-and-manually-loaded-images) question was similar to mine and solved my problem. – Globalhost Mar 17 '21 at 09:27

1 Answers1

0

Conv2D expects 4D tensor with shape: batch_shape + (channels, rows, cols) if data_format='channels_first' or 4+D tensor with shape: batch_shape + (rows, cols, channels) if data_format='channels_last'

In case of Gray-scale images channels value will be 1, where as for RGB images value will be 3.

import tensorflow as tf
input_shape = (4, 28, 28, 1)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv2D(
2, 3, activation='relu', input_shape=input_shape[1:])(x)
print(y.shape)

Output

(4, 26, 26, 2)