Liam Spring
Liam Spring

Reputation: 59

Input_shape in 3D CNN

I have a dataset of 100000 binary 3D arrays of shape (6, 4, 4) so the shape of my input is (10000, 6, 4, 4). I'm trying to set up a 3D Convolutional Neural Network (CNN) using Keras; however, there seems to be a problem with the input_shape that I enter. My first layer is:

model.add(Conv3D(20, kernel_size=(2, 2, 2), strides=(1, 1, 1), activation='relu', kernel_initializer='he_uniform', input_shape=(None, 6, 4, 4, 1)))

The error I receive is:

ValueError: The last dimension of the inputs to Dense should be defined. Found None.

Still, when I replace None with an integer and I try to fit the model to my dataset the error I get is:

ValueError: Input 0 of layer sequential_13 is incompatible with the layer: : expected min_ndim=5, found ndim=4. Full shape received: (10, 6, 4, 4)

What should be modified here?

Upvotes: 0

Views: 1772

Answers (1)

Frightera
Frightera

Reputation: 5079

Example with dummy data:

import tensorflow as tf

x = tf.random.normal((100000, 6, 4, 4))
x.numpy().shape # (100000, 6, 4, 4) # 4 --> channel size by default

That will not work with 3D convolution:

y = tf.keras.layers.Conv3D(2, 3, activation='relu', 
                           input_shape=(6, 4, 4, 1))(x)
print('After convolution', y.shape)

It will say expected min_ndim=5, found ndim=4 .... You need to add a channel dimension to that data. This can be simply done with:

x_expanded = tf.expand_dims(x, axis = -1)
print('Expand_dims shape', x_expanded.numpy().shape) # (100000, 6, 4, 4, 1)

y = tf.keras.layers.Conv3D(2, 3, activation='relu', 
                           input_shape=(6, 4, 4, 1))(x_expanded)
print('After convolution', y.shape) # (100000, 4, 2, 2, 2)

Or at the first time, you can use the data with 2D convolution:

x = tf.random.normal((100000, 6, 4, 4))
y = tf.keras.layers.Conv2D(2, 3, activation='relu', 
                           input_shape=(6, 4, 4))(x)
print('After convolution', y.shape) # (100000, 4, 2, 2)

You can also change the order of the channels in the convolution layer:

y = tf.keras.layers.Conv2D(2, 3, activation='relu', 
                           input_shape=(6, 4, 4),
                           data_format="channels_first")(x) # --> 6 will be channels
print(y.shape) # (100000, 2, 2, 2)

Edit:

model.add(tf.keras.layers.Conv3D(2, 3, activation='relu', input_shape=(6, 4, 4, 1)))

model(x_expanded).shape # TensorShape([100000, 4, 2, 2, 2])

Upvotes: 1

Related Questions