Image normalization after loading dataset

I want to ask you how to normalize batch-images again.

After loading cifar10 dataset, I did custom transformation on image,
and I want to normalize image again before passing to the network.

I followed this code (Image normalization in PyTorch - Deep Learning - Deep Learning Course Forums)
and could get mean and std from each channel of image

and I want to normalize [128,3,32,32] transformed image again, and pass to the image
and I don’t know how to apply normalization
(do I have to use for-loop…? )

for batch_idx in range(128):
    for channel_idx in range(3):
           image = (image-mean[channel_idx])/std[channel_idx]

I wanted to use transforms.Normalize(mean, std) but
I don’t know how to change shape of mean and standard
mean,std shapes are [ , , ] [ , , ] for three channels
and if I pass

transforms.Normalize(mean,std)(data)

then I get error message,

valueError: Expected tensor to be a tensor image of size (C, H, W). Got tensor.size() = torch.Size([128, 3, 32, 32]).

in what shape I can pass Normalize function?
or do I have to use other iterative method?

thank you for reading my question.

1 Like

I think you should pass a single image for the normalize transform. Usually the transform is called inside the ‘getitem’ function of the dataset class. You are I think passing a whole batch of 128 images.

1 Like

thank you for answering to my question!
I followed your suggestion and
Normalize function works
as I passed each [3,32,32] image (not batch together [128,3,32,32])

for I in range(data.shape[0]):
    data[i]  = transforms.Normalize(mean,std)(data[i])

Please define a customed dataset following the official tutoral.
Inside the CustomedDataset class

def __getitem__(self, idx):

define:

 compose = transforms.Compose([transforms.ToPILImage(), 
                                         transforms.Resize((128,128),interpolation=Image.NEAREST),
                                         transforms.ToTensor(),
                                         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

then use:

img_tensor = compose(img_tensor)

Thank you for answering to my question!
I am sorry but I didn’t get it.
you mean I have to create another customDataset
after loading cifar10 dataset?

I already loaded cifar10 dataset and did my-custom transformation on cifar10 tensor images
and want to normalize transformed-cifar10-tensors again before passing to the network.

why you want to normalize twice ?

because I hope <image transformation & the network> performs better.
after loading cifar10 dataset, I performed some transformations on the image.
Then, mean,stds are not same as before.
So, I wanted to make images normalized again before passing to the network.
and in this case, what you suggested does not work? right?

ok, I misunderstood.
In this case, just try to get image one by one and do the normalization.

In your error, it said you get the batched data (128 batch size) instead of C,H,W which is one data.

Thank you for your answer!

You can vectorize this for faster processing using

mean = torch.tensor(mean)[None, :, None, None] # assuming mean is 3 values for 3 channels, 
# convert it into shape (1, 3, 1, 1) for broadcasting, similarly
std = torch.tensor(std)[None, :, None, None]
# and then you can do normalization using
data = (data - mean) / (std + 1e-11)
1 Like

I was finding the fast way to normalize images rather than iterative method, and you gave the right solution!
thank you so much :smile: