我正在使用Fastai中的城市景观数据集进行语义分割。我想在计算准确性时忽略某些类。这是我根据Fastai深度学习课程定义准确性的方式:
name2id = {v:k for k,v in enumerate(classes)}
unlabeled = name2id['unlabeled']
ego_v = name2id['ego vehicle']
rectification = name2id['rectification border']
roi = name2id['out of roi']
static = name2id['static']
dynamic = name2id['dynamic']
ground = name2id['ground']
def acc_cityscapes(input, target):
target = target.squeeze(1)
mask=(target!=unlabeled and target!= ego_v and target!= rectification
and target!=roi and target !=static and target!=dynamic and
target!=ground)
return (input.argmax(dim=1)[mask]==target[mask]).float().mean()
如果我只忽略其中一个类,则此代码有效:
mask=target!=unlabeled
但是当我试图忽略这样的多个类时:
mask=(target!=unlabeled and target!= ego_v and target!= rectification
and target!=roi and target !=static and target!=dynamic and
target!=ground)
我收到此错误:
runtimeError: bool value of tensor with more than one value is ambiguous
任何想法我该如何解决?
答案 0 :(得分:2)
问题可能是因为您的张量包含1个以上的布尔值,这在执行逻辑运算(和或)时会导致错误。例如,
>>> import torch
>>> a = torch.zeros(2)
>>> b = torch.ones(2)
>>> a == b
tensor([False, False])
>>> a == 0
tensor([True, True])
>>> a == 0 and True
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: bool value of Tensor with more than one value is ambiguous
>>> if a == b:
... print (a)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: bool value of Tensor with more than one value is ambiguous
潜在的解决方案可能是直接使用逻辑运算符。
>>> (a != b) & (a == b)
tensor([False, False])
>>> mask = (a != b) & (a == b)
>>> c = torch.rand(2)
>>> c[mask]
tensor([])
希望有帮助。