I cannot reproduce the issue using random inputs:
class CustomDataset(Dataset):
    def __init__(self, transforms=None): 
        self.images = np.random.randn(1000, 28, 28, 1).astype(np.float32)
        self.masks = np.random.randn(1000, 28, 28, 1).astype(np.float32)
        self.transforms = transforms

    def __getitem__(self, index):
        image = self.images[index]
        mask = self.masks[index]
        transformed_image = self.transforms(image)
      
        return transformed_image, mask

    def __len__(self):  
       return len(self.images)
   
    
transform = transforms.Compose(
    [transforms.ToPILImage(),
     transforms.ToTensor(),
     transforms.Normalize((0.5, ), (0.5, ))
])

train_data = CustomDataset(transform)
trainloader = DataLoader(train_data, batch_size=128, shuffle=True)

data, mask = next(iter(trainloader))
print(data.shape)
> torch.Size([128, 1, 28, 28])

print(mask.shape)
> torch.Size([128, 28, 28, 1])

and get the expected shapes.
Could you check, what the difference between the codes might be?