File size: 2,270 Bytes
15d6c34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from os.path import join as pjoin

import numpy as np
from motionx_explorer import (calc_mean_stddev_pose,
                              drop_shapes_from_motion_arr, get_seq_names)

if __name__ == "__main__":
    # read names from ./data/GRAB/train.txt
    with open(pjoin("./data/GRAB", "train.txt"), "r") as f:
        names = f.readlines()
    names = [name.strip() for name in names]
    print(f"names: {names}")
    all_arrays = []
    for name in names:
        # Load each NumPy array and add it to the list
        array = np.load(pjoin("./data/GRAB/joints", f"{name}.npy"))
        # drop shapes -> 212 dims
        array = drop_shapes_from_motion_arr(array)
        print(f"shape of {name}: {array.shape}")
        all_arrays.append(array)
    mean, stddev = calc_mean_stddev_pose(all_arrays)
    pose_dims = 212
    assert mean.shape[0] == pose_dims
    assert stddev.shape[0] == pose_dims
    # check if stddev has 0's
    stdev_zeros = np.where(stddev == 0)
    n_zeros = len(stdev_zeros[0])
    print(f"idx of stddev where 0: {stdev_zeros}")
    assert n_zeros == 0, "stddev has 0's, but it should not..."
    # save to ./data/GRAB/Mean.npy and ./data/GRAB/Std.npy
    mean_write_path = pjoin("./data/GRAB", "Mean.npy")
    stddev_write_path = pjoin("./data/GRAB", "Std.npy")
    with open(mean_write_path, "wb") as f:
        print(f"saving mean to {mean_write_path}")
        np.save(f, mean)
    with open(stddev_write_path, "wb") as f:
        print(f"saving stddev to {stddev_write_path}")
        np.save(f, stddev)
    
    
    # test calculate_mean_stddev
    # pose_dim = 3
    # arrays_1s = np.full((4, pose_dim), 3)
    # arrays_2s = np.full((2, pose_dim), 2)
    # single_mean = (4*3 + 2*2) / (4+2)
    # std_dev_single = np.sqrt((4*(3-single_mean)**2 + 2*(2-single_mean)**2) / (4+2))
    # exp_mean = np.full((pose_dim), single_mean)
    # exp_stddev = np.full((pose_dim), std_dev_single)
    # all_arrays = [arrays_1s, arrays_2s]
    # mean, stddev = calc_mean_stddev_pose(all_arrays)
    # print(f"mean: {mean}, exp mean: {exp_mean}")
    # print(f"stddev: {stddev}, exp stddev: {exp_stddev}")
    # assert mean.shape == (3,)
    # assert np.all(mean == exp_mean)
    # assert stddev.shape == (3,)
    # assert np.all(stddev == exp_stddev)