pytorchdicebatchsize

Different batch sizes give different test score (pytorch)


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!


Solution

  • 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