Spaces:
Runtime error
Runtime error
Upload src/utils/normalize.py with huggingface_hub
Browse files- src/utils/normalize.py +27 -0
src/utils/normalize.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
def set_up_normalization(device='cpu', seq_len=16, scale=3, norm_path='../data/norm_scaled.npy'):
|
| 5 |
+
assert scale==3, 'Currently only support scale=3'
|
| 6 |
+
|
| 7 |
+
# return normalizeation function with given device
|
| 8 |
+
norm = np.load(norm_path, allow_pickle=True).item()[(seq_len, scale)]
|
| 9 |
+
min_val = torch.tensor(norm[0]).to(device, dtype=torch.float32)
|
| 10 |
+
max_val = torch.tensor(norm[1]).to(device, dtype=torch.float32)
|
| 11 |
+
|
| 12 |
+
# input: joints (..., 28*3) or (..., 28, 3)
|
| 13 |
+
def denormalize(data: torch.Tensor):
|
| 14 |
+
shape_orig = data.shape
|
| 15 |
+
data = data.reshape((-1, 3))
|
| 16 |
+
data = (data + 1.) * (max_val - min_val) / 2. + min_val
|
| 17 |
+
data = data.reshape(shape_orig)
|
| 18 |
+
return data
|
| 19 |
+
|
| 20 |
+
def normalize(data: torch.Tensor):
|
| 21 |
+
shape_orig = data.shape
|
| 22 |
+
data = data.reshape((-1, 3))
|
| 23 |
+
data = -1. + 2. * (data - min_val) / (max_val - min_val)
|
| 24 |
+
data = data.reshape(shape_orig)
|
| 25 |
+
return data
|
| 26 |
+
|
| 27 |
+
return normalize, denormalize
|