0
$\begingroup$

I have an image segmentation task where my input image shape is (140, 85, 95, 4) and the output label shape is (140, 85, 95). Below is my model:

from tensorflow.keras.layers import Conv2D, Conv2DTranspose, Input, Rescaling

num_classes = 4

my_model = tf.keras.Sequential([

Input(shape = (85, 95, 4), name = 'image'),
Rescaling(scale = 1./255),
Conv2D(filters = 64, kernel_size = 3, strides = 1, activation = 'relu', padding = 'same'),
Conv2D(filters = 64, kernel_size = 3, activation = 'relu', padding = 'same'),
Conv2D(filters = 128, kernel_size = 3, strides = 1, activation = 'relu', padding = 'same'),
Conv2D(filters = 128, kernel_size = 3, activation = 'relu', padding = 'same'),
Conv2D(filters = 256, kernel_size = 3, strides = 1, activation = 'relu', padding = 'same'),
Conv2D(filters = 256, kernel_size = 3, activation = 'relu', padding = 'same'),

Conv2DTranspose(filters = 256, kernel_size = 3, activation = 'relu', padding = 'same'),
Conv2DTranspose(filters = 256, kernel_size = 3, strides = 1, activation = 'relu', padding = 'same'),
Conv2DTranspose(filters = 128, kernel_size = 3, activation = 'relu', padding = 'same'),
Conv2DTranspose(filters = 128, kernel_size = 3, strides = 1, activation = 'relu', padding = 'same'),
Conv2DTranspose(filters = 64, kernel_size = 3, activation = 'relu', padding = 'same'),
Conv2DTranspose(filters = 64, kernel_size = 3, strides = 1, activation = 'relu', padding = 'same'),

Conv2D(filters = num_classes, kernel_size = 3, activation = 'softmax', padding = 'same')

])

After training, I tried predicting one image and the model produced a label with shape (140, 85, 95, 4) as the output but I want it to be (140, 85, 95) or (140, 85, 95, 1).

How can I fix this? Thank you.

$\endgroup$

1 Answer 1

1
$\begingroup$

Your last layer uses 4 filter because num_classes is set to 4, resulting in an array with 4 channels in the last dimension. If you simply want only one channel simply change the number of filters for the last convolutional layer to one (filters=1).

$\endgroup$
4
  • $\begingroup$ I tried setting the last "filters" to 1 but it resulted in another error: labels.shape must equal logits.shape except for the last dimension. Received: labels.shape=(1130500,) and logits.shape=(11900, 95) $\endgroup$ Commented Dec 24, 2021 at 1:53
  • $\begingroup$ That has to do with the shape of the labels and the output from your model. Make sure that both contain the same number of observations. $\endgroup$
    – Oxbowerce
    Commented Dec 24, 2021 at 8:59
  • $\begingroup$ The number of observations are the same (140). Do you think it's because of the image shape, each one has a shape of (85, 95, 4) whereas a normal image's shape is (x, y, 3)? What else can I try? $\endgroup$ Commented Dec 24, 2021 at 14:07
  • $\begingroup$ It seems that it simply a shape error since 1130500 is equal to 11900x95. You should therefore make sure that bot arrays have the correct shape (i.e. (140, 85, 95) or (1130500,) depending on how you want to calculate your loss). $\endgroup$
    – Oxbowerce
    Commented Dec 24, 2021 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.