user1447257
user1447257

Reputation:

Deep Net with keras for image segmentation

I am pretty new to deep learning; I want to train a network on image patches of size (256, 256, 3) to predict three labels of pixel-wise segmentation. As a start I want to provide one convolutional layer:

model = Sequential()
model.add(Convolution2d(32, 16, 16, input_shape=(3, 256, 256))

The model output so far is an image with 32 channels. Now, I want to add a dense layer which merges all of these 32 channels into three channels, each one predicting the probability of a class for one pixel.

How can I do that?

Upvotes: 2

Views: 3047

Answers (2)

FiReTiTi
FiReTiTi

Reputation: 5898

You have to use flatten between the convolution layers and the dense layer:

model = Sequential()
model.add(Convolution2d(32, 16, 16, input_shape=(3, 256, 256))
# Do not forget to add an activation layer after your convolution layer, so here.

model.add(Flatten())

model.add(Dense(3))
model.add(Activation("sigmoid")) # whatever activation you want.

Upvotes: 1

maz
maz

Reputation: 1980

The simplest method to merge your 32 channels back to 3 would be to add another convolution, this time with three filters (I arbitrarily set the filter sizes to be 1x1):

model = Sequential()
model.add(Convolution2d(32, 16, 16, input_shape=(3, 256, 256))
model.add(Convolution2d(3, 1, 1))

And then finally add an activation function for segmentation

model.add(Activation("tanh"))

Or you could add it all at once if you want to with activation parameter (arbitrarily chosen to be tanh)

model = Sequential()
model.add(Convolution2d(32, 16, 16, input_shape=(3, 256, 256))
model.add(Convolution2d(3, 1, 1,activation="tanh"))

https://keras.io/layers/convolutional/

Upvotes: 2

Related Questions