Hi all,
I am trying to understand the values that we pass to the transform.Normalize, for example the very seen ((0.5,0.5,0.5),(0.5,0.5,0.5)).
Is that the distribution we want our channels to follow? Or is that the mean and the variance we want to use to perform the normalization operation?
If the latter, after that step we should get values in the range[-1,1]. Is this for the CNN to perform better? If we want to visualize, however, one sample image on matplotlib, we need to perform the required transformation, right?
Is there a way I can get my values in the range [0,1]? Will that reduce the performance of my CNN?
This is the code I am using to plot a sample image, in case it helps someone.
# 0 - Pre-define tranformations
# -----------------------------
transform = transforms.Compose([
transforms.Resize(256),
transforms.RandomCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
"""Convert a color image to grayscale and normalize the color range to [0,1]."""
# 1 - Loading the dataset
# -----------------------
data_path = os.path.join(os.getcwd(), 'datasets')
#dataset = 'Fashion MNIST'
dataset = 'SVHN'
if dataset == 'Fashion MNIST':
root = os.path.join(data_path, 'FMNIST')
train_dataset = FMNIST(root = root, download = True, train = True, transform = transform)
test_dataset = FMNIST(root = root, download = False, train = False, transform = transform)
if dataset == 'SVHN':
root = os.path.join(data_path, 'SVNH')
train_dataset = SVHN(root = root, download = True, split = 'train', transform = transform)
test_dataset = SVHN(root = root, download = True, split = 'test', transform = transform)
train_loader = data.DataLoader(dataset = train_dataset,
batch_size = batch_size,
shuffle = True)
test_loader = data.DataLoader(dataset = train_dataset,
batch_size = batch_size,
shuffle = True)
# Grab a sample image
idx = random.randint(0,10)
tensor = train_dataset.__getitem__(idx)[0]
image = np.squeeze(tensor.numpy())
image = (image - np.min(image)) / (np.max(image) - np.min(image))
image = image.transpose((1, 2, 0))
plt.imshow(image)