不同的批次尺寸给出不同的测试分数(Pytorch)

发布于 2025-02-10 01:44:30 字数 193 浏览 3 评论 0原文

我正在尝试使用不同的批次大小来测试我的模型,并且我的批次大小不同。 我是初学者。我已经尝试解决这个问题很长时间了,但是找不到任何有效的解决方案。 代码在这里。谢谢!

I am trying to test my model with different batch sizes and I am getting different dice for different batch sizes.
I am a beginner. I have tried to fix this problem for a long time, but I can’t find any effective solution.
The code is here. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小兔几 2025-02-17 01:44:30

它与您对平均指标的计算有关,无论它们是图像的(案例批量尺寸= 1)平均值还是在Minibatch平均值。无论哪种方式,无论使用什么批次尺寸,您都必须保持平均值。一种直接的解决方案是修改您的添加评分方法:

class Scoring():
    def add(self,pred,target):
        self.number += pred.shape[0]
        for pre, tar in zip(pred.unsqueeze(1), target.unsqueeze(1)):
            dice = torch.tensor(get_Dice(pre,tar))
            self.dice += dice
            self.iou += torch.tensor(get_Iou(pre,tar))
            self.spe += torch.tensor(get_Spe(pre,tar))
            self.sen += torch.tensor(get_Sen(pre,tar))
            self.acc += torch.tensor(get_Acc(pre,tar))
            self.pre += torch.tensor(get_Pre(pre,tar))
        return dice

It has something to do with your calculation of average metrics, whether they are image-wise (case batch size = 1) averages or minibatch-wise averages. Either way, you have to keep the averaging consistent no matter what batch size you use. One direct solution is to modify your add method of Scoring:

class Scoring():
    def add(self,pred,target):
        self.number += pred.shape[0]
        for pre, tar in zip(pred.unsqueeze(1), target.unsqueeze(1)):
            dice = torch.tensor(get_Dice(pre,tar))
            self.dice += dice
            self.iou += torch.tensor(get_Iou(pre,tar))
            self.spe += torch.tensor(get_Spe(pre,tar))
            self.sen += torch.tensor(get_Sen(pre,tar))
            self.acc += torch.tensor(get_Acc(pre,tar))
            self.pre += torch.tensor(get_Pre(pre,tar))
        return dice
溺孤伤于心 2025-02-17 01:44:30

结果根据批处理大小而改变?

这通常与批处理层(例如批处理标准化层)有关。测试代码时,请务必检查您处于评估模式。在您的情况下,net.eval()应解决问题。

Results changing depending on the batch size?

This is typically related to batch-wise layers such as batch normalization layers. Always check you are in eval mode when testing your code. In your case, net.eval() should fix the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文