【问题标题】:Pytorch: '<=' not supported between instances of 'float' and 'function'Pytorch:'float'和'function'的实例之间不支持'<='
【发布时间】:2021-11-25 11:10:46
【问题描述】:

我正在尝试计算联合交叉 (IOU) 分数。这是我的代码实现,效果很好。

def IoU(predict: torch.Tensor, target: torch.Tensor):

    i = (predict & target).float().sum()
    u = (predict | target).float().sum()
    x = i/u
    IOU = x.item()

    return IoU

但是当我运行单元测试时:

def test_IoU1():
    pred = torch.tensor([[1, 0], [1, 0]])
    target = torch.tensor([[1, 0], [1, 1]])
    
    iou = IoU(pred,target)
    
    assert 0.66 <= iou
    assert iou <= 2/3

我明白了:

 TypeError: '<=' not supported between instances of 'float' and 'function'

如何在不更改单元测试的任何内容的情况下解决此问题?谢谢

【问题讨论】:

    标签: python image-processing deep-learning pytorch


    【解决方案1】:

    在这个函数中

    def IoU(predict: torch.Tensor, target: torch.Tensor):
    
        i = (predict & target).float().sum()
        u = (predict | target).float().sum()
        x = i/u
        IOU = x.item()
        
        return IoU
    

    您正在返回IoU,这是函数的名称,我想您需要返回IOU。所以正确的方法是-

    def IoU(predict: torch.Tensor, target: torch.Tensor):
    
        i = (predict & target).float().sum()
        u = (predict | target).float().sum()
        x = i/u
        IOU = x.item()
        
        return IOU
    

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2018-03-13
      • 2020-07-28
      • 2020-02-12
      • 2017-09-22
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多