Semantic Segmentation Keras model.predict produces duplicate mask

This question regards the Semantic Segmentation example Assignment 2 of Wk3 in Course 4.
I wrote a nearly identical segmenter module (“unet”), but for a binary task on biomedical images. The model trains well, but the prediction masks (rank 4) produced by the model (mine as well as Coursera’s) have what to me is an extra dimension.
Furthermore, the last axis (axis 3) of the predicted masks, of shape 2, contains two arrays/images, shown below, which seem identical. The duplication seems redundant and is the cause of a Keras error (the module can’t compute binary crossentropy as metric --because “true mask” and “predicted mask” are of different shapes).
Here is the code that generates the mask:

pred = unet.predict(sample_image[tf.newaxis, …])
pred.shape
(1, 1024, 1024, 2)
pred = (tf.squeeze(pred,[0]))
pred.shape
(1024,1024,2)

pred0= tf.expand_dims(pred[:, :, 0],-1) #slicing the tensor and expanding for display
pred1= tf.expand_dims(pred[:, :, 1],-1)
display([pred0, pred1])
surprisingly, the two slices are identical.
WHY does model.predict do this? And, could the duplication be avoided?

The model must be written to deal with “batches” of inputs, right? So if you feed it a single input, then you need to do “expand dims” to add the “samples” dimension. It only accepts and produces 4D tensors.

As to the output last dimension being 2, what’s the definition of the output layer of your model? The example in the assignment is a multiclass case so it is a Dense layer with n_classes outputs, with the value passed being 23. You probably specified it as n_classes = 2, but in a binary classification, you only need one output, right?

1 Like

As for the rank 4, I got it Paul. thanks!
What I still didn’t see was how the final layer turns these two arrays into 0s ad 1s. Now I see that’s the role of the “create_mask” function…
On my end this is resolved. Thanks again