-
Notifications
You must be signed in to change notification settings - Fork 4
/
compute_stats.py
executable file
·54 lines (36 loc) · 1.49 KB
/
compute_stats.py
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
import logging
import hydra
from omegaconf import DictConfig, OmegaConf
from pathlib import Path
import sinc.launch.prepare # noqa
logger = logging.getLogger(__name__)
@hydra.main(config_path="configs", config_name="stats")
def _stats(cfg: DictConfig):
return stats(cfg)
def stats(cfg: DictConfig):
logger.info(f"Computing stats for this data: {cfg.data.dataname}")
logger.info("Loading data module")
data_module = hydra.utils.instantiate(cfg.data)
logger.info(f"Data module '{cfg.data.dataname}' loaded")
dataset = data_module.train_dataset
import torch
datastructs = [x["datastruct"] for x in dataset]
def compute_stats_feats(datastructs, key):
feats = torch.cat([datastruct[key] for datastruct in datastructs])
mean = feats.mean(0)
std = feats.std(0)
return mean, std
for transformation in ["joints2jfeats", "rots2rfeats"]:
if transformation not in cfg.transforms:
continue
savepath = Path(f'{cfg.transforms[transformation].path}')
savepath.mkdir(parents=True, exist_ok=True)
feat = transformation[transformation.find("2")+1:]
logger.info(f"Computing {feat} stats.")
logger.info(f"It will be saved there: {savepath}")
mean, std = compute_stats_feats(datastructs, feat)
torch.save(mean, savepath / f"{feat}_mean.pt")
torch.save(std, savepath / f"{feat}_std.pt")
logger.info("All stats saved.")
if __name__ == '__main__':
_stats()