How to perform normalization in pytorch tensor?

I do not load images directly, as most tutorials show. I load numpy arrays from an hdf5 file that are indeed images itself.

Since I was using Keras, the dimensions order of my numpy arrays are (B,W,H,C). I switched the dimensions W and C, since this is the order PyTorch uses, right (B, C, H, W)?

X_train = torch.from_numpy(np.array(np.rollaxis(X_train, 3, 1), dtype=np.dtype(“d”)))

This is the code I’m using to obtain the PyTorch tensor. I’m totally new to this library and have no idea on how to normalize this PyTorch tensor, whereas all tutorials use the normalize together with other things that are not suitable to my problem.


Edi: Trying to run this code, even without normalization, I faced an error “RuntimeError: Expected object of scalar type Double but got scalar type Float” in the line :

outputs = model(inputs)

of the function train_model() in the tutorial

The simplest way would be to just write out the images to folder and use

Also you got this error because your model is defined in 32 bit floating point but your data is probably in 64 bit floating point

First, thanks for the answer! Second: yes, the error was exactly that, now it’s working with np.dtype(“Float32”). Unfortunately, I cannot use this ImageFolder, I have access only to the hdf5 file with the images in numpy arrays

You can write a simple script to read hdf5 file and write it as files. And then use the Imagefolder.

But also you can call transforms on any torch tensor, their ops are just defined in call.
so for ex -
x = torchvision.transforms.Normalize(mean= 0, std= 0.1)
x( your tensor)

I have tried the following :

normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
X_train = torch.from_numpy(np.array(np.rollaxis(X_train, 3, 1), dtype=np.dtype(“Float32”)))

And it gives me the following error : TypeError: tensor is not a torch image.

The input tensor to this function has to be (C,H,W) in format

1 Like

Thanks! My bad, I did not notice that the input tensor needed to be in (C, H, W) format. Now it’s working, I’m iterating over my X_train tensor to use the normalize function now.