Zaesar vumichien commited on
Commit
c5f8b57
0 Parent(s):

Duplicate from vumichien/generate_human_motion

Browse files

Co-authored-by: vumichien <vumichien@users.noreply.huggingface.co>

This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. VQ-Trans/.gitignore +70 -0
  4. VQ-Trans/GPT_eval_multi.py +121 -0
  5. VQ-Trans/README.md +400 -0
  6. VQ-Trans/VQ_eval.py +95 -0
  7. VQ-Trans/ViT-B-32.pt +3 -0
  8. VQ-Trans/body_models/smpl/J_regressor_extra.npy +3 -0
  9. VQ-Trans/body_models/smpl/SMPL_NEUTRAL.pkl +3 -0
  10. VQ-Trans/body_models/smpl/kintree_table.pkl +3 -0
  11. VQ-Trans/body_models/smpl/smplfaces.npy +3 -0
  12. VQ-Trans/checkpoints/kit.zip +3 -0
  13. VQ-Trans/checkpoints/t2m.zip +3 -0
  14. VQ-Trans/checkpoints/train_vq.py +171 -0
  15. VQ-Trans/dataset/dataset_TM_eval.py +217 -0
  16. VQ-Trans/dataset/dataset_TM_train.py +161 -0
  17. VQ-Trans/dataset/dataset_VQ.py +109 -0
  18. VQ-Trans/dataset/dataset_tokenize.py +117 -0
  19. VQ-Trans/dataset/prepare/download_extractor.sh +15 -0
  20. VQ-Trans/dataset/prepare/download_glove.sh +9 -0
  21. VQ-Trans/dataset/prepare/download_model.sh +12 -0
  22. VQ-Trans/dataset/prepare/download_smpl.sh +13 -0
  23. VQ-Trans/environment.yml +121 -0
  24. VQ-Trans/models/encdec.py +67 -0
  25. VQ-Trans/models/evaluator_wrapper.py +92 -0
  26. VQ-Trans/models/modules.py +109 -0
  27. VQ-Trans/models/pos_encoding.py +43 -0
  28. VQ-Trans/models/quantize_cnn.py +415 -0
  29. VQ-Trans/models/resnet.py +82 -0
  30. VQ-Trans/models/rotation2xyz.py +92 -0
  31. VQ-Trans/models/smpl.py +97 -0
  32. VQ-Trans/models/t2m_trans.py +211 -0
  33. VQ-Trans/models/vqvae.py +118 -0
  34. VQ-Trans/options/get_eval_option.py +83 -0
  35. VQ-Trans/options/option_transformer.py +68 -0
  36. VQ-Trans/options/option_vq.py +61 -0
  37. VQ-Trans/output/23cb7d0e26bb1646b3d386331971449c_pred.pt +3 -0
  38. VQ-Trans/output/90dd3007b93da07eca7527c836b4d6d0_pred.pt +3 -0
  39. VQ-Trans/output/c3785325ba8f17ce7427b43d49903e51_pred.pt +3 -0
  40. VQ-Trans/pyrender +1 -0
  41. VQ-Trans/render_final.py +194 -0
  42. VQ-Trans/train_t2m_trans.py +191 -0
  43. VQ-Trans/train_vq.py +171 -0
  44. VQ-Trans/utils/config.py +17 -0
  45. VQ-Trans/utils/eval_trans.py +580 -0
  46. VQ-Trans/utils/losses.py +30 -0
  47. VQ-Trans/utils/motion_process.py +59 -0
  48. VQ-Trans/utils/paramUtil.py +63 -0
  49. VQ-Trans/utils/quaternion.py +423 -0
  50. VQ-Trans/utils/rotation_conversions.py +532 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Generate Human Motion
3
+ emoji: 🏃
4
+ colorFrom: green
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 3.16.2
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: vumichien/generate_human_motion
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
VQ-Trans/.gitignore ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # C extensions
2
+ *.so
3
+
4
+
5
+ # Translations
6
+ *.mo
7
+ *.pot
8
+
9
+ # Django stuff:
10
+ *.log
11
+ local_settings.py
12
+ db.sqlite3
13
+ db.sqlite3-journal
14
+
15
+ # Flask stuff:
16
+ instance/
17
+ .webassets-cache
18
+
19
+ # Scrapy stuff:
20
+ .scrapy
21
+
22
+ # Sphinx documentation
23
+ docs/_build/
24
+
25
+ # PyBuilder
26
+ target/
27
+
28
+ # Jupyter Notebook
29
+ .ipynb_checkpoints
30
+
31
+ # IPython
32
+ profile_default/
33
+ ipython_config.py
34
+
35
+
36
+ # Celery stuff
37
+ celerybeat-schedule
38
+ celerybeat.pid
39
+
40
+ # SageMath parsed files
41
+ *.sage.py
42
+
43
+ # Environments
44
+ .env
45
+ .venv
46
+ env/
47
+ venv/
48
+ ENV/
49
+ env.bak/
50
+ venv.bak/
51
+
52
+ # Spyder project settings
53
+ .spyderproject
54
+ .spyproject
55
+
56
+ # Rope project settings
57
+ .ropeproject
58
+
59
+ # mkdocs documentation
60
+ /site
61
+
62
+ # mypy
63
+ .mypy_cache/
64
+ .dmypy.json
65
+ dmypy.json
66
+
67
+ # Pyre type checker
68
+ .pyre/
69
+
70
+ .vscode
VQ-Trans/GPT_eval_multi.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import numpy as np
4
+ from torch.utils.tensorboard import SummaryWriter
5
+ import json
6
+ import clip
7
+
8
+ import options.option_transformer as option_trans
9
+ import models.vqvae as vqvae
10
+ import utils.utils_model as utils_model
11
+ import utils.eval_trans as eval_trans
12
+ from dataset import dataset_TM_eval
13
+ import models.t2m_trans as trans
14
+ from options.get_eval_option import get_opt
15
+ from models.evaluator_wrapper import EvaluatorModelWrapper
16
+ import warnings
17
+ warnings.filterwarnings('ignore')
18
+
19
+ ##### ---- Exp dirs ---- #####
20
+ args = option_trans.get_args_parser()
21
+ torch.manual_seed(args.seed)
22
+
23
+ args.out_dir = os.path.join(args.out_dir, f'{args.exp_name}')
24
+ os.makedirs(args.out_dir, exist_ok = True)
25
+
26
+ ##### ---- Logger ---- #####
27
+ logger = utils_model.get_logger(args.out_dir)
28
+ writer = SummaryWriter(args.out_dir)
29
+ logger.info(json.dumps(vars(args), indent=4, sort_keys=True))
30
+
31
+ from utils.word_vectorizer import WordVectorizer
32
+ w_vectorizer = WordVectorizer('./glove', 'our_vab')
33
+ val_loader = dataset_TM_eval.DATALoader(args.dataname, True, 32, w_vectorizer)
34
+
35
+ dataset_opt_path = 'checkpoints/kit/Comp_v6_KLD005/opt.txt' if args.dataname == 'kit' else 'checkpoints/t2m/Comp_v6_KLD005/opt.txt'
36
+
37
+ wrapper_opt = get_opt(dataset_opt_path, torch.device('cuda'))
38
+ eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
39
+
40
+ ##### ---- Network ---- #####
41
+
42
+ ## load clip model and datasets
43
+ clip_model, clip_preprocess = clip.load("ViT-B/32", device=torch.device('cuda'), jit=False, download_root='/apdcephfs_cq2/share_1290939/maelyszhang/.cache/clip') # Must set jit=False for training
44
+ clip.model.convert_weights(clip_model) # Actually this line is unnecessary since clip by default already on float16
45
+ clip_model.eval()
46
+ for p in clip_model.parameters():
47
+ p.requires_grad = False
48
+
49
+ net = vqvae.HumanVQVAE(args, ## use args to define different parameters in different quantizers
50
+ args.nb_code,
51
+ args.code_dim,
52
+ args.output_emb_width,
53
+ args.down_t,
54
+ args.stride_t,
55
+ args.width,
56
+ args.depth,
57
+ args.dilation_growth_rate)
58
+
59
+
60
+ trans_encoder = trans.Text2Motion_Transformer(num_vq=args.nb_code,
61
+ embed_dim=args.embed_dim_gpt,
62
+ clip_dim=args.clip_dim,
63
+ block_size=args.block_size,
64
+ num_layers=args.num_layers,
65
+ n_head=args.n_head_gpt,
66
+ drop_out_rate=args.drop_out_rate,
67
+ fc_rate=args.ff_rate)
68
+
69
+
70
+ print ('loading checkpoint from {}'.format(args.resume_pth))
71
+ ckpt = torch.load(args.resume_pth, map_location='cpu')
72
+ net.load_state_dict(ckpt['net'], strict=True)
73
+ net.eval()
74
+ net.cuda()
75
+
76
+ if args.resume_trans is not None:
77
+ print ('loading transformer checkpoint from {}'.format(args.resume_trans))
78
+ ckpt = torch.load(args.resume_trans, map_location='cpu')
79
+ trans_encoder.load_state_dict(ckpt['trans'], strict=True)
80
+ trans_encoder.train()
81
+ trans_encoder.cuda()
82
+
83
+
84
+ fid = []
85
+ div = []
86
+ top1 = []
87
+ top2 = []
88
+ top3 = []
89
+ matching = []
90
+ multi = []
91
+ repeat_time = 20
92
+
93
+
94
+ for i in range(repeat_time):
95
+ best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, best_multi, writer, logger = eval_trans.evaluation_transformer_test(args.out_dir, val_loader, net, trans_encoder, logger, writer, 0, best_fid=1000, best_iter=0, best_div=100, best_top1=0, best_top2=0, best_top3=0, best_matching=100, best_multi=0, clip_model=clip_model, eval_wrapper=eval_wrapper, draw=False, savegif=False, save=False, savenpy=(i==0))
96
+ fid.append(best_fid)
97
+ div.append(best_div)
98
+ top1.append(best_top1)
99
+ top2.append(best_top2)
100
+ top3.append(best_top3)
101
+ matching.append(best_matching)
102
+ multi.append(best_multi)
103
+
104
+ print('final result:')
105
+ print('fid: ', sum(fid)/repeat_time)
106
+ print('div: ', sum(div)/repeat_time)
107
+ print('top1: ', sum(top1)/repeat_time)
108
+ print('top2: ', sum(top2)/repeat_time)
109
+ print('top3: ', sum(top3)/repeat_time)
110
+ print('matching: ', sum(matching)/repeat_time)
111
+ print('multi: ', sum(multi)/repeat_time)
112
+
113
+ fid = np.array(fid)
114
+ div = np.array(div)
115
+ top1 = np.array(top1)
116
+ top2 = np.array(top2)
117
+ top3 = np.array(top3)
118
+ matching = np.array(matching)
119
+ multi = np.array(multi)
120
+ msg_final = f"FID. {np.mean(fid):.3f}, conf. {np.std(fid)*1.96/np.sqrt(repeat_time):.3f}, Diversity. {np.mean(div):.3f}, conf. {np.std(div)*1.96/np.sqrt(repeat_time):.3f}, TOP1. {np.mean(top1):.3f}, conf. {np.std(top1)*1.96/np.sqrt(repeat_time):.3f}, TOP2. {np.mean(top2):.3f}, conf. {np.std(top2)*1.96/np.sqrt(repeat_time):.3f}, TOP3. {np.mean(top3):.3f}, conf. {np.std(top3)*1.96/np.sqrt(repeat_time):.3f}, Matching. {np.mean(matching):.3f}, conf. {np.std(matching)*1.96/np.sqrt(repeat_time):.3f}, Multi. {np.mean(multi):.3f}, conf. {np.std(multi)*1.96/np.sqrt(repeat_time):.3f}"
121
+ logger.info(msg_final)
VQ-Trans/README.md ADDED
@@ -0,0 +1,400 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Motion VQ-Trans
2
+ Pytorch implementation of paper "Generating Human Motion from Textual Descriptions with High Quality Discrete Representation"
3
+
4
+
5
+ [[Notebook Demo]](https://colab.research.google.com/drive/1tAHlmcpKcjg_zZrqKku7AfpqdVAIFrF8?usp=sharing)
6
+
7
+
8
+ ![teaser](img/Teaser.png)
9
+
10
+ If our project is helpful for your research, please consider citing : (todo)
11
+ ```
12
+ @inproceedings{shen2020ransac,
13
+ title={RANSAC-Flow: generic two-stage image alignment},
14
+ author={Shen, Xi and Darmon, Fran{\c{c}}ois and Efros, Alexei A and Aubry, Mathieu},
15
+ booktitle={16th European Conference on Computer Vision}
16
+ year={2020}
17
+ }
18
+ ```
19
+
20
+
21
+ ## Table of Content
22
+ * [1. Visual Results](#1-visual-results)
23
+ * [2. Installation](#2-installation)
24
+ * [3. Quick Start](#3-quick-start)
25
+ * [4. Train](#4-train)
26
+ * [5. Evaluation](#5-evaluation)
27
+ * [6. Motion Render](#6-motion-render)
28
+ * [7. Acknowledgement](#7-acknowledgement)
29
+ * [8. ChangLog](#8-changlog)
30
+
31
+
32
+
33
+
34
+ ## 1. Visual Results (More results can be found in our project page (todo))
35
+
36
+ ![visualization](img/ALLvis.png)
37
+
38
+
39
+ ## 2. Installation
40
+
41
+ ### 2.1. Environment
42
+
43
+ <!-- Our model can be learnt in a **single GPU GeForce GTX 1080Ti** (12G).
44
+
45
+ Install Pytorch adapted to your CUDA version :
46
+
47
+ * [Pytorch 1.2.0](https://pytorch.org/get-started/previous-versions/#linux-and-windows-1)
48
+ * [Torchvision 0.4.0](https://pytorch.org/get-started/previous-versions/#linux-and-windows-1)
49
+
50
+ Other dependencies (tqdm, visdom, pandas, kornia, opencv-python) :
51
+ ``` Bash
52
+ bash requirement.sh
53
+ ``` -->
54
+
55
+ Our model can be learnt in a **single GPU V100-32G**
56
+
57
+ ```bash
58
+ conda env create -f environment.yml
59
+ conda activate VQTrans
60
+ ```
61
+
62
+ The code was tested on Python 3.8 and PyTorch 1.8.1.
63
+
64
+
65
+ ### 2.2. Dependencies
66
+
67
+ ```bash
68
+ bash dataset/prepare/download_glove.sh
69
+ ```
70
+
71
+
72
+ ### 2.3. Datasets
73
+
74
+
75
+ We are using two 3D human motion-language dataset: HumanML3D and KIT-ML. For both datasets, you could find the details as well as download link [[here]](https://github.com/EricGuo5513/HumanML3D).
76
+
77
+ Take HumanML3D for an example, the file directory should look like this:
78
+ ```
79
+ ./dataset/HumanML3D/
80
+ ├── new_joint_vecs/
81
+ ├── texts/
82
+ ├── Mean.npy # same as in [HumanML3D](https://github.com/EricGuo5513/HumanML3D)
83
+ ├── Std.npy # same as in [HumanML3D](https://github.com/EricGuo5513/HumanML3D)
84
+ ├── train.txt
85
+ ├── val.txt
86
+ ├── test.txt
87
+ ├── train_val.txt
88
+ └──all.txt
89
+ ```
90
+
91
+
92
+ ### 2.4. Motion & text feature extractors:
93
+
94
+ We use the same extractors provided by [t2m](https://github.com/EricGuo5513/text-to-motion) to evaluate our generated motions. Please download the extractors.
95
+
96
+ ```bash
97
+ bash dataset/prepare/download_extractor.sh
98
+ ```
99
+
100
+ ### 2.5. Pre-trained models
101
+
102
+ The pretrained model files will be stored in the 'pretrained' folder:
103
+ ```bash
104
+ bash dataset/prepare/download_model.sh
105
+ ```
106
+
107
+ <!-- Quick download :
108
+
109
+ ``` Bash
110
+ cd model/pretrained
111
+ bash download_model.sh
112
+ ```
113
+
114
+ For more details of the pre-trained models, see [here](https://github.com/XiSHEN0220/RANSAC-Flow/blob/master/model/pretrained) -->
115
+
116
+ ### 2.6. Render motion (optional)
117
+
118
+ If you want to render the generated motion, you need to install:
119
+
120
+ ```bash
121
+ sudo sh dataset/prepare/download_smpl.sh
122
+ conda install -c menpo osmesa
123
+ conda install h5py
124
+ conda install -c conda-forge shapely pyrender trimesh mapbox_earcut
125
+ ```
126
+
127
+
128
+
129
+ ## 3. Quick Start
130
+
131
+ A quick start guide of how to use our code is available in [demo.ipynb](https://colab.research.google.com/drive/1tAHlmcpKcjg_zZrqKku7AfpqdVAIFrF8?usp=sharing)
132
+
133
+ <p align="center">
134
+ <img src="img/demo.png" width="400px" alt="demo">
135
+ </p>
136
+
137
+
138
+ ## 4. Train
139
+
140
+ Note that, for kit dataset, just need to set '--dataname kit'.
141
+
142
+ ### 4.1. VQ-VAE
143
+
144
+ The results are saved in the folder output_vqfinal.
145
+
146
+ <details>
147
+ <summary>
148
+ VQ training
149
+ </summary>
150
+
151
+ ```bash
152
+ python3 train_vq.py \
153
+ --batch-size 256 \
154
+ --lr 2e-4 \
155
+ --total-iter 300000 \
156
+ --lr-scheduler 200000 \
157
+ --nb-code 512 \
158
+ --down-t 2 \
159
+ --depth 3 \
160
+ --dilation-growth-rate 3 \
161
+ --out-dir output \
162
+ --dataname t2m \
163
+ --vq-act relu \
164
+ --quantizer ema_reset \
165
+ --loss-vel 0.5 \
166
+ --recons-loss l1_smooth \
167
+ --exp-name VQVAE
168
+ ```
169
+
170
+ </details>
171
+
172
+ ### 4.2. Motion-Transformer
173
+
174
+ The results are saved in the folder output_transformer.
175
+
176
+ <details>
177
+ <summary>
178
+ MoTrans training
179
+ </summary>
180
+
181
+ ```bash
182
+ python3 train_t2m_trans.py \
183
+ --exp-name VQTransformer \
184
+ --batch-size 128 \
185
+ --num-layers 9 \
186
+ --embed-dim-gpt 1024 \
187
+ --nb-code 512 \
188
+ --n-head-gpt 16 \
189
+ --block-size 51 \
190
+ --ff-rate 4 \
191
+ --drop-out-rate 0.1 \
192
+ --resume-pth output/VQVAE/net_last.pth \
193
+ --vq-name VQVAE \
194
+ --out-dir output \
195
+ --total-iter 300000 \
196
+ --lr-scheduler 150000 \
197
+ --lr 0.0001 \
198
+ --dataname t2m \
199
+ --down-t 2 \
200
+ --depth 3 \
201
+ --quantizer ema_reset \
202
+ --eval-iter 10000 \
203
+ --pkeep 0.5 \
204
+ --dilation-growth-rate 3 \
205
+ --vq-act relu
206
+ ```
207
+
208
+ </details>
209
+
210
+ ## 5. Evaluation
211
+
212
+ ### 5.1. VQ-VAE
213
+ <details>
214
+ <summary>
215
+ VQ eval
216
+ </summary>
217
+
218
+ ```bash
219
+ python3 VQ_eval.py \
220
+ --batch-size 256 \
221
+ --lr 2e-4 \
222
+ --total-iter 300000 \
223
+ --lr-scheduler 200000 \
224
+ --nb-code 512 \
225
+ --down-t 2 \
226
+ --depth 3 \
227
+ --dilation-growth-rate 3 \
228
+ --out-dir output \
229
+ --dataname t2m \
230
+ --vq-act relu \
231
+ --quantizer ema_reset \
232
+ --loss-vel 0.5 \
233
+ --recons-loss l1_smooth \
234
+ --exp-name TEST_VQVAE \
235
+ --resume-pth output/VQVAE/net_last.pth
236
+ ```
237
+
238
+ </details>
239
+
240
+ ### 5.2. Motion-Transformer
241
+
242
+ <details>
243
+ <summary>
244
+ MoTrans eval
245
+ </summary>
246
+
247
+ ```bash
248
+ python3 GPT_eval_multi.py \
249
+ --exp-name TEST_VQTransformer \
250
+ --batch-size 128 \
251
+ --num-layers 9 \
252
+ --embed-dim-gpt 1024 \
253
+ --nb-code 512 \
254
+ --n-head-gpt 16 \
255
+ --block-size 51 \
256
+ --ff-rate 4 \
257
+ --drop-out-rate 0.1 \
258
+ --resume-pth output/VQVAE/net_last.pth \
259
+ --vq-name VQVAE \
260
+ --out-dir output \
261
+ --total-iter 300000 \
262
+ --lr-scheduler 150000 \
263
+ --lr 0.0001 \
264
+ --dataname t2m \
265
+ --down-t 2 \
266
+ --depth 3 \
267
+ --quantizer ema_reset \
268
+ --eval-iter 10000 \
269
+ --pkeep 0.5 \
270
+ --dilation-growth-rate 3 \
271
+ --vq-act relu \
272
+ --resume-gpt output/VQTransformer/net_best_fid.pth
273
+ ```
274
+
275
+ </details>
276
+
277
+
278
+ ## 6. Motion Render
279
+
280
+ <details>
281
+ <summary>
282
+ Motion Render
283
+ </summary>
284
+
285
+ You should input the npy folder address and the motion names. Here is an example:
286
+
287
+ ```bash
288
+ python3 render_final.py --filedir output/TEST_VQTransformer/ --motion-list 000019 005485
289
+ ```
290
+
291
+ </details>
292
+
293
+ ### 7. Acknowledgement
294
+
295
+ We appreciate helps from :
296
+
297
+ * Public code like [text-to-motion](https://github.com/EricGuo5513/text-to-motion), [TM2T](https://github.com/EricGuo5513/TM2T) etc.
298
+
299
+ ### 8. ChangLog
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+ <!-- # VQGPT
324
+
325
+ ```
326
+ # VQ during training OT
327
+ /apdcephfs_cq2/share_1290939/jirozhang/anaconda3/envs/motionclip/bin/python3 train_251_cnn_all.py \
328
+ --batch-size 128 \
329
+ --exp-name xxxxxx \
330
+ --lr 2e-4 \
331
+ --total-iter 300000 \
332
+ --lr-scheduler 200000 \
333
+ --nb-code 512 \
334
+ --down-t 2 \
335
+ --depth 5 \
336
+ --out-dir /apdcephfs_cq2/share_1290939/jirozhang/VQCNN_HUMAN/ \
337
+ --dataname t2m \
338
+ --vq-act relu \
339
+ --quantizer ot \
340
+ --ot-temperature 1 \
341
+ --ot-eps 0.5 \
342
+ --commit 0.001 \
343
+ ```
344
+
345
+ ```
346
+ # VQ251 training baseline
347
+ /apdcephfs_cq2/share_1290939/jirozhang/anaconda3/envs/motionclip/bin/python3 train_251_cnn_all.py \
348
+ --batch-size 128 \
349
+ --exp-name VQ263_300K_512cb_down4_t2m_ema_relu_test \
350
+ --lr 2e-4 \
351
+ --total-iter 300000 \
352
+ --lr-scheduler 200000 \
353
+ --nb-code 512 \
354
+ --down-t 2 \
355
+ --depth 5 \
356
+ --out-dir /apdcephfs_cq2/share_1290939/jirozhang/VQCNN_HUMAN/ \
357
+ --dataname t2m \
358
+ --vq-act relu \
359
+ --quantizer ema \
360
+ ```
361
+
362
+
363
+ ```bash
364
+ # gpt training + noise
365
+ /apdcephfs_cq2/share_1290939/jirozhang/anaconda3/envs/motionclip/bin/python3 train_gpt_cnn_noise.py \
366
+ --exp-name GPT_VQ_300K_512cb_down4_t2m_ema_relu_bs128_ws64_fid_mask1_08 \
367
+ --batch-size 128 \
368
+ --num-layers 4 \
369
+ --block-size 51 \
370
+ --n-head-gpt 8 \
371
+ --ff-rate 4 \
372
+ --drop-out-rate 0.1 \
373
+ --resume-pth output_vqhuman/VQ_300K_512cb_down4_t2m_ema_relu_bs128_ws64/net_best_fid.pth \
374
+ --vq-name VQ_300K_512cb_down4_t2m_ema_relu_bs128_ws64_fid_mask1_08 \
375
+ --total-iter 300000 \
376
+ --lr-scheduler 150000 \
377
+ --lr 0.0001 \
378
+ --if-auxloss \
379
+ --dataname t2m \
380
+ --down-t 2 \
381
+ --depth 5 \
382
+ --quantizer ema \
383
+ --eval-iter 5000 \
384
+ --pkeep 0.8
385
+ ```
386
+
387
+
388
+ ### Visualize VQ (Arch Taming) in HTML
389
+
390
+ * Generate motion. This will save generated motions in `./visual_results/vel05_taming_l1s`
391
+
392
+ ```
393
+ python vis.py --dataname t2m --resume-pth /apdcephfs_cq2/share_1290939/jirozhang/VQ_t2m_bailando_relu_NoNorm_dilate3_vel05_taming_l1s/net_last.pth --visual-name vel05_taming_l1s --vis-gt --nb-vis 20
394
+ ```
395
+
396
+ * Make a Webpage. Go to visual_html.py, modify the name, then run :
397
+
398
+ ```
399
+ python visual_html.py
400
+ ``` -->
VQ-Trans/VQ_eval.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ import torch
5
+ from torch.utils.tensorboard import SummaryWriter
6
+ import numpy as np
7
+ import models.vqvae as vqvae
8
+ import options.option_vq as option_vq
9
+ import utils.utils_model as utils_model
10
+ from dataset import dataset_TM_eval
11
+ import utils.eval_trans as eval_trans
12
+ from options.get_eval_option import get_opt
13
+ from models.evaluator_wrapper import EvaluatorModelWrapper
14
+ import warnings
15
+ warnings.filterwarnings('ignore')
16
+ import numpy as np
17
+ ##### ---- Exp dirs ---- #####
18
+ args = option_vq.get_args_parser()
19
+ torch.manual_seed(args.seed)
20
+
21
+ args.out_dir = os.path.join(args.out_dir, f'{args.exp_name}')
22
+ os.makedirs(args.out_dir, exist_ok = True)
23
+
24
+ ##### ---- Logger ---- #####
25
+ logger = utils_model.get_logger(args.out_dir)
26
+ writer = SummaryWriter(args.out_dir)
27
+ logger.info(json.dumps(vars(args), indent=4, sort_keys=True))
28
+
29
+
30
+ from utils.word_vectorizer import WordVectorizer
31
+ w_vectorizer = WordVectorizer('./glove', 'our_vab')
32
+
33
+
34
+ dataset_opt_path = 'checkpoints/kit/Comp_v6_KLD005/opt.txt' if args.dataname == 'kit' else 'checkpoints/t2m/Comp_v6_KLD005/opt.txt'
35
+
36
+ wrapper_opt = get_opt(dataset_opt_path, torch.device('cuda'))
37
+ eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
38
+
39
+
40
+ ##### ---- Dataloader ---- #####
41
+ args.nb_joints = 21 if args.dataname == 'kit' else 22
42
+
43
+ val_loader = dataset_TM_eval.DATALoader(args.dataname, True, 32, w_vectorizer, unit_length=2**args.down_t)
44
+
45
+ ##### ---- Network ---- #####
46
+ net = vqvae.HumanVQVAE(args, ## use args to define different parameters in different quantizers
47
+ args.nb_code,
48
+ args.code_dim,
49
+ args.output_emb_width,
50
+ args.down_t,
51
+ args.stride_t,
52
+ args.width,
53
+ args.depth,
54
+ args.dilation_growth_rate,
55
+ args.vq_act,
56
+ args.vq_norm)
57
+
58
+ if args.resume_pth :
59
+ logger.info('loading checkpoint from {}'.format(args.resume_pth))
60
+ ckpt = torch.load(args.resume_pth, map_location='cpu')
61
+ net.load_state_dict(ckpt['net'], strict=True)
62
+ net.train()
63
+ net.cuda()
64
+
65
+ fid = []
66
+ div = []
67
+ top1 = []
68
+ top2 = []
69
+ top3 = []
70
+ matching = []
71
+ repeat_time = 20
72
+ for i in range(repeat_time):
73
+ best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger = eval_trans.evaluation_vqvae(args.out_dir, val_loader, net, logger, writer, 0, best_fid=1000, best_iter=0, best_div=100, best_top1=0, best_top2=0, best_top3=0, best_matching=100, eval_wrapper=eval_wrapper, draw=False, save=False, savenpy=(i==0))
74
+ fid.append(best_fid)
75
+ div.append(best_div)
76
+ top1.append(best_top1)
77
+ top2.append(best_top2)
78
+ top3.append(best_top3)
79
+ matching.append(best_matching)
80
+ print('final result:')
81
+ print('fid: ', sum(fid)/repeat_time)
82
+ print('div: ', sum(div)/repeat_time)
83
+ print('top1: ', sum(top1)/repeat_time)
84
+ print('top2: ', sum(top2)/repeat_time)
85
+ print('top3: ', sum(top3)/repeat_time)
86
+ print('matching: ', sum(matching)/repeat_time)
87
+
88
+ fid = np.array(fid)
89
+ div = np.array(div)
90
+ top1 = np.array(top1)
91
+ top2 = np.array(top2)
92
+ top3 = np.array(top3)
93
+ matching = np.array(matching)
94
+ msg_final = f"FID. {np.mean(fid):.3f}, conf. {np.std(fid)*1.96/np.sqrt(repeat_time):.3f}, Diversity. {np.mean(div):.3f}, conf. {np.std(div)*1.96/np.sqrt(repeat_time):.3f}, TOP1. {np.mean(top1):.3f}, conf. {np.std(top1)*1.96/np.sqrt(repeat_time):.3f}, TOP2. {np.mean(top2):.3f}, conf. {np.std(top2)*1.96/np.sqrt(repeat_time):.3f}, TOP3. {np.mean(top3):.3f}, conf. {np.std(top3)*1.96/np.sqrt(repeat_time):.3f}, Matching. {np.mean(matching):.3f}, conf. {np.std(matching)*1.96/np.sqrt(repeat_time):.3f}"
95
+ logger.info(msg_final)
VQ-Trans/ViT-B-32.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:40d365715913c9da98579312b702a82c18be219cc2a73407c4526f58eba950af
3
+ size 353976522
VQ-Trans/body_models/smpl/J_regressor_extra.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cc968ea4f9855571e82f90203280836b01f13ee42a8e1b89d8d580b801242a89
3
+ size 496160
VQ-Trans/body_models/smpl/SMPL_NEUTRAL.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:98e65c74ad9b998783132f00880d1025a8d64b158e040e6ef13a557e5098bc42
3
+ size 39001280
VQ-Trans/body_models/smpl/kintree_table.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:62116ec76c6192ae912557122ea935267ba7188144efb9306ea1366f0e50d4d2
3
+ size 349
VQ-Trans/body_models/smpl/smplfaces.npy ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7ee8e99db736acf178a6078ab5710ca942edc3738d34c72f41a35c40b370e045
3
+ size 165440
VQ-Trans/checkpoints/kit.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e9d54e1c68bacad61277f89c7d05f9c88a68fd92ff79f79644128bb9b2508cb
3
+ size 704518254
VQ-Trans/checkpoints/t2m.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:09e0628dbc585416217617c0583415c8f654ff855703d72fdb713f7061c0863e
3
+ size 1222422692
VQ-Trans/checkpoints/train_vq.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ import torch
5
+ import torch.optim as optim
6
+ from torch.utils.tensorboard import SummaryWriter
7
+
8
+ import models.vqvae as vqvae
9
+ import utils.losses as losses
10
+ import options.option_vq as option_vq
11
+ import utils.utils_model as utils_model
12
+ from dataset import dataset_VQ, dataset_TM_eval
13
+ import utils.eval_trans as eval_trans
14
+ from options.get_eval_option import get_opt
15
+ from models.evaluator_wrapper import EvaluatorModelWrapper
16
+ import warnings
17
+ warnings.filterwarnings('ignore')
18
+ from utils.word_vectorizer import WordVectorizer
19
+
20
+ def update_lr_warm_up(optimizer, nb_iter, warm_up_iter, lr):
21
+
22
+ current_lr = lr * (nb_iter + 1) / (warm_up_iter + 1)
23
+ for param_group in optimizer.param_groups:
24
+ param_group["lr"] = current_lr
25
+
26
+ return optimizer, current_lr
27
+
28
+ ##### ---- Exp dirs ---- #####
29
+ args = option_vq.get_args_parser()
30
+ torch.manual_seed(args.seed)
31
+
32
+ args.out_dir = os.path.join(args.out_dir, f'{args.exp_name}')
33
+ os.makedirs(args.out_dir, exist_ok = True)
34
+
35
+ ##### ---- Logger ---- #####
36
+ logger = utils_model.get_logger(args.out_dir)
37
+ writer = SummaryWriter(args.out_dir)
38
+ logger.info(json.dumps(vars(args), indent=4, sort_keys=True))
39
+
40
+
41
+
42
+ w_vectorizer = WordVectorizer('./glove', 'our_vab')
43
+
44
+ if args.dataname == 'kit' :
45
+ dataset_opt_path = 'checkpoints/kit/Comp_v6_KLD005/opt.txt'
46
+ args.nb_joints = 21
47
+
48
+ else :
49
+ dataset_opt_path = 'checkpoints/t2m/Comp_v6_KLD005/opt.txt'
50
+ args.nb_joints = 22
51
+
52
+ logger.info(f'Training on {args.dataname}, motions are with {args.nb_joints} joints')
53
+
54
+ wrapper_opt = get_opt(dataset_opt_path, torch.device('cuda'))
55
+ eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
56
+
57
+
58
+ ##### ---- Dataloader ---- #####
59
+ train_loader = dataset_VQ.DATALoader(args.dataname,
60
+ args.batch_size,
61
+ window_size=args.window_size,
62
+ unit_length=2**args.down_t)
63
+
64
+ train_loader_iter = dataset_VQ.cycle(train_loader)
65
+
66
+ val_loader = dataset_TM_eval.DATALoader(args.dataname, False,
67
+ 32,
68
+ w_vectorizer,
69
+ unit_length=2**args.down_t)
70
+
71
+ ##### ---- Network ---- #####
72
+ net = vqvae.HumanVQVAE(args, ## use args to define different parameters in different quantizers
73
+ args.nb_code,
74
+ args.code_dim,
75
+ args.output_emb_width,
76
+ args.down_t,
77
+ args.stride_t,
78
+ args.width,
79
+ args.depth,
80
+ args.dilation_growth_rate,
81
+ args.vq_act,
82
+ args.vq_norm)
83
+
84
+
85
+ if args.resume_pth :
86
+ logger.info('loading checkpoint from {}'.format(args.resume_pth))
87
+ ckpt = torch.load(args.resume_pth, map_location='cpu')
88
+ net.load_state_dict(ckpt['net'], strict=True)
89
+ net.train()
90
+ net.cuda()
91
+
92
+ ##### ---- Optimizer & Scheduler ---- #####
93
+ optimizer = optim.AdamW(net.parameters(), lr=args.lr, betas=(0.9, 0.99), weight_decay=args.weight_decay)
94
+ scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=args.lr_scheduler, gamma=args.gamma)
95
+
96
+
97
+ Loss = losses.ReConsLoss(args.recons_loss, args.nb_joints)
98
+
99
+ ##### ------ warm-up ------- #####
100
+ avg_recons, avg_perplexity, avg_commit = 0., 0., 0.
101
+
102
+ for nb_iter in range(1, args.warm_up_iter):
103
+
104
+ optimizer, current_lr = update_lr_warm_up(optimizer, nb_iter, args.warm_up_iter, args.lr)
105
+
106
+ gt_motion = next(train_loader_iter)
107
+ gt_motion = gt_motion.cuda().float() # (bs, 64, dim)
108
+
109
+ pred_motion, loss_commit, perplexity = net(gt_motion)
110
+ loss_motion = Loss(pred_motion, gt_motion)
111
+ loss_vel = Loss.forward_vel(pred_motion, gt_motion)
112
+
113
+ loss = loss_motion + args.commit * loss_commit + args.loss_vel * loss_vel
114
+
115
+ optimizer.zero_grad()
116
+ loss.backward()
117
+ optimizer.step()
118
+
119
+ avg_recons += loss_motion.item()
120
+ avg_perplexity += perplexity.item()
121
+ avg_commit += loss_commit.item()
122
+
123
+ if nb_iter % args.print_iter == 0 :
124
+ avg_recons /= args.print_iter
125
+ avg_perplexity /= args.print_iter
126
+ avg_commit /= args.print_iter
127
+
128
+ logger.info(f"Warmup. Iter {nb_iter} : lr {current_lr:.5f} \t Commit. {avg_commit:.5f} \t PPL. {avg_perplexity:.2f} \t Recons. {avg_recons:.5f}")
129
+
130
+ avg_recons, avg_perplexity, avg_commit = 0., 0., 0.
131
+
132
+ ##### ---- Training ---- #####
133
+ avg_recons, avg_perplexity, avg_commit = 0., 0., 0.
134
+ best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger = eval_trans.evaluation_vqvae(args.out_dir, val_loader, net, logger, writer, 0, best_fid=1000, best_iter=0, best_div=100, best_top1=0, best_top2=0, best_top3=0, best_matching=100, eval_wrapper=eval_wrapper)
135
+
136
+ for nb_iter in range(1, args.total_iter + 1):
137
+
138
+ gt_motion = next(train_loader_iter)
139
+ gt_motion = gt_motion.cuda().float() # bs, nb_joints, joints_dim, seq_len
140
+
141
+ pred_motion, loss_commit, perplexity = net(gt_motion)
142
+ loss_motion = Loss(pred_motion, gt_motion)
143
+ loss_vel = Loss.forward_vel(pred_motion, gt_motion)
144
+
145
+ loss = loss_motion + args.commit * loss_commit + args.loss_vel * loss_vel
146
+
147
+ optimizer.zero_grad()
148
+ loss.backward()
149
+ optimizer.step()
150
+ scheduler.step()
151
+
152
+ avg_recons += loss_motion.item()
153
+ avg_perplexity += perplexity.item()
154
+ avg_commit += loss_commit.item()
155
+
156
+ if nb_iter % args.print_iter == 0 :
157
+ avg_recons /= args.print_iter
158
+ avg_perplexity /= args.print_iter
159
+ avg_commit /= args.print_iter
160
+
161
+ writer.add_scalar('./Train/L1', avg_recons, nb_iter)
162
+ writer.add_scalar('./Train/PPL', avg_perplexity, nb_iter)
163
+ writer.add_scalar('./Train/Commit', avg_commit, nb_iter)
164
+
165
+ logger.info(f"Train. Iter {nb_iter} : \t Commit. {avg_commit:.5f} \t PPL. {avg_perplexity:.2f} \t Recons. {avg_recons:.5f}")
166
+
167
+ avg_recons, avg_perplexity, avg_commit = 0., 0., 0.,
168
+
169
+ if nb_iter % args.eval_iter==0 :
170
+ best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger = eval_trans.evaluation_vqvae(args.out_dir, val_loader, net, logger, writer, nb_iter, best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, eval_wrapper=eval_wrapper)
171
+
VQ-Trans/dataset/dataset_TM_eval.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.utils import data
3
+ import numpy as np
4
+ from os.path import join as pjoin
5
+ import random
6
+ import codecs as cs
7
+ from tqdm import tqdm
8
+
9
+ import utils.paramUtil as paramUtil
10
+ from torch.utils.data._utils.collate import default_collate
11
+
12
+
13
+ def collate_fn(batch):
14
+ batch.sort(key=lambda x: x[3], reverse=True)
15
+ return default_collate(batch)
16
+
17
+
18
+ '''For use of training text-2-motion generative model'''
19
+ class Text2MotionDataset(data.Dataset):
20
+ def __init__(self, dataset_name, is_test, w_vectorizer, feat_bias = 5, max_text_len = 20, unit_length = 4):
21
+
22
+ self.max_length = 20
23
+ self.pointer = 0
24
+ self.dataset_name = dataset_name
25
+ self.is_test = is_test
26
+ self.max_text_len = max_text_len
27
+ self.unit_length = unit_length
28
+ self.w_vectorizer = w_vectorizer
29
+ if dataset_name == 't2m':
30
+ self.data_root = './dataset/HumanML3D'
31
+ self.motion_dir = pjoin(self.data_root, 'new_joint_vecs')
32
+ self.text_dir = pjoin(self.data_root, 'texts')
33
+ self.joints_num = 22
34
+ radius = 4
35
+ fps = 20
36
+ self.max_motion_length = 196
37
+ dim_pose = 263
38
+ kinematic_chain = paramUtil.t2m_kinematic_chain
39
+ self.meta_dir = 'checkpoints/t2m/VQVAEV3_CB1024_CMT_H1024_NRES3/meta'
40
+ elif dataset_name == 'kit':
41
+ self.data_root = './dataset/KIT-ML'
42
+ self.motion_dir = pjoin(self.data_root, 'new_joint_vecs')
43
+ self.text_dir = pjoin(self.data_root, 'texts')
44
+ self.joints_num = 21
45
+ radius = 240 * 8
46
+ fps = 12.5
47
+ dim_pose = 251
48
+ self.max_motion_length = 196
49
+ kinematic_chain = paramUtil.kit_kinematic_chain
50
+ self.meta_dir = 'checkpoints/kit/VQVAEV3_CB1024_CMT_H1024_NRES3/meta'
51
+
52
+ mean = np.load(pjoin(self.meta_dir, 'mean.npy'))
53
+ std = np.load(pjoin(self.meta_dir, 'std.npy'))
54
+
55
+ if is_test:
56
+ split_file = pjoin(self.data_root, 'test.txt')
57
+ else:
58
+ split_file = pjoin(self.data_root, 'val.txt')
59
+
60
+ min_motion_len = 40 if self.dataset_name =='t2m' else 24
61
+ # min_motion_len = 64
62
+
63
+ joints_num = self.joints_num
64
+
65
+ data_dict = {}
66
+ id_list = []
67
+ with cs.open(split_file, 'r') as f:
68
+ for line in f.readlines():
69
+ id_list.append(line.strip())
70
+
71
+ new_name_list = []
72
+ length_list = []
73
+ for name in tqdm(id_list):
74
+ try:
75
+ motion = np.load(pjoin(self.motion_dir, name + '.npy'))
76
+ if (len(motion)) < min_motion_len or (len(motion) >= 200):
77
+ continue
78
+ text_data = []
79
+ flag = False
80
+ with cs.open(pjoin(self.text_dir, name + '.txt')) as f:
81
+ for line in f.readlines():
82
+ text_dict = {}
83
+ line_split = line.strip().split('#')
84
+ caption = line_split[0]
85
+ tokens = line_split[1].split(' ')
86
+ f_tag = float(line_split[2])
87
+ to_tag = float(line_split[3])
88
+ f_tag = 0.0 if np.isnan(f_tag) else f_tag
89
+ to_tag = 0.0 if np.isnan(to_tag) else to_tag
90
+
91
+ text_dict['caption'] = caption
92
+ text_dict['tokens'] = tokens
93
+ if f_tag == 0.0 and to_tag == 0.0:
94
+ flag = True
95
+ text_data.append(text_dict)
96
+ else:
97
+ try:
98
+ n_motion = motion[int(f_tag*fps) : int(to_tag*fps)]
99
+ if (len(n_motion)) < min_motion_len or (len(n_motion) >= 200):
100
+ continue
101
+ new_name = random.choice('ABCDEFGHIJKLMNOPQRSTUVW') + '_' + name
102
+ while new_name in data_dict:
103
+ new_name = random.choice('ABCDEFGHIJKLMNOPQRSTUVW') + '_' + name
104
+ data_dict[new_name] = {'motion': n_motion,
105
+ 'length': len(n_motion),
106
+ 'text':[text_dict]}
107
+ new_name_list.append(new_name)
108
+ length_list.append(len(n_motion))
109
+ except:
110
+ print(line_split)
111
+ print(line_split[2], line_split[3], f_tag, to_tag, name)
112
+ # break
113
+
114
+ if flag:
115
+ data_dict[name] = {'motion': motion,
116
+ 'length': len(motion),
117
+ 'text': text_data}
118
+ new_name_list.append(name)
119
+ length_list.append(len(motion))
120
+ except Exception as e:
121
+ # print(e)
122
+ pass
123
+
124
+ name_list, length_list = zip(*sorted(zip(new_name_list, length_list), key=lambda x: x[1]))
125
+ self.mean = mean
126
+ self.std = std
127
+ self.length_arr = np.array(length_list)
128
+ self.data_dict = data_dict
129
+ self.name_list = name_list
130
+ self.reset_max_len(self.max_length)
131
+
132
+ def reset_max_len(self, length):
133
+ assert length <= self.max_motion_length
134
+ self.pointer = np.searchsorted(self.length_arr, length)
135
+ print("Pointer Pointing at %d"%self.pointer)
136
+ self.max_length = length
137
+
138
+ def inv_transform(self, data):
139
+ return data * self.std + self.mean
140
+
141
+ def forward_transform(self, data):
142
+ return (data - self.mean) / self.std
143
+
144
+ def __len__(self):
145
+ return len(self.data_dict) - self.pointer
146
+
147
+ def __getitem__(self, item):
148
+ idx = self.pointer + item
149
+ name = self.name_list[idx]
150
+ data = self.data_dict[name]
151
+ # data = self.data_dict[self.name_list[idx]]
152
+ motion, m_length, text_list = data['motion'], data['length'], data['text']
153
+ # Randomly select a caption
154
+ text_data = random.choice(text_list)
155
+ caption, tokens = text_data['caption'], text_data['tokens']
156
+
157
+ if len(tokens) < self.max_text_len:
158
+ # pad with "unk"
159
+ tokens = ['sos/OTHER'] + tokens + ['eos/OTHER']
160
+ sent_len = len(tokens)
161
+ tokens = tokens + ['unk/OTHER'] * (self.max_text_len + 2 - sent_len)
162
+ else:
163
+ # crop
164
+ tokens = tokens[:self.max_text_len]
165
+ tokens = ['sos/OTHER'] + tokens + ['eos/OTHER']
166
+ sent_len = len(tokens)
167
+ pos_one_hots = []
168
+ word_embeddings = []
169
+ for token in tokens:
170
+ word_emb, pos_oh = self.w_vectorizer[token]
171
+ pos_one_hots.append(pos_oh[None, :])
172
+ word_embeddings.append(word_emb[None, :])
173
+ pos_one_hots = np.concatenate(pos_one_hots, axis=0)
174
+ word_embeddings = np.concatenate(word_embeddings, axis=0)
175
+
176
+ if self.unit_length < 10:
177
+ coin2 = np.random.choice(['single', 'single', 'double'])
178
+ else:
179
+ coin2 = 'single'
180
+
181
+ if coin2 == 'double':
182
+ m_length = (m_length // self.unit_length - 1) * self.unit_length
183
+ elif coin2 == 'single':
184
+ m_length = (m_length // self.unit_length) * self.unit_length
185
+ idx = random.randint(0, len(motion) - m_length)
186
+ motion = motion[idx:idx+m_length]
187
+
188
+ "Z Normalization"
189
+ motion = (motion - self.mean) / self.std
190
+
191
+ if m_length < self.max_motion_length:
192
+ motion = np.concatenate([motion,
193
+ np.zeros((self.max_motion_length - m_length, motion.shape[1]))
194
+ ], axis=0)
195
+
196
+ return word_embeddings, pos_one_hots, caption, sent_len, motion, m_length, '_'.join(tokens), name
197
+
198
+
199
+
200
+
201
+ def DATALoader(dataset_name, is_test,
202
+ batch_size, w_vectorizer,
203
+ num_workers = 8, unit_length = 4) :
204
+
205
+ val_loader = torch.utils.data.DataLoader(Text2MotionDataset(dataset_name, is_test, w_vectorizer, unit_length=unit_length),
206
+ batch_size,
207
+ shuffle = True,
208
+ num_workers=num_workers,
209
+ collate_fn=collate_fn,
210
+ drop_last = True)
211
+ return val_loader
212
+
213
+
214
+ def cycle(iterable):
215
+ while True:
216
+ for x in iterable:
217
+ yield x
VQ-Trans/dataset/dataset_TM_train.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.utils import data
3
+ import numpy as np
4
+ from os.path import join as pjoin
5
+ import random
6
+ import codecs as cs
7
+ from tqdm import tqdm
8
+ import utils.paramUtil as paramUtil
9
+ from torch.utils.data._utils.collate import default_collate
10
+
11
+
12
+ def collate_fn(batch):
13
+ batch.sort(key=lambda x: x[3], reverse=True)
14
+ return default_collate(batch)
15
+
16
+
17
+ '''For use of training text-2-motion generative model'''
18
+ class Text2MotionDataset(data.Dataset):
19
+ def __init__(self, dataset_name, feat_bias = 5, unit_length = 4, codebook_size = 1024, tokenizer_name=None):
20
+
21
+ self.max_length = 64
22
+ self.pointer = 0
23
+ self.dataset_name = dataset_name
24
+
25
+ self.unit_length = unit_length
26
+ # self.mot_start_idx = codebook_size
27
+ self.mot_end_idx = codebook_size
28
+ self.mot_pad_idx = codebook_size + 1
29
+ if dataset_name == 't2m':
30
+ self.data_root = './dataset/HumanML3D'
31
+ self.motion_dir = pjoin(self.data_root, 'new_joint_vecs')
32
+ self.text_dir = pjoin(self.data_root, 'texts')
33
+ self.joints_num = 22
34
+ radius = 4
35
+ fps = 20
36
+ self.max_motion_length = 26 if unit_length == 8 else 51
37
+ dim_pose = 263
38
+ kinematic_chain = paramUtil.t2m_kinematic_chain
39
+ elif dataset_name == 'kit':
40
+ self.data_root = './dataset/KIT-ML'
41
+ self.motion_dir = pjoin(self.data_root, 'new_joint_vecs')
42
+ self.text_dir = pjoin(self.data_root, 'texts')
43
+ self.joints_num = 21
44
+ radius = 240 * 8
45
+ fps = 12.5
46
+ dim_pose = 251
47
+ self.max_motion_length = 26 if unit_length == 8 else 51
48
+ kinematic_chain = paramUtil.kit_kinematic_chain
49
+
50
+ split_file = pjoin(self.data_root, 'train.txt')
51
+
52
+
53
+ id_list = []
54
+ with cs.open(split_file, 'r') as f:
55
+ for line in f.readlines():
56
+ id_list.append(line.strip())
57
+
58
+ new_name_list = []
59
+ data_dict = {}
60
+ for name in tqdm(id_list):
61
+ try:
62
+ m_token_list = np.load(pjoin(self.data_root, tokenizer_name, '%s.npy'%name))
63
+
64
+ # Read text
65
+ with cs.open(pjoin(self.text_dir, name + '.txt')) as f:
66
+ text_data = []
67
+ flag = False
68
+ lines = f.readlines()
69
+
70
+ for line in lines:
71
+ try:
72
+ text_dict = {}
73
+ line_split = line.strip().split('#')
74
+ caption = line_split[0]
75
+ t_tokens = line_split[1].split(' ')
76
+ f_tag = float(line_split[2])
77
+ to_tag = float(line_split[3])
78
+ f_tag = 0.0 if np.isnan(f_tag) else f_tag
79
+ to_tag = 0.0 if np.isnan(to_tag) else to_tag
80
+
81
+ text_dict['caption'] = caption
82
+ text_dict['tokens'] = t_tokens
83
+ if f_tag == 0.0 and to_tag == 0.0:
84
+ flag = True
85
+ text_data.append(text_dict)
86
+ else:
87
+ m_token_list_new = [tokens[int(f_tag*fps/unit_length) : int(to_tag*fps/unit_length)] for tokens in m_token_list if int(f_tag*fps/unit_length) < int(to_tag*fps/unit_length)]
88
+
89
+ if len(m_token_list_new) == 0:
90
+ continue
91
+ new_name = '%s_%f_%f'%(name, f_tag, to_tag)
92
+
93
+ data_dict[new_name] = {'m_token_list': m_token_list_new,
94
+ 'text':[text_dict]}
95
+ new_name_list.append(new_name)
96
+ except:
97
+ pass
98
+
99
+ if flag:
100
+ data_dict[name] = {'m_token_list': m_token_list,
101
+ 'text':text_data}
102
+ new_name_list.append(name)
103
+ except:
104
+ pass
105
+ self.data_dict = data_dict
106
+ self.name_list = new_name_list
107
+
108
+ def __len__(self):
109
+ return len(self.data_dict)
110
+
111
+ def __getitem__(self, item):
112
+ data = self.data_dict[self.name_list[item]]
113
+ m_token_list, text_list = data['m_token_list'], data['text']
114
+ m_tokens = random.choice(m_token_list)
115
+
116
+ text_data = random.choice(text_list)
117
+ caption= text_data['caption']
118
+
119
+
120
+ coin = np.random.choice([False, False, True])
121
+ # print(len(m_tokens))
122
+ if coin:
123
+ # drop one token at the head or tail
124
+ coin2 = np.random.choice([True, False])
125
+ if coin2:
126
+ m_tokens = m_tokens[:-1]
127
+ else:
128
+ m_tokens = m_tokens[1:]
129
+ m_tokens_len = m_tokens.shape[0]
130
+
131
+ if m_tokens_len+1 < self.max_motion_length:
132
+ m_tokens = np.concatenate([m_tokens, np.ones((1), dtype=int) * self.mot_end_idx, np.ones((self.max_motion_length-1-m_tokens_len), dtype=int) * self.mot_pad_idx], axis=0)
133
+ else:
134
+ m_tokens = np.concatenate([m_tokens, np.ones((1), dtype=int) * self.mot_end_idx], axis=0)
135
+
136
+ return caption, m_tokens.reshape(-1), m_tokens_len
137
+
138
+
139
+
140
+
141
+ def DATALoader(dataset_name,
142
+ batch_size, codebook_size, tokenizer_name, unit_length=4,
143
+ num_workers = 8) :
144
+
145
+ train_loader = torch.utils.data.DataLoader(Text2MotionDataset(dataset_name, codebook_size = codebook_size, tokenizer_name = tokenizer_name, unit_length=unit_length),
146
+ batch_size,
147
+ shuffle=True,
148
+ num_workers=num_workers,
149
+ #collate_fn=collate_fn,
150
+ drop_last = True)
151
+
152
+
153
+ return train_loader
154
+
155
+
156
+ def cycle(iterable):
157
+ while True:
158
+ for x in iterable:
159
+ yield x
160
+
161
+
VQ-Trans/dataset/dataset_VQ.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.utils import data
3
+ import numpy as np
4
+ from os.path import join as pjoin
5
+ import random
6
+ import codecs as cs
7
+ from tqdm import tqdm
8
+
9
+
10
+
11
+ class VQMotionDataset(data.Dataset):
12
+ def __init__(self, dataset_name, window_size = 64, unit_length = 4):
13
+ self.window_size = window_size
14
+ self.unit_length = unit_length
15
+ self.dataset_name = dataset_name
16
+
17
+ if dataset_name == 't2m':
18
+ self.data_root = './dataset/HumanML3D'
19
+ self.motion_dir = pjoin(self.data_root, 'new_joint_vecs')
20
+ self.text_dir = pjoin(self.data_root, 'texts')
21
+ self.joints_num = 22
22
+ self.max_motion_length = 196
23
+ self.meta_dir = 'checkpoints/t2m/VQVAEV3_CB1024_CMT_H1024_NRES3/meta'
24
+
25
+ elif dataset_name == 'kit':
26
+ self.data_root = './dataset/KIT-ML'
27
+ self.motion_dir = pjoin(self.data_root, 'new_joint_vecs')
28
+ self.text_dir = pjoin(self.data_root, 'texts')
29
+ self.joints_num = 21
30
+
31
+ self.max_motion_length = 196
32
+ self.meta_dir = 'checkpoints/kit/VQVAEV3_CB1024_CMT_H1024_NRES3/meta'
33
+
34
+ joints_num = self.joints_num
35
+
36
+ mean = np.load(pjoin(self.meta_dir, 'mean.npy'))
37
+ std = np.load(pjoin(self.meta_dir, 'std.npy'))
38
+
39
+ split_file = pjoin(self.data_root, 'train.txt')
40
+
41
+ self.data = []
42
+ self.lengths = []
43
+ id_list = []
44
+ with cs.open(split_file, 'r') as f:
45
+ for line in f.readlines():
46
+ id_list.append(line.strip())
47
+
48
+ for name in tqdm(id_list):
49
+ try:
50
+ motion = np.load(pjoin(self.motion_dir, name + '.npy'))
51
+ if motion.shape[0] < self.window_size:
52
+ continue
53
+ self.lengths.append(motion.shape[0] - self.window_size)
54
+ self.data.append(motion)
55
+ except:
56
+ # Some motion may not exist in KIT dataset
57
+ pass
58
+
59
+
60
+ self.mean = mean
61
+ self.std = std
62
+ print("Total number of motions {}".format(len(self.data)))
63
+
64
+ def inv_transform(self, data):
65
+ return data * self.std + self.mean
66
+
67
+ def compute_sampling_prob(self) :
68
+
69
+ prob = np.array(self.lengths, dtype=np.float32)
70
+ prob /= np.sum(prob)
71
+ return prob
72
+
73
+ def __len__(self):
74
+ return len(self.data)
75
+
76
+ def __getitem__(self, item):
77
+ motion = self.data[item]
78
+
79
+ idx = random.randint(0, len(motion) - self.window_size)
80
+
81
+ motion = motion[idx:idx+self.window_size]
82
+ "Z Normalization"
83
+ motion = (motion - self.mean) / self.std
84
+
85
+ return motion
86
+
87
+ def DATALoader(dataset_name,
88
+ batch_size,
89
+ num_workers = 8,
90
+ window_size = 64,
91
+ unit_length = 4):
92
+
93
+ trainSet = VQMotionDataset(dataset_name, window_size=window_size, unit_length=unit_length)
94
+ prob = trainSet.compute_sampling_prob()
95
+ sampler = torch.utils.data.WeightedRandomSampler(prob, num_samples = len(trainSet) * 1000, replacement=True)
96
+ train_loader = torch.utils.data.DataLoader(trainSet,
97
+ batch_size,
98
+ shuffle=True,
99
+ #sampler=sampler,
100
+ num_workers=num_workers,
101
+ #collate_fn=collate_fn,
102
+ drop_last = True)
103
+
104
+ return train_loader
105
+
106
+ def cycle(iterable):
107
+ while True:
108
+ for x in iterable:
109
+ yield x
VQ-Trans/dataset/dataset_tokenize.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.utils import data
3
+ import numpy as np
4
+ from os.path import join as pjoin
5
+ import random
6
+ import codecs as cs
7
+ from tqdm import tqdm
8
+
9
+
10
+
11
+ class VQMotionDataset(data.Dataset):
12
+ def __init__(self, dataset_name, feat_bias = 5, window_size = 64, unit_length = 8):
13
+ self.window_size = window_size
14
+ self.unit_length = unit_length
15
+ self.feat_bias = feat_bias
16
+
17
+ self.dataset_name = dataset_name
18
+ min_motion_len = 40 if dataset_name =='t2m' else 24
19
+
20
+ if dataset_name == 't2m':
21
+ self.data_root = './dataset/HumanML3D'
22
+ self.motion_dir = pjoin(self.data_root, 'new_joint_vecs')
23
+ self.text_dir = pjoin(self.data_root, 'texts')
24
+ self.joints_num = 22
25
+ radius = 4
26
+ fps = 20
27
+ self.max_motion_length = 196
28
+ dim_pose = 263
29
+ self.meta_dir = 'checkpoints/t2m/VQVAEV3_CB1024_CMT_H1024_NRES3/meta'
30
+ #kinematic_chain = paramUtil.t2m_kinematic_chain
31
+ elif dataset_name == 'kit':
32
+ self.data_root = './dataset/KIT-ML'
33
+ self.motion_dir = pjoin(self.data_root, 'new_joint_vecs')
34
+ self.text_dir = pjoin(self.data_root, 'texts')
35
+ self.joints_num = 21
36
+ radius = 240 * 8
37
+ fps = 12.5
38
+ dim_pose = 251
39
+ self.max_motion_length = 196
40
+ self.meta_dir = 'checkpoints/kit/VQVAEV3_CB1024_CMT_H1024_NRES3/meta'
41
+ #kinematic_chain = paramUtil.kit_kinematic_chain
42
+
43
+ joints_num = self.joints_num
44
+
45
+ mean = np.load(pjoin(self.meta_dir, 'mean.npy'))
46
+ std = np.load(pjoin(self.meta_dir, 'std.npy'))
47
+
48
+ split_file = pjoin(self.data_root, 'train.txt')
49
+
50
+ data_dict = {}
51
+ id_list = []
52
+ with cs.open(split_file, 'r') as f:
53
+ for line in f.readlines():
54
+ id_list.append(line.strip())
55
+
56
+ new_name_list = []
57
+ length_list = []
58
+ for name in tqdm(id_list):
59
+ try:
60
+ motion = np.load(pjoin(self.motion_dir, name + '.npy'))
61
+ if (len(motion)) < min_motion_len or (len(motion) >= 200):
62
+ continue
63
+
64
+ data_dict[name] = {'motion': motion,
65
+ 'length': len(motion),
66
+ 'name': name}
67
+ new_name_list.append(name)
68
+ length_list.append(len(motion))
69
+ except:
70
+ # Some motion may not exist in KIT dataset
71
+ pass
72
+
73
+
74
+ self.mean = mean
75
+ self.std = std
76
+ self.length_arr = np.array(length_list)
77
+ self.data_dict = data_dict
78
+ self.name_list = new_name_list
79
+
80
+ def inv_transform(self, data):
81
+ return data * self.std + self.mean
82
+
83
+ def __len__(self):
84
+ return len(self.data_dict)
85
+
86
+ def __getitem__(self, item):
87
+ name = self.name_list[item]
88
+ data = self.data_dict[name]
89
+ motion, m_length = data['motion'], data['length']
90
+
91
+ m_length = (m_length // self.unit_length) * self.unit_length
92
+
93
+ idx = random.randint(0, len(motion) - m_length)
94
+ motion = motion[idx:idx+m_length]
95
+
96
+ "Z Normalization"
97
+ motion = (motion - self.mean) / self.std
98
+
99
+ return motion, name
100
+
101
+ def DATALoader(dataset_name,
102
+ batch_size = 1,
103
+ num_workers = 8, unit_length = 4) :
104
+
105
+ train_loader = torch.utils.data.DataLoader(VQMotionDataset(dataset_name, unit_length=unit_length),
106
+ batch_size,
107
+ shuffle=True,
108
+ num_workers=num_workers,
109
+ #collate_fn=collate_fn,
110
+ drop_last = True)
111
+
112
+ return train_loader
113
+
114
+ def cycle(iterable):
115
+ while True:
116
+ for x in iterable:
117
+ yield x
VQ-Trans/dataset/prepare/download_extractor.sh ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ rm -rf checkpoints
2
+ mkdir checkpoints
3
+ cd checkpoints
4
+ echo -e "Downloading extractors"
5
+ gdown --fuzzy https://drive.google.com/file/d/1o7RTDQcToJjTm9_mNWTyzvZvjTWpZfug/view
6
+ gdown --fuzzy https://drive.google.com/file/d/1tX79xk0fflp07EZ660Xz1RAFE33iEyJR/view
7
+
8
+
9
+ unzip t2m.zip
10
+ unzip kit.zip
11
+
12
+ echo -e "Cleaning\n"
13
+ rm t2m.zip
14
+ rm kit.zip
15
+ echo -e "Downloading done!"
VQ-Trans/dataset/prepare/download_glove.sh ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ echo -e "Downloading glove (in use by the evaluators)"
2
+ gdown --fuzzy https://drive.google.com/file/d/1bCeS6Sh_mLVTebxIgiUHgdPrroW06mb6/view?usp=sharing
3
+ rm -rf glove
4
+
5
+ unzip glove.zip
6
+ echo -e "Cleaning\n"
7
+ rm glove.zip
8
+
9
+ echo -e "Downloading done!"
VQ-Trans/dataset/prepare/download_model.sh ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ mkdir -p pretrained
3
+ cd pretrained/
4
+
5
+ echo -e "The pretrained model files will be stored in the 'pretrained' folder\n"
6
+ gdown 1LaOvwypF-jM2Axnq5dc-Iuvv3w_G-WDE
7
+
8
+ unzip VQTrans_pretrained.zip
9
+ echo -e "Cleaning\n"
10
+ rm VQTrans_pretrained.zip
11
+
12
+ echo -e "Downloading done!"
VQ-Trans/dataset/prepare/download_smpl.sh ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ mkdir -p body_models
3
+ cd body_models/
4
+
5
+ echo -e "The smpl files will be stored in the 'body_models/smpl/' folder\n"
6
+ gdown 1INYlGA76ak_cKGzvpOV2Pe6RkYTlXTW2
7
+ rm -rf smpl
8
+
9
+ unzip smpl.zip
10
+ echo -e "Cleaning\n"
11
+ rm smpl.zip
12
+
13
+ echo -e "Downloading done!"
VQ-Trans/environment.yml ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: VQTrans
2
+ channels:
3
+ - pytorch
4
+ - defaults
5
+ dependencies:
6
+ - _libgcc_mutex=0.1=main
7
+ - _openmp_mutex=4.5=1_gnu
8
+ - blas=1.0=mkl
9
+ - bzip2=1.0.8=h7b6447c_0
10
+ - ca-certificates=2021.7.5=h06a4308_1
11
+ - certifi=2021.5.30=py38h06a4308_0
12
+ - cudatoolkit=10.1.243=h6bb024c_0
13
+ - ffmpeg=4.3=hf484d3e_0
14
+ - freetype=2.10.4=h5ab3b9f_0
15
+ - gmp=6.2.1=h2531618_2
16
+ - gnutls=3.6.15=he1e5248_0
17
+ - intel-openmp=2021.3.0=h06a4308_3350
18
+ - jpeg=9b=h024ee3a_2
19
+ - lame=3.100=h7b6447c_0
20
+ - lcms2=2.12=h3be6417_0
21
+ - ld_impl_linux-64=2.35.1=h7274673_9
22
+ - libffi=3.3=he6710b0_2
23
+ - libgcc-ng=9.3.0=h5101ec6_17
24
+ - libgomp=9.3.0=h5101ec6_17
25
+ - libiconv=1.15=h63c8f33_5
26
+ - libidn2=2.3.2=h7f8727e_0
27
+ - libpng=1.6.37=hbc83047_0
28
+ - libstdcxx-ng=9.3.0=hd4cf53a_17
29
+ - libtasn1=4.16.0=h27cfd23_0
30
+ - libtiff=4.2.0=h85742a9_0
31
+ - libunistring=0.9.10=h27cfd23_0
32
+ - libuv=1.40.0=h7b6447c_0
33
+ - libwebp-base=1.2.0=h27cfd23_0
34
+ - lz4-c=1.9.3=h295c915_1
35
+ - mkl=2021.3.0=h06a4308_520
36
+ - mkl-service=2.4.0=py38h7f8727e_0
37
+ - mkl_fft=1.3.0=py38h42c9631_2
38
+ - mkl_random=1.2.2=py38h51133e4_0
39
+ - ncurses=6.2=he6710b0_1
40
+ - nettle=3.7.3=hbbd107a_1
41
+ - ninja=1.10.2=hff7bd54_1
42
+ - numpy=1.20.3=py38hf144106_0
43
+ - numpy-base=1.20.3=py38h74d4b33_0
44
+ - olefile=0.46=py_0
45
+ - openh264=2.1.0=hd408876_0
46
+ - openjpeg=2.3.0=h05c96fa_1
47
+ - openssl=1.1.1k=h27cfd23_0
48
+ - pillow=8.3.1=py38h2c7a002_0
49
+ - pip=21.0.1=py38h06a4308_0
50
+ - python=3.8.11=h12debd9_0_cpython
51
+ - pytorch=1.8.1=py3.8_cuda10.1_cudnn7.6.3_0
52
+ - readline=8.1=h27cfd23_0
53
+ - setuptools=52.0.0=py38h06a4308_0
54
+ - six=1.16.0=pyhd3eb1b0_0
55
+ - sqlite=3.36.0=hc218d9a_0
56
+ - tk=8.6.10=hbc83047_0
57
+ - torchaudio=0.8.1=py38
58
+ - torchvision=0.9.1=py38_cu101
59
+ - typing_extensions=3.10.0.0=pyh06a4308_0
60
+ - wheel=0.37.0=pyhd3eb1b0_0
61
+ - xz=5.2.5=h7b6447c_0
62
+ - zlib=1.2.11=h7b6447c_3
63
+ - zstd=1.4.9=haebb681_0
64
+ - pip:
65
+ - absl-py==0.13.0
66
+ - backcall==0.2.0
67
+ - cachetools==4.2.2
68
+ - charset-normalizer==2.0.4
69
+ - chumpy==0.70
70
+ - cycler==0.10.0
71
+ - decorator==5.0.9
72
+ - google-auth==1.35.0
73
+ - google-auth-oauthlib==0.4.5
74
+ - grpcio==1.39.0
75
+ - idna==3.2
76
+ - imageio==2.9.0
77
+ - ipdb==0.13.9
78
+ - ipython==7.26.0
79
+ - ipython-genutils==0.2.0
80
+ - jedi==0.18.0
81
+ - joblib==1.0.1
82
+ - kiwisolver==1.3.1
83
+ - markdown==3.3.4
84
+ - matplotlib==3.4.3
85
+ - matplotlib-inline==0.1.2
86
+ - oauthlib==3.1.1
87
+ - pandas==1.3.2
88
+ - parso==0.8.2
89
+ - pexpect==4.8.0
90
+ - pickleshare==0.7.5
91
+ - prompt-toolkit==3.0.20
92
+ - protobuf==3.17.3
93
+ - ptyprocess==0.7.0
94
+ - pyasn1==0.4.8
95
+ - pyasn1-modules==0.2.8
96
+ - pygments==2.10.0
97
+ - pyparsing==2.4.7
98
+ - python-dateutil==2.8.2
99
+ - pytz==2021.1
100
+ - pyyaml==5.4.1
101
+ - requests==2.26.0
102
+ - requests-oauthlib==1.3.0
103
+ - rsa==4.7.2
104
+ - scikit-learn==0.24.2
105
+ - scipy==1.7.1
106
+ - sklearn==0.0
107
+ - smplx==0.1.28
108
+ - tensorboard==2.6.0
109
+ - tensorboard-data-server==0.6.1
110
+ - tensorboard-plugin-wit==1.8.0
111
+ - threadpoolctl==2.2.0
112
+ - toml==0.10.2
113
+ - tqdm==4.62.2
114
+ - traitlets==5.0.5
115
+ - urllib3==1.26.6
116
+ - wcwidth==0.2.5
117
+ - werkzeug==2.0.1
118
+ - git+https://github.com/openai/CLIP.git
119
+ - git+https://github.com/nghorbani/human_body_prior
120
+ - gdown
121
+ - moviepy
VQ-Trans/models/encdec.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ from models.resnet import Resnet1D
3
+
4
+ class Encoder(nn.Module):
5
+ def __init__(self,
6
+ input_emb_width = 3,
7
+ output_emb_width = 512,
8
+ down_t = 3,
9
+ stride_t = 2,
10
+ width = 512,
11
+ depth = 3,
12
+ dilation_growth_rate = 3,
13
+ activation='relu',
14
+ norm=None):
15
+ super().__init__()
16
+
17
+ blocks = []
18
+ filter_t, pad_t = stride_t * 2, stride_t // 2
19
+ blocks.append(nn.Conv1d(input_emb_width, width, 3, 1, 1))
20
+ blocks.append(nn.ReLU())
21
+
22
+ for i in range(down_t):
23
+ input_dim = width
24
+ block = nn.Sequential(
25
+ nn.Conv1d(input_dim, width, filter_t, stride_t, pad_t),
26
+ Resnet1D(width, depth, dilation_growth_rate, activation=activation, norm=norm),
27
+ )
28
+ blocks.append(block)
29
+ blocks.append(nn.Conv1d(width, output_emb_width, 3, 1, 1))
30
+ self.model = nn.Sequential(*blocks)
31
+
32
+ def forward(self, x):
33
+ return self.model(x)
34
+
35
+ class Decoder(nn.Module):
36
+ def __init__(self,
37
+ input_emb_width = 3,
38
+ output_emb_width = 512,
39
+ down_t = 3,
40
+ stride_t = 2,
41
+ width = 512,
42
+ depth = 3,
43
+ dilation_growth_rate = 3,
44
+ activation='relu',
45
+ norm=None):
46
+ super().__init__()
47
+ blocks = []
48
+
49
+ filter_t, pad_t = stride_t * 2, stride_t // 2
50
+ blocks.append(nn.Conv1d(output_emb_width, width, 3, 1, 1))
51
+ blocks.append(nn.ReLU())
52
+ for i in range(down_t):
53
+ out_dim = width
54
+ block = nn.Sequential(
55
+ Resnet1D(width, depth, dilation_growth_rate, reverse_dilation=True, activation=activation, norm=norm),
56
+ nn.Upsample(scale_factor=2, mode='nearest'),
57
+ nn.Conv1d(width, out_dim, 3, 1, 1)
58
+ )
59
+ blocks.append(block)
60
+ blocks.append(nn.Conv1d(width, width, 3, 1, 1))
61
+ blocks.append(nn.ReLU())
62
+ blocks.append(nn.Conv1d(width, input_emb_width, 3, 1, 1))
63
+ self.model = nn.Sequential(*blocks)
64
+
65
+ def forward(self, x):
66
+ return self.model(x)
67
+
VQ-Trans/models/evaluator_wrapper.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ from os.path import join as pjoin
4
+ import numpy as np
5
+ from models.modules import MovementConvEncoder, TextEncoderBiGRUCo, MotionEncoderBiGRUCo
6
+ from utils.word_vectorizer import POS_enumerator
7
+
8
+ def build_models(opt):
9
+ movement_enc = MovementConvEncoder(opt.dim_pose-4, opt.dim_movement_enc_hidden, opt.dim_movement_latent)
10
+ text_enc = TextEncoderBiGRUCo(word_size=opt.dim_word,
11
+ pos_size=opt.dim_pos_ohot,
12
+ hidden_size=opt.dim_text_hidden,
13
+ output_size=opt.dim_coemb_hidden,
14
+ device=opt.device)
15
+
16
+ motion_enc = MotionEncoderBiGRUCo(input_size=opt.dim_movement_latent,
17
+ hidden_size=opt.dim_motion_hidden,
18
+ output_size=opt.dim_coemb_hidden,
19
+ device=opt.device)
20
+
21
+ checkpoint = torch.load(pjoin(opt.checkpoints_dir, opt.dataset_name, 'text_mot_match', 'model', 'finest.tar'),
22
+ map_location=opt.device)
23
+ movement_enc.load_state_dict(checkpoint['movement_encoder'])
24
+ text_enc.load_state_dict(checkpoint['text_encoder'])
25
+ motion_enc.load_state_dict(checkpoint['motion_encoder'])
26
+ print('Loading Evaluation Model Wrapper (Epoch %d) Completed!!' % (checkpoint['epoch']))
27
+ return text_enc, motion_enc, movement_enc
28
+
29
+
30
+ class EvaluatorModelWrapper(object):
31
+
32
+ def __init__(self, opt):
33
+
34
+ if opt.dataset_name == 't2m':
35
+ opt.dim_pose = 263
36
+ elif opt.dataset_name == 'kit':
37
+ opt.dim_pose = 251
38
+ else:
39
+ raise KeyError('Dataset not Recognized!!!')
40
+
41
+ opt.dim_word = 300
42
+ opt.max_motion_length = 196
43
+ opt.dim_pos_ohot = len(POS_enumerator)
44
+ opt.dim_motion_hidden = 1024
45
+ opt.max_text_len = 20
46
+ opt.dim_text_hidden = 512
47
+ opt.dim_coemb_hidden = 512
48
+
49
+ # print(opt)
50
+
51
+ self.text_encoder, self.motion_encoder, self.movement_encoder = build_models(opt)
52
+ self.opt = opt
53
+ self.device = opt.device
54
+
55
+ self.text_encoder.to(opt.device)
56
+ self.motion_encoder.to(opt.device)
57
+ self.movement_encoder.to(opt.device)
58
+
59
+ self.text_encoder.eval()
60
+ self.motion_encoder.eval()
61
+ self.movement_encoder.eval()
62
+
63
+ # Please note that the results does not following the order of inputs
64
+ def get_co_embeddings(self, word_embs, pos_ohot, cap_lens, motions, m_lens):
65
+ with torch.no_grad():
66
+ word_embs = word_embs.detach().to(self.device).float()
67
+ pos_ohot = pos_ohot.detach().to(self.device).float()
68
+ motions = motions.detach().to(self.device).float()
69
+
70
+ '''Movement Encoding'''
71
+ movements = self.movement_encoder(motions[..., :-4]).detach()
72
+ m_lens = m_lens // self.opt.unit_length
73
+ motion_embedding = self.motion_encoder(movements, m_lens)
74
+
75
+ '''Text Encoding'''
76
+ text_embedding = self.text_encoder(word_embs, pos_ohot, cap_lens)
77
+ return text_embedding, motion_embedding
78
+
79
+ # Please note that the results does not following the order of inputs
80
+ def get_motion_embeddings(self, motions, m_lens):
81
+ with torch.no_grad():
82
+ motions = motions.detach().to(self.device).float()
83
+
84
+ align_idx = np.argsort(m_lens.data.tolist())[::-1].copy()
85
+ motions = motions[align_idx]
86
+ m_lens = m_lens[align_idx]
87
+
88
+ '''Movement Encoding'''
89
+ movements = self.movement_encoder(motions[..., :-4]).detach()
90
+ m_lens = m_lens // self.opt.unit_length
91
+ motion_embedding = self.motion_encoder(movements, m_lens)
92
+ return motion_embedding
VQ-Trans/models/modules.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from torch.nn.utils.rnn import pack_padded_sequence
4
+
5
+ def init_weight(m):
6
+ if isinstance(m, nn.Conv1d) or isinstance(m, nn.Linear) or isinstance(m, nn.ConvTranspose1d):
7
+ nn.init.xavier_normal_(m.weight)
8
+ # m.bias.data.fill_(0.01)
9
+ if m.bias is not None:
10
+ nn.init.constant_(m.bias, 0)
11
+
12
+
13
+ class MovementConvEncoder(nn.Module):
14
+ def __init__(self, input_size, hidden_size, output_size):
15
+ super(MovementConvEncoder, self).__init__()
16
+ self.main = nn.Sequential(
17
+ nn.Conv1d(input_size, hidden_size, 4, 2, 1),
18
+ nn.Dropout(0.2, inplace=True),
19
+ nn.LeakyReLU(0.2, inplace=True),
20
+ nn.Conv1d(hidden_size, output_size, 4, 2, 1),
21
+ nn.Dropout(0.2, inplace=True),
22
+ nn.LeakyReLU(0.2, inplace=True),
23
+ )
24
+ self.out_net = nn.Linear(output_size, output_size)
25
+ self.main.apply(init_weight)
26
+ self.out_net.apply(init_weight)
27
+
28
+ def forward(self, inputs):
29
+ inputs = inputs.permute(0, 2, 1)
30
+ outputs = self.main(inputs).permute(0, 2, 1)
31
+ # print(outputs.shape)
32
+ return self.out_net(outputs)
33
+
34
+
35
+
36
+ class TextEncoderBiGRUCo(nn.Module):
37
+ def __init__(self, word_size, pos_size, hidden_size, output_size, device):
38
+ super(TextEncoderBiGRUCo, self).__init__()
39
+ self.device = device
40
+
41
+ self.pos_emb = nn.Linear(pos_size, word_size)
42
+ self.input_emb = nn.Linear(word_size, hidden_size)
43
+ self.gru = nn.GRU(hidden_size, hidden_size, batch_first=True, bidirectional=True)
44
+ self.output_net = nn.Sequential(
45
+ nn.Linear(hidden_size * 2, hidden_size),
46
+ nn.LayerNorm(hidden_size),
47
+ nn.LeakyReLU(0.2, inplace=True),
48
+ nn.Linear(hidden_size, output_size)
49
+ )
50
+
51
+ self.input_emb.apply(init_weight)
52
+ self.pos_emb.apply(init_weight)
53
+ self.output_net.apply(init_weight)
54
+ self.hidden_size = hidden_size
55
+ self.hidden = nn.Parameter(torch.randn((2, 1, self.hidden_size), requires_grad=True))
56
+
57
+ # input(batch_size, seq_len, dim)
58
+ def forward(self, word_embs, pos_onehot, cap_lens):
59
+ num_samples = word_embs.shape[0]
60
+
61
+ pos_embs = self.pos_emb(pos_onehot)
62
+ inputs = word_embs + pos_embs
63
+ input_embs = self.input_emb(inputs)
64
+ hidden = self.hidden.repeat(1, num_samples, 1)
65
+
66
+ cap_lens = cap_lens.data.tolist()
67
+ emb = pack_padded_sequence(input_embs, cap_lens, batch_first=True)
68
+
69
+ gru_seq, gru_last = self.gru(emb, hidden)
70
+
71
+ gru_last = torch.cat([gru_last[0], gru_last[1]], dim=-1)
72
+
73
+ return self.output_net(gru_last)
74
+
75
+
76
+ class MotionEncoderBiGRUCo(nn.Module):
77
+ def __init__(self, input_size, hidden_size, output_size, device):
78
+ super(MotionEncoderBiGRUCo, self).__init__()
79
+ self.device = device
80
+
81
+ self.input_emb = nn.Linear(input_size, hidden_size)
82
+ self.gru = nn.GRU(hidden_size, hidden_size, batch_first=True, bidirectional=True)
83
+ self.output_net = nn.Sequential(
84
+ nn.Linear(hidden_size*2, hidden_size),
85
+ nn.LayerNorm(hidden_size),
86
+ nn.LeakyReLU(0.2, inplace=True),
87
+ nn.Linear(hidden_size, output_size)
88
+ )
89
+
90
+ self.input_emb.apply(init_weight)
91
+ self.output_net.apply(init_weight)
92
+ self.hidden_size = hidden_size
93
+ self.hidden = nn.Parameter(torch.randn((2, 1, self.hidden_size), requires_grad=True))
94
+
95
+ # input(batch_size, seq_len, dim)
96
+ def forward(self, inputs, m_lens):
97
+ num_samples = inputs.shape[0]
98
+
99
+ input_embs = self.input_emb(inputs)
100
+ hidden = self.hidden.repeat(1, num_samples, 1)
101
+
102
+ cap_lens = m_lens.data.tolist()
103
+ emb = pack_padded_sequence(input_embs, cap_lens, batch_first=True, enforce_sorted=False)
104
+
105
+ gru_seq, gru_last = self.gru(emb, hidden)
106
+
107
+ gru_last = torch.cat([gru_last[0], gru_last[1]], dim=-1)
108
+
109
+ return self.output_net(gru_last)
VQ-Trans/models/pos_encoding.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Various positional encodings for the transformer.
3
+ """
4
+ import math
5
+ import torch
6
+ from torch import nn
7
+
8
+ def PE1d_sincos(seq_length, dim):
9
+ """
10
+ :param d_model: dimension of the model
11
+ :param length: length of positions
12
+ :return: length*d_model position matrix
13
+ """
14
+ if dim % 2 != 0:
15
+ raise ValueError("Cannot use sin/cos positional encoding with "
16
+ "odd dim (got dim={:d})".format(dim))
17
+ pe = torch.zeros(seq_length, dim)
18
+ position = torch.arange(0, seq_length).unsqueeze(1)
19
+ div_term = torch.exp((torch.arange(0, dim, 2, dtype=torch.float) *
20
+ -(math.log(10000.0) / dim)))
21
+ pe[:, 0::2] = torch.sin(position.float() * div_term)
22
+ pe[:, 1::2] = torch.cos(position.float() * div_term)
23
+
24
+ return pe.unsqueeze(1)
25
+
26
+
27
+ class PositionEmbedding(nn.Module):
28
+ """
29
+ Absolute pos embedding (standard), learned.
30
+ """
31
+ def __init__(self, seq_length, dim, dropout, grad=False):
32
+ super().__init__()
33
+ self.embed = nn.Parameter(data=PE1d_sincos(seq_length, dim), requires_grad=grad)
34
+ self.dropout = nn.Dropout(p=dropout)
35
+
36
+ def forward(self, x):
37
+ # x.shape: bs, seq_len, feat_dim
38
+ l = x.shape[1]
39
+ x = x.permute(1, 0, 2) + self.embed[:l].expand(x.permute(1, 0, 2).shape)
40
+ x = self.dropout(x.permute(1, 0, 2))
41
+ return x
42
+
43
+
VQ-Trans/models/quantize_cnn.py ADDED
@@ -0,0 +1,415 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+
6
+ class QuantizeEMAReset(nn.Module):
7
+ def __init__(self, nb_code, code_dim, args):
8
+ super().__init__()
9
+ self.nb_code = nb_code
10
+ self.code_dim = code_dim
11
+ self.mu = args.mu
12
+ self.reset_codebook()
13
+
14
+ def reset_codebook(self):
15
+ self.init = False
16
+ self.code_sum = None
17
+ self.code_count = None
18
+ if torch.cuda.is_available():
19
+ self.register_buffer('codebook', torch.zeros(self.nb_code, self.code_dim).cuda())
20
+ else:
21
+ self.register_buffer('codebook', torch.zeros(self.nb_code, self.code_dim))
22
+
23
+ def _tile(self, x):
24
+ nb_code_x, code_dim = x.shape
25
+ if nb_code_x < self.nb_code:
26
+ n_repeats = (self.nb_code + nb_code_x - 1) // nb_code_x
27
+ std = 0.01 / np.sqrt(code_dim)
28
+ out = x.repeat(n_repeats, 1)
29
+ out = out + torch.randn_like(out) * std
30
+ else :
31
+ out = x
32
+ return out
33
+
34
+ def init_codebook(self, x):
35
+ out = self._tile(x)
36
+ self.codebook = out[:self.nb_code]
37
+ self.code_sum = self.codebook.clone()
38
+ self.code_count = torch.ones(self.nb_code, device=self.codebook.device)
39
+ self.init = True
40
+
41
+ @torch.no_grad()
42
+ def compute_perplexity(self, code_idx) :
43
+ # Calculate new centres
44
+ code_onehot = torch.zeros(self.nb_code, code_idx.shape[0], device=code_idx.device) # nb_code, N * L
45
+ code_onehot.scatter_(0, code_idx.view(1, code_idx.shape[0]), 1)
46
+
47
+ code_count = code_onehot.sum(dim=-1) # nb_code
48
+ prob = code_count / torch.sum(code_count)
49
+ perplexity = torch.exp(-torch.sum(prob * torch.log(prob + 1e-7)))
50
+ return perplexity
51
+
52
+ @torch.no_grad()
53
+ def update_codebook(self, x, code_idx):
54
+
55
+ code_onehot = torch.zeros(self.nb_code, x.shape[0], device=x.device) # nb_code, N * L
56
+ code_onehot.scatter_(0, code_idx.view(1, x.shape[0]), 1)
57
+
58
+ code_sum = torch.matmul(code_onehot, x) # nb_code, w
59
+ code_count = code_onehot.sum(dim=-1) # nb_code
60
+
61
+ out = self._tile(x)
62
+ code_rand = out[:self.nb_code]
63
+
64
+ # Update centres
65
+ self.code_sum = self.mu * self.code_sum + (1. - self.mu) * code_sum # w, nb_code
66
+ self.code_count = self.mu * self.code_count + (1. - self.mu) * code_count # nb_code
67
+
68
+ usage = (self.code_count.view(self.nb_code, 1) >= 1.0).float()
69
+ code_update = self.code_sum.view(self.nb_code, self.code_dim) / self.code_count.view(self.nb_code, 1)
70
+
71
+ self.codebook = usage * code_update + (1 - usage) * code_rand
72
+ prob = code_count / torch.sum(code_count)
73
+ perplexity = torch.exp(-torch.sum(prob * torch.log(prob + 1e-7)))
74
+
75
+
76
+ return perplexity
77
+
78
+ def preprocess(self, x):
79
+ # NCT -> NTC -> [NT, C]
80
+ x = x.permute(0, 2, 1).contiguous()
81
+ x = x.view(-1, x.shape[-1])
82
+ return x
83
+
84
+ def quantize(self, x):
85
+ # Calculate latent code x_l
86
+ k_w = self.codebook.t()
87
+ distance = torch.sum(x ** 2, dim=-1, keepdim=True) - 2 * torch.matmul(x, k_w) + torch.sum(k_w ** 2, dim=0,
88
+ keepdim=True) # (N * L, b)
89
+ _, code_idx = torch.min(distance, dim=-1)
90
+ return code_idx
91
+
92
+ def dequantize(self, code_idx):
93
+ x = F.embedding(code_idx, self.codebook)
94
+ return x
95
+
96
+
97
+ def forward(self, x):
98
+ N, width, T = x.shape
99
+
100
+ # Preprocess
101
+ x = self.preprocess(x)
102
+
103
+ # Init codebook if not inited
104
+ if self.training and not self.init:
105
+ self.init_codebook(x)
106
+
107
+ # quantize and dequantize through bottleneck
108
+ code_idx = self.quantize(x)
109
+ x_d = self.dequantize(code_idx)
110
+
111
+ # Update embeddings
112
+ if self.training:
113
+ perplexity = self.update_codebook(x, code_idx)
114
+ else :
115
+ perplexity = self.compute_perplexity(code_idx)
116
+
117
+ # Loss
118
+ commit_loss = F.mse_loss(x, x_d.detach())
119
+
120
+ # Passthrough
121
+ x_d = x + (x_d - x).detach()
122
+
123
+ # Postprocess
124
+ x_d = x_d.view(N, T, -1).permute(0, 2, 1).contiguous() #(N, DIM, T)
125
+
126
+ return x_d, commit_loss, perplexity
127
+
128
+
129
+
130
+ class Quantizer(nn.Module):
131
+ def __init__(self, n_e, e_dim, beta):
132
+ super(Quantizer, self).__init__()
133
+
134
+ self.e_dim = e_dim
135
+ self.n_e = n_e
136
+ self.beta = beta
137
+
138
+ self.embedding = nn.Embedding(self.n_e, self.e_dim)
139
+ self.embedding.weight.data.uniform_(-1.0 / self.n_e, 1.0 / self.n_e)
140
+
141
+ def forward(self, z):
142
+
143
+ N, width, T = z.shape
144
+ z = self.preprocess(z)
145
+ assert z.shape[-1] == self.e_dim
146
+ z_flattened = z.contiguous().view(-1, self.e_dim)
147
+
148
+ # B x V
149
+ d = torch.sum(z_flattened ** 2, dim=1, keepdim=True) + \
150
+ torch.sum(self.embedding.weight**2, dim=1) - 2 * \
151
+ torch.matmul(z_flattened, self.embedding.weight.t())
152
+ # B x 1
153
+ min_encoding_indices = torch.argmin(d, dim=1)
154
+ z_q = self.embedding(min_encoding_indices).view(z.shape)
155
+
156
+ # compute loss for embedding
157
+ loss = torch.mean((z_q - z.detach())**2) + self.beta * \
158
+ torch.mean((z_q.detach() - z)**2)
159
+
160
+ # preserve gradients
161
+ z_q = z + (z_q - z).detach()
162
+ z_q = z_q.view(N, T, -1).permute(0, 2, 1).contiguous() #(N, DIM, T)
163
+
164
+ min_encodings = F.one_hot(min_encoding_indices, self.n_e).type(z.dtype)
165
+ e_mean = torch.mean(min_encodings, dim=0)
166
+ perplexity = torch.exp(-torch.sum(e_mean*torch.log(e_mean + 1e-10)))
167
+ return z_q, loss, perplexity
168
+
169
+ def quantize(self, z):
170
+
171
+ assert z.shape[-1] == self.e_dim
172
+
173
+ # B x V
174
+ d = torch.sum(z ** 2, dim=1, keepdim=True) + \
175
+ torch.sum(self.embedding.weight ** 2, dim=1) - 2 * \
176
+ torch.matmul(z, self.embedding.weight.t())
177
+ # B x 1
178
+ min_encoding_indices = torch.argmin(d, dim=1)
179
+ return min_encoding_indices
180
+
181
+ def dequantize(self, indices):
182
+
183
+ index_flattened = indices.view(-1)
184
+ z_q = self.embedding(index_flattened)
185
+ z_q = z_q.view(indices.shape + (self.e_dim, )).contiguous()
186
+ return z_q
187
+
188
+ def preprocess(self, x):
189
+ # NCT -> NTC -> [NT, C]
190
+ x = x.permute(0, 2, 1).contiguous()
191
+ x = x.view(-1, x.shape[-1])
192
+ return x
193
+
194
+
195
+
196
+ class QuantizeReset(nn.Module):
197
+ def __init__(self, nb_code, code_dim, args):
198
+ super().__init__()
199
+ self.nb_code = nb_code
200
+ self.code_dim = code_dim
201
+ self.reset_codebook()
202
+ self.codebook = nn.Parameter(torch.randn(nb_code, code_dim))
203
+
204
+ def reset_codebook(self):
205
+ self.init = False
206
+ self.code_count = None
207
+
208
+ def _tile(self, x):
209
+ nb_code_x, code_dim = x.shape
210
+ if nb_code_x < self.nb_code:
211
+ n_repeats = (self.nb_code + nb_code_x - 1) // nb_code_x
212
+ std = 0.01 / np.sqrt(code_dim)
213
+ out = x.repeat(n_repeats, 1)
214
+ out = out + torch.randn_like(out) * std
215
+ else :
216
+ out = x
217
+ return out
218
+
219
+ def init_codebook(self, x):
220
+ out = self._tile(x)
221
+ self.codebook = nn.Parameter(out[:self.nb_code])
222
+ self.code_count = torch.ones(self.nb_code, device=self.codebook.device)
223
+ self.init = True
224
+
225
+ @torch.no_grad()
226
+ def compute_perplexity(self, code_idx) :
227
+ # Calculate new centres
228
+ code_onehot = torch.zeros(self.nb_code, code_idx.shape[0], device=code_idx.device) # nb_code, N * L
229
+ code_onehot.scatter_(0, code_idx.view(1, code_idx.shape[0]), 1)
230
+
231
+ code_count = code_onehot.sum(dim=-1) # nb_code
232
+ prob = code_count / torch.sum(code_count)
233
+ perplexity = torch.exp(-torch.sum(prob * torch.log(prob + 1e-7)))
234
+ return perplexity
235
+
236
+ def update_codebook(self, x, code_idx):
237
+
238
+ code_onehot = torch.zeros(self.nb_code, x.shape[0], device=x.device) # nb_code, N * L
239
+ code_onehot.scatter_(0, code_idx.view(1, x.shape[0]), 1)
240
+
241
+ code_count = code_onehot.sum(dim=-1) # nb_code
242
+
243
+ out = self._tile(x)
244
+ code_rand = out[:self.nb_code]
245
+
246
+ # Update centres
247
+ self.code_count = code_count # nb_code
248
+ usage = (self.code_count.view(self.nb_code, 1) >= 1.0).float()
249
+
250
+ self.codebook.data = usage * self.codebook.data + (1 - usage) * code_rand
251
+ prob = code_count / torch.sum(code_count)
252
+ perplexity = torch.exp(-torch.sum(prob * torch.log(prob + 1e-7)))
253
+
254
+
255
+ return perplexity
256
+
257
+ def preprocess(self, x):
258
+ # NCT -> NTC -> [NT, C]
259
+ x = x.permute(0, 2, 1).contiguous()
260
+ x = x.view(-1, x.shape[-1])
261
+ return x
262
+
263
+ def quantize(self, x):
264
+ # Calculate latent code x_l
265
+ k_w = self.codebook.t()
266
+ distance = torch.sum(x ** 2, dim=-1, keepdim=True) - 2 * torch.matmul(x, k_w) + torch.sum(k_w ** 2, dim=0,
267
+ keepdim=True) # (N * L, b)
268
+ _, code_idx = torch.min(distance, dim=-1)
269
+ return code_idx
270
+
271
+ def dequantize(self, code_idx):
272
+ x = F.embedding(code_idx, self.codebook)
273
+ return x
274
+
275
+
276
+ def forward(self, x):
277
+ N, width, T = x.shape
278
+ # Preprocess
279
+ x = self.preprocess(x)
280
+ # Init codebook if not inited
281
+ if self.training and not self.init:
282
+ self.init_codebook(x)
283
+ # quantize and dequantize through bottleneck
284
+ code_idx = self.quantize(x)
285
+ x_d = self.dequantize(code_idx)
286
+ # Update embeddings
287
+ if self.training:
288
+ perplexity = self.update_codebook(x, code_idx)
289
+ else :
290
+ perplexity = self.compute_perplexity(code_idx)
291
+
292
+ # Loss
293
+ commit_loss = F.mse_loss(x, x_d.detach())
294
+
295
+ # Passthrough
296
+ x_d = x + (x_d - x).detach()
297
+
298
+ # Postprocess
299
+ x_d = x_d.view(N, T, -1).permute(0, 2, 1).contiguous() #(N, DIM, T)
300
+
301
+ return x_d, commit_loss, perplexity
302
+
303
+ class QuantizeEMA(nn.Module):
304
+ def __init__(self, nb_code, code_dim, args):
305
+ super().__init__()
306
+ self.nb_code = nb_code
307
+ self.code_dim = code_dim
308
+ self.mu = 0.99
309
+ self.reset_codebook()
310
+
311
+ def reset_codebook(self):
312
+ self.init = False
313
+ self.code_sum = None
314
+ self.code_count = None
315
+ self.register_buffer('codebook', torch.zeros(self.nb_code, self.code_dim).cuda())
316
+
317
+ def _tile(self, x):
318
+ nb_code_x, code_dim = x.shape
319
+ if nb_code_x < self.nb_code:
320
+ n_repeats = (self.nb_code + nb_code_x - 1) // nb_code_x
321
+ std = 0.01 / np.sqrt(code_dim)
322
+ out = x.repeat(n_repeats, 1)
323
+ out = out + torch.randn_like(out) * std
324
+ else :
325
+ out = x
326
+ return out
327
+
328
+ def init_codebook(self, x):
329
+ out = self._tile(x)
330
+ self.codebook = out[:self.nb_code]
331
+ self.code_sum = self.codebook.clone()
332
+ self.code_count = torch.ones(self.nb_code, device=self.codebook.device)
333
+ self.init = True
334
+
335
+ @torch.no_grad()
336
+ def compute_perplexity(self, code_idx) :
337
+ # Calculate new centres
338
+ code_onehot = torch.zeros(self.nb_code, code_idx.shape[0], device=code_idx.device) # nb_code, N * L
339
+ code_onehot.scatter_(0, code_idx.view(1, code_idx.shape[0]), 1)
340
+
341
+ code_count = code_onehot.sum(dim=-1) # nb_code
342
+ prob = code_count / torch.sum(code_count)
343
+ perplexity = torch.exp(-torch.sum(prob * torch.log(prob + 1e-7)))
344
+ return perplexity
345
+
346
+ @torch.no_grad()
347
+ def update_codebook(self, x, code_idx):
348
+
349
+ code_onehot = torch.zeros(self.nb_code, x.shape[0], device=x.device) # nb_code, N * L
350
+ code_onehot.scatter_(0, code_idx.view(1, x.shape[0]), 1)
351
+
352
+ code_sum = torch.matmul(code_onehot, x) # nb_code, w
353
+ code_count = code_onehot.sum(dim=-1) # nb_code
354
+
355
+ # Update centres
356
+ self.code_sum = self.mu * self.code_sum + (1. - self.mu) * code_sum # w, nb_code
357
+ self.code_count = self.mu * self.code_count + (1. - self.mu) * code_count # nb_code
358
+
359
+ code_update = self.code_sum.view(self.nb_code, self.code_dim) / self.code_count.view(self.nb_code, 1)
360
+
361
+ self.codebook = code_update
362
+ prob = code_count / torch.sum(code_count)
363
+ perplexity = torch.exp(-torch.sum(prob * torch.log(prob + 1e-7)))
364
+
365
+ return perplexity
366
+
367
+ def preprocess(self, x):
368
+ # NCT -> NTC -> [NT, C]
369
+ x = x.permute(0, 2, 1).contiguous()
370
+ x = x.view(-1, x.shape[-1])
371
+ return x
372
+
373
+ def quantize(self, x):
374
+ # Calculate latent code x_l
375
+ k_w = self.codebook.t()
376
+ distance = torch.sum(x ** 2, dim=-1, keepdim=True) - 2 * torch.matmul(x, k_w) + torch.sum(k_w ** 2, dim=0,
377
+ keepdim=True) # (N * L, b)
378
+ _, code_idx = torch.min(distance, dim=-1)
379
+ return code_idx
380
+
381
+ def dequantize(self, code_idx):
382
+ x = F.embedding(code_idx, self.codebook)
383
+ return x
384
+
385
+
386
+ def forward(self, x):
387
+ N, width, T = x.shape
388
+
389
+ # Preprocess
390
+ x = self.preprocess(x)
391
+
392
+ # Init codebook if not inited
393
+ if self.training and not self.init:
394
+ self.init_codebook(x)
395
+
396
+ # quantize and dequantize through bottleneck
397
+ code_idx = self.quantize(x)
398
+ x_d = self.dequantize(code_idx)
399
+
400
+ # Update embeddings
401
+ if self.training:
402
+ perplexity = self.update_codebook(x, code_idx)
403
+ else :
404
+ perplexity = self.compute_perplexity(code_idx)
405
+
406
+ # Loss
407
+ commit_loss = F.mse_loss(x, x_d.detach())
408
+
409
+ # Passthrough
410
+ x_d = x + (x_d - x).detach()
411
+
412
+ # Postprocess
413
+ x_d = x_d.view(N, T, -1).permute(0, 2, 1).contiguous() #(N, DIM, T)
414
+
415
+ return x_d, commit_loss, perplexity
VQ-Trans/models/resnet.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ import torch
3
+
4
+ class nonlinearity(nn.Module):
5
+ def __init__(self):
6
+ super().__init__()
7
+
8
+ def forward(self, x):
9
+ # swish
10
+ return x * torch.sigmoid(x)
11
+
12
+ class ResConv1DBlock(nn.Module):
13
+ def __init__(self, n_in, n_state, dilation=1, activation='silu', norm=None, dropout=None):
14
+ super().__init__()
15
+ padding = dilation
16
+ self.norm = norm
17
+ if norm == "LN":
18
+ self.norm1 = nn.LayerNorm(n_in)
19
+ self.norm2 = nn.LayerNorm(n_in)
20
+ elif norm == "GN":
21
+ self.norm1 = nn.GroupNorm(num_groups=32, num_channels=n_in, eps=1e-6, affine=True)
22
+ self.norm2 = nn.GroupNorm(num_groups=32, num_channels=n_in, eps=1e-6, affine=True)
23
+ elif norm == "BN":
24
+ self.norm1 = nn.BatchNorm1d(num_features=n_in, eps=1e-6, affine=True)
25
+ self.norm2 = nn.BatchNorm1d(num_features=n_in, eps=1e-6, affine=True)
26
+
27
+ else:
28
+ self.norm1 = nn.Identity()
29
+ self.norm2 = nn.Identity()
30
+
31
+ if activation == "relu":
32
+ self.activation1 = nn.ReLU()
33
+ self.activation2 = nn.ReLU()
34
+
35
+ elif activation == "silu":
36
+ self.activation1 = nonlinearity()
37
+ self.activation2 = nonlinearity()
38
+
39
+ elif activation == "gelu":
40
+ self.activation1 = nn.GELU()
41
+ self.activation2 = nn.GELU()
42
+
43
+
44
+
45
+ self.conv1 = nn.Conv1d(n_in, n_state, 3, 1, padding, dilation)
46
+ self.conv2 = nn.Conv1d(n_state, n_in, 1, 1, 0,)
47
+
48
+
49
+ def forward(self, x):
50
+ x_orig = x
51
+ if self.norm == "LN":
52
+ x = self.norm1(x.transpose(-2, -1))
53
+ x = self.activation1(x.transpose(-2, -1))
54
+ else:
55
+ x = self.norm1(x)
56
+ x = self.activation1(x)
57
+
58
+ x = self.conv1(x)
59
+
60
+ if self.norm == "LN":
61
+ x = self.norm2(x.transpose(-2, -1))
62
+ x = self.activation2(x.transpose(-2, -1))
63
+ else:
64
+ x = self.norm2(x)
65
+ x = self.activation2(x)
66
+
67
+ x = self.conv2(x)
68
+ x = x + x_orig
69
+ return x
70
+
71
+ class Resnet1D(nn.Module):
72
+ def __init__(self, n_in, n_depth, dilation_growth_rate=1, reverse_dilation=True, activation='relu', norm=None):
73
+ super().__init__()
74
+
75
+ blocks = [ResConv1DBlock(n_in, n_in, dilation=dilation_growth_rate ** depth, activation=activation, norm=norm) for depth in range(n_depth)]
76
+ if reverse_dilation:
77
+ blocks = blocks[::-1]
78
+
79
+ self.model = nn.Sequential(*blocks)
80
+
81
+ def forward(self, x):
82
+ return self.model(x)
VQ-Trans/models/rotation2xyz.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This code is based on https://github.com/Mathux/ACTOR.git
2
+ import torch
3
+ import utils.rotation_conversions as geometry
4
+
5
+
6
+ from models.smpl import SMPL, JOINTSTYPE_ROOT
7
+ # from .get_model import JOINTSTYPES
8
+ JOINTSTYPES = ["a2m", "a2mpl", "smpl", "vibe", "vertices"]
9
+
10
+
11
+ class Rotation2xyz:
12
+ def __init__(self, device, dataset='amass'):
13
+ self.device = device
14
+ self.dataset = dataset
15
+ self.smpl_model = SMPL().eval().to(device)
16
+
17
+ def __call__(self, x, mask, pose_rep, translation, glob,
18
+ jointstype, vertstrans, betas=None, beta=0,
19
+ glob_rot=None, get_rotations_back=False, **kwargs):
20
+ if pose_rep == "xyz":
21
+ return x
22
+
23
+ if mask is None:
24
+ mask = torch.ones((x.shape[0], x.shape[-1]), dtype=bool, device=x.device)
25
+
26
+ if not glob and glob_rot is None:
27
+ raise TypeError("You must specify global rotation if glob is False")
28
+
29
+ if jointstype not in JOINTSTYPES:
30
+ raise NotImplementedError("This jointstype is not implemented.")
31
+
32
+ if translation:
33
+ x_translations = x[:, -1, :3]
34
+ x_rotations = x[:, :-1]
35
+ else:
36
+ x_rotations = x
37
+
38
+ x_rotations = x_rotations.permute(0, 3, 1, 2)
39
+ nsamples, time, njoints, feats = x_rotations.shape
40
+
41
+ # Compute rotations (convert only masked sequences output)
42
+ if pose_rep == "rotvec":
43
+ rotations = geometry.axis_angle_to_matrix(x_rotations[mask])
44
+ elif pose_rep == "rotmat":
45
+ rotations = x_rotations[mask].view(-1, njoints, 3, 3)
46
+ elif pose_rep == "rotquat":
47
+ rotations = geometry.quaternion_to_matrix(x_rotations[mask])
48
+ elif pose_rep == "rot6d":
49
+ rotations = geometry.rotation_6d_to_matrix(x_rotations[mask])
50
+ else:
51
+ raise NotImplementedError("No geometry for this one.")
52
+
53
+ if not glob:
54
+ global_orient = torch.tensor(glob_rot, device=x.device)
55
+ global_orient = geometry.axis_angle_to_matrix(global_orient).view(1, 1, 3, 3)
56
+ global_orient = global_orient.repeat(len(rotations), 1, 1, 1)
57
+ else:
58
+ global_orient = rotations[:, 0]
59
+ rotations = rotations[:, 1:]
60
+
61
+ if betas is None:
62
+ betas = torch.zeros([rotations.shape[0], self.smpl_model.num_betas],
63
+ dtype=rotations.dtype, device=rotations.device)
64
+ betas[:, 1] = beta
65
+ # import ipdb; ipdb.set_trace()
66
+ out = self.smpl_model(body_pose=rotations, global_orient=global_orient, betas=betas)
67
+
68
+ # get the desirable joints
69
+ joints = out[jointstype]
70
+
71
+ x_xyz = torch.empty(nsamples, time, joints.shape[1], 3, device=x.device, dtype=x.dtype)
72
+ x_xyz[~mask] = 0
73
+ x_xyz[mask] = joints
74
+
75
+ x_xyz = x_xyz.permute(0, 2, 3, 1).contiguous()
76
+
77
+ # the first translation root at the origin on the prediction
78
+ if jointstype != "vertices":
79
+ rootindex = JOINTSTYPE_ROOT[jointstype]
80
+ x_xyz = x_xyz - x_xyz[:, [rootindex], :, :]
81
+
82
+ if translation and vertstrans:
83
+ # the first translation root at the origin
84
+ x_translations = x_translations - x_translations[:, :, [0]]
85
+
86
+ # add the translation to all the joints
87
+ x_xyz = x_xyz + x_translations[:, None, :, :]
88
+
89
+ if get_rotations_back:
90
+ return x_xyz, rotations, global_orient
91
+ else:
92
+ return x_xyz
VQ-Trans/models/smpl.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This code is based on https://github.com/Mathux/ACTOR.git
2
+ import numpy as np
3
+ import torch
4
+
5
+ import contextlib
6
+
7
+ from smplx import SMPLLayer as _SMPLLayer
8
+ from smplx.lbs import vertices2joints
9
+
10
+
11
+ # action2motion_joints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 21, 24, 38]
12
+ # change 0 and 8
13
+ action2motion_joints = [8, 1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 21, 24, 38]
14
+
15
+ from utils.config import SMPL_MODEL_PATH, JOINT_REGRESSOR_TRAIN_EXTRA
16
+
17
+ JOINTSTYPE_ROOT = {"a2m": 0, # action2motion
18
+ "smpl": 0,
19
+ "a2mpl": 0, # set(smpl, a2m)
20
+ "vibe": 8} # 0 is the 8 position: OP MidHip below
21
+
22
+ JOINT_MAP = {
23
+ 'OP Nose': 24, 'OP Neck': 12, 'OP RShoulder': 17,
24
+ 'OP RElbow': 19, 'OP RWrist': 21, 'OP LShoulder': 16,
25
+ 'OP LElbow': 18, 'OP LWrist': 20, 'OP MidHip': 0,
26
+ 'OP RHip': 2, 'OP RKnee': 5, 'OP RAnkle': 8,
27
+ 'OP LHip': 1, 'OP LKnee': 4, 'OP LAnkle': 7,
28
+ 'OP REye': 25, 'OP LEye': 26, 'OP REar': 27,
29
+ 'OP LEar': 28, 'OP LBigToe': 29, 'OP LSmallToe': 30,
30
+ 'OP LHeel': 31, 'OP RBigToe': 32, 'OP RSmallToe': 33, 'OP RHeel': 34,
31
+ 'Right Ankle': 8, 'Right Knee': 5, 'Right Hip': 45,
32
+ 'Left Hip': 46, 'Left Knee': 4, 'Left Ankle': 7,
33
+ 'Right Wrist': 21, 'Right Elbow': 19, 'Right Shoulder': 17,
34
+ 'Left Shoulder': 16, 'Left Elbow': 18, 'Left Wrist': 20,
35
+ 'Neck (LSP)': 47, 'Top of Head (LSP)': 48,
36
+ 'Pelvis (MPII)': 49, 'Thorax (MPII)': 50,
37
+ 'Spine (H36M)': 51, 'Jaw (H36M)': 52,
38
+ 'Head (H36M)': 53, 'Nose': 24, 'Left Eye': 26,
39
+ 'Right Eye': 25, 'Left Ear': 28, 'Right Ear': 27
40
+ }
41
+
42
+ JOINT_NAMES = [
43
+ 'OP Nose', 'OP Neck', 'OP RShoulder',
44
+ 'OP RElbow', 'OP RWrist', 'OP LShoulder',
45
+ 'OP LElbow', 'OP LWrist', 'OP MidHip',
46
+ 'OP RHip', 'OP RKnee', 'OP RAnkle',
47
+ 'OP LHip', 'OP LKnee', 'OP LAnkle',
48
+ 'OP REye', 'OP LEye', 'OP REar',
49
+ 'OP LEar', 'OP LBigToe', 'OP LSmallToe',
50
+ 'OP LHeel', 'OP RBigToe', 'OP RSmallToe', 'OP RHeel',
51
+ 'Right Ankle', 'Right Knee', 'Right Hip',
52
+ 'Left Hip', 'Left Knee', 'Left Ankle',
53
+ 'Right Wrist', 'Right Elbow', 'Right Shoulder',
54
+ 'Left Shoulder', 'Left Elbow', 'Left Wrist',
55
+ 'Neck (LSP)', 'Top of Head (LSP)',
56
+ 'Pelvis (MPII)', 'Thorax (MPII)',
57
+ 'Spine (H36M)', 'Jaw (H36M)',
58
+ 'Head (H36M)', 'Nose', 'Left Eye',
59
+ 'Right Eye', 'Left Ear', 'Right Ear'
60
+ ]
61
+
62
+
63
+ # adapted from VIBE/SPIN to output smpl_joints, vibe joints and action2motion joints
64
+ class SMPL(_SMPLLayer):
65
+ """ Extension of the official SMPL implementation to support more joints """
66
+
67
+ def __init__(self, model_path=SMPL_MODEL_PATH, **kwargs):
68
+ kwargs["model_path"] = model_path
69
+
70
+ # remove the verbosity for the 10-shapes beta parameters
71
+ with contextlib.redirect_stdout(None):
72
+ super(SMPL, self).__init__(**kwargs)
73
+
74
+ J_regressor_extra = np.load(JOINT_REGRESSOR_TRAIN_EXTRA)
75
+ self.register_buffer('J_regressor_extra', torch.tensor(J_regressor_extra, dtype=torch.float32))
76
+ vibe_indexes = np.array([JOINT_MAP[i] for i in JOINT_NAMES])
77
+ a2m_indexes = vibe_indexes[action2motion_joints]
78
+ smpl_indexes = np.arange(24)
79
+ a2mpl_indexes = np.unique(np.r_[smpl_indexes, a2m_indexes])
80
+
81
+ self.maps = {"vibe": vibe_indexes,
82
+ "a2m": a2m_indexes,
83
+ "smpl": smpl_indexes,
84
+ "a2mpl": a2mpl_indexes}
85
+
86
+ def forward(self, *args, **kwargs):
87
+ smpl_output = super(SMPL, self).forward(*args, **kwargs)
88
+
89
+ extra_joints = vertices2joints(self.J_regressor_extra, smpl_output.vertices)
90
+ all_joints = torch.cat([smpl_output.joints, extra_joints], dim=1)
91
+
92
+ output = {"vertices": smpl_output.vertices}
93
+
94
+ for joinstype, indexes in self.maps.items():
95
+ output[joinstype] = all_joints[:, indexes]
96
+
97
+ return output
VQ-Trans/models/t2m_trans.py ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import torch
3
+ import torch.nn as nn
4
+ from torch.nn import functional as F
5
+ from torch.distributions import Categorical
6
+ import models.pos_encoding as pos_encoding
7
+
8
+ class Text2Motion_Transformer(nn.Module):
9
+
10
+ def __init__(self,
11
+ num_vq=1024,
12
+ embed_dim=512,
13
+ clip_dim=512,
14
+ block_size=16,
15
+ num_layers=2,
16
+ n_head=8,
17
+ drop_out_rate=0.1,
18
+ fc_rate=4):
19
+ super().__init__()
20
+ self.trans_base = CrossCondTransBase(num_vq, embed_dim, clip_dim, block_size, num_layers, n_head, drop_out_rate, fc_rate)
21
+ self.trans_head = CrossCondTransHead(num_vq, embed_dim, block_size, num_layers, n_head, drop_out_rate, fc_rate)
22
+ self.block_size = block_size
23
+ self.num_vq = num_vq
24
+
25
+ def get_block_size(self):
26
+ return self.block_size
27
+
28
+ def forward(self, idxs, clip_feature):
29
+ feat = self.trans_base(idxs, clip_feature)
30
+ logits = self.trans_head(feat)
31
+ return logits
32
+
33
+ def sample(self, clip_feature, if_categorial=False):
34
+ for k in range(self.block_size):
35
+ if k == 0:
36
+ x = []
37
+ else:
38
+ x = xs
39
+ logits = self.forward(x, clip_feature)
40
+ logits = logits[:, -1, :]
41
+ probs = F.softmax(logits, dim=-1)
42
+ if if_categorial:
43
+ dist = Categorical(probs)
44
+ idx = dist.sample()
45
+ if idx == self.num_vq:
46
+ break
47
+ idx = idx.unsqueeze(-1)
48
+ else:
49
+ _, idx = torch.topk(probs, k=1, dim=-1)
50
+ if idx[0] == self.num_vq:
51
+ break
52
+ # append to the sequence and continue
53
+ if k == 0:
54
+ xs = idx
55
+ else:
56
+ xs = torch.cat((xs, idx), dim=1)
57
+
58
+ if k == self.block_size - 1:
59
+ return xs[:, :-1]
60
+ return xs
61
+
62
+ class CausalCrossConditionalSelfAttention(nn.Module):
63
+
64
+ def __init__(self, embed_dim=512, block_size=16, n_head=8, drop_out_rate=0.1):
65
+ super().__init__()
66
+ assert embed_dim % 8 == 0
67
+ # key, query, value projections for all heads
68
+ self.key = nn.Linear(embed_dim, embed_dim)
69
+ self.query = nn.Linear(embed_dim, embed_dim)
70
+ self.value = nn.Linear(embed_dim, embed_dim)
71
+
72
+ self.attn_drop = nn.Dropout(drop_out_rate)
73
+ self.resid_drop = nn.Dropout(drop_out_rate)
74
+
75
+ self.proj = nn.Linear(embed_dim, embed_dim)
76
+ # causal mask to ensure that attention is only applied to the left in the input sequence
77
+ self.register_buffer("mask", torch.tril(torch.ones(block_size, block_size)).view(1, 1, block_size, block_size))
78
+ self.n_head = n_head
79
+
80
+ def forward(self, x):
81
+ B, T, C = x.size()
82
+
83
+ # calculate query, key, values for all heads in batch and move head forward to be the batch dim
84
+ k = self.key(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
85
+ q = self.query(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
86
+ v = self.value(x).view(B, T, self.n_head, C // self.n_head).transpose(1, 2) # (B, nh, T, hs)
87
+ # causal self-attention; Self-attend: (B, nh, T, hs) x (B, nh, hs, T) -> (B, nh, T, T)
88
+ att = (q @ k.transpose(-2, -1)) * (1.0 / math.sqrt(k.size(-1)))
89
+ att = att.masked_fill(self.mask[:,:,:T,:T] == 0, float('-inf'))
90
+ att = F.softmax(att, dim=-1)
91
+ att = self.attn_drop(att)
92
+ y = att @ v # (B, nh, T, T) x (B, nh, T, hs) -> (B, nh, T, hs)
93
+ y = y.transpose(1, 2).contiguous().view(B, T, C) # re-assemble all head outputs side by side
94
+
95
+ # output projection
96
+ y = self.resid_drop(self.proj(y))
97
+ return y
98
+
99
+ class Block(nn.Module):
100
+
101
+ def __init__(self, embed_dim=512, block_size=16, n_head=8, drop_out_rate=0.1, fc_rate=4):
102
+ super().__init__()
103
+ self.ln1 = nn.LayerNorm(embed_dim)
104
+ self.ln2 = nn.LayerNorm(embed_dim)
105
+ self.attn = CausalCrossConditionalSelfAttention(embed_dim, block_size, n_head, drop_out_rate)
106
+ self.mlp = nn.Sequential(
107
+ nn.Linear(embed_dim, fc_rate * embed_dim),
108
+ nn.GELU(),
109
+ nn.Linear(fc_rate * embed_dim, embed_dim),
110
+ nn.Dropout(drop_out_rate),
111
+ )
112
+
113
+ def forward(self, x):
114
+ x = x + self.attn(self.ln1(x))
115
+ x = x + self.mlp(self.ln2(x))
116
+ return x
117
+
118
+ class CrossCondTransBase(nn.Module):
119
+
120
+ def __init__(self,
121
+ num_vq=1024,
122
+ embed_dim=512,
123
+ clip_dim=512,
124
+ block_size=16,
125
+ num_layers=2,
126
+ n_head=8,
127
+ drop_out_rate=0.1,
128
+ fc_rate=4):
129
+ super().__init__()
130
+ self.tok_emb = nn.Embedding(num_vq + 2, embed_dim)
131
+ self.cond_emb = nn.Linear(clip_dim, embed_dim)
132
+ self.pos_embedding = nn.Embedding(block_size, embed_dim)
133
+ self.drop = nn.Dropout(drop_out_rate)
134
+ # transformer block
135
+ self.blocks = nn.Sequential(*[Block(embed_dim, block_size, n_head, drop_out_rate, fc_rate) for _ in range(num_layers)])
136
+ self.pos_embed = pos_encoding.PositionEmbedding(block_size, embed_dim, 0.0, False)
137
+
138
+ self.block_size = block_size
139
+
140
+ self.apply(self._init_weights)
141
+
142
+ def get_block_size(self):
143
+ return self.block_size
144
+
145
+ def _init_weights(self, module):
146
+ if isinstance(module, (nn.Linear, nn.Embedding)):
147
+ module.weight.data.normal_(mean=0.0, std=0.02)
148
+ if isinstance(module, nn.Linear) and module.bias is not None:
149
+ module.bias.data.zero_()
150
+ elif isinstance(module, nn.LayerNorm):
151
+ module.bias.data.zero_()
152
+ module.weight.data.fill_(1.0)
153
+
154
+ def forward(self, idx, clip_feature):
155
+ if len(idx) == 0:
156
+ token_embeddings = self.cond_emb(clip_feature).unsqueeze(1)
157
+ else:
158
+ b, t = idx.size()
159
+ assert t <= self.block_size, "Cannot forward, model block size is exhausted."
160
+ # forward the Trans model
161
+ token_embeddings = self.tok_emb(idx)
162
+ token_embeddings = torch.cat([self.cond_emb(clip_feature).unsqueeze(1), token_embeddings], dim=1)
163
+
164
+ x = self.pos_embed(token_embeddings)
165
+ x = self.blocks(x)
166
+
167
+ return x
168
+
169
+
170
+ class CrossCondTransHead(nn.Module):
171
+
172
+ def __init__(self,
173
+ num_vq=1024,
174
+ embed_dim=512,
175
+ block_size=16,
176
+ num_layers=2,
177
+ n_head=8,
178
+ drop_out_rate=0.1,
179
+ fc_rate=4):
180
+ super().__init__()
181
+
182
+ self.blocks = nn.Sequential(*[Block(embed_dim, block_size, n_head, drop_out_rate, fc_rate) for _ in range(num_layers)])
183
+ self.ln_f = nn.LayerNorm(embed_dim)
184
+ self.head = nn.Linear(embed_dim, num_vq + 1, bias=False)
185
+ self.block_size = block_size
186
+
187
+ self.apply(self._init_weights)
188
+
189
+ def get_block_size(self):
190
+ return self.block_size
191
+
192
+ def _init_weights(self, module):
193
+ if isinstance(module, (nn.Linear, nn.Embedding)):
194
+ module.weight.data.normal_(mean=0.0, std=0.02)
195
+ if isinstance(module, nn.Linear) and module.bias is not None:
196
+ module.bias.data.zero_()
197
+ elif isinstance(module, nn.LayerNorm):
198
+ module.bias.data.zero_()
199
+ module.weight.data.fill_(1.0)
200
+
201
+ def forward(self, x):
202
+ x = self.blocks(x)
203
+ x = self.ln_f(x)
204
+ logits = self.head(x)
205
+ return logits
206
+
207
+
208
+
209
+
210
+
211
+
VQ-Trans/models/vqvae.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ from models.encdec import Encoder, Decoder
3
+ from models.quantize_cnn import QuantizeEMAReset, Quantizer, QuantizeEMA, QuantizeReset
4
+
5
+
6
+ class VQVAE_251(nn.Module):
7
+ def __init__(self,
8
+ args,
9
+ nb_code=1024,
10
+ code_dim=512,
11
+ output_emb_width=512,
12
+ down_t=3,
13
+ stride_t=2,
14
+ width=512,
15
+ depth=3,
16
+ dilation_growth_rate=3,
17
+ activation='relu',
18
+ norm=None):
19
+
20
+ super().__init__()
21
+ self.code_dim = code_dim
22
+ self.num_code = nb_code
23
+ self.quant = args.quantizer
24
+ self.encoder = Encoder(251 if args.dataname == 'kit' else 263, output_emb_width, down_t, stride_t, width, depth, dilation_growth_rate, activation=activation, norm=norm)
25
+ self.decoder = Decoder(251 if args.dataname == 'kit' else 263, output_emb_width, down_t, stride_t, width, depth, dilation_growth_rate, activation=activation, norm=norm)
26
+ if args.quantizer == "ema_reset":
27
+ self.quantizer = QuantizeEMAReset(nb_code, code_dim, args)
28
+ elif args.quantizer == "orig":
29
+ self.quantizer = Quantizer(nb_code, code_dim, 1.0)
30
+ elif args.quantizer == "ema":
31
+ self.quantizer = QuantizeEMA(nb_code, code_dim, args)
32
+ elif args.quantizer == "reset":
33
+ self.quantizer = QuantizeReset(nb_code, code_dim, args)
34
+
35
+
36
+ def preprocess(self, x):
37
+ # (bs, T, Jx3) -> (bs, Jx3, T)
38
+ x = x.permute(0,2,1).float()
39
+ return x
40
+
41
+
42
+ def postprocess(self, x):
43
+ # (bs, Jx3, T) -> (bs, T, Jx3)
44
+ x = x.permute(0,2,1)
45
+ return x
46
+
47
+
48
+ def encode(self, x):
49
+ N, T, _ = x.shape
50
+ x_in = self.preprocess(x)
51
+ x_encoder = self.encoder(x_in)
52
+ x_encoder = self.postprocess(x_encoder)
53
+ x_encoder = x_encoder.contiguous().view(-1, x_encoder.shape[-1]) # (NT, C)
54
+ code_idx = self.quantizer.quantize(x_encoder)
55
+ code_idx = code_idx.view(N, -1)
56
+ return code_idx
57
+
58
+
59
+ def forward(self, x):
60
+
61
+ x_in = self.preprocess(x)
62
+ # Encode
63
+ x_encoder = self.encoder(x_in)
64
+
65
+ ## quantization
66
+ x_quantized, loss, perplexity = self.quantizer(x_encoder)
67
+
68
+ ## decoder
69
+ x_decoder = self.decoder(x_quantized)
70
+ x_out = self.postprocess(x_decoder)
71
+ return x_out, loss, perplexity
72
+
73
+
74
+ def forward_decoder(self, x):
75
+ x_d = self.quantizer.dequantize(x)
76
+ x_d = x_d.view(1, -1, self.code_dim).permute(0, 2, 1).contiguous()
77
+
78
+ # decoder
79
+ x_decoder = self.decoder(x_d)
80
+ x_out = self.postprocess(x_decoder)
81
+ return x_out
82
+
83
+
84
+
85
+ class HumanVQVAE(nn.Module):
86
+ def __init__(self,
87
+ args,
88
+ nb_code=512,
89
+ code_dim=512,
90
+ output_emb_width=512,
91
+ down_t=3,
92
+ stride_t=2,
93
+ width=512,
94
+ depth=3,
95
+ dilation_growth_rate=3,
96
+ activation='relu',
97
+ norm=None):
98
+
99
+ super().__init__()
100
+
101
+ self.nb_joints = 21 if args.dataname == 'kit' else 22
102
+ self.vqvae = VQVAE_251(args, nb_code, code_dim, output_emb_width, down_t, stride_t, width, depth, dilation_growth_rate, activation=activation, norm=norm)
103
+
104
+ def encode(self, x):
105
+ b, t, c = x.size()
106
+ quants = self.vqvae.encode(x) # (N, T)
107
+ return quants
108
+
109
+ def forward(self, x):
110
+
111
+ x_out, loss, perplexity = self.vqvae(x)
112
+
113
+ return x_out, loss, perplexity
114
+
115
+ def forward_decoder(self, x):
116
+ x_out = self.vqvae.forward_decoder(x)
117
+ return x_out
118
+
VQ-Trans/options/get_eval_option.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from argparse import Namespace
2
+ import re
3
+ from os.path import join as pjoin
4
+
5
+
6
+ def is_float(numStr):
7
+ flag = False
8
+ numStr = str(numStr).strip().lstrip('-').lstrip('+')
9
+ try:
10
+ reg = re.compile(r'^[-+]?[0-9]+\.[0-9]+$')
11
+ res = reg.match(str(numStr))
12
+ if res:
13
+ flag = True
14
+ except Exception as ex:
15
+ print("is_float() - error: " + str(ex))
16
+ return flag
17
+
18
+
19
+ def is_number(numStr):
20
+ flag = False
21
+ numStr = str(numStr).strip().lstrip('-').lstrip('+')
22
+ if str(numStr).isdigit():
23
+ flag = True
24
+ return flag
25
+
26
+
27
+ def get_opt(opt_path, device):
28
+ opt = Namespace()
29
+ opt_dict = vars(opt)
30
+
31
+ skip = ('-------------- End ----------------',
32
+ '------------ Options -------------',
33
+ '\n')
34
+ print('Reading', opt_path)
35
+ with open(opt_path) as f:
36
+ for line in f:
37
+ if line.strip() not in skip:
38
+ # print(line.strip())
39
+ key, value = line.strip().split(': ')
40
+ if value in ('True', 'False'):
41
+ opt_dict[key] = (value == 'True')
42
+ # print(key, value)
43
+ elif is_float(value):
44
+ opt_dict[key] = float(value)
45
+ elif is_number(value):
46
+ opt_dict[key] = int(value)
47
+ else:
48
+ opt_dict[key] = str(value)
49
+
50
+ # print(opt)
51
+ opt_dict['which_epoch'] = 'finest'
52
+ opt.save_root = pjoin(opt.checkpoints_dir, opt.dataset_name, opt.name)
53
+ opt.model_dir = pjoin(opt.save_root, 'model')
54
+ opt.meta_dir = pjoin(opt.save_root, 'meta')
55
+
56
+ if opt.dataset_name == 't2m':
57
+ opt.data_root = './dataset/HumanML3D/'
58
+ opt.motion_dir = pjoin(opt.data_root, 'new_joint_vecs')
59
+ opt.text_dir = pjoin(opt.data_root, 'texts')
60
+ opt.joints_num = 22
61
+ opt.dim_pose = 263
62
+ opt.max_motion_length = 196
63
+ opt.max_motion_frame = 196
64
+ opt.max_motion_token = 55
65
+ elif opt.dataset_name == 'kit':
66
+ opt.data_root = './dataset/KIT-ML/'
67
+ opt.motion_dir = pjoin(opt.data_root, 'new_joint_vecs')
68
+ opt.text_dir = pjoin(opt.data_root, 'texts')
69
+ opt.joints_num = 21
70
+ opt.dim_pose = 251
71
+ opt.max_motion_length = 196
72
+ opt.max_motion_frame = 196
73
+ opt.max_motion_token = 55
74
+ else:
75
+ raise KeyError('Dataset not recognized')
76
+
77
+ opt.dim_word = 300
78
+ opt.num_classes = 200 // opt.unit_length
79
+ opt.is_train = False
80
+ opt.is_continue = False
81
+ opt.device = device
82
+
83
+ return opt
VQ-Trans/options/option_transformer.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+
3
+ def get_args_parser():
4
+ parser = argparse.ArgumentParser(description='Optimal Transport AutoEncoder training for Amass',
5
+ add_help=True,
6
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter)
7
+
8
+ ## dataloader
9
+
10
+ parser.add_argument('--dataname', type=str, default='kit', help='dataset directory')
11
+ parser.add_argument('--batch-size', default=128, type=int, help='batch size')
12
+ parser.add_argument('--fps', default=[20], nargs="+", type=int, help='frames per second')
13
+ parser.add_argument('--seq-len', type=int, default=64, help='training motion length')
14
+
15
+ ## optimization
16
+ parser.add_argument('--total-iter', default=100000, type=int, help='number of total iterations to run')
17
+ parser.add_argument('--warm-up-iter', default=1000, type=int, help='number of total iterations for warmup')
18
+ parser.add_argument('--lr', default=2e-4, type=float, help='max learning rate')
19
+ parser.add_argument('--lr-scheduler', default=[60000], nargs="+", type=int, help="learning rate schedule (iterations)")
20
+ parser.add_argument('--gamma', default=0.05, type=float, help="learning rate decay")
21
+
22
+ parser.add_argument('--weight-decay', default=1e-6, type=float, help='weight decay')
23
+ parser.add_argument('--decay-option',default='all', type=str, choices=['all', 'noVQ'], help='disable weight decay on codebook')
24
+ parser.add_argument('--optimizer',default='adamw', type=str, choices=['adam', 'adamw'], help='disable weight decay on codebook')
25
+
26
+ ## vqvae arch
27
+ parser.add_argument("--code-dim", type=int, default=512, help="embedding dimension")
28
+ parser.add_argument("--nb-code", type=int, default=512, help="nb of embedding")
29
+ parser.add_argument("--mu", type=float, default=0.99, help="exponential moving average to update the codebook")
30
+ parser.add_argument("--down-t", type=int, default=3, help="downsampling rate")
31
+ parser.add_argument("--stride-t", type=int, default=2, help="stride size")
32
+ parser.add_argument("--width", type=int, default=512, help="width of the network")
33
+ parser.add_argument("--depth", type=int, default=3, help="depth of the network")
34
+ parser.add_argument("--dilation-growth-rate", type=int, default=3, help="dilation growth rate")
35
+ parser.add_argument("--output-emb-width", type=int, default=512, help="output embedding width")
36
+ parser.add_argument('--vq-act', type=str, default='relu', choices = ['relu', 'silu', 'gelu'], help='dataset directory')
37
+
38
+ ## gpt arch
39
+ parser.add_argument("--block-size", type=int, default=25, help="seq len")
40
+ parser.add_argument("--embed-dim-gpt", type=int, default=512, help="embedding dimension")
41
+ parser.add_argument("--clip-dim", type=int, default=512, help="latent dimension in the clip feature")
42
+ parser.add_argument("--num-layers", type=int, default=2, help="nb of transformer layers")
43
+ parser.add_argument("--n-head-gpt", type=int, default=8, help="nb of heads")
44
+ parser.add_argument("--ff-rate", type=int, default=4, help="feedforward size")
45
+ parser.add_argument("--drop-out-rate", type=float, default=0.1, help="dropout ratio in the pos encoding")
46
+
47
+ ## quantizer
48
+ parser.add_argument("--quantizer", type=str, default='ema_reset', choices = ['ema', 'orig', 'ema_reset', 'reset'], help="eps for optimal transport")
49
+ parser.add_argument('--quantbeta', type=float, default=1.0, help='dataset directory')
50
+
51
+ ## resume
52
+ parser.add_argument("--resume-pth", type=str, default=None, help='resume vq pth')
53
+ parser.add_argument("--resume-trans", type=str, default=None, help='resume gpt pth')
54
+
55
+
56
+ ## output directory
57
+ parser.add_argument('--out-dir', type=str, default='output_GPT_Final/', help='output directory')
58
+ parser.add_argument('--exp-name', type=str, default='exp_debug', help='name of the experiment, will create a file inside out-dir')
59
+ parser.add_argument('--vq-name', type=str, default='exp_debug', help='name of the generated dataset .npy, will create a file inside out-dir')
60
+ ## other
61
+ parser.add_argument('--print-iter', default=200, type=int, help='print frequency')
62
+ parser.add_argument('--eval-iter', default=5000, type=int, help='evaluation frequency')
63
+ parser.add_argument('--seed', default=123, type=int, help='seed for initializing training. ')
64
+ parser.add_argument("--if-maxtest", action='store_true', help="test in max")
65
+ parser.add_argument('--pkeep', type=float, default=1.0, help='keep rate for gpt training')
66
+
67
+
68
+ return parser.parse_args()
VQ-Trans/options/option_vq.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+
3
+ def get_args_parser():
4
+ parser = argparse.ArgumentParser(description='Optimal Transport AutoEncoder training for AIST',
5
+ add_help=True,
6
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter)
7
+
8
+ ## dataloader
9
+ parser.add_argument('--dataname', type=str, default='kit', help='dataset directory')
10
+ parser.add_argument('--batch-size', default=128, type=int, help='batch size')
11
+ parser.add_argument('--window-size', type=int, default=64, help='training motion length')
12
+
13
+ ## optimization
14
+ parser.add_argument('--total-iter', default=200000, type=int, help='number of total iterations to run')
15
+ parser.add_argument('--warm-up-iter', default=1000, type=int, help='number of total iterations for warmup')
16
+ parser.add_argument('--lr', default=2e-4, type=float, help='max learning rate')
17
+ parser.add_argument('--lr-scheduler', default=[50000, 400000], nargs="+", type=int, help="learning rate schedule (iterations)")
18
+ parser.add_argument('--gamma', default=0.05, type=float, help="learning rate decay")
19
+
20
+ parser.add_argument('--weight-decay', default=0.0, type=float, help='weight decay')
21
+ parser.add_argument("--commit", type=float, default=0.02, help="hyper-parameter for the commitment loss")
22
+ parser.add_argument('--loss-vel', type=float, default=0.1, help='hyper-parameter for the velocity loss')
23
+ parser.add_argument('--recons-loss', type=str, default='l2', help='reconstruction loss')
24
+
25
+ ## vqvae arch
26
+ parser.add_argument("--code-dim", type=int, default=512, help="embedding dimension")
27
+ parser.add_argument("--nb-code", type=int, default=512, help="nb of embedding")
28
+ parser.add_argument("--mu", type=float, default=0.99, help="exponential moving average to update the codebook")
29
+ parser.add_argument("--down-t", type=int, default=2, help="downsampling rate")
30
+ parser.add_argument("--stride-t", type=int, default=2, help="stride size")
31
+ parser.add_argument("--width", type=int, default=512, help="width of the network")
32
+ parser.add_argument("--depth", type=int, default=3, help="depth of the network")
33
+ parser.add_argument("--dilation-growth-rate", type=int, default=3, help="dilation growth rate")
34
+ parser.add_argument("--output-emb-width", type=int, default=512, help="output embedding width")
35
+ parser.add_argument('--vq-act', type=str, default='relu', choices = ['relu', 'silu', 'gelu'], help='dataset directory')
36
+ parser.add_argument('--vq-norm', type=str, default=None, help='dataset directory')
37
+
38
+ ## quantizer
39
+ parser.add_argument("--quantizer", type=str, default='ema_reset', choices = ['ema', 'orig', 'ema_reset', 'reset'], help="eps for optimal transport")
40
+ parser.add_argument('--beta', type=float, default=1.0, help='commitment loss in standard VQ')
41
+
42
+ ## resume
43
+ parser.add_argument("--resume-pth", type=str, default=None, help='resume pth for VQ')
44
+ parser.add_argument("--resume-gpt", type=str, default=None, help='resume pth for GPT')
45
+
46
+
47
+ ## output directory
48
+ parser.add_argument('--out-dir', type=str, default='output_vqfinal/', help='output directory')
49
+ parser.add_argument('--results-dir', type=str, default='visual_results/', help='output directory')
50
+ parser.add_argument('--visual-name', type=str, default='baseline', help='output directory')
51
+ parser.add_argument('--exp-name', type=str, default='exp_debug', help='name of the experiment, will create a file inside out-dir')
52
+ ## other
53
+ parser.add_argument('--print-iter', default=200, type=int, help='print frequency')
54
+ parser.add_argument('--eval-iter', default=1000, type=int, help='evaluation frequency')
55
+ parser.add_argument('--seed', default=123, type=int, help='seed for initializing training.')
56
+
57
+ parser.add_argument('--vis-gt', action='store_true', help='whether visualize GT motions')
58
+ parser.add_argument('--nb-vis', default=20, type=int, help='nb of visualizations')
59
+
60
+
61
+ return parser.parse_args()
VQ-Trans/output/23cb7d0e26bb1646b3d386331971449c_pred.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e05998e1d4ac1eebcba89ec989112b6fcdb55c8cceeef2faea7cb564a381525
3
+ size 16206213
VQ-Trans/output/90dd3007b93da07eca7527c836b4d6d0_pred.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a24b9eecef62fc4a498c45e9342b265304191b9b0bd0f673bf3e029a6524bedd
3
+ size 16206213
VQ-Trans/output/c3785325ba8f17ce7427b43d49903e51_pred.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3513af257c3ffa7b2168f02288a9d3349648efbfba4dcf88e00e9bca1e850855
3
+ size 10584005
VQ-Trans/pyrender ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit a59963ef890891656fd17c90e12d663233dcaa99
VQ-Trans/render_final.py ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from models.rotation2xyz import Rotation2xyz
2
+ import numpy as np
3
+ from trimesh import Trimesh
4
+ import os
5
+ os.environ['PYOPENGL_PLATFORM'] = "osmesa"
6
+
7
+ import torch
8
+ from visualize.simplify_loc2rot import joints2smpl
9
+ import pyrender
10
+ import matplotlib.pyplot as plt
11
+
12
+ import io
13
+ import imageio
14
+ from shapely import geometry
15
+ import trimesh
16
+ from pyrender.constants import RenderFlags
17
+ import math
18
+ # import ffmpeg
19
+ from PIL import Image
20
+
21
+ class WeakPerspectiveCamera(pyrender.Camera):
22
+ def __init__(self,
23
+ scale,
24
+ translation,
25
+ znear=pyrender.camera.DEFAULT_Z_NEAR,
26
+ zfar=None,
27
+ name=None):
28
+ super(WeakPerspectiveCamera, self).__init__(
29
+ znear=znear,
30
+ zfar=zfar,
31
+ name=name,
32
+ )
33
+ self.scale = scale
34
+ self.translation = translation
35
+
36
+ def get_projection_matrix(self, width=None, height=None):
37
+ P = np.eye(4)
38
+ P[0, 0] = self.scale[0]
39
+ P[1, 1] = self.scale[1]
40
+ P[0, 3] = self.translation[0] * self.scale[0]
41
+ P[1, 3] = -self.translation[1] * self.scale[1]
42
+ P[2, 2] = -1
43
+ return P
44
+
45
+ def render(motions, outdir='test_vis', device_id=0, name=None, pred=True):
46
+ frames, njoints, nfeats = motions.shape
47
+ MINS = motions.min(axis=0).min(axis=0)
48
+ MAXS = motions.max(axis=0).max(axis=0)
49
+
50
+ height_offset = MINS[1]
51
+ motions[:, :, 1] -= height_offset
52
+ trajec = motions[:, 0, [0, 2]]
53
+
54
+ j2s = joints2smpl(num_frames=frames, device_id=0, cuda=True)
55
+ rot2xyz = Rotation2xyz(device=torch.device("cuda:0"))
56
+ faces = rot2xyz.smpl_model.faces
57
+
58
+ if (not os.path.exists(outdir + name+'_pred.pt') and pred) or (not os.path.exists(outdir + name+'_gt.pt') and not pred):
59
+ print(f'Running SMPLify, it may take a few minutes.')
60
+ motion_tensor, opt_dict = j2s.joint2smpl(motions) # [nframes, njoints, 3]
61
+
62
+ vertices = rot2xyz(torch.tensor(motion_tensor).clone(), mask=None,
63
+ pose_rep='rot6d', translation=True, glob=True,
64
+ jointstype='vertices',
65
+ vertstrans=True)
66
+
67
+ if pred:
68
+ torch.save(vertices, outdir + name+'_pred.pt')
69
+ else:
70
+ torch.save(vertices, outdir + name+'_gt.pt')
71
+ else:
72
+ if pred:
73
+ vertices = torch.load(outdir + name+'_pred.pt')
74
+ else:
75
+ vertices = torch.load(outdir + name+'_gt.pt')
76
+ frames = vertices.shape[3] # shape: 1, nb_frames, 3, nb_joints
77
+ print (vertices.shape)
78
+ MINS = torch.min(torch.min(vertices[0], axis=0)[0], axis=1)[0]
79
+ MAXS = torch.max(torch.max(vertices[0], axis=0)[0], axis=1)[0]
80
+ # vertices[:,:,1,:] -= MINS[1] + 1e-5
81
+
82
+
83
+ out_list = []
84
+
85
+ minx = MINS[0] - 0.5
86
+ maxx = MAXS[0] + 0.5
87
+ minz = MINS[2] - 0.5
88
+ maxz = MAXS[2] + 0.5
89
+ polygon = geometry.Polygon([[minx, minz], [minx, maxz], [maxx, maxz], [maxx, minz]])
90
+ polygon_mesh = trimesh.creation.extrude_polygon(polygon, 1e-5)
91
+
92
+ vid = []
93
+ for i in range(frames):
94
+ if i % 10 == 0:
95
+ print(i)
96
+
97
+ mesh = Trimesh(vertices=vertices[0, :, :, i].squeeze().tolist(), faces=faces)
98
+
99
+ base_color = (0.11, 0.53, 0.8, 0.5)
100
+ ## OPAQUE rendering without alpha
101
+ ## BLEND rendering consider alpha
102
+ material = pyrender.MetallicRoughnessMaterial(
103
+ metallicFactor=0.7,
104
+ alphaMode='OPAQUE',
105
+ baseColorFactor=base_color
106
+ )
107
+
108
+
109
+ mesh = pyrender.Mesh.from_trimesh(mesh, material=material)
110
+
111
+ polygon_mesh.visual.face_colors = [0, 0, 0, 0.21]
112
+ polygon_render = pyrender.Mesh.from_trimesh(polygon_mesh, smooth=False)
113
+
114
+ bg_color = [1, 1, 1, 0.8]
115
+ scene = pyrender.Scene(bg_color=bg_color, ambient_light=(0.4, 0.4, 0.4))
116
+
117
+ sx, sy, tx, ty = [0.75, 0.75, 0, 0.10]
118
+
119
+ camera = pyrender.PerspectiveCamera(yfov=(np.pi / 3.0))
120
+
121
+ light = pyrender.DirectionalLight(color=[1,1,1], intensity=300)
122
+
123
+ scene.add(mesh)
124
+
125
+ c = np.pi / 2
126
+
127
+ scene.add(polygon_render, pose=np.array([[ 1, 0, 0, 0],
128
+
129
+ [ 0, np.cos(c), -np.sin(c), MINS[1].cpu().numpy()],
130
+
131
+ [ 0, np.sin(c), np.cos(c), 0],
132
+
133
+ [ 0, 0, 0, 1]]))
134
+
135
+ light_pose = np.eye(4)
136
+ light_pose[:3, 3] = [0, -1, 1]
137
+ scene.add(light, pose=light_pose.copy())
138
+
139
+ light_pose[:3, 3] = [0, 1, 1]
140
+ scene.add(light, pose=light_pose.copy())
141
+
142
+ light_pose[:3, 3] = [1, 1, 2]
143
+ scene.add(light, pose=light_pose.copy())
144
+
145
+
146
+ c = -np.pi / 6
147
+
148
+ scene.add(camera, pose=[[ 1, 0, 0, (minx+maxx).cpu().numpy()/2],
149
+
150
+ [ 0, np.cos(c), -np.sin(c), 1.5],
151
+
152
+ [ 0, np.sin(c), np.cos(c), max(4, minz.cpu().numpy()+(1.5-MINS[1].cpu().numpy())*2, (maxx-minx).cpu().numpy())],
153
+
154
+ [ 0, 0, 0, 1]
155
+ ])
156
+
157
+ # render scene
158
+ r = pyrender.OffscreenRenderer(960, 960)
159
+
160
+ color, _ = r.render(scene, flags=RenderFlags.RGBA)
161
+ # Image.fromarray(color).save(outdir+'/'+name+'_'+str(i)+'.png')
162
+
163
+ vid.append(color)
164
+
165
+ r.delete()
166
+
167
+ out = np.stack(vid, axis=0)
168
+ if pred:
169
+ imageio.mimsave(outdir + name+'_pred.gif', out, fps=20)
170
+ else:
171
+ imageio.mimsave(outdir + name+'_gt.gif', out, fps=20)
172
+
173
+
174
+
175
+
176
+
177
+ if __name__ == "__main__":
178
+ import argparse
179
+ parser = argparse.ArgumentParser()
180
+ parser.add_argument("--filedir", type=str, default=None, help='motion npy file dir')
181
+ parser.add_argument('--motion-list', default=None, nargs="+", type=str, help="motion name list")
182
+ args = parser.parse_args()
183
+
184
+ filename_list = args.motion_list
185
+ filedir = args.filedir
186
+
187
+ for filename in filename_list:
188
+ motions = np.load(filedir + filename+'_pred.npy')
189
+ print('pred', motions.shape, filename)
190
+ render(motions[0], outdir=filedir, device_id=0, name=filename, pred=True)
191
+
192
+ motions = np.load(filedir + filename+'_gt.npy')
193
+ print('gt', motions.shape, filename)
194
+ render(motions[0], outdir=filedir, device_id=0, name=filename, pred=False)
VQ-Trans/train_t2m_trans.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import numpy as np
4
+
5
+ from torch.utils.tensorboard import SummaryWriter
6
+ from os.path import join as pjoin
7
+ from torch.distributions import Categorical
8
+ import json
9
+ import clip
10
+
11
+ import options.option_transformer as option_trans
12
+ import models.vqvae as vqvae
13
+ import utils.utils_model as utils_model
14
+ import utils.eval_trans as eval_trans
15
+ from dataset import dataset_TM_train
16
+ from dataset import dataset_TM_eval
17
+ from dataset import dataset_tokenize
18
+ import models.t2m_trans as trans
19
+ from options.get_eval_option import get_opt
20
+ from models.evaluator_wrapper import EvaluatorModelWrapper
21
+ import warnings
22
+ warnings.filterwarnings('ignore')
23
+
24
+ ##### ---- Exp dirs ---- #####
25
+ args = option_trans.get_args_parser()
26
+ torch.manual_seed(args.seed)
27
+
28
+ args.out_dir = os.path.join(args.out_dir, f'{args.exp_name}')
29
+ args.vq_dir= os.path.join("./dataset/KIT-ML" if args.dataname == 'kit' else "./dataset/HumanML3D", f'{args.vq_name}')
30
+ os.makedirs(args.out_dir, exist_ok = True)
31
+ os.makedirs(args.vq_dir, exist_ok = True)
32
+
33
+ ##### ---- Logger ---- #####
34
+ logger = utils_model.get_logger(args.out_dir)
35
+ writer = SummaryWriter(args.out_dir)
36
+ logger.info(json.dumps(vars(args), indent=4, sort_keys=True))
37
+
38
+ ##### ---- Dataloader ---- #####
39
+ train_loader_token = dataset_tokenize.DATALoader(args.dataname, 1, unit_length=2**args.down_t)
40
+
41
+ from utils.word_vectorizer import WordVectorizer
42
+ w_vectorizer = WordVectorizer('./glove', 'our_vab')
43
+ val_loader = dataset_TM_eval.DATALoader(args.dataname, False, 32, w_vectorizer)
44
+
45
+ dataset_opt_path = 'checkpoints/kit/Comp_v6_KLD005/opt.txt' if args.dataname == 'kit' else 'checkpoints/t2m/Comp_v6_KLD005/opt.txt'
46
+
47
+ wrapper_opt = get_opt(dataset_opt_path, torch.device('cuda'))
48
+ eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
49
+
50
+ ##### ---- Network ---- #####
51
+ clip_model, clip_preprocess = clip.load("ViT-B/32", device=torch.device('cuda'), jit=False, download_root='/apdcephfs_cq2/share_1290939/maelyszhang/.cache/clip') # Must set jit=False for training
52
+ clip.model.convert_weights(clip_model) # Actually this line is unnecessary since clip by default already on float16
53
+ clip_model.eval()
54
+ for p in clip_model.parameters():
55
+ p.requires_grad = False
56
+
57
+ net = vqvae.HumanVQVAE(args, ## use args to define different parameters in different quantizers
58
+ args.nb_code,
59
+ args.code_dim,
60
+ args.output_emb_width,
61
+ args.down_t,
62
+ args.stride_t,
63
+ args.width,
64
+ args.depth,
65
+ args.dilation_growth_rate)
66
+
67
+
68
+ trans_encoder = trans.Text2Motion_Transformer(num_vq=args.nb_code,
69
+ embed_dim=args.embed_dim_gpt,
70
+ clip_dim=args.clip_dim,
71
+ block_size=args.block_size,
72
+ num_layers=args.num_layers,
73
+ n_head=args.n_head_gpt,
74
+ drop_out_rate=args.drop_out_rate,
75
+ fc_rate=args.ff_rate)
76
+
77
+
78
+ print ('loading checkpoint from {}'.format(args.resume_pth))
79
+ ckpt = torch.load(args.resume_pth, map_location='cpu')
80
+ net.load_state_dict(ckpt['net'], strict=True)
81
+ net.eval()
82
+ net.cuda()
83
+
84
+ if args.resume_trans is not None:
85
+ print ('loading transformer checkpoint from {}'.format(args.resume_trans))
86
+ ckpt = torch.load(args.resume_trans, map_location='cpu')
87
+ trans_encoder.load_state_dict(ckpt['trans'], strict=True)
88
+ trans_encoder.train()
89
+ trans_encoder.cuda()
90
+
91
+ ##### ---- Optimizer & Scheduler ---- #####
92
+ optimizer = utils_model.initial_optim(args.decay_option, args.lr, args.weight_decay, trans_encoder, args.optimizer)
93
+ scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=args.lr_scheduler, gamma=args.gamma)
94
+
95
+ ##### ---- Optimization goals ---- #####
96
+ loss_ce = torch.nn.CrossEntropyLoss()
97
+
98
+ nb_iter, avg_loss_cls, avg_acc = 0, 0., 0.
99
+ right_num = 0
100
+ nb_sample_train = 0
101
+
102
+ ##### ---- get code ---- #####
103
+ for batch in train_loader_token:
104
+ pose, name = batch
105
+ bs, seq = pose.shape[0], pose.shape[1]
106
+
107
+ pose = pose.cuda().float() # bs, nb_joints, joints_dim, seq_len
108
+ target = net.encode(pose)
109
+ target = target.cpu().numpy()
110
+ np.save(pjoin(args.vq_dir, name[0] +'.npy'), target)
111
+
112
+
113
+ train_loader = dataset_TM_train.DATALoader(args.dataname, args.batch_size, args.nb_code, args.vq_name, unit_length=2**args.down_t)
114
+ train_loader_iter = dataset_TM_train.cycle(train_loader)
115
+
116
+
117
+ ##### ---- Training ---- #####
118
+ best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger = eval_trans.evaluation_transformer(args.out_dir, val_loader, net, trans_encoder, logger, writer, 0, best_fid=1000, best_iter=0, best_div=100, best_top1=0, best_top2=0, best_top3=0, best_matching=100, clip_model=clip_model, eval_wrapper=eval_wrapper)
119
+ while nb_iter <= args.total_iter:
120
+
121
+ batch = next(train_loader_iter)
122
+ clip_text, m_tokens, m_tokens_len = batch
123
+ m_tokens, m_tokens_len = m_tokens.cuda(), m_tokens_len.cuda()
124
+ bs = m_tokens.shape[0]
125
+ target = m_tokens # (bs, 26)
126
+ target = target.cuda()
127
+
128
+ text = clip.tokenize(clip_text, truncate=True).cuda()
129
+
130
+ feat_clip_text = clip_model.encode_text(text).float()
131
+
132
+ input_index = target[:,:-1]
133
+
134
+ if args.pkeep == -1:
135
+ proba = np.random.rand(1)[0]
136
+ mask = torch.bernoulli(proba * torch.ones(input_index.shape,
137
+ device=input_index.device))
138
+ else:
139
+ mask = torch.bernoulli(args.pkeep * torch.ones(input_index.shape,
140
+ device=input_index.device))
141
+ mask = mask.round().to(dtype=torch.int64)
142
+ r_indices = torch.randint_like(input_index, args.nb_code)
143
+ a_indices = mask*input_index+(1-mask)*r_indices
144
+
145
+ cls_pred = trans_encoder(a_indices, feat_clip_text)
146
+ cls_pred = cls_pred.contiguous()
147
+
148
+ loss_cls = 0.0
149
+ for i in range(bs):
150
+ # loss function (26), (26, 513)
151
+ loss_cls += loss_ce(cls_pred[i][:m_tokens_len[i] + 1], target[i][:m_tokens_len[i] + 1]) / bs
152
+
153
+ # Accuracy
154
+ probs = torch.softmax(cls_pred[i][:m_tokens_len[i] + 1], dim=-1)
155
+
156
+ if args.if_maxtest:
157
+ _, cls_pred_index = torch.max(probs, dim=-1)
158
+
159
+ else:
160
+ dist = Categorical(probs)
161
+ cls_pred_index = dist.sample()
162
+ right_num += (cls_pred_index.flatten(0) == target[i][:m_tokens_len[i] + 1].flatten(0)).sum().item()
163
+
164
+ ## global loss
165
+ optimizer.zero_grad()
166
+ loss_cls.backward()
167
+ optimizer.step()
168
+ scheduler.step()
169
+
170
+ avg_loss_cls = avg_loss_cls + loss_cls.item()
171
+ nb_sample_train = nb_sample_train + (m_tokens_len + 1).sum().item()
172
+
173
+ nb_iter += 1
174
+ if nb_iter % args.print_iter == 0 :
175
+ avg_loss_cls = avg_loss_cls / args.print_iter
176
+ avg_acc = right_num * 100 / nb_sample_train
177
+ writer.add_scalar('./Loss/train', avg_loss_cls, nb_iter)
178
+ writer.add_scalar('./ACC/train', avg_acc, nb_iter)
179
+ msg = f"Train. Iter {nb_iter} : Loss. {avg_loss_cls:.5f}, ACC. {avg_acc:.4f}"
180
+ logger.info(msg)
181
+ avg_loss_cls = 0.
182
+ right_num = 0
183
+ nb_sample_train = 0
184
+
185
+ if nb_iter % args.eval_iter == 0:
186
+ best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger = eval_trans.evaluation_transformer(args.out_dir, val_loader, net, trans_encoder, logger, writer, nb_iter, best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, clip_model=clip_model, eval_wrapper=eval_wrapper)
187
+
188
+ if nb_iter == args.total_iter:
189
+ msg_final = f"Train. Iter {best_iter} : FID. {best_fid:.5f}, Diversity. {best_div:.4f}, TOP1. {best_top1:.4f}, TOP2. {best_top2:.4f}, TOP3. {best_top3:.4f}"
190
+ logger.info(msg_final)
191
+ break
VQ-Trans/train_vq.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+
4
+ import torch
5
+ import torch.optim as optim
6
+ from torch.utils.tensorboard import SummaryWriter
7
+
8
+ import models.vqvae as vqvae
9
+ import utils.losses as losses
10
+ import options.option_vq as option_vq
11
+ import utils.utils_model as utils_model
12
+ from dataset import dataset_VQ, dataset_TM_eval
13
+ import utils.eval_trans as eval_trans
14
+ from options.get_eval_option import get_opt
15
+ from models.evaluator_wrapper import EvaluatorModelWrapper
16
+ import warnings
17
+ warnings.filterwarnings('ignore')
18
+ from utils.word_vectorizer import WordVectorizer
19
+
20
+ def update_lr_warm_up(optimizer, nb_iter, warm_up_iter, lr):
21
+
22
+ current_lr = lr * (nb_iter + 1) / (warm_up_iter + 1)
23
+ for param_group in optimizer.param_groups:
24
+ param_group["lr"] = current_lr
25
+
26
+ return optimizer, current_lr
27
+
28
+ ##### ---- Exp dirs ---- #####
29
+ args = option_vq.get_args_parser()
30
+ torch.manual_seed(args.seed)
31
+
32
+ args.out_dir = os.path.join(args.out_dir, f'{args.exp_name}')
33
+ os.makedirs(args.out_dir, exist_ok = True)
34
+
35
+ ##### ---- Logger ---- #####
36
+ logger = utils_model.get_logger(args.out_dir)
37
+ writer = SummaryWriter(args.out_dir)
38
+ logger.info(json.dumps(vars(args), indent=4, sort_keys=True))
39
+
40
+
41
+
42
+ w_vectorizer = WordVectorizer('./glove', 'our_vab')
43
+
44
+ if args.dataname == 'kit' :
45
+ dataset_opt_path = 'checkpoints/kit/Comp_v6_KLD005/opt.txt'
46
+ args.nb_joints = 21
47
+
48
+ else :
49
+ dataset_opt_path = 'checkpoints/t2m/Comp_v6_KLD005/opt.txt'
50
+ args.nb_joints = 22
51
+
52
+ logger.info(f'Training on {args.dataname}, motions are with {args.nb_joints} joints')
53
+
54
+ wrapper_opt = get_opt(dataset_opt_path, torch.device('cuda'))
55
+ eval_wrapper = EvaluatorModelWrapper(wrapper_opt)
56
+
57
+
58
+ ##### ---- Dataloader ---- #####
59
+ train_loader = dataset_VQ.DATALoader(args.dataname,
60
+ args.batch_size,
61
+ window_size=args.window_size,
62
+ unit_length=2**args.down_t)
63
+
64
+ train_loader_iter = dataset_VQ.cycle(train_loader)
65
+
66
+ val_loader = dataset_TM_eval.DATALoader(args.dataname, False,
67
+ 32,
68
+ w_vectorizer,
69
+ unit_length=2**args.down_t)
70
+
71
+ ##### ---- Network ---- #####
72
+ net = vqvae.HumanVQVAE(args, ## use args to define different parameters in different quantizers
73
+ args.nb_code,
74
+ args.code_dim,
75
+ args.output_emb_width,
76
+ args.down_t,
77
+ args.stride_t,
78
+ args.width,
79
+ args.depth,
80
+ args.dilation_growth_rate,
81
+ args.vq_act,
82
+ args.vq_norm)
83
+
84
+
85
+ if args.resume_pth :
86
+ logger.info('loading checkpoint from {}'.format(args.resume_pth))
87
+ ckpt = torch.load(args.resume_pth, map_location='cpu')
88
+ net.load_state_dict(ckpt['net'], strict=True)
89
+ net.train()
90
+ net.cuda()
91
+
92
+ ##### ---- Optimizer & Scheduler ---- #####
93
+ optimizer = optim.AdamW(net.parameters(), lr=args.lr, betas=(0.9, 0.99), weight_decay=args.weight_decay)
94
+ scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=args.lr_scheduler, gamma=args.gamma)
95
+
96
+
97
+ Loss = losses.ReConsLoss(args.recons_loss, args.nb_joints)
98
+
99
+ ##### ------ warm-up ------- #####
100
+ avg_recons, avg_perplexity, avg_commit = 0., 0., 0.
101
+
102
+ for nb_iter in range(1, args.warm_up_iter):
103
+
104
+ optimizer, current_lr = update_lr_warm_up(optimizer, nb_iter, args.warm_up_iter, args.lr)
105
+
106
+ gt_motion = next(train_loader_iter)
107
+ gt_motion = gt_motion.cuda().float() # (bs, 64, dim)
108
+
109
+ pred_motion, loss_commit, perplexity = net(gt_motion)
110
+ loss_motion = Loss(pred_motion, gt_motion)
111
+ loss_vel = Loss.forward_vel(pred_motion, gt_motion)
112
+
113
+ loss = loss_motion + args.commit * loss_commit + args.loss_vel * loss_vel
114
+
115
+ optimizer.zero_grad()
116
+ loss.backward()
117
+ optimizer.step()
118
+
119
+ avg_recons += loss_motion.item()
120
+ avg_perplexity += perplexity.item()
121
+ avg_commit += loss_commit.item()
122
+
123
+ if nb_iter % args.print_iter == 0 :
124
+ avg_recons /= args.print_iter
125
+ avg_perplexity /= args.print_iter
126
+ avg_commit /= args.print_iter
127
+
128
+ logger.info(f"Warmup. Iter {nb_iter} : lr {current_lr:.5f} \t Commit. {avg_commit:.5f} \t PPL. {avg_perplexity:.2f} \t Recons. {avg_recons:.5f}")
129
+
130
+ avg_recons, avg_perplexity, avg_commit = 0., 0., 0.
131
+
132
+ ##### ---- Training ---- #####
133
+ avg_recons, avg_perplexity, avg_commit = 0., 0., 0.
134
+ best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger = eval_trans.evaluation_vqvae(args.out_dir, val_loader, net, logger, writer, 0, best_fid=1000, best_iter=0, best_div=100, best_top1=0, best_top2=0, best_top3=0, best_matching=100, eval_wrapper=eval_wrapper)
135
+
136
+ for nb_iter in range(1, args.total_iter + 1):
137
+
138
+ gt_motion = next(train_loader_iter)
139
+ gt_motion = gt_motion.cuda().float() # bs, nb_joints, joints_dim, seq_len
140
+
141
+ pred_motion, loss_commit, perplexity = net(gt_motion)
142
+ loss_motion = Loss(pred_motion, gt_motion)
143
+ loss_vel = Loss.forward_vel(pred_motion, gt_motion)
144
+
145
+ loss = loss_motion + args.commit * loss_commit + args.loss_vel * loss_vel
146
+
147
+ optimizer.zero_grad()
148
+ loss.backward()
149
+ optimizer.step()
150
+ scheduler.step()
151
+
152
+ avg_recons += loss_motion.item()
153
+ avg_perplexity += perplexity.item()
154
+ avg_commit += loss_commit.item()
155
+
156
+ if nb_iter % args.print_iter == 0 :
157
+ avg_recons /= args.print_iter
158
+ avg_perplexity /= args.print_iter
159
+ avg_commit /= args.print_iter
160
+
161
+ writer.add_scalar('./Train/L1', avg_recons, nb_iter)
162
+ writer.add_scalar('./Train/PPL', avg_perplexity, nb_iter)
163
+ writer.add_scalar('./Train/Commit', avg_commit, nb_iter)
164
+
165
+ logger.info(f"Train. Iter {nb_iter} : \t Commit. {avg_commit:.5f} \t PPL. {avg_perplexity:.2f} \t Recons. {avg_recons:.5f}")
166
+
167
+ avg_recons, avg_perplexity, avg_commit = 0., 0., 0.,
168
+
169
+ if nb_iter % args.eval_iter==0 :
170
+ best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger = eval_trans.evaluation_vqvae(args.out_dir, val_loader, net, logger, writer, nb_iter, best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, eval_wrapper=eval_wrapper)
171
+
VQ-Trans/utils/config.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ SMPL_DATA_PATH = "./body_models/smpl"
4
+
5
+ SMPL_KINTREE_PATH = os.path.join(SMPL_DATA_PATH, "kintree_table.pkl")
6
+ SMPL_MODEL_PATH = os.path.join(SMPL_DATA_PATH, "SMPL_NEUTRAL.pkl")
7
+ JOINT_REGRESSOR_TRAIN_EXTRA = os.path.join(SMPL_DATA_PATH, 'J_regressor_extra.npy')
8
+
9
+ ROT_CONVENTION_TO_ROT_NUMBER = {
10
+ 'legacy': 23,
11
+ 'no_hands': 21,
12
+ 'full_hands': 51,
13
+ 'mitten_hands': 33,
14
+ }
15
+
16
+ GENDERS = ['neutral', 'male', 'female']
17
+ NUM_BETAS = 10
VQ-Trans/utils/eval_trans.py ADDED
@@ -0,0 +1,580 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import clip
4
+ import numpy as np
5
+ import torch
6
+ from scipy import linalg
7
+
8
+ import visualization.plot_3d_global as plot_3d
9
+ from utils.motion_process import recover_from_ric
10
+
11
+
12
+ def tensorborad_add_video_xyz(writer, xyz, nb_iter, tag, nb_vis=4, title_batch=None, outname=None):
13
+ xyz = xyz[:1]
14
+ bs, seq = xyz.shape[:2]
15
+ xyz = xyz.reshape(bs, seq, -1, 3)
16
+ plot_xyz = plot_3d.draw_to_batch(xyz.cpu().numpy(),title_batch, outname)
17
+ plot_xyz =np.transpose(plot_xyz, (0, 1, 4, 2, 3))
18
+ writer.add_video(tag, plot_xyz, nb_iter, fps = 20)
19
+
20
+ @torch.no_grad()
21
+ def evaluation_vqvae(out_dir, val_loader, net, logger, writer, nb_iter, best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, eval_wrapper, draw = True, save = True, savegif=False, savenpy=False) :
22
+ net.eval()
23
+ nb_sample = 0
24
+
25
+ draw_org = []
26
+ draw_pred = []
27
+ draw_text = []
28
+
29
+
30
+ motion_annotation_list = []
31
+ motion_pred_list = []
32
+
33
+ R_precision_real = 0
34
+ R_precision = 0
35
+
36
+ nb_sample = 0
37
+ matching_score_real = 0
38
+ matching_score_pred = 0
39
+ for batch in val_loader:
40
+ word_embeddings, pos_one_hots, caption, sent_len, motion, m_length, token, name = batch
41
+
42
+ motion = motion.cuda()
43
+ et, em = eval_wrapper.get_co_embeddings(word_embeddings, pos_one_hots, sent_len, motion, m_length)
44
+ bs, seq = motion.shape[0], motion.shape[1]
45
+
46
+ num_joints = 21 if motion.shape[-1] == 251 else 22
47
+
48
+ pred_pose_eval = torch.zeros((bs, seq, motion.shape[-1])).cuda()
49
+
50
+ for i in range(bs):
51
+ pose = val_loader.dataset.inv_transform(motion[i:i+1, :m_length[i], :].detach().cpu().numpy())
52
+ pose_xyz = recover_from_ric(torch.from_numpy(pose).float().cuda(), num_joints)
53
+
54
+
55
+ pred_pose, loss_commit, perplexity = net(motion[i:i+1, :m_length[i]])
56
+ pred_denorm = val_loader.dataset.inv_transform(pred_pose.detach().cpu().numpy())
57
+ pred_xyz = recover_from_ric(torch.from_numpy(pred_denorm).float().cuda(), num_joints)
58
+
59
+ if savenpy:
60
+ np.save(os.path.join(out_dir, name[i]+'_gt.npy'), pose_xyz[:, :m_length[i]].cpu().numpy())
61
+ np.save(os.path.join(out_dir, name[i]+'_pred.npy'), pred_xyz.detach().cpu().numpy())
62
+
63
+ pred_pose_eval[i:i+1,:m_length[i],:] = pred_pose
64
+
65
+ if i < min(4, bs):
66
+ draw_org.append(pose_xyz)
67
+ draw_pred.append(pred_xyz)
68
+ draw_text.append(caption[i])
69
+
70
+ et_pred, em_pred = eval_wrapper.get_co_embeddings(word_embeddings, pos_one_hots, sent_len, pred_pose_eval, m_length)
71
+
72
+ motion_pred_list.append(em_pred)
73
+ motion_annotation_list.append(em)
74
+
75
+ temp_R, temp_match = calculate_R_precision(et.cpu().numpy(), em.cpu().numpy(), top_k=3, sum_all=True)
76
+ R_precision_real += temp_R
77
+ matching_score_real += temp_match
78
+ temp_R, temp_match = calculate_R_precision(et_pred.cpu().numpy(), em_pred.cpu().numpy(), top_k=3, sum_all=True)
79
+ R_precision += temp_R
80
+ matching_score_pred += temp_match
81
+
82
+ nb_sample += bs
83
+
84
+ motion_annotation_np = torch.cat(motion_annotation_list, dim=0).cpu().numpy()
85
+ motion_pred_np = torch.cat(motion_pred_list, dim=0).cpu().numpy()
86
+ gt_mu, gt_cov = calculate_activation_statistics(motion_annotation_np)
87
+ mu, cov= calculate_activation_statistics(motion_pred_np)
88
+
89
+ diversity_real = calculate_diversity(motion_annotation_np, 300 if nb_sample > 300 else 100)
90
+ diversity = calculate_diversity(motion_pred_np, 300 if nb_sample > 300 else 100)
91
+
92
+ R_precision_real = R_precision_real / nb_sample
93
+ R_precision = R_precision / nb_sample
94
+
95
+ matching_score_real = matching_score_real / nb_sample
96
+ matching_score_pred = matching_score_pred / nb_sample
97
+
98
+ fid = calculate_frechet_distance(gt_mu, gt_cov, mu, cov)
99
+
100
+ msg = f"--> \t Eva. Iter {nb_iter} :, FID. {fid:.4f}, Diversity Real. {diversity_real:.4f}, Diversity. {diversity:.4f}, R_precision_real. {R_precision_real}, R_precision. {R_precision}, matching_score_real. {matching_score_real}, matching_score_pred. {matching_score_pred}"
101
+ logger.info(msg)
102
+
103
+ if draw:
104
+ writer.add_scalar('./Test/FID', fid, nb_iter)
105
+ writer.add_scalar('./Test/Diversity', diversity, nb_iter)
106
+ writer.add_scalar('./Test/top1', R_precision[0], nb_iter)
107
+ writer.add_scalar('./Test/top2', R_precision[1], nb_iter)
108
+ writer.add_scalar('./Test/top3', R_precision[2], nb_iter)
109
+ writer.add_scalar('./Test/matching_score', matching_score_pred, nb_iter)
110
+
111
+
112
+ if nb_iter % 5000 == 0 :
113
+ for ii in range(4):
114
+ tensorborad_add_video_xyz(writer, draw_org[ii], nb_iter, tag='./Vis/org_eval'+str(ii), nb_vis=1, title_batch=[draw_text[ii]], outname=[os.path.join(out_dir, 'gt'+str(ii)+'.gif')] if savegif else None)
115
+
116
+ if nb_iter % 5000 == 0 :
117
+ for ii in range(4):
118
+ tensorborad_add_video_xyz(writer, draw_pred[ii], nb_iter, tag='./Vis/pred_eval'+str(ii), nb_vis=1, title_batch=[draw_text[ii]], outname=[os.path.join(out_dir, 'pred'+str(ii)+'.gif')] if savegif else None)
119
+
120
+
121
+ if fid < best_fid :
122
+ msg = f"--> --> \t FID Improved from {best_fid:.5f} to {fid:.5f} !!!"
123
+ logger.info(msg)
124
+ best_fid, best_iter = fid, nb_iter
125
+ if save:
126
+ torch.save({'net' : net.state_dict()}, os.path.join(out_dir, 'net_best_fid.pth'))
127
+
128
+ if abs(diversity_real - diversity) < abs(diversity_real - best_div) :
129
+ msg = f"--> --> \t Diversity Improved from {best_div:.5f} to {diversity:.5f} !!!"
130
+ logger.info(msg)
131
+ best_div = diversity
132
+ if save:
133
+ torch.save({'net' : net.state_dict()}, os.path.join(out_dir, 'net_best_div.pth'))
134
+
135
+ if R_precision[0] > best_top1 :
136
+ msg = f"--> --> \t Top1 Improved from {best_top1:.4f} to {R_precision[0]:.4f} !!!"
137
+ logger.info(msg)
138
+ best_top1 = R_precision[0]
139
+ if save:
140
+ torch.save({'net' : net.state_dict()}, os.path.join(out_dir, 'net_best_top1.pth'))
141
+
142
+ if R_precision[1] > best_top2 :
143
+ msg = f"--> --> \t Top2 Improved from {best_top2:.4f} to {R_precision[1]:.4f} !!!"
144
+ logger.info(msg)
145
+ best_top2 = R_precision[1]
146
+
147
+ if R_precision[2] > best_top3 :
148
+ msg = f"--> --> \t Top3 Improved from {best_top3:.4f} to {R_precision[2]:.4f} !!!"
149
+ logger.info(msg)
150
+ best_top3 = R_precision[2]
151
+
152
+ if matching_score_pred < best_matching :
153
+ msg = f"--> --> \t matching_score Improved from {best_matching:.5f} to {matching_score_pred:.5f} !!!"
154
+ logger.info(msg)
155
+ best_matching = matching_score_pred
156
+ if save:
157
+ torch.save({'net' : net.state_dict()}, os.path.join(out_dir, 'net_best_matching.pth'))
158
+
159
+ if save:
160
+ torch.save({'net' : net.state_dict()}, os.path.join(out_dir, 'net_last.pth'))
161
+
162
+ net.train()
163
+ return best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger
164
+
165
+
166
+ @torch.no_grad()
167
+ def evaluation_transformer(out_dir, val_loader, net, trans, logger, writer, nb_iter, best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, clip_model, eval_wrapper, draw = True, save = True, savegif=False) :
168
+
169
+ trans.eval()
170
+ nb_sample = 0
171
+
172
+ draw_org = []
173
+ draw_pred = []
174
+ draw_text = []
175
+ draw_text_pred = []
176
+
177
+ motion_annotation_list = []
178
+ motion_pred_list = []
179
+ R_precision_real = 0
180
+ R_precision = 0
181
+ matching_score_real = 0
182
+ matching_score_pred = 0
183
+
184
+ nb_sample = 0
185
+ for i in range(1):
186
+ for batch in val_loader:
187
+ word_embeddings, pos_one_hots, clip_text, sent_len, pose, m_length, token, name = batch
188
+
189
+ bs, seq = pose.shape[:2]
190
+ num_joints = 21 if pose.shape[-1] == 251 else 22
191
+
192
+ text = clip.tokenize(clip_text, truncate=True).cuda()
193
+
194
+ feat_clip_text = clip_model.encode_text(text).float()
195
+ pred_pose_eval = torch.zeros((bs, seq, pose.shape[-1])).cuda()
196
+ pred_len = torch.ones(bs).long()
197
+
198
+ for k in range(bs):
199
+ try:
200
+ index_motion = trans.sample(feat_clip_text[k:k+1], False)
201
+ except:
202
+ index_motion = torch.ones(1,1).cuda().long()
203
+
204
+ pred_pose = net.forward_decoder(index_motion)
205
+ cur_len = pred_pose.shape[1]
206
+
207
+ pred_len[k] = min(cur_len, seq)
208
+ pred_pose_eval[k:k+1, :cur_len] = pred_pose[:, :seq]
209
+
210
+ if draw:
211
+ pred_denorm = val_loader.dataset.inv_transform(pred_pose.detach().cpu().numpy())
212
+ pred_xyz = recover_from_ric(torch.from_numpy(pred_denorm).float().cuda(), num_joints)
213
+
214
+ if i == 0 and k < 4:
215
+ draw_pred.append(pred_xyz)
216
+ draw_text_pred.append(clip_text[k])
217
+
218
+ et_pred, em_pred = eval_wrapper.get_co_embeddings(word_embeddings, pos_one_hots, sent_len, pred_pose_eval, pred_len)
219
+
220
+ if i == 0:
221
+ pose = pose.cuda().float()
222
+
223
+ et, em = eval_wrapper.get_co_embeddings(word_embeddings, pos_one_hots, sent_len, pose, m_length)
224
+ motion_annotation_list.append(em)
225
+ motion_pred_list.append(em_pred)
226
+
227
+ if draw:
228
+ pose = val_loader.dataset.inv_transform(pose.detach().cpu().numpy())
229
+ pose_xyz = recover_from_ric(torch.from_numpy(pose).float().cuda(), num_joints)
230
+
231
+
232
+ for j in range(min(4, bs)):
233
+ draw_org.append(pose_xyz[j][:m_length[j]].unsqueeze(0))
234
+ draw_text.append(clip_text[j])
235
+
236
+ temp_R, temp_match = calculate_R_precision(et.cpu().numpy(), em.cpu().numpy(), top_k=3, sum_all=True)
237
+ R_precision_real += temp_R
238
+ matching_score_real += temp_match
239
+ temp_R, temp_match = calculate_R_precision(et_pred.cpu().numpy(), em_pred.cpu().numpy(), top_k=3, sum_all=True)
240
+ R_precision += temp_R
241
+ matching_score_pred += temp_match
242
+
243
+ nb_sample += bs
244
+
245
+ motion_annotation_np = torch.cat(motion_annotation_list, dim=0).cpu().numpy()
246
+ motion_pred_np = torch.cat(motion_pred_list, dim=0).cpu().numpy()
247
+ gt_mu, gt_cov = calculate_activation_statistics(motion_annotation_np)
248
+ mu, cov= calculate_activation_statistics(motion_pred_np)
249
+
250
+ diversity_real = calculate_diversity(motion_annotation_np, 300 if nb_sample > 300 else 100)
251
+ diversity = calculate_diversity(motion_pred_np, 300 if nb_sample > 300 else 100)
252
+
253
+ R_precision_real = R_precision_real / nb_sample
254
+ R_precision = R_precision / nb_sample
255
+
256
+ matching_score_real = matching_score_real / nb_sample
257
+ matching_score_pred = matching_score_pred / nb_sample
258
+
259
+
260
+ fid = calculate_frechet_distance(gt_mu, gt_cov, mu, cov)
261
+
262
+ msg = f"--> \t Eva. Iter {nb_iter} :, FID. {fid:.4f}, Diversity Real. {diversity_real:.4f}, Diversity. {diversity:.4f}, R_precision_real. {R_precision_real}, R_precision. {R_precision}, matching_score_real. {matching_score_real}, matching_score_pred. {matching_score_pred}"
263
+ logger.info(msg)
264
+
265
+
266
+ if draw:
267
+ writer.add_scalar('./Test/FID', fid, nb_iter)
268
+ writer.add_scalar('./Test/Diversity', diversity, nb_iter)
269
+ writer.add_scalar('./Test/top1', R_precision[0], nb_iter)
270
+ writer.add_scalar('./Test/top2', R_precision[1], nb_iter)
271
+ writer.add_scalar('./Test/top3', R_precision[2], nb_iter)
272
+ writer.add_scalar('./Test/matching_score', matching_score_pred, nb_iter)
273
+
274
+
275
+ if nb_iter % 10000 == 0 :
276
+ for ii in range(4):
277
+ tensorborad_add_video_xyz(writer, draw_org[ii], nb_iter, tag='./Vis/org_eval'+str(ii), nb_vis=1, title_batch=[draw_text[ii]], outname=[os.path.join(out_dir, 'gt'+str(ii)+'.gif')] if savegif else None)
278
+
279
+ if nb_iter % 10000 == 0 :
280
+ for ii in range(4):
281
+ tensorborad_add_video_xyz(writer, draw_pred[ii], nb_iter, tag='./Vis/pred_eval'+str(ii), nb_vis=1, title_batch=[draw_text_pred[ii]], outname=[os.path.join(out_dir, 'pred'+str(ii)+'.gif')] if savegif else None)
282
+
283
+
284
+ if fid < best_fid :
285
+ msg = f"--> --> \t FID Improved from {best_fid:.5f} to {fid:.5f} !!!"
286
+ logger.info(msg)
287
+ best_fid, best_iter = fid, nb_iter
288
+ if save:
289
+ torch.save({'trans' : trans.state_dict()}, os.path.join(out_dir, 'net_best_fid.pth'))
290
+
291
+ if matching_score_pred < best_matching :
292
+ msg = f"--> --> \t matching_score Improved from {best_matching:.5f} to {matching_score_pred:.5f} !!!"
293
+ logger.info(msg)
294
+ best_matching = matching_score_pred
295
+
296
+ if abs(diversity_real - diversity) < abs(diversity_real - best_div) :
297
+ msg = f"--> --> \t Diversity Improved from {best_div:.5f} to {diversity:.5f} !!!"
298
+ logger.info(msg)
299
+ best_div = diversity
300
+
301
+ if R_precision[0] > best_top1 :
302
+ msg = f"--> --> \t Top1 Improved from {best_top1:.4f} to {R_precision[0]:.4f} !!!"
303
+ logger.info(msg)
304
+ best_top1 = R_precision[0]
305
+
306
+ if R_precision[1] > best_top2 :
307
+ msg = f"--> --> \t Top2 Improved from {best_top2:.4f} to {R_precision[1]:.4f} !!!"
308
+ logger.info(msg)
309
+ best_top2 = R_precision[1]
310
+
311
+ if R_precision[2] > best_top3 :
312
+ msg = f"--> --> \t Top3 Improved from {best_top3:.4f} to {R_precision[2]:.4f} !!!"
313
+ logger.info(msg)
314
+ best_top3 = R_precision[2]
315
+
316
+ if save:
317
+ torch.save({'trans' : trans.state_dict()}, os.path.join(out_dir, 'net_last.pth'))
318
+
319
+ trans.train()
320
+ return best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, writer, logger
321
+
322
+
323
+ @torch.no_grad()
324
+ def evaluation_transformer_test(out_dir, val_loader, net, trans, logger, writer, nb_iter, best_fid, best_iter, best_div, best_top1, best_top2, best_top3, best_matching, best_multi, clip_model, eval_wrapper, draw = True, save = True, savegif=False, savenpy=False) :
325
+
326
+ trans.eval()
327
+ nb_sample = 0
328
+
329
+ draw_org = []
330
+ draw_pred = []
331
+ draw_text = []
332
+ draw_text_pred = []
333
+ draw_name = []
334
+
335
+ motion_annotation_list = []
336
+ motion_pred_list = []
337
+ motion_multimodality = []
338
+ R_precision_real = 0
339
+ R_precision = 0
340
+ matching_score_real = 0
341
+ matching_score_pred = 0
342
+
343
+ nb_sample = 0
344
+
345
+ for batch in val_loader:
346
+
347
+ word_embeddings, pos_one_hots, clip_text, sent_len, pose, m_length, token, name = batch
348
+ bs, seq = pose.shape[:2]
349
+ num_joints = 21 if pose.shape[-1] == 251 else 22
350
+
351
+ text = clip.tokenize(clip_text, truncate=True).cuda()
352
+
353
+ feat_clip_text = clip_model.encode_text(text).float()
354
+ motion_multimodality_batch = []
355
+ for i in range(30):
356
+ pred_pose_eval = torch.zeros((bs, seq, pose.shape[-1])).cuda()
357
+ pred_len = torch.ones(bs).long()
358
+
359
+ for k in range(bs):
360
+ try:
361
+ index_motion = trans.sample(feat_clip_text[k:k+1], True)
362
+ except:
363
+ index_motion = torch.ones(1,1).cuda().long()
364
+
365
+ pred_pose = net.forward_decoder(index_motion)
366
+ cur_len = pred_pose.shape[1]
367
+
368
+ pred_len[k] = min(cur_len, seq)
369
+ pred_pose_eval[k:k+1, :cur_len] = pred_pose[:, :seq]
370
+
371
+ if i == 0 and (draw or savenpy):
372
+ pred_denorm = val_loader.dataset.inv_transform(pred_pose.detach().cpu().numpy())
373
+ pred_xyz = recover_from_ric(torch.from_numpy(pred_denorm).float().cuda(), num_joints)
374
+
375
+ if savenpy:
376
+ np.save(os.path.join(out_dir, name[k]+'_pred.npy'), pred_xyz.detach().cpu().numpy())
377
+
378
+ if draw:
379
+ if i == 0:
380
+ draw_pred.append(pred_xyz)
381
+ draw_text_pred.append(clip_text[k])
382
+ draw_name.append(name[k])
383
+
384
+ et_pred, em_pred = eval_wrapper.get_co_embeddings(word_embeddings, pos_one_hots, sent_len, pred_pose_eval, pred_len)
385
+
386
+ motion_multimodality_batch.append(em_pred.reshape(bs, 1, -1))
387
+
388
+ if i == 0:
389
+ pose = pose.cuda().float()
390
+
391
+ et, em = eval_wrapper.get_co_embeddings(word_embeddings, pos_one_hots, sent_len, pose, m_length)
392
+ motion_annotation_list.append(em)
393
+ motion_pred_list.append(em_pred)
394
+
395
+ if draw or savenpy:
396
+ pose = val_loader.dataset.inv_transform(pose.detach().cpu().numpy())
397
+ pose_xyz = recover_from_ric(torch.from_numpy(pose).float().cuda(), num_joints)
398
+
399
+ if savenpy:
400
+ for j in range(bs):
401
+ np.save(os.path.join(out_dir, name[j]+'_gt.npy'), pose_xyz[j][:m_length[j]].unsqueeze(0).cpu().numpy())
402
+
403
+ if draw:
404
+ for j in range(bs):
405
+ draw_org.append(pose_xyz[j][:m_length[j]].unsqueeze(0))
406
+ draw_text.append(clip_text[j])
407
+
408
+ temp_R, temp_match = calculate_R_precision(et.cpu().numpy(), em.cpu().numpy(), top_k=3, sum_all=True)
409
+ R_precision_real += temp_R
410
+ matching_score_real += temp_match
411
+ temp_R, temp_match = calculate_R_precision(et_pred.cpu().numpy(), em_pred.cpu().numpy(), top_k=3, sum_all=True)
412
+ R_precision += temp_R
413
+ matching_score_pred += temp_match
414
+
415
+ nb_sample += bs
416
+
417
+ motion_multimodality.append(torch.cat(motion_multimodality_batch, dim=1))
418
+
419
+ motion_annotation_np = torch.cat(motion_annotation_list, dim=0).cpu().numpy()
420
+ motion_pred_np = torch.cat(motion_pred_list, dim=0).cpu().numpy()
421
+ gt_mu, gt_cov = calculate_activation_statistics(motion_annotation_np)
422
+ mu, cov= calculate_activation_statistics(motion_pred_np)
423
+
424
+ diversity_real = calculate_diversity(motion_annotation_np, 300 if nb_sample > 300 else 100)
425
+ diversity = calculate_diversity(motion_pred_np, 300 if nb_sample > 300 else 100)
426
+
427
+ R_precision_real = R_precision_real / nb_sample
428
+ R_precision = R_precision / nb_sample
429
+
430
+ matching_score_real = matching_score_real / nb_sample
431
+ matching_score_pred = matching_score_pred / nb_sample
432
+
433
+ multimodality = 0
434
+ motion_multimodality = torch.cat(motion_multimodality, dim=0).cpu().numpy()
435
+ multimodality = calculate_multimodality(motion_multimodality, 10)
436
+
437
+ fid = calculate_frechet_distance(gt_mu, gt_cov, mu, cov)
438
+
439
+ msg = f"--> \t Eva. Iter {nb_iter} :, FID. {fid:.4f}, Diversity Real. {diversity_real:.4f}, Diversity. {diversity:.4f}, R_precision_real. {R_precision_real}, R_precision. {R_precision}, matching_score_real. {matching_score_real}, matching_score_pred. {matching_score_pred}, multimodality. {multimodality:.4f}"
440
+ logger.info(msg)
441
+
442
+
443
+ if draw:
444
+ for ii in range(len(draw_org)):
445
+ tensorborad_add_video_xyz(writer, draw_org[ii], nb_iter, tag='./Vis/'+draw_name[ii]+'_org', nb_vis=1, title_batch=[draw_text[ii]], outname=[os.path.join(out_dir, draw_name[ii]+'_skel_gt.gif')] if savegif else None)
446
+
447
+ tensorborad_add_video_xyz(writer, draw_pred[ii], nb_iter, tag='./Vis/'+draw_name[ii]+'_pred', nb_vis=1, title_batch=[draw_text_pred[ii]], outname=[os.path.join(out_dir, draw_name[ii]+'_skel_pred.gif')] if savegif else None)
448
+
449
+ trans.train()
450
+ return fid, best_iter, diversity, R_precision[0], R_precision[1], R_precision[2], matching_score_pred, multimodality, writer, logger
451
+
452
+ # (X - X_train)*(X - X_train) = -2X*X_train + X*X + X_train*X_train
453
+ def euclidean_distance_matrix(matrix1, matrix2):
454
+ """
455
+ Params:
456
+ -- matrix1: N1 x D
457
+ -- matrix2: N2 x D
458
+ Returns:
459
+ -- dist: N1 x N2
460
+ dist[i, j] == distance(matrix1[i], matrix2[j])
461
+ """
462
+ assert matrix1.shape[1] == matrix2.shape[1]
463
+ d1 = -2 * np.dot(matrix1, matrix2.T) # shape (num_test, num_train)
464
+ d2 = np.sum(np.square(matrix1), axis=1, keepdims=True) # shape (num_test, 1)
465
+ d3 = np.sum(np.square(matrix2), axis=1) # shape (num_train, )
466
+ dists = np.sqrt(d1 + d2 + d3) # broadcasting
467
+ return dists
468
+
469
+
470
+
471
+ def calculate_top_k(mat, top_k):
472
+ size = mat.shape[0]
473
+ gt_mat = np.expand_dims(np.arange(size), 1).repeat(size, 1)
474
+ bool_mat = (mat == gt_mat)
475
+ correct_vec = False
476
+ top_k_list = []
477
+ for i in range(top_k):
478
+ # print(correct_vec, bool_mat[:, i])
479
+ correct_vec = (correct_vec | bool_mat[:, i])
480
+ # print(correct_vec)
481
+ top_k_list.append(correct_vec[:, None])
482
+ top_k_mat = np.concatenate(top_k_list, axis=1)
483
+ return top_k_mat
484
+
485
+
486
+ def calculate_R_precision(embedding1, embedding2, top_k, sum_all=False):
487
+ dist_mat = euclidean_distance_matrix(embedding1, embedding2)
488
+ matching_score = dist_mat.trace()
489
+ argmax = np.argsort(dist_mat, axis=1)
490
+ top_k_mat = calculate_top_k(argmax, top_k)
491
+ if sum_all:
492
+ return top_k_mat.sum(axis=0), matching_score
493
+ else:
494
+ return top_k_mat, matching_score
495
+
496
+ def calculate_multimodality(activation, multimodality_times):
497
+ assert len(activation.shape) == 3
498
+ assert activation.shape[1] > multimodality_times
499
+ num_per_sent = activation.shape[1]
500
+
501
+ first_dices = np.random.choice(num_per_sent, multimodality_times, replace=False)
502
+ second_dices = np.random.choice(num_per_sent, multimodality_times, replace=False)
503
+ dist = linalg.norm(activation[:, first_dices] - activation[:, second_dices], axis=2)
504
+ return dist.mean()
505
+
506
+
507
+ def calculate_diversity(activation, diversity_times):
508
+ assert len(activation.shape) == 2
509
+ assert activation.shape[0] > diversity_times
510
+ num_samples = activation.shape[0]
511
+
512
+ first_indices = np.random.choice(num_samples, diversity_times, replace=False)
513
+ second_indices = np.random.choice(num_samples, diversity_times, replace=False)
514
+ dist = linalg.norm(activation[first_indices] - activation[second_indices], axis=1)
515
+ return dist.mean()
516
+
517
+
518
+
519
+ def calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):
520
+
521
+ mu1 = np.atleast_1d(mu1)
522
+ mu2 = np.atleast_1d(mu2)
523
+
524
+ sigma1 = np.atleast_2d(sigma1)
525
+ sigma2 = np.atleast_2d(sigma2)
526
+
527
+ assert mu1.shape == mu2.shape, \
528
+ 'Training and test mean vectors have different lengths'
529
+ assert sigma1.shape == sigma2.shape, \
530
+ 'Training and test covariances have different dimensions'
531
+
532
+ diff = mu1 - mu2
533
+
534
+ # Product might be almost singular
535
+ covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)
536
+ if not np.isfinite(covmean).all():
537
+ msg = ('fid calculation produces singular product; '
538
+ 'adding %s to diagonal of cov estimates') % eps
539
+ print(msg)
540
+ offset = np.eye(sigma1.shape[0]) * eps
541
+ covmean = linalg.sqrtm((sigma1 + offset).dot(sigma2 + offset))
542
+
543
+ # Numerical error might give slight imaginary component
544
+ if np.iscomplexobj(covmean):
545
+ if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3):
546
+ m = np.max(np.abs(covmean.imag))
547
+ raise ValueError('Imaginary component {}'.format(m))
548
+ covmean = covmean.real
549
+
550
+ tr_covmean = np.trace(covmean)
551
+
552
+ return (diff.dot(diff) + np.trace(sigma1)
553
+ + np.trace(sigma2) - 2 * tr_covmean)
554
+
555
+
556
+
557
+ def calculate_activation_statistics(activations):
558
+
559
+ mu = np.mean(activations, axis=0)
560
+ cov = np.cov(activations, rowvar=False)
561
+ return mu, cov
562
+
563
+
564
+ def calculate_frechet_feature_distance(feature_list1, feature_list2):
565
+ feature_list1 = np.stack(feature_list1)
566
+ feature_list2 = np.stack(feature_list2)
567
+
568
+ # normalize the scale
569
+ mean = np.mean(feature_list1, axis=0)
570
+ std = np.std(feature_list1, axis=0) + 1e-10
571
+ feature_list1 = (feature_list1 - mean) / std
572
+ feature_list2 = (feature_list2 - mean) / std
573
+
574
+ dist = calculate_frechet_distance(
575
+ mu1=np.mean(feature_list1, axis=0),
576
+ sigma1=np.cov(feature_list1, rowvar=False),
577
+ mu2=np.mean(feature_list2, axis=0),
578
+ sigma2=np.cov(feature_list2, rowvar=False),
579
+ )
580
+ return dist
VQ-Trans/utils/losses.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+ class ReConsLoss(nn.Module):
5
+ def __init__(self, recons_loss, nb_joints):
6
+ super(ReConsLoss, self).__init__()
7
+
8
+ if recons_loss == 'l1':
9
+ self.Loss = torch.nn.L1Loss()
10
+ elif recons_loss == 'l2' :
11
+ self.Loss = torch.nn.MSELoss()
12
+ elif recons_loss == 'l1_smooth' :
13
+ self.Loss = torch.nn.SmoothL1Loss()
14
+
15
+ # 4 global motion associated to root
16
+ # 12 local motion (3 local xyz, 3 vel xyz, 6 rot6d)
17
+ # 3 global vel xyz
18
+ # 4 foot contact
19
+ self.nb_joints = nb_joints
20
+ self.motion_dim = (nb_joints - 1) * 12 + 4 + 3 + 4
21
+
22
+ def forward(self, motion_pred, motion_gt) :
23
+ loss = self.Loss(motion_pred[..., : self.motion_dim], motion_gt[..., :self.motion_dim])
24
+ return loss
25
+
26
+ def forward_vel(self, motion_pred, motion_gt) :
27
+ loss = self.Loss(motion_pred[..., 4 : (self.nb_joints - 1) * 3 + 4], motion_gt[..., 4 : (self.nb_joints - 1) * 3 + 4])
28
+ return loss
29
+
30
+
VQ-Trans/utils/motion_process.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from utils.quaternion import quaternion_to_cont6d, qrot, qinv
3
+
4
+ def recover_root_rot_pos(data):
5
+ rot_vel = data[..., 0]
6
+ r_rot_ang = torch.zeros_like(rot_vel).to(data.device)
7
+ '''Get Y-axis rotation from rotation velocity'''
8
+ r_rot_ang[..., 1:] = rot_vel[..., :-1]
9
+ r_rot_ang = torch.cumsum(r_rot_ang, dim=-1)
10
+
11
+ r_rot_quat = torch.zeros(data.shape[:-1] + (4,)).to(data.device)
12
+ r_rot_quat[..., 0] = torch.cos(r_rot_ang)
13
+ r_rot_quat[..., 2] = torch.sin(r_rot_ang)
14
+
15
+ r_pos = torch.zeros(data.shape[:-1] + (3,)).to(data.device)
16
+ r_pos[..., 1:, [0, 2]] = data[..., :-1, 1:3]
17
+ '''Add Y-axis rotation to root position'''
18
+ r_pos = qrot(qinv(r_rot_quat), r_pos)
19
+
20
+ r_pos = torch.cumsum(r_pos, dim=-2)
21
+
22
+ r_pos[..., 1] = data[..., 3]
23
+ return r_rot_quat, r_pos
24
+
25
+
26
+ def recover_from_rot(data, joints_num, skeleton):
27
+ r_rot_quat, r_pos = recover_root_rot_pos(data)
28
+
29
+ r_rot_cont6d = quaternion_to_cont6d(r_rot_quat)
30
+
31
+ start_indx = 1 + 2 + 1 + (joints_num - 1) * 3
32
+ end_indx = start_indx + (joints_num - 1) * 6
33
+ cont6d_params = data[..., start_indx:end_indx]
34
+ # print(r_rot_cont6d.shape, cont6d_params.shape, r_pos.shape)
35
+ cont6d_params = torch.cat([r_rot_cont6d, cont6d_params], dim=-1)
36
+ cont6d_params = cont6d_params.view(-1, joints_num, 6)
37
+
38
+ positions = skeleton.forward_kinematics_cont6d(cont6d_params, r_pos)
39
+
40
+ return positions
41
+
42
+
43
+ def recover_from_ric(data, joints_num):
44
+ r_rot_quat, r_pos = recover_root_rot_pos(data)
45
+ positions = data[..., 4:(joints_num - 1) * 3 + 4]
46
+ positions = positions.view(positions.shape[:-1] + (-1, 3))
47
+
48
+ '''Add Y-axis rotation to local joints'''
49
+ positions = qrot(qinv(r_rot_quat[..., None, :]).expand(positions.shape[:-1] + (4,)), positions)
50
+
51
+ '''Add root XZ to joints'''
52
+ positions[..., 0] += r_pos[..., 0:1]
53
+ positions[..., 2] += r_pos[..., 2:3]
54
+
55
+ '''Concate root and joints'''
56
+ positions = torch.cat([r_pos.unsqueeze(-2), positions], dim=-2)
57
+
58
+ return positions
59
+
VQ-Trans/utils/paramUtil.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ # Define a kinematic tree for the skeletal struture
4
+ kit_kinematic_chain = [[0, 11, 12, 13, 14, 15], [0, 16, 17, 18, 19, 20], [0, 1, 2, 3, 4], [3, 5, 6, 7], [3, 8, 9, 10]]
5
+
6
+ kit_raw_offsets = np.array(
7
+ [
8
+ [0, 0, 0],
9
+ [0, 1, 0],
10
+ [0, 1, 0],
11
+ [0, 1, 0],
12
+ [0, 1, 0],
13
+ [1, 0, 0],
14
+ [0, -1, 0],
15
+ [0, -1, 0],
16
+ [-1, 0, 0],
17
+ [0, -1, 0],
18
+ [0, -1, 0],
19
+ [1, 0, 0],
20
+ [0, -1, 0],
21
+ [0, -1, 0],
22
+ [0, 0, 1],
23
+ [0, 0, 1],
24
+ [-1, 0, 0],
25
+ [0, -1, 0],
26
+ [0, -1, 0],
27
+ [0, 0, 1],
28
+ [0, 0, 1]
29
+ ]
30
+ )
31
+
32
+ t2m_raw_offsets = np.array([[0,0,0],
33
+ [1,0,0],
34
+ [-1,0,0],
35
+ [0,1,0],
36
+ [0,-1,0],
37
+ [0,-1,0],
38
+ [0,1,0],
39
+ [0,-1,0],
40
+ [0,-1,0],
41
+ [0,1,0],
42
+ [0,0,1],
43
+ [0,0,1],
44
+ [0,1,0],
45
+ [1,0,0],
46
+ [-1,0,0],
47
+ [0,0,1],
48
+ [0,-1,0],
49
+ [0,-1,0],
50
+ [0,-1,0],
51
+ [0,-1,0],
52
+ [0,-1,0],
53
+ [0,-1,0]])
54
+
55
+ t2m_kinematic_chain = [[0, 2, 5, 8, 11], [0, 1, 4, 7, 10], [0, 3, 6, 9, 12, 15], [9, 14, 17, 19, 21], [9, 13, 16, 18, 20]]
56
+ t2m_left_hand_chain = [[20, 22, 23, 24], [20, 34, 35, 36], [20, 25, 26, 27], [20, 31, 32, 33], [20, 28, 29, 30]]
57
+ t2m_right_hand_chain = [[21, 43, 44, 45], [21, 46, 47, 48], [21, 40, 41, 42], [21, 37, 38, 39], [21, 49, 50, 51]]
58
+
59
+
60
+ kit_tgt_skel_id = '03950'
61
+
62
+ t2m_tgt_skel_id = '000021'
63
+
VQ-Trans/utils/quaternion.py ADDED
@@ -0,0 +1,423 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2018-present, Facebook, Inc.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the license found in the
5
+ # LICENSE file in the root directory of this source tree.
6
+ #
7
+
8
+ import torch
9
+ import numpy as np
10
+
11
+ _EPS4 = np.finfo(float).eps * 4.0
12
+
13
+ _FLOAT_EPS = np.finfo(np.float).eps
14
+
15
+ # PyTorch-backed implementations
16
+ def qinv(q):
17
+ assert q.shape[-1] == 4, 'q must be a tensor of shape (*, 4)'
18
+ mask = torch.ones_like(q)
19
+ mask[..., 1:] = -mask[..., 1:]
20
+ return q * mask
21
+
22
+
23
+ def qinv_np(q):
24
+ assert q.shape[-1] == 4, 'q must be a tensor of shape (*, 4)'
25
+ return qinv(torch.from_numpy(q).float()).numpy()
26
+
27
+
28
+ def qnormalize(q):
29
+ assert q.shape[-1] == 4, 'q must be a tensor of shape (*, 4)'
30
+ return q / torch.norm(q, dim=-1, keepdim=True)
31
+
32
+
33
+ def qmul(q, r):
34
+ """
35
+ Multiply quaternion(s) q with quaternion(s) r.
36
+ Expects two equally-sized tensors of shape (*, 4), where * denotes any number of dimensions.
37
+ Returns q*r as a tensor of shape (*, 4).
38
+ """
39
+ assert q.shape[-1] == 4
40
+ assert r.shape[-1] == 4
41
+
42
+ original_shape = q.shape
43
+
44
+ # Compute outer product
45
+ terms = torch.bmm(r.view(-1, 4, 1), q.view(-1, 1, 4))
46
+
47
+ w = terms[:, 0, 0] - terms[:, 1, 1] - terms[:, 2, 2] - terms[:, 3, 3]
48
+ x = terms[:, 0, 1] + terms[:, 1, 0] - terms[:, 2, 3] + terms[:, 3, 2]
49
+ y = terms[:, 0, 2] + terms[:, 1, 3] + terms[:, 2, 0] - terms[:, 3, 1]
50
+ z = terms[:, 0, 3] - terms[:, 1, 2] + terms[:, 2, 1] + terms[:, 3, 0]
51
+ return torch.stack((w, x, y, z), dim=1).view(original_shape)
52
+
53
+
54
+ def qrot(q, v):
55
+ """
56
+ Rotate vector(s) v about the rotation described by quaternion(s) q.
57
+ Expects a tensor of shape (*, 4) for q and a tensor of shape (*, 3) for v,
58
+ where * denotes any number of dimensions.
59
+ Returns a tensor of shape (*, 3).
60
+ """
61
+ assert q.shape[-1] == 4
62
+ assert v.shape[-1] == 3
63
+ assert q.shape[:-1] == v.shape[:-1]
64
+
65
+ original_shape = list(v.shape)
66
+ # print(q.shape)
67
+ q = q.contiguous().view(-1, 4)
68
+ v = v.contiguous().view(-1, 3)
69
+
70
+ qvec = q[:, 1:]
71
+ uv = torch.cross(qvec, v, dim=1)
72
+ uuv = torch.cross(qvec, uv, dim=1)
73
+ return (v + 2 * (q[:, :1] * uv + uuv)).view(original_shape)
74
+
75
+
76
+ def qeuler(q, order, epsilon=0, deg=True):
77
+ """
78
+ Convert quaternion(s) q to Euler angles.
79
+ Expects a tensor of shape (*, 4), where * denotes any number of dimensions.
80
+ Returns a tensor of shape (*, 3).
81
+ """
82
+ assert q.shape[-1] == 4
83
+
84
+ original_shape = list(q.shape)
85
+ original_shape[-1] = 3
86
+ q = q.view(-1, 4)
87
+
88
+ q0 = q[:, 0]
89
+ q1 = q[:, 1]
90
+ q2 = q[:, 2]
91
+ q3 = q[:, 3]
92
+
93
+ if order == 'xyz':
94
+ x = torch.atan2(2 * (q0 * q1 - q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2))
95
+ y = torch.asin(torch.clamp(2 * (q1 * q3 + q0 * q2), -1 + epsilon, 1 - epsilon))
96
+ z = torch.atan2(2 * (q0 * q3 - q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3))
97
+ elif order == 'yzx':
98
+ x = torch.atan2(2 * (q0 * q1 - q2 * q3), 1 - 2 * (q1 * q1 + q3 * q3))
99
+ y = torch.atan2(2 * (q0 * q2 - q1 * q3), 1 - 2 * (q2 * q2 + q3 * q3))
100
+ z = torch.asin(torch.clamp(2 * (q1 * q2 + q0 * q3), -1 + epsilon, 1 - epsilon))
101
+ elif order == 'zxy':
102
+ x = torch.asin(torch.clamp(2 * (q0 * q1 + q2 * q3), -1 + epsilon, 1 - epsilon))
103
+ y = torch.atan2(2 * (q0 * q2 - q1 * q3), 1 - 2 * (q1 * q1 + q2 * q2))
104
+ z = torch.atan2(2 * (q0 * q3 - q1 * q2), 1 - 2 * (q1 * q1 + q3 * q3))
105
+ elif order == 'xzy':
106
+ x = torch.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q3 * q3))
107
+ y = torch.atan2(2 * (q0 * q2 + q1 * q3), 1 - 2 * (q2 * q2 + q3 * q3))
108
+ z = torch.asin(torch.clamp(2 * (q0 * q3 - q1 * q2), -1 + epsilon, 1 - epsilon))
109
+ elif order == 'yxz':
110
+ x = torch.asin(torch.clamp(2 * (q0 * q1 - q2 * q3), -1 + epsilon, 1 - epsilon))
111
+ y = torch.atan2(2 * (q1 * q3 + q0 * q2), 1 - 2 * (q1 * q1 + q2 * q2))
112
+ z = torch.atan2(2 * (q1 * q2 + q0 * q3), 1 - 2 * (q1 * q1 + q3 * q3))
113
+ elif order == 'zyx':
114
+ x = torch.atan2(2 * (q0 * q1 + q2 * q3), 1 - 2 * (q1 * q1 + q2 * q2))
115
+ y = torch.asin(torch.clamp(2 * (q0 * q2 - q1 * q3), -1 + epsilon, 1 - epsilon))
116
+ z = torch.atan2(2 * (q0 * q3 + q1 * q2), 1 - 2 * (q2 * q2 + q3 * q3))
117
+ else:
118
+ raise
119
+
120
+ if deg:
121
+ return torch.stack((x, y, z), dim=1).view(original_shape) * 180 / np.pi
122
+ else:
123
+ return torch.stack((x, y, z), dim=1).view(original_shape)
124
+
125
+
126
+ # Numpy-backed implementations
127
+
128
+ def qmul_np(q, r):
129
+ q = torch.from_numpy(q).contiguous().float()
130
+ r = torch.from_numpy(r).contiguous().float()
131
+ return qmul(q, r).numpy()
132
+
133
+
134
+ def qrot_np(q, v):
135
+ q = torch.from_numpy(q).contiguous().float()
136
+ v = torch.from_numpy(v).contiguous().float()
137
+ return qrot(q, v).numpy()
138
+
139
+
140
+ def qeuler_np(q, order, epsilon=0, use_gpu=False):
141
+ if use_gpu:
142
+ q = torch.from_numpy(q).cuda().float()
143
+ return qeuler(q, order, epsilon).cpu().numpy()
144
+ else:
145
+ q = torch.from_numpy(q).contiguous().float()
146
+ return qeuler(q, order, epsilon).numpy()
147
+
148
+
149
+ def qfix(q):
150
+ """
151
+ Enforce quaternion continuity across the time dimension by selecting
152
+ the representation (q or -q) with minimal distance (or, equivalently, maximal dot product)
153
+ between two consecutive frames.
154
+
155
+ Expects a tensor of shape (L, J, 4), where L is the sequence length and J is the number of joints.
156
+ Returns a tensor of the same shape.
157
+ """
158
+ assert len(q.shape) == 3
159
+ assert q.shape[-1] == 4
160
+
161
+ result = q.copy()
162
+ dot_products = np.sum(q[1:] * q[:-1], axis=2)
163
+ mask = dot_products < 0
164
+ mask = (np.cumsum(mask, axis=0) % 2).astype(bool)
165
+ result[1:][mask] *= -1
166
+ return result
167
+
168
+
169
+ def euler2quat(e, order, deg=True):
170
+ """
171
+ Convert Euler angles to quaternions.
172
+ """
173
+ assert e.shape[-1] == 3
174
+
175
+ original_shape = list(e.shape)
176
+ original_shape[-1] = 4
177
+
178
+ e = e.view(-1, 3)
179
+
180
+ ## if euler angles in degrees
181
+ if deg:
182
+ e = e * np.pi / 180.
183
+
184
+ x = e[:, 0]
185
+ y = e[:, 1]
186
+ z = e[:, 2]
187
+
188
+ rx = torch.stack((torch.cos(x / 2), torch.sin(x / 2), torch.zeros_like(x), torch.zeros_like(x)), dim=1)
189
+ ry = torch.stack((torch.cos(y / 2), torch.zeros_like(y), torch.sin(y / 2), torch.zeros_like(y)), dim=1)
190
+ rz = torch.stack((torch.cos(z / 2), torch.zeros_like(z), torch.zeros_like(z), torch.sin(z / 2)), dim=1)
191
+
192
+ result = None
193
+ for coord in order:
194
+ if coord == 'x':
195
+ r = rx
196
+ elif coord == 'y':
197
+ r = ry
198
+ elif coord == 'z':
199
+ r = rz
200
+ else:
201
+ raise
202
+ if result is None:
203
+ result = r
204
+ else:
205
+ result = qmul(result, r)
206
+
207
+ # Reverse antipodal representation to have a non-negative "w"
208
+ if order in ['xyz', 'yzx', 'zxy']:
209
+ result *= -1
210
+
211
+ return result.view(original_shape)
212
+
213
+
214
+ def expmap_to_quaternion(e):
215
+ """
216
+ Convert axis-angle rotations (aka exponential maps) to quaternions.
217
+ Stable formula from "Practical Parameterization of Rotations Using the Exponential Map".
218
+ Expects a tensor of shape (*, 3), where * denotes any number of dimensions.
219
+ Returns a tensor of shape (*, 4).
220
+ """
221
+ assert e.shape[-1] == 3
222
+
223
+ original_shape = list(e.shape)
224
+ original_shape[-1] = 4
225
+ e = e.reshape(-1, 3)
226
+
227
+ theta = np.linalg.norm(e, axis=1).reshape(-1, 1)
228
+ w = np.cos(0.5 * theta).reshape(-1, 1)
229
+ xyz = 0.5 * np.sinc(0.5 * theta / np.pi) * e
230
+ return np.concatenate((w, xyz), axis=1).reshape(original_shape)
231
+
232
+
233
+ def euler_to_quaternion(e, order):
234
+ """
235
+ Convert Euler angles to quaternions.
236
+ """
237
+ assert e.shape[-1] == 3
238
+
239
+ original_shape = list(e.shape)
240
+ original_shape[-1] = 4
241
+
242
+ e = e.reshape(-1, 3)
243
+
244
+ x = e[:, 0]
245
+ y = e[:, 1]
246
+ z = e[:, 2]
247
+
248
+ rx = np.stack((np.cos(x / 2), np.sin(x / 2), np.zeros_like(x), np.zeros_like(x)), axis=1)
249
+ ry = np.stack((np.cos(y / 2), np.zeros_like(y), np.sin(y / 2), np.zeros_like(y)), axis=1)
250
+ rz = np.stack((np.cos(z / 2), np.zeros_like(z), np.zeros_like(z), np.sin(z / 2)), axis=1)
251
+
252
+ result = None
253
+ for coord in order:
254
+ if coord == 'x':
255
+ r = rx
256
+ elif coord == 'y':
257
+ r = ry
258
+ elif coord == 'z':
259
+ r = rz
260
+ else:
261
+ raise
262
+ if result is None:
263
+ result = r
264
+ else:
265
+ result = qmul_np(result, r)
266
+
267
+ # Reverse antipodal representation to have a non-negative "w"
268
+ if order in ['xyz', 'yzx', 'zxy']:
269
+ result *= -1
270
+
271
+ return result.reshape(original_shape)
272
+
273
+
274
+ def quaternion_to_matrix(quaternions):
275
+ """
276
+ Convert rotations given as quaternions to rotation matrices.
277
+ Args:
278
+ quaternions: quaternions with real part first,
279
+ as tensor of shape (..., 4).
280
+ Returns:
281
+ Rotation matrices as tensor of shape (..., 3, 3).
282
+ """
283
+ r, i, j, k = torch.unbind(quaternions, -1)
284
+ two_s = 2.0 / (quaternions * quaternions).sum(-1)
285
+
286
+ o = torch.stack(
287
+ (
288
+ 1 - two_s * (j * j + k * k),
289
+ two_s * (i * j - k * r),
290
+ two_s * (i * k + j * r),
291
+ two_s * (i * j + k * r),
292
+ 1 - two_s * (i * i + k * k),
293
+ two_s * (j * k - i * r),
294
+ two_s * (i * k - j * r),
295
+ two_s * (j * k + i * r),
296
+ 1 - two_s * (i * i + j * j),
297
+ ),
298
+ -1,
299
+ )
300
+ return o.reshape(quaternions.shape[:-1] + (3, 3))
301
+
302
+
303
+ def quaternion_to_matrix_np(quaternions):
304
+ q = torch.from_numpy(quaternions).contiguous().float()
305
+ return quaternion_to_matrix(q).numpy()
306
+
307
+
308
+ def quaternion_to_cont6d_np(quaternions):
309
+ rotation_mat = quaternion_to_matrix_np(quaternions)
310
+ cont_6d = np.concatenate([rotation_mat[..., 0], rotation_mat[..., 1]], axis=-1)
311
+ return cont_6d
312
+
313
+
314
+ def quaternion_to_cont6d(quaternions):
315
+ rotation_mat = quaternion_to_matrix(quaternions)
316
+ cont_6d = torch.cat([rotation_mat[..., 0], rotation_mat[..., 1]], dim=-1)
317
+ return cont_6d
318
+
319
+
320
+ def cont6d_to_matrix(cont6d):
321
+ assert cont6d.shape[-1] == 6, "The last dimension must be 6"
322
+ x_raw = cont6d[..., 0:3]
323
+ y_raw = cont6d[..., 3:6]
324
+
325
+ x = x_raw / torch.norm(x_raw, dim=-1, keepdim=True)
326
+ z = torch.cross(x, y_raw, dim=-1)
327
+ z = z / torch.norm(z, dim=-1, keepdim=True)
328
+
329
+ y = torch.cross(z, x, dim=-1)
330
+
331
+ x = x[..., None]
332
+ y = y[..., None]
333
+ z = z[..., None]
334
+
335
+ mat = torch.cat([x, y, z], dim=-1)
336
+ return mat
337
+
338
+
339
+ def cont6d_to_matrix_np(cont6d):
340
+ q = torch.from_numpy(cont6d).contiguous().float()
341
+ return cont6d_to_matrix(q).numpy()
342
+
343
+
344
+ def qpow(q0, t, dtype=torch.float):
345
+ ''' q0 : tensor of quaternions
346
+ t: tensor of powers
347
+ '''
348
+ q0 = qnormalize(q0)
349
+ theta0 = torch.acos(q0[..., 0])
350
+
351
+ ## if theta0 is close to zero, add epsilon to avoid NaNs
352
+ mask = (theta0 <= 10e-10) * (theta0 >= -10e-10)
353
+ theta0 = (1 - mask) * theta0 + mask * 10e-10
354
+ v0 = q0[..., 1:] / torch.sin(theta0).view(-1, 1)
355
+
356
+ if isinstance(t, torch.Tensor):
357
+ q = torch.zeros(t.shape + q0.shape)
358
+ theta = t.view(-1, 1) * theta0.view(1, -1)
359
+ else: ## if t is a number
360
+ q = torch.zeros(q0.shape)
361
+ theta = t * theta0
362
+
363
+ q[..., 0] = torch.cos(theta)
364
+ q[..., 1:] = v0 * torch.sin(theta).unsqueeze(-1)
365
+
366
+ return q.to(dtype)
367
+
368
+
369
+ def qslerp(q0, q1, t):
370
+ '''
371
+ q0: starting quaternion
372
+ q1: ending quaternion
373
+ t: array of points along the way
374
+
375
+ Returns:
376
+ Tensor of Slerps: t.shape + q0.shape
377
+ '''
378
+
379
+ q0 = qnormalize(q0)
380
+ q1 = qnormalize(q1)
381
+ q_ = qpow(qmul(q1, qinv(q0)), t)
382
+
383
+ return qmul(q_,
384
+ q0.contiguous().view(torch.Size([1] * len(t.shape)) + q0.shape).expand(t.shape + q0.shape).contiguous())
385
+
386
+
387
+ def qbetween(v0, v1):
388
+ '''
389
+ find the quaternion used to rotate v0 to v1
390
+ '''
391
+ assert v0.shape[-1] == 3, 'v0 must be of the shape (*, 3)'
392
+ assert v1.shape[-1] == 3, 'v1 must be of the shape (*, 3)'
393
+
394
+ v = torch.cross(v0, v1)
395
+ w = torch.sqrt((v0 ** 2).sum(dim=-1, keepdim=True) * (v1 ** 2).sum(dim=-1, keepdim=True)) + (v0 * v1).sum(dim=-1,
396
+ keepdim=True)
397
+ return qnormalize(torch.cat([w, v], dim=-1))
398
+
399
+
400
+ def qbetween_np(v0, v1):
401
+ '''
402
+ find the quaternion used to rotate v0 to v1
403
+ '''
404
+ assert v0.shape[-1] == 3, 'v0 must be of the shape (*, 3)'
405
+ assert v1.shape[-1] == 3, 'v1 must be of the shape (*, 3)'
406
+
407
+ v0 = torch.from_numpy(v0).float()
408
+ v1 = torch.from_numpy(v1).float()
409
+ return qbetween(v0, v1).numpy()
410
+
411
+
412
+ def lerp(p0, p1, t):
413
+ if not isinstance(t, torch.Tensor):
414
+ t = torch.Tensor([t])
415
+
416
+ new_shape = t.shape + p0.shape
417
+ new_view_t = t.shape + torch.Size([1] * len(p0.shape))
418
+ new_view_p = torch.Size([1] * len(t.shape)) + p0.shape
419
+ p0 = p0.view(new_view_p).expand(new_shape)
420
+ p1 = p1.view(new_view_p).expand(new_shape)
421
+ t = t.view(new_view_t).expand(new_shape)
422
+
423
+ return p0 + t * (p1 - p0)
VQ-Trans/utils/rotation_conversions.py ADDED
@@ -0,0 +1,532 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
2
+ # Check PYTORCH3D_LICENCE before use
3
+
4
+ import functools
5
+ from typing import Optional
6
+
7
+ import torch
8
+ import torch.nn.functional as F
9
+
10
+
11
+ """
12
+ The transformation matrices returned from the functions in this file assume
13
+ the points on which the transformation will be applied are column vectors.
14
+ i.e. the R matrix is structured as
15
+ R = [
16
+ [Rxx, Rxy, Rxz],
17
+ [Ryx, Ryy, Ryz],
18
+ [Rzx, Rzy, Rzz],
19
+ ] # (3, 3)
20
+ This matrix can be applied to column vectors by post multiplication
21
+ by the points e.g.
22
+ points = [[0], [1], [2]] # (3 x 1) xyz coordinates of a point
23
+ transformed_points = R * points
24
+ To apply the same matrix to points which are row vectors, the R matrix
25
+ can be transposed and pre multiplied by the points:
26
+ e.g.
27
+ points = [[0, 1, 2]] # (1 x 3) xyz coordinates of a point
28
+ transformed_points = points * R.transpose(1, 0)
29
+ """
30
+
31
+
32
+ def quaternion_to_matrix(quaternions):
33
+ """
34
+ Convert rotations given as quaternions to rotation matrices.
35
+ Args:
36
+ quaternions: quaternions with real part first,
37
+ as tensor of shape (..., 4).
38
+ Returns:
39
+ Rotation matrices as tensor of shape (..., 3, 3).
40
+ """
41
+ r, i, j, k = torch.unbind(quaternions, -1)
42
+ two_s = 2.0 / (quaternions * quaternions).sum(-1)
43
+
44
+ o = torch.stack(
45
+ (
46
+ 1 - two_s * (j * j + k * k),
47
+ two_s * (i * j - k * r),
48
+ two_s * (i * k + j * r),
49
+ two_s * (i * j + k * r),
50
+ 1 - two_s * (i * i + k * k),
51
+ two_s * (j * k - i * r),
52
+ two_s * (i * k - j * r),
53
+ two_s * (j * k + i * r),
54
+ 1 - two_s * (i * i + j * j),
55
+ ),
56
+ -1,
57
+ )
58
+ return o.reshape(quaternions.shape[:-1] + (3, 3))
59
+
60
+
61
+ def _copysign(a, b):
62
+ """
63
+ Return a tensor where each element has the absolute value taken from the,
64
+ corresponding element of a, with sign taken from the corresponding
65
+ element of b. This is like the standard copysign floating-point operation,
66
+ but is not careful about negative 0 and NaN.
67
+ Args:
68
+ a: source tensor.
69
+ b: tensor whose signs will be used, of the same shape as a.
70
+ Returns:
71
+ Tensor of the same shape as a with the signs of b.
72
+ """
73
+ signs_differ = (a < 0) != (b < 0)
74
+ return torch.where(signs_differ, -a, a)
75
+
76
+
77
+ def _sqrt_positive_part(x):
78
+ """
79
+ Returns torch.sqrt(torch.max(0, x))
80
+ but with a zero subgradient where x is 0.
81
+ """
82
+ ret = torch.zeros_like(x)
83
+ positive_mask = x > 0
84
+ ret[positive_mask] = torch.sqrt(x[positive_mask])
85
+ return ret
86
+
87
+
88
+ def matrix_to_quaternion(matrix):
89
+ """
90
+ Convert rotations given as rotation matrices to quaternions.
91
+ Args:
92
+ matrix: Rotation matrices as tensor of shape (..., 3, 3).
93
+ Returns:
94
+ quaternions with real part first, as tensor of shape (..., 4).
95
+ """
96
+ if matrix.size(-1) != 3 or matrix.size(-2) != 3:
97
+ raise ValueError(f"Invalid rotation matrix shape f{matrix.shape}.")
98
+ m00 = matrix[..., 0, 0]
99
+ m11 = matrix[..., 1, 1]
100
+ m22 = matrix[..., 2, 2]
101
+ o0 = 0.5 * _sqrt_positive_part(1 + m00 + m11 + m22)
102
+ x = 0.5 * _sqrt_positive_part(1 + m00 - m11 - m22)
103
+ y = 0.5 * _sqrt_positive_part(1 - m00 + m11 - m22)
104
+ z = 0.5 * _sqrt_positive_part(1 - m00 - m11 + m22)
105
+ o1 = _copysign(x, matrix[..., 2, 1] - matrix[..., 1, 2])
106
+ o2 = _copysign(y, matrix[..., 0, 2] - matrix[..., 2, 0])
107
+ o3 = _copysign(z, matrix[..., 1, 0] - matrix[..., 0, 1])
108
+ return torch.stack((o0, o1, o2, o3), -1)
109
+
110
+
111
+ def _axis_angle_rotation(axis: str, angle):
112
+ """
113
+ Return the rotation matrices for one of the rotations about an axis
114
+ of which Euler angles describe, for each value of the angle given.
115
+ Args:
116
+ axis: Axis label "X" or "Y or "Z".
117
+ angle: any shape tensor of Euler angles in radians
118
+ Returns:
119
+ Rotation matrices as tensor of shape (..., 3, 3).
120
+ """
121
+
122
+ cos = torch.cos(angle)
123
+ sin = torch.sin(angle)
124
+ one = torch.ones_like(angle)
125
+ zero = torch.zeros_like(angle)
126
+
127
+ if axis == "X":
128
+ R_flat = (one, zero, zero, zero, cos, -sin, zero, sin, cos)
129
+ if axis == "Y":
130
+ R_flat = (cos, zero, sin, zero, one, zero, -sin, zero, cos)
131
+ if axis == "Z":
132
+ R_flat = (cos, -sin, zero, sin, cos, zero, zero, zero, one)
133
+
134
+ return torch.stack(R_flat, -1).reshape(angle.shape + (3, 3))
135
+
136
+
137
+ def euler_angles_to_matrix(euler_angles, convention: str):
138
+ """
139
+ Convert rotations given as Euler angles in radians to rotation matrices.
140
+ Args:
141
+ euler_angles: Euler angles in radians as tensor of shape (..., 3).
142
+ convention: Convention string of three uppercase letters from
143
+ {"X", "Y", and "Z"}.
144
+ Returns:
145
+ Rotation matrices as tensor of shape (..., 3, 3).
146
+ """
147
+ if euler_angles.dim() == 0 or euler_angles.shape[-1] != 3:
148
+ raise ValueError("Invalid input euler angles.")
149
+ if len(convention) != 3:
150
+ raise ValueError("Convention must have 3 letters.")
151
+ if convention[1] in (convention[0], convention[2]):
152
+ raise ValueError(f"Invalid convention {convention}.")
153
+ for letter in convention:
154
+ if letter not in ("X", "Y", "Z"):
155
+ raise ValueError(f"Invalid letter {letter} in convention string.")
156
+ matrices = map(_axis_angle_rotation, convention, torch.unbind(euler_angles, -1))
157
+ return functools.reduce(torch.matmul, matrices)
158
+
159
+
160
+ def _angle_from_tan(
161
+ axis: str, other_axis: str, data, horizontal: bool, tait_bryan: bool
162
+ ):
163
+ """
164
+ Extract the first or third Euler angle from the two members of
165
+ the matrix which are positive constant times its sine and cosine.
166
+ Args:
167
+ axis: Axis label "X" or "Y or "Z" for the angle we are finding.
168
+ other_axis: Axis label "X" or "Y or "Z" for the middle axis in the
169
+ convention.
170
+ data: Rotation matrices as tensor of shape (..., 3, 3).
171
+ horizontal: Whether we are looking for the angle for the third axis,
172
+ which means the relevant entries are in the same row of the
173
+ rotation matrix. If not, they are in the same column.
174
+ tait_bryan: Whether the first and third axes in the convention differ.
175
+ Returns:
176
+ Euler Angles in radians for each matrix in data as a tensor
177
+ of shape (...).
178
+ """
179
+
180
+ i1, i2 = {"X": (2, 1), "Y": (0, 2), "Z": (1, 0)}[axis]
181
+ if horizontal:
182
+ i2, i1 = i1, i2
183
+ even = (axis + other_axis) in ["XY", "YZ", "ZX"]
184
+ if horizontal == even:
185
+ return torch.atan2(data[..., i1], data[..., i2])
186
+ if tait_bryan:
187
+ return torch.atan2(-data[..., i2], data[..., i1])
188
+ return torch.atan2(data[..., i2], -data[..., i1])
189
+
190
+
191
+ def _index_from_letter(letter: str):
192
+ if letter == "X":
193
+ return 0
194
+ if letter == "Y":
195
+ return 1
196
+ if letter == "Z":
197
+ return 2
198
+
199
+
200
+ def matrix_to_euler_angles(matrix, convention: str):
201
+ """
202
+ Convert rotations given as rotation matrices to Euler angles in radians.
203
+ Args:
204
+ matrix: Rotation matrices as tensor of shape (..., 3, 3).
205
+ convention: Convention string of three uppercase letters.
206
+ Returns:
207
+ Euler angles in radians as tensor of shape (..., 3).
208
+ """
209
+ if len(convention) != 3:
210
+ raise ValueError("Convention must have 3 letters.")
211
+ if convention[1] in (convention[0], convention[2]):
212
+ raise ValueError(f"Invalid convention {convention}.")
213
+ for letter in convention:
214
+ if letter not in ("X", "Y", "Z"):
215
+ raise ValueError(f"Invalid letter {letter} in convention string.")
216
+ if matrix.size(-1) != 3 or matrix.size(-2) != 3:
217
+ raise ValueError(f"Invalid rotation matrix shape f{matrix.shape}.")
218
+ i0 = _index_from_letter(convention[0])
219
+ i2 = _index_from_letter(convention[2])
220
+ tait_bryan = i0 != i2
221
+ if tait_bryan:
222
+ central_angle = torch.asin(
223
+ matrix[..., i0, i2] * (-1.0 if i0 - i2 in [-1, 2] else 1.0)
224
+ )
225
+ else:
226
+ central_angle = torch.acos(matrix[..., i0, i0])
227
+
228
+ o = (
229
+ _angle_from_tan(
230
+ convention[0], convention[1], matrix[..., i2], False, tait_bryan
231
+ ),
232
+ central_angle,
233
+ _angle_from_tan(
234
+ convention[2], convention[1], matrix[..., i0, :], True, tait_bryan
235
+ ),
236
+ )
237
+ return torch.stack(o, -1)
238
+
239
+
240
+ def random_quaternions(
241
+ n: int, dtype: Optional[torch.dtype] = None, device=None, requires_grad=False
242
+ ):
243
+ """
244
+ Generate random quaternions representing rotations,
245
+ i.e. versors with nonnegative real part.
246
+ Args:
247
+ n: Number of quaternions in a batch to return.
248
+ dtype: Type to return.
249
+ device: Desired device of returned tensor. Default:
250
+ uses the current device for the default tensor type.
251
+ requires_grad: Whether the resulting tensor should have the gradient
252
+ flag set.
253
+ Returns:
254
+ Quaternions as tensor of shape (N, 4).
255
+ """
256
+ o = torch.randn((n, 4), dtype=dtype, device=device, requires_grad=requires_grad)
257
+ s = (o * o).sum(1)
258
+ o = o / _copysign(torch.sqrt(s), o[:, 0])[:, None]
259
+ return o
260
+
261
+
262
+ def random_rotations(
263
+ n: int, dtype: Optional[torch.dtype] = None, device=None, requires_grad=False
264
+ ):
265
+ """
266
+ Generate random rotations as 3x3 rotation matrices.
267
+ Args:
268
+ n: Number of rotation matrices in a batch to return.
269
+ dtype: Type to return.
270
+ device: Device of returned tensor. Default: if None,
271
+ uses the current device for the default tensor type.
272
+ requires_grad: Whether the resulting tensor should have the gradient
273
+ flag set.
274
+ Returns:
275
+ Rotation matrices as tensor of shape (n, 3, 3).
276
+ """
277
+ quaternions = random_quaternions(
278
+ n, dtype=dtype, device=device, requires_grad=requires_grad
279
+ )
280
+ return quaternion_to_matrix(quaternions)
281
+
282
+
283
+ def random_rotation(
284
+ dtype: Optional[torch.dtype] = None, device=None, requires_grad=False
285
+ ):
286
+ """
287
+ Generate a single random 3x3 rotation matrix.
288
+ Args:
289
+ dtype: Type to return
290
+ device: Device of returned tensor. Default: if None,
291
+ uses the current device for the default tensor type
292
+ requires_grad: Whether the resulting tensor should have the gradient
293
+ flag set
294
+ Returns:
295
+ Rotation matrix as tensor of shape (3, 3).
296
+ """
297
+ return random_rotations(1, dtype, device, requires_grad)[0]
298
+
299
+
300
+ def standardize_quaternion(quaternions):
301
+ """
302
+ Convert a unit quaternion to a standard form: one in which the real
303
+ part is non negative.
304
+ Args:
305
+ quaternions: Quaternions with real part first,
306
+ as tensor of shape (..., 4).
307
+ Returns:
308
+ Standardized quaternions as tensor of shape (..., 4).
309
+ """
310
+ return torch.where(quaternions[..., 0:1] < 0, -quaternions, quaternions)
311
+
312
+
313
+ def quaternion_raw_multiply(a, b):
314
+ """
315
+ Multiply two quaternions.
316
+ Usual torch rules for broadcasting apply.
317
+ Args:
318
+ a: Quaternions as tensor of shape (..., 4), real part first.
319
+ b: Quaternions as tensor of shape (..., 4), real part first.
320
+ Returns:
321
+ The product of a and b, a tensor of quaternions shape (..., 4).
322
+ """
323
+ aw, ax, ay, az = torch.unbind(a, -1)
324
+ bw, bx, by, bz = torch.unbind(b, -1)
325
+ ow = aw * bw - ax * bx - ay * by - az * bz
326
+ ox = aw * bx + ax * bw + ay * bz - az * by
327
+ oy = aw * by - ax * bz + ay * bw + az * bx
328
+ oz = aw * bz + ax * by - ay * bx + az * bw
329
+ return torch.stack((ow, ox, oy, oz), -1)
330
+
331
+
332
+ def quaternion_multiply(a, b):
333
+ """
334
+ Multiply two quaternions representing rotations, returning the quaternion
335
+ representing their composition, i.e. the versor with nonnegative real part.
336
+ Usual torch rules for broadcasting apply.
337
+ Args:
338
+ a: Quaternions as tensor of shape (..., 4), real part first.
339
+ b: Quaternions as tensor of shape (..., 4), real part first.
340
+ Returns:
341
+ The product of a and b, a tensor of quaternions of shape (..., 4).
342
+ """
343
+ ab = quaternion_raw_multiply(a, b)
344
+ return standardize_quaternion(ab)
345
+
346
+
347
+ def quaternion_invert(quaternion):
348
+ """
349
+ Given a quaternion representing rotation, get the quaternion representing
350
+ its inverse.
351
+ Args:
352
+ quaternion: Quaternions as tensor of shape (..., 4), with real part
353
+ first, which must be versors (unit quaternions).
354
+ Returns:
355
+ The inverse, a tensor of quaternions of shape (..., 4).
356
+ """
357
+
358
+ return quaternion * quaternion.new_tensor([1, -1, -1, -1])
359
+
360
+
361
+ def quaternion_apply(quaternion, point):
362
+ """
363
+ Apply the rotation given by a quaternion to a 3D point.
364
+ Usual torch rules for broadcasting apply.
365
+ Args:
366
+ quaternion: Tensor of quaternions, real part first, of shape (..., 4).
367
+ point: Tensor of 3D points of shape (..., 3).
368
+ Returns:
369
+ Tensor of rotated points of shape (..., 3).
370
+ """
371
+ if point.size(-1) != 3:
372
+ raise ValueError(f"Points are not in 3D, f{point.shape}.")
373
+ real_parts = point.new_zeros(point.shape[:-1] + (1,))
374
+ point_as_quaternion = torch.cat((real_parts, point), -1)
375
+ out = quaternion_raw_multiply(
376
+ quaternion_raw_multiply(quaternion, point_as_quaternion),
377
+ quaternion_invert(quaternion),
378
+ )
379
+ return out[..., 1:]
380
+
381
+
382
+ def axis_angle_to_matrix(axis_angle):
383
+ """
384
+ Convert rotations given as axis/angle to rotation matrices.
385
+ Args:
386
+ axis_angle: Rotations given as a vector in axis angle form,
387
+ as a tensor of shape (..., 3), where the magnitude is
388
+ the angle turned anticlockwise in radians around the
389
+ vector's direction.
390
+ Returns:
391
+ Rotation matrices as tensor of shape (..., 3, 3).
392
+ """
393
+ return quaternion_to_matrix(axis_angle_to_quaternion(axis_angle))
394
+
395
+
396
+ def matrix_to_axis_angle(matrix):
397
+ """
398
+ Convert rotations given as rotation matrices to axis/angle.
399
+ Args:
400
+ matrix: Rotation matrices as tensor of shape (..., 3, 3).
401
+ Returns:
402
+ Rotations given as a vector in axis angle form, as a tensor
403
+ of shape (..., 3), where the magnitude is the angle
404
+ turned anticlockwise in radians around the vector's
405
+ direction.
406
+ """
407
+ return quaternion_to_axis_angle(matrix_to_quaternion(matrix))
408
+
409
+
410
+ def axis_angle_to_quaternion(axis_angle):
411
+ """
412
+ Convert rotations given as axis/angle to quaternions.
413
+ Args:
414
+ axis_angle: Rotations given as a vector in axis angle form,
415
+ as a tensor of shape (..., 3), where the magnitude is
416
+ the angle turned anticlockwise in radians around the
417
+ vector's direction.
418
+ Returns:
419
+ quaternions with real part first, as tensor of shape (..., 4).
420
+ """
421
+ angles = torch.norm(axis_angle, p=2, dim=-1, keepdim=True)
422
+ half_angles = 0.5 * angles
423
+ eps = 1e-6
424
+ small_angles = angles.abs() < eps
425
+ sin_half_angles_over_angles = torch.empty_like(angles)
426
+ sin_half_angles_over_angles[~small_angles] = (
427
+ torch.sin(half_angles[~small_angles]) / angles[~small_angles]
428
+ )
429
+ # for x small, sin(x/2) is about x/2 - (x/2)^3/6
430
+ # so sin(x/2)/x is about 1/2 - (x*x)/48
431
+ sin_half_angles_over_angles[small_angles] = (
432
+ 0.5 - (angles[small_angles] * angles[small_angles]) / 48
433
+ )
434
+ quaternions = torch.cat(
435
+ [torch.cos(half_angles), axis_angle * sin_half_angles_over_angles], dim=-1
436
+ )
437
+ return quaternions
438
+
439
+
440
+ def quaternion_to_axis_angle(quaternions):
441
+ """
442
+ Convert rotations given as quaternions to axis/angle.
443
+ Args:
444
+ quaternions: quaternions with real part first,
445
+ as tensor of shape (..., 4).
446
+ Returns:
447
+ Rotations given as a vector in axis angle form, as a tensor
448
+ of shape (..., 3), where the magnitude is the angle
449
+ turned anticlockwise in radians around the vector's
450
+ direction.
451
+ """
452
+ norms = torch.norm(quaternions[..., 1:], p=2, dim=-1, keepdim=True)
453
+ half_angles = torch.atan2(norms, quaternions[..., :1])
454
+ angles = 2 * half_angles
455
+ eps = 1e-6
456
+ small_angles = angles.abs() < eps
457
+ sin_half_angles_over_angles = torch.empty_like(angles)
458
+ sin_half_angles_over_angles[~small_angles] = (
459
+ torch.sin(half_angles[~small_angles]) / angles[~small_angles]
460
+ )
461
+ # for x small, sin(x/2) is about x/2 - (x/2)^3/6
462
+ # so sin(x/2)/x is about 1/2 - (x*x)/48
463
+ sin_half_angles_over_angles[small_angles] = (
464
+ 0.5 - (angles[small_angles] * angles[small_angles]) / 48
465
+ )
466
+ return quaternions[..., 1:] / sin_half_angles_over_angles
467
+
468
+
469
+ def rotation_6d_to_matrix(d6: torch.Tensor) -> torch.Tensor:
470
+ """
471
+ Converts 6D rotation representation by Zhou et al. [1] to rotation matrix
472
+ using Gram--Schmidt orthogonalisation per Section B of [1].
473
+ Args:
474
+ d6: 6D rotation representation, of size (*, 6)
475
+ Returns:
476
+ batch of rotation matrices of size (*, 3, 3)
477
+ [1] Zhou, Y., Barnes, C., Lu, J., Yang, J., & Li, H.
478
+ On the Continuity of Rotation Representations in Neural Networks.
479
+ IEEE Conference on Computer Vision and Pattern Recognition, 2019.
480
+ Retrieved from http://arxiv.org/abs/1812.07035
481
+ """
482
+
483
+ a1, a2 = d6[..., :3], d6[..., 3:]
484
+ b1 = F.normalize(a1, dim=-1)
485
+ b2 = a2 - (b1 * a2).sum(-1, keepdim=True) * b1
486
+ b2 = F.normalize(b2, dim=-1)
487
+ b3 = torch.cross(b1, b2, dim=-1)
488
+ return torch.stack((b1, b2, b3), dim=-2)
489
+
490
+
491
+ def matrix_to_rotation_6d(matrix: torch.Tensor) -> torch.Tensor:
492
+ """
493
+ Converts rotation matrices to 6D rotation representation by Zhou et al. [1]
494
+ by dropping the last row. Note that 6D representation is not unique.
495
+ Args:
496
+ matrix: batch of rotation matrices of size (*, 3, 3)
497
+ Returns:
498
+ 6D rotation representation, of size (*, 6)
499
+ [1] Zhou, Y., Barnes, C., Lu, J., Yang, J., & Li, H.
500
+ On the Continuity of Rotation Representations in Neural Networks.
501
+ IEEE Conference on Computer Vision and Pattern Recognition, 2019.
502
+ Retrieved from http://arxiv.org/abs/1812.07035
503
+ """
504
+ return matrix[..., :2, :].clone().reshape(*matrix.size()[:-2], 6)
505
+
506
+ def canonicalize_smplh(poses, trans = None):
507
+ bs, nframes, njoints = poses.shape[:3]
508
+
509
+ global_orient = poses[:, :, 0]
510
+
511
+ # first global rotations
512
+ rot2d = matrix_to_axis_angle(global_orient[:, 0])
513
+ #rot2d[:, :2] = 0 # Remove the rotation along the vertical axis
514
+ rot2d = axis_angle_to_matrix(rot2d)
515
+
516
+ # Rotate the global rotation to eliminate Z rotations
517
+ global_orient = torch.einsum("ikj,imkl->imjl", rot2d, global_orient)
518
+
519
+ # Construct canonicalized version of x
520
+ xc = torch.cat((global_orient[:, :, None], poses[:, :, 1:]), dim=2)
521
+
522
+ if trans is not None:
523
+ vel = trans[:, 1:] - trans[:, :-1]
524
+ # Turn the translation as well
525
+ vel = torch.einsum("ikj,ilk->ilj", rot2d, vel)
526
+ trans = torch.cat((torch.zeros(bs, 1, 3, device=vel.device),
527
+ torch.cumsum(vel, 1)), 1)
528
+ return xc, trans
529
+ else:
530
+ return xc
531
+
532
+