GhostRider
GhostRider

Reputation: 2170

How to implement a simple CNN for 3D data using Keras Conv3D

I am trying to implement a 3D convolutional neural network with medical imaging that are made up of 10 contiguous image slices that are 64x64 in shape. They are gray scale images. Therefore my input dimension is 64 x 64 x 10 and my first layer is

model = Sequential()
model.add(Conv3D(32, kernel_size=(3,3,3), strides=(1, 1, 1), input_shape=(64, 64, 10)))
model.add(Activation('relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))

With this code I get the error

Input 0 is incompatible with layer conv3d_1: expected ndim=5, found ndim=4

Therefore I reshaped my input to

model = Sequential()
model.add(Conv3D(32, kernel_size=(3,3,3), strides=(1, 1, 1), input_shape=(64, 64, 10, 1)))
model.add(Activation('relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))

Now I get the error

ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (128, 64, 64, 10, 1))

I have tried to override this in the Keras code but that leads to more errors and I am pretty sure that a volume of slices can be inputed - I just can't see where the issue is.

Upvotes: 1

Views: 5041

Answers (2)

photeesh
photeesh

Reputation: 312

Input_shape for Conv3D has 4 dimensions (time_sequence, width, height, channels)

In your case:

input_shape = (10, 64, 64, 1)

Upvotes: 2

Alvaro Romero Diaz
Alvaro Romero Diaz

Reputation: 320

This is a headache I've had for a few days.

What is happening is that Keras automatically sets the number of channels in an image as the depth, and uses it to set the final filter size. You should use Conv2D instead due to you have 3-dim images (you can understand it as RGB images).

As I have said, Keras fix the depth automatically as the number of channels. So if you use Conv2D an fix the filter size as (5x5) it really will be (5x5xnºchannels).

Replace:

model.add(Conv3D(32, kernel_size=(3,3,3), strides=(1, 1, 1), input_shape=(64, 64, 10)))

To:

model.add(Conv2D(32, kernel_size=(3,3), strides=(1, 1), input_shape=(64, 64, 10)))

You can see really what is happening in this image: KerasConv2D

In case you want to you want to work combining different channels you will have to create different towers with Keras (that receive different channels) and then put them together.

You can see also what is happening in this link.

Upvotes: 2

Related Questions