Segmented input area

Hello all,
I have my input image segmented into water region and land region.
I have my masked image:
image = Image.open(image_path).convert(“RGB”)
mask = Image.open(mask_path) .convert(“L”)

Convert to numpy arrays

image_np = np.array(image)
mask_np = np.array(mask)
binary_mask = (mask_np > 0).astype(np.uint8)

Apply mask to the image

segmented_part = image_np * np.expand_dims(mask_np, axis=2)
segmented_part.astype(“float32”)
segmented_part_image = Image.fromarray(segmented_part)

I have a U-Net network and my input is the masked image the tensor size is (batch size, channels (I have 3), width, height) like below
plot

How can I work with the features that are only in the water region, just predict the water region and have the loss for that region?

Thank you for your help

Write your own Dataset class that works with three things: image, object_mask, water_mask (where water pixel =1, land_pixel=0)

Do augmentation (I use albumentations lib) like this:
sample = self.augmentation(image=image, masks=[object_mask, water_mask ])

Write your own loss (likely IoU + BCE) and metrics that takes three objects (pred, object_mask, water_mask), then do
pred = pred * water_mask
object_mask= object_mask * water_mask
and then do standard IoU_loss(pred, object_mask) + BCE(pred, object_mask) on these new entities.

ChatGPT writes decent code for this task.