Normalizing the keypoints/landmarks on an image to find its mean/std

Mona_Jalal: <p>I am trying to normalize my targets (here landmark values) but to_tensor is not forcing the values to be between 0 and 1. How should that be enforced?</p>
<p>Also, am I looking for only one value for mean and one value for std, right? Each image has 4 landmarks and each landmark has x and y coordinates.</p>
<pre><code class="lang-auto">to_tensor = transforms.ToTensor()

landmarks_arr = []

for i in range(len(train_dataset)):
    landmarks = to_tensor(train_dataset[i]['landmarks'])
    landmarks[landmarks != landmarks] = 0  # this will set all nan to zero
    landmarks_arr.append(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))
</code></pre>
<p>result is:</p>
<pre><code class="lang-auto">torch.Size([])
mean is 367.7143527453419 and std is 160.8974583202625
</code></pre>
<p>This is how landmarks are:</p>
<pre><code class="lang-auto">landmarks_arr[0]

tensor([[[502.2869, 240.4949],
         [688.0000, 293.0000],
         [346.0000, 317.0000],
         [560.8283, 322.6830]]], dtype=torch.float64)
</code></pre>
<p>On a side note, is this line even correct?</p>
<pre><code class="lang-auto">mean = torch.mean(torch.stack(landmarks_arr, dim=0))#, dim=(0, 2, 3))
</code></pre>

Mona_Jalal: <p>Please let me know if this answer makes sense</p>
<pre><code class="lang-auto">landmarks_arr = []


for i in range(len(train_dataset)):
    landmarks = to_tensor(train_dataset[i]['landmarks'])
    landmarks[landmarks != landmarks] = 0  # this will set all nan to zero
    landmarks_arr.append(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))

max_val = 0.
tmp_max = 0.
for i in range(len(landmarks_arr)):
    tmp_max = torch.max(landmarks_arr[i])
    if max_val &lt;= tmp_max:
        max_val = tmp_max
        
print('max val is: ', max_val)

print(mean.shape)
print("mean is {} and std is {}".format(mean/max_val, std/max_val))
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/a/6ac4364f5db2f1e0123181eb05436049155cbe6f.png" data-download-href="https://discuss.pytorch.org/uploads/default/6ac4364f5db2f1e0123181eb05436049155cbe6f" title="Screenshot_2020-10-15_20-46-15"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/a/6ac4364f5db2f1e0123181eb05436049155cbe6f_2_690x442.png" alt="Screenshot_2020-10-15_20-46-15" data-base62-sha1="feuZEGXma1FRFSml72svNjluYjB" width="690" height="442" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/a/6ac4364f5db2f1e0123181eb05436049155cbe6f_2_690x442.png, https://discuss.pytorch.org/uploads/default/optimized/3X/6/a/6ac4364f5db2f1e0123181eb05436049155cbe6f_2_1035x663.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/6/a/6ac4364f5db2f1e0123181eb05436049155cbe6f_2_1380x884.png 2x" data-dominant-color="EFF0F0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot_2020-10-15_20-46-15</span><span class="informations">1782×1144 186 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>