我有三個張量:A - (1, 3, 256, 256) B - (1, 3, 256, 256) - 這是一個白色影像張量 C - (256, 256) - 這是段張量
例如 C 看起來像:
tensor([[ 337, 337, 337, ..., 340, 340, 340],
[ 337, 337, 337, ..., 340, 340, 340],
[ 337, 337, 337, ..., 340, 340, 340],
...,
[1022, 1022, 1022, ..., 1010, 1010, 1010],
[1022, 1022, 1022, ..., 1010, 1010, 1010],
[1022, 1022, 1022, ..., 1010, 1010, 1010]], device='cuda:0')
其中 37 可以表示建筑物等。
張量 C 給出了線段形狀的位置。我想要的是根據張量 A 的位置將同一段復制到張量 B 上。這將是將段進行 photoshopping 到白色影像張量上。
這類似于掩蔽,我查看了 mask_select ( https://pytorch.org/docs/stable/generated/torch.masked_select.html ) 但它只回傳一維張量。
uj5u.com熱心網友回復:
您不需要選擇 中的像素C
,只需屏蔽它們:
select = 337 # which segment to select
select_mask = (C == select)[None, None, ...] # create binary mask and add singleton dimensions
# this is the part where you select the right part of A
B = B * (1 - select_mask) A * select_mask
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/342495.html