Dimension mismatch between output mask and original mask in dice loss in Semantic segmentation
I am doing multi-class semantic segmentation(4 classes + background). My mask dimension is (256, 256, 3) and the output mask dimension is (256, 256, 5). I took 5 because it is the number of classes.
Dice Loss Function
inputs = inputs.view(-1)
targets = targets.view(-1)
        
intersection = (inputs * targets).sum() ---&gt; error                       
dice = (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth)  
        
return 1 - dice

What should I do to make the two dimensions the same? The mask was extracted from a TIF file.
I have attached my mask image below.


I'm assuming the target segmentation you are showing is a RGB encoded map. You are looking to convert this 3-channel image into a 1-channel label map.
Assuming seg is your ground-truth segmentation map shaped as (b, 3, h, w). The label to color mapping can be arbitrarily set as:
colors = torch.FloatTensor([[0, 0, 0],
                            [1, 1, 0],
                            [1, 0, 0],
                            [0, 1, 0],
                            [0, 0, 1]])

For each color construct a mask of matching pixel and assign the corresponding label in a new tensor at those pixel positions:
b, _, h, w = seg.shape
gt = torch.zeros(b,1,h,w)
seg_perm = seg.permute(0,2,3,1)

for label, color in enumerate(colors):
    mask = torch.all(seg_perm == color, dim=-1).unsqueeze(1)
    gt[mask] = label


For example taking the following segmentation map:
&gt;&gt;&gt; seg = tensor([[[[1., 1., 0., 0.],
                    [1., 0., 0., 0.]],

                   [[0., 1., 0., 0.],
                    [0., 1., 0., 1.]],

                   [[0., 0., 0., 0.],
                    [0., 0., 1., 0.]]]])

For visualization purposes:
&gt;&gt;&gt; T.ToPILImage()(seg[0].repeat_interleave(100,2).repeat_interleave(100,1))


And the resulting label map will:
&gt;&gt;&gt; gt
tensor([[[[2., 1., 0., 0.],
          [2., 3., 4., 3.]]]])

