简体   繁体   English

pytorch 操作:火炬张量中值的并集和反转

[英]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)我在 cuda/GPU 上有一组对应于图像中不同对象的掩码(下面的形状和大小)

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? 1:我可以使用 torch.tensor 操作生成这些面具的并集吗? so that I can apply all of those once on the image?以便我可以在图像上应用所有这些一次?

2: How do I invert the values in torch tensor? 2:如何反转火炬张量中的值? I mean for 1, turn into 0 and vice versa.我的意思是 1,变成 0,反之亦然。 I have tried to ~mytensor but it says the operator is only applicable to integer or bool values.我试过 ~mytensor 但它说该运算符仅适用于整数或布尔值。 I have got float values in my tensors ie [1.] etc.我的张量中有浮点值,即 [1.] 等。

I intent to do all these operations on GPU without moving data back on CPU.我打算在 GPU 上执行所有这些操作,而不将数据移回 CPU。

Thank you.谢谢你。

2: How do I invert the values in torch tensor? 2:如何反转火炬张量中的值?

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 中减去值

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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