Mean calculation with integer valued tensors

I am puzzled, why would Pytorch force me to explicitly cast a uint8 3 channel image tensor to a floating point valued tensor before being able to calculate the statistics?

And what is the most efficient way of doing this then?

Right now I am using

        image.double().mean((1,2))

but this seems overly complicated…

Simply because it makes sense. If you have 2 integers, to compute the mean you have to sum them. They are gonna overflow for sure. If they are double you can express them with more flexibility and accuracy. The only thing you could do is to cast them to int32 or int64. However neural networks does not expect to work with integers and therefore cpu computations are neither supported for integers. You can probably cast them to a higher int type in numpy and then compute those metrics as integers if you code it. Because I’m pretty sure that numpy mean also cast to floating point since you only need to round the result to get the integer counterpart

1 Like