Hi, suppose I have a segmentation map a
with dimension of torch.Size([1, 1, 6, 6])
print(a)
tensor([[[[ 0., 0., 0., 0., 0., 0.],
[ 0., 15., 15., 16., 16., 0.],
[ 0., 15., 15., 16., 16., 0.],
[ 0., 13., 13., 9., 9., 0.],
[ 0., 13., 13., 9., 9., 0.],
[ 0., 0., 0., 0., 0., 0.]]]])
How can I get the binary masks for each id without using for
loop? The binary masks should have a dimension of torch.Size([1, 4, 6, 6])
, currently im doing something like this and the reason I want it without for
loop is that the dimension of a
might change and there might be more/less classes. Thanks.
a1 = torch.where(segmentation_a == 15, 1, 0)
a2 = torch.where(segmentation_a == 16, 1, 0)
a3 = torch.where(segmentation_a == 13, 1, 0)
a4 = torch.where(segmentation_a == 9, 1, 0)
b = torch.cat((a1, a2, a3, a4), dim=1)
print(b)
tensor([[[[0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]],
[[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]],
[[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]],
[[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0]]]])