Hi,
I have a code that calculates mean and std
of my image dataset through dataloader:
tfms = transforms.Compose([
transforms.Resize((128, 128)),
transforms.ToTensor(),
transforms.Normalize(mean = (0, 0, 0), std = (1, 1, 1))])
train_files = glob.glob(train_dir + '/*.jpg')
train_ds = MyDataset(train_files , transform = tfms)
train_dl = torch.utils.data.DataLoader(train_ds,
batch_size=16,
shuffle=True,)
####### COMPUTE MEAN / STD
# placeholders
psum = torch.tensor([0.0, 0.0, 0.0]).to(device)
psum_sq = torch.tensor([0.0, 0.0, 0.0]).to(device)
# loop through images
for inputs, _ in tqdm(train_dl):
inputs = inputs.to(device)
psum += inputs.sum(axis = [0, 2, 3])
psum_sq += (inputs ** 2).sum(axis = [0, 2, 3])
# ####### FINAL CALCULATIONS
# pixel count
print(len(train_ds))
count = len(train_ds) * resize * resize
# mean and std
total_mean = psum / count
total_var = (psum_sq / count) - (total_mean ** 2)
total_std = torch.sqrt(total_var)
# output
print('mean: ' + str(total_mean))
print('std: ' + str(total_std))
I want to add transforms.Lambda(noise_extract)
to transforms.Compose
that extract noise residual and converts images from 3 channels to 1 channel:
tfms = transforms.Compose([
transforms.Resize((128, 128)),
transforms.Lambda(noise_extract),
transforms.ToTensor(),
transforms.Normalize(mean = (0, 0, 0), std = (1, 1, 1))])
I want to know how I must change the above code for calculating mean & std
for the new one channel
output of the second tfms
?