torch.masked_select 用于带mask的图像分割测试集DRIVE

类似与DRIVE这样的数据集,输入图像有一个非矩形的边界,并非我们图像分割感兴趣的部分,数据集给出了二值mask,用于屏蔽

网络结果向前传播后得到output,在training中与grand truth计算loss,在test中计算准确度指数,显然都需要忽略在mask黑色区域,test中相对容易解决mask的使用问题,那么training中怎么忽略mask黑色区域的损失呢?


torch.masked_select(很遗憾pytorch的官网手册被墙了),一下先搬运原文

torch.masked_select(input, mask, out=None) → Tensor

Returns a new 1-D tensor which indexes the input tensor according to the binary mask mask which is a ByteTensor.

The shapes of the mask tensor and the input tensor don’t need to match, but they mmaskust be broadcastable. s


mask表示一个dtype=ByteTensor的tensor,用mask=mask.byte()即可转化,但mask只能取0,1值

mask的shape与input一致这种情况很好理解,返回一个一维tensor,表示input中对应位置上mask值为1的tensor

mask的shape与input不一致时,比如dim数量不相等,维度长度不相等。显然需要在某种条件下,将mask唯一扩充为input的shape的方法,这就是文中的broadcasting字眼,下面是原文描述

Two tensors are “broadcastable” if the following rules hold:

  • Each tensor has at least one dimension.
  • When iterating over the dimension sizes, starting at the trailing dimension, the dimension sizes must either be equal, one of them is 1, or one of them does not exist.
>>> x=torch.empty(5,7,3)
>>> y=torch.empty(5,7,3)
# same shapes are always broadcastable (i.e. the above rules always hold)

>>> x=torch.empty((0,))
>>> y=torch.empty(2,2)
# x and y are not broadcastable, because x does not have at least 1 dimension

# can line up trailing dimensions
>>> x=torch.empty(5,3,4,1)
>>> y=torch.empty(  3,1,1)
# x and y are broadcastable.
# 1st trailing dimension: both have size 1
# 2nd trailing dimension: y has size 1
# 3rd trailing dimension: x size == y size
# 4th trailing dimension: y dimension doesn't exist

# but:
>>> x=torch.empty(5,2,4,1)
>>> y=torch.empty(  3,1,1)
# x and y are not broadcastable, because in the 3rd trailing dimension 2 != 3
  1. 维数短的一方在torch前部,注意是前面插入size为1的维至维数一致
  2. 逐维比较长度,如果不相等,则必需有一方在该维度长为1,然后在这个维度上复制叠加,至长度相等,否则失败

input 与mask满足broadcasting时就不会报错,一个小细节:

当mask是那个维度长的一方时,多出来的维度会自动忽略

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: torch.masked_select 是 PyTorch 中的一个函数,它可以根据给定的 mask(布尔类型的 tensor)来选择输入 tensor 中的元素。选中的元素将被组合成一个新的 1-D tensor,并返回。例如: ``` import torch x = torch.randn(3, 4) mask = x.ge(0) y = torch.masked_select(x, mask) ``` 在这个例子中, mask 是一个大小为 (3, 4) 的 tensor,其中包含 x 中每个元素是否大于等于 0 的布尔值, y 是一个 1-D tensor,其中包含了 x 中所有大于等于 0 的元素。 ### 回答2: torch.masked_select是PyTorch中的一个函数,用于根据给定的掩码(mask)从输入张量中选择元素。掩码是一个布尔张量,与输入张量具有相同的形状。 具体来说,torch.masked_select会返回一个新的一维张量,并包含输入张量中满足掩码为True的元素。返回的张量中的元素顺序与输入张量中的顺序保持一致。 使用torch.masked_select时,需要传入两个参数:输入张量和掩码。例如,如果有一个大小为(3, 3)的输入张量t和一个与其形状相同的掩码m,我们可以这样使用torch.masked_select: output = torch.masked_select(t, m) 返回的output就是满足掩码m为True的元素组成的一维张量。 需要注意的是,输入张量和掩码的形状必须是一致的,否则会引发错误。此外,如果掩码中的元素数量与输入张量的元素数量不匹配,也会引发错误。 torch.masked_select函数在很多情况下都很有用,比如在计算损失函数时,可以根据掩码选择特定的预测值和目标值。此外,它还可以用于过滤数据,只保留满足特定条件的元素。 总之,torch.masked_select是一个用于根据掩码从输入张量中选择元素的函数,返回的是由满足掩码为True的元素组成的一维张量。 ### 回答3: torch.masked_select是一个函数,用于提取符合指定mask条件的元素。它的输入是一个tensor和一个布尔类型的mask tensor,输出是一个一维的tensor,其中包含了满足mask条件的元素。 具体来说,假设输入的tensor是A,形状为(M,N),mask tensor是mask,形状也为(M,N)。对于A中的每个元素,如果对应位置上的mask值为True,则该位置的元素会被保留,否则被忽略。输出的tensor的形状取决于满足mask条件的元素个数,它的长度为满足条件的元素个数。 这个函数在实际应用中非常有用,例如在计算机视觉任务中,可以使用它来提取指定类别的目标物体的特征向量。另外,在自然语言处理中,可以利用它来提取包含特定关键词的文本。 总的来说,torch.masked_select函数提供了一种快速有效地提取满足条件元素的方法,可以在各种深度学习任务中发挥重要作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值