I am trying to calculate to mean and std for an array of torch tensors. My dataset has 720 training images and each of these images has 4 landmarks with X and Y representing a 2D point on the image.
to_tensor = transforms.ToTensor()
landmarks_arr = []
for i in range(len(train_dataset)):
landmarks_arr.append(to_tensor(train_dataset[i]['landmarks']))
mean = torch.mean(torch.stack(landmarks_arr, dim=0))#, dim=(0, 2, 3))
std = torch.std(torch.stack(landmarks_arr, dim=0)) #, dim=(0, 2, 3))
print(mean.shape)
print("mean is {} and std is {}".format(mean, std))
Result:
torch.Size([])
mean is nan and std is nan
There is a couple of problems above:
- Why to_tensor is not converting the values between 0 and 1?
- how to calculate mean correctly?
- Should I divide by 255 and where?
I have:
len(landmarks_arr)
720
and
landmarks_arr[0].shape
torch.Size([1, 4, 2])
and
landmarks_arr[0]
tensor([[[502.2869, 240.4949],
[688.0000, 293.0000],
[346.0000, 317.0000],
[560.8283, 322.6830]]], dtype=torch.float64)
