[u-net体系结构中有一个著名的技巧,可以使用自定义权重图来提高准确性。下面是它的详细信息-
现在,通过在这里和其他多个地方询问,我了解了两种方法。我想知道哪种方法正确,或者还有其他正确的方法更正确吗?
1)首先是在训练循环中使用torch.nn.Functional
方法-
loss = torch.nn.functional.cross_entropy(output, target, w)
w将是计算得出的自定义重量。
2)其次是在训练循环之外的损失函数调用中使用reduction='none'
criterion = torch.nn.CrossEntropy(reduction='none')
然后在训练循环中乘以自定义权重-
gt # Ground truth, format torch.long
pd # Network output
W # per-element weighting based on the distance map from UNet
loss = criterion(pd, gt)
loss = W*loss # Ensure that weights are scaled appropriately
loss = torch.sum(loss.flatten(start_dim=1), axis=0) # Sums the loss per image
loss = torch.mean(loss) # Average across a batch
现在,我有点困惑,哪个是正确的,或者还有其他方法,或者两者都是正确的?