Image normalization and transformation

I’m trying to normalize the intensity of the images in the dataset that I have and crop the images to 32 by 32 pixels. The image dataset shape is (36, 2152, 2552, 3) and I want it to be something like (x,32,32)

You could use torchvision.transforms to crop and normalize the input data.
Could you describe a bit more where exactly you are stuck or which errors you are facing?

my data is not normalized between 0 and 1
here is my code

from PIL import Image
import os, sys
import cv2
import numpy as np

path = "C03/"
dirs = os.listdir( path )
dirs.sort()
x_test=[]

def load_dataset():
    for item in dirs:
        if os.path.isfile(path+item):
            im = Image.open(path+item).convert('RGB')
            im = np.array(im)
            x_test.append(im)
            
load_dataset() 
imgset=np.array(x_test)
np.save("imgds.npy",imgset)

import torchvision.transforms as T
preprocess = T.Compose([
   T.ToPILImage(),
   T.CenterCrop(32),
   T.ToTensor(),
   T.Normalize(
       mean=[0.485, 0.456, 0.406],
       std=[0.229, 0.224, 0.225]
   ),
   T.Grayscale(num_output_channels=1)
   
])
x=[]

for i in imgset: 
    i=np.array(preprocess(i))
    x.append(i)
x=np.array(x)

And I would love to, later on, apply this code to the data:

imgset_test = x.astype(np.float64)
imgset_test = imgset_test/ (2**14)

imgset_test = imgset_test.reshape(imgset_test.shape[0],1,32,32)
imgset_loader  = DataLoader(dataset=x,  batch_size=batch_size, shuffle=False).

and then predict the dataset labels by a model I already trained

I don’t know which range your data values are in, but T.Normalize using the current stats would expect an input with values in [0, 1], so you might need to normalize the raw data before applying the transformation.

yes I guess I forgot to add that since I switched notebooks :slight_smile: