Numpy to tensor changes values dramatically?

Hi everyone,

I’m struggling with the following issue, and can’t find a possible explanation (I’m quite the pytorch amateur, so please forgive me for not finding possible obvious solutions).
I’m feeding MR images to a 3D Unet, and reshape the already normalized numpy array image like this:

imagefinal=(image.reshape(1,1, image.shape[0], image.shape[1], image.shape[2]))

after which I convert it to a tensor using

tensor_x = torch.from_numpy(imagefinal).float()

However, using




tensor(2652589.7500, device=‘cuda:0’)
tensor(4033200., device=‘cuda:0’)
tensor(3447660.5000, device=‘cuda:0’)
tensor(3224289., device=‘cuda:0’)

Do you have any idea why the maximum value of the array would change to very large values after converting it to a tensor? I’ve tried forcing the tensor to be of different datatypes with no success. Is there something obvious I’m missing here?

Thanks in advance for your help!

Consider this toy example:

np_array = np.random.randn(2,3,4)
print(np_array.shape, np_array.dtype, np_array.max())
>>((2, 3, 4), dtype('float64'), 2.2915268560525193)
tensor = torch.from_numpy(np_array)
print(tensor.shape, tensor.dtype, tensor.max())
>>torch.Size([2, 3, 4]) torch.float64 tensor(2.3412, dtype=torch.float64)

Everything is as expected. Notice, that default numpy float type is float64, torch is float32. So, you may convert float64 to float32 and have possible overflow (which I am a bit doubt). I would also suggest to carefully review if you may change imagefinal somewhere in the code before converting to tensor.

Hi Alexey,

Thank you very much for your reply. After some additional digging, I found the problem. I’m masking my MR image arrays with the function, returning a MaskedArray datatype. I wasn’t able to find an explanation for this online, but torch.from_numpy doesn’t seem able to directly copy values from MaskedArray types. After first converting the MaskedArray to a normal numpy array using x=np.asarray(MaskedArray), the torch.from_numpy function works as intended.



Good to hear, you figure it out.