简体   繁体   中英

pytorch operations: union and inversion of values in torch tensor

I have set of masks on cuda/GPU corresponding to different objects in an image (shapes and sizes below)

masks.shape:            torch.Size([10, 240, 320, 1])
masks[0].shape:         torch.Size([240, 320, 1])
masks[0][:,:,0].shape:  torch.Size([240, 320])

1: Can I produce union of the these masks using torch.tensor operation? so that I can apply all of those once on the image?

2: How do I invert the values in torch tensor? I mean for 1, turn into 0 and vice versa. I have tried to ~mytensor but it says the operator is only applicable to integer or bool values. I have got float values in my tensors ie [1.] etc.

I intent to do all these operations on GPU without moving data back on CPU.

Thank you.

2: How do I invert the values in torch tensor?

t = torch.tensor([1., 0., 0., 1., 0., 1., 1.])

You can subtract values from 1 if you don't want to change type

1 - t
tensor([0., 1., 1., 0., 1., 0., 0.])

Or better you can convert it into boolean type and then use ~

~t.type(torch.bool)
tensor([False,  True,  True, False,  True, False, False])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM