代码之家  ›  专栏  ›  技术社区  ›  StuckInPhDNoMore

骰子损失和目标张量中不包含数据的样本?

  •  1
  • StuckInPhDNoMore  · 技术社区  · 7 年前

    我有一个高度不平衡的3D数据集,其中大约80%的体积是背景数据,我只对前景元素感兴趣,这些元素在随机位置约占总体积的20%。这些位置在给定给网络的标签张量中注明。目标张量是二进制的,其中0表示背景,1表示我们感兴趣或想要分割的区域。

    每个卷的大小为 [30,512,1024] 。我使用大小块迭代每个卷 [30,64,64] 。因此,我的大多数块在目标张量中只有0个值。

    我读到了 DiceLoss 非常适合此类问题,并成功用于分割3D MRI扫描。下面是一个简单的实现: https://github.com/pytorch/pytorch/issues/1249#issuecomment-305088398

    def dice_loss(input, target):
        smooth = 1.
    
        iflat = input.view(-1)
        tflat = target.view(-1)
        intersection = (iflat * tflat).sum()
    
        return 1 - ((2. * intersection + smooth) /
                  (iflat.sum() + tflat.sum() + smooth))
    

    这对我不起作用,我的意思是对于一个我只有背景的补丁 tflat.sum() 可能是 0 。这将使 intersection 0 同样,对于我的大多数补丁或块,我将得到 1 。

    是这样吗?这不是应该的工作方式。但我正在努力解决这个问题,因为这是我的网络输出:

    idx:  0 of  312 - Training Loss:  1.0 - Training Accuracy:  3.204042239857152e-11
    idx:  5 of  312 - Training Loss:  0.9876335859298706 - Training Accuracy:  0.0119545953348279
    idx:  10 of  312 - Training Loss:  1.0 - Training Accuracy:  7.269467666715101e-11
    idx:  15 of  312 - Training Loss:  0.7320756912231445 - Training Accuracy:  0.22638492286205292
    idx:  20 of  312 - Training Loss:  0.3599294424057007 - Training Accuracy:  0.49074622988700867
    idx:  25 of  312 - Training Loss:  1.0 - Training Accuracy:  1.0720428988975073e-09
    idx:  30 of  312 - Training Loss:  1.0 - Training Accuracy:  1.19782361807097e-09
    idx:  35 of  312 - Training Loss:  1.0 - Training Accuracy:  1.956790285362331e-09
    idx:  40 of  312 - Training Loss:  1.0 - Training Accuracy:  1.6055999862985004e-09
    idx:  45 of  312 - Training Loss:  1.0 - Training Accuracy:  7.580232552761856e-10
    idx:  50 of  312 - Training Loss:  1.0 - Training Accuracy:  9.510597864803572e-10
    idx:  55 of  312 - Training Loss:  1.0 - Training Accuracy:  1.341515676323013e-09
    idx:  60 of  312 - Training Loss:  0.7165247797966003 - Training Accuracy:  0.02658153884112835
    idx:  65 of  312 - Training Loss:  1.0 - Training Accuracy:  4.528208030762926e-09
    idx:  70 of  312 - Training Loss:  0.3205708861351013 - Training Accuracy:  0.6673439145088196
    idx:  75 of  312 - Training Loss:  0.9305377006530762 - Training Accuracy:  2.3437689378624782e-05
    idx:  80 of  312 - Training Loss:  1.0 - Training Accuracy:  5.305786885401176e-07
    idx:  85 of  312 - Training Loss:  1.0 - Training Accuracy:  4.0612556517771736e-07
    idx:  90 of  312 - Training Loss:  0.8207412362098694 - Training Accuracy:  0.0344742126762867
    idx:  95 of  312 - Training Loss:  0.7463213205337524 - Training Accuracy:  0.19459737837314606
    idx:  100 of  312 - Training Loss:  1.0 - Training Accuracy:  4.863646818620282e-09
    idx:  105 of  312 - Training Loss:  0.35790306329727173 - Training Accuracy:  0.608722984790802
    idx:  110 of  312 - Training Loss:  1.0 - Training Accuracy:  3.3852198821904267e-09
    idx:  115 of  312 - Training Loss:  1.0 - Training Accuracy:  1.5268487585373691e-09
    idx:  120 of  312 - Training Loss:  1.0 - Training Accuracy:  3.46353523639209e-09
    idx:  125 of  312 - Training Loss:  1.0 - Training Accuracy:  2.5878148582347826e-11
    idx:  130 of  312 - Training Loss:  1.0 - Training Accuracy:  2.3601216467272756e-11
    idx:  135 of  312 - Training Loss:  1.0 - Training Accuracy:  1.1504343033763575e-09
    idx:  140 of  312 - Training Loss:  0.4516671299934387 - Training Accuracy:  0.13879922032356262
    

    我认为网络没有从中学习到任何东西。。

    现在我很困惑,因为我的问题不应该太复杂,因为我确信MRI扫描也有目标张量,其中大多数张量表示背景。。我做错了什么?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jatentaki    7 年前

    如果算法预测所有背景体素的值都应该正好为0,则返回值为1,但如果它预测任何正值(如果使用sigmoid激活,肯定会这样),则仍然可以通过尽可能少的输出来改善损失。换句话说,分子不能超过 smooth 但该算法仍然可以学习保持分母尽可能小。

    如果您对算法的行为不满意,可以尝试增加批大小(这样就不会有任何卷的前景下降),或者直接跳过这些批。它可能有助于学习,也可能没有帮助。

    尽管如此,我个人从未成功地将骰子/借条作为损失函数学习分段,通常选择二进制交叉熵或类似损失,将前者作为验证指标。

    推荐文章