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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import random
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import smplx
import copy
from .motion_encoder import * 

# ----------- AE, VAE ------------- #
class VAEConvZero(nn.Module):
    def __init__(self, args):
        super(VAEConvZero, self).__init__()
        self.encoder = VQEncoderV5(args)
        # self.quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        self.decoder = VQDecoderV5(args)
        
    def forward(self, inputs):
        pre_latent = self.encoder(inputs)
        # print(pre_latent.shape)
        # embedding_loss, vq_latent, _, perplexity = self.quantizer(pre_latent)
        rec_pose = self.decoder(pre_latent)
        return {
            # "poses_feat":vq_latent,
            # "embedding_loss":embedding_loss,
            # "perplexity":perplexity,
            "rec_pose": rec_pose
            }
    
class VAEConv(nn.Module):
    def __init__(self, args):
        super(VAEConv, self).__init__()
        self.encoder = VQEncoderV3(args)
        self.decoder = VQDecoderV3(args)
        self.fc_mu = nn.Linear(args.vae_length, args.vae_length)
        self.fc_logvar = nn.Linear(args.vae_length, args.vae_length)
        self.variational = args.variational
        
    def forward(self, inputs):
        pre_latent = self.encoder(inputs)
        mu, logvar = None, None
        if self.variational:
            mu = self.fc_mu(pre_latent)
            logvar = self.fc_logvar(pre_latent)
            pre_latent = reparameterize(mu, logvar)
        rec_pose = self.decoder(pre_latent)
        return {
            "poses_feat":pre_latent,
            "rec_pose": rec_pose,
            "pose_mu": mu,
            "pose_logvar": logvar,
            }
    
    def map2latent(self, inputs):
        pre_latent = self.encoder(inputs)
        if self.variational:
            mu = self.fc_mu(pre_latent)
            logvar = self.fc_logvar(pre_latent)
            pre_latent = reparameterize(mu, logvar)
        return pre_latent
    
    def decode(self, pre_latent):
        rec_pose = self.decoder(pre_latent)
        return rec_pose

class VAESKConv(VAEConv):
    def __init__(self, args):
        super(VAESKConv, self).__init__(args)
        smpl_fname = args.data_path_1+'smplx_models/smplx/SMPLX_NEUTRAL_2020.npz'
        smpl_data = np.load(smpl_fname, encoding='latin1')
        parents = smpl_data['kintree_table'][0].astype(np.int32)
        edges = build_edge_topology(parents)
        self.encoder = LocalEncoder(args, edges)
        self.decoder = VQDecoderV3(args)
        
class VAEConvMLP(VAEConv):
    def __init__(self, args):
        super(VAEConvMLP, self).__init__(args)
        self.encoder = PoseEncoderConv(args.vae_test_len, args.vae_test_dim, feature_length=args.vae_length)
        self.decoder = PoseDecoderConv(args.vae_test_len, args.vae_test_dim, feature_length=args.vae_length)
 
class VAELSTM(VAEConv):
    def __init__(self, args):
        super(VAELSTM, self).__init__(args)
        pose_dim = args.vae_test_dim
        feature_length = args.vae_length
        self.encoder = PoseEncoderLSTM_Resnet(pose_dim, feature_length=feature_length)
        self.decoder = PoseDecoderLSTM(pose_dim, feature_length=feature_length)

class VAETransformer(VAEConv):
    def __init__(self, args):
        super(VAETransformer, self).__init__(args)
        self.encoder = Encoder_TRANSFORMER(args)
        self.decoder = Decoder_TRANSFORMER(args)

# ----------- VQVAE --------------- #
class VQVAEConv(nn.Module):
    def __init__(self, args):
        super(VQVAEConv, self).__init__()
        self.encoder = VQEncoderV3(args)
        self.quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        self.decoder = VQDecoderV3(args)
        
    def forward(self, inputs):
        pre_latent = self.encoder(inputs)
        # print(pre_latent.shape)
        embedding_loss, vq_latent, _, perplexity = self.quantizer(pre_latent)
        rec_pose = self.decoder(vq_latent)
        return {
            "poses_feat":vq_latent,
            "embedding_loss":embedding_loss,
            "perplexity":perplexity,
            "rec_pose": rec_pose
            }
    
    def map2index(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        return index
    
    def map2latent(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        z_q = self.quantizer.get_codebook_entry(index)
        return z_q
    
    def decode(self, index):
        z_q = self.quantizer.get_codebook_entry(index)
        rec_pose = self.decoder(z_q)
        return rec_pose

class VQVAESKConv(VQVAEConv):
    def __init__(self, args):
        super(VQVAESKConv, self).__init__(args)
        smpl_fname = args.data_path_1+'smplx_models/smplx/SMPLX_NEUTRAL_2020.npz'
        smpl_data = np.load(smpl_fname, encoding='latin1')
        parents = smpl_data['kintree_table'][0].astype(np.int32)
        edges = build_edge_topology(parents)
        self.encoder = LocalEncoder(args, edges)


class VQVAEConvStride(nn.Module):
    def __init__(self, args):
        super(VQVAEConvStride, self).__init__()
        self.encoder = VQEncoderV4(args)
        self.quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        self.decoder = VQDecoderV4(args)
        
    def forward(self, inputs):
        pre_latent = self.encoder(inputs)
        # print(pre_latent.shape)
        embedding_loss, vq_latent, _, perplexity = self.quantizer(pre_latent)
        rec_pose = self.decoder(vq_latent)
        return {
            "poses_feat":vq_latent,
            "embedding_loss":embedding_loss,
            "perplexity":perplexity,
            "rec_pose": rec_pose
            }
    
    def map2index(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        return index
    
    def map2latent(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        z_q = self.quantizer.get_codebook_entry(index)
        return z_q
    
    def decode(self, index):
        z_q = self.quantizer.get_codebook_entry(index)
        rec_pose = self.decoder(z_q)
        return rec_pose

class VQVAEConvZero(nn.Module):
    def __init__(self, args):
        super(VQVAEConvZero, self).__init__()
        self.encoder = VQEncoderV5(args)
        self.quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        self.decoder = VQDecoderV5(args)
        
    def forward(self, inputs):
        pre_latent = self.encoder(inputs)
        # print(pre_latent.shape)
        embedding_loss, vq_latent, _, perplexity = self.quantizer(pre_latent)
        rec_pose = self.decoder(vq_latent)
        return {
            "poses_feat":vq_latent,
            "embedding_loss":embedding_loss,
            "perplexity":perplexity,
            "rec_pose": rec_pose
            }
    
    def map2index(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        return index
    
    def map2latent(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        z_q = self.quantizer.get_codebook_entry(index)
        return z_q
    
    def decode(self, index):
        z_q = self.quantizer.get_codebook_entry(index)
        rec_pose = self.decoder(z_q)
        return rec_pose
    

class VAEConvZero(nn.Module):
    def __init__(self, args):
        super(VAEConvZero, self).__init__()
        self.encoder = VQEncoderV5(args)
        # self.quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        self.decoder = VQDecoderV5(args)
        
    def forward(self, inputs):
        pre_latent = self.encoder(inputs)
        # print(pre_latent.shape)
        # embedding_loss, vq_latent, _, perplexity = self.quantizer(pre_latent)
        rec_pose = self.decoder(pre_latent)
        return {
            # "poses_feat":vq_latent,
            # "embedding_loss":embedding_loss,
            # "perplexity":perplexity,
            "rec_pose": rec_pose
            }
    
    # def map2index(self, inputs):
    #     pre_latent = self.encoder(inputs)
    #     index = self.quantizer.map2index(pre_latent)
    #     return index
    
    # def map2latent(self, inputs):
    #     pre_latent = self.encoder(inputs)
    #     index = self.quantizer.map2index(pre_latent)
    #     z_q = self.quantizer.get_codebook_entry(index)
    #     return z_q
    
    # def decode(self, index):
    #     z_q = self.quantizer.get_codebook_entry(index)
    #     rec_pose = self.decoder(z_q)
    #     return rec_pose


class VQVAEConvZero3(nn.Module):
    def __init__(self, args):
        super(VQVAEConvZero3, self).__init__()
        self.encoder = VQEncoderV5(args)
        self.quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        self.decoder = VQDecoderV5(args)
        
    def forward(self, inputs):
        pre_latent = self.encoder(inputs)
        # print(pre_latent.shape)
        embedding_loss, vq_latent, _, perplexity = self.quantizer(pre_latent)
        rec_pose = self.decoder(vq_latent)
        return {
            "poses_feat":vq_latent,
            "embedding_loss":embedding_loss,
            "perplexity":perplexity,
            "rec_pose": rec_pose
            }
    
    def map2index(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        return index
    
    def map2latent(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        z_q = self.quantizer.get_codebook_entry(index)
        return z_q
    
    def decode(self, index):
        z_q = self.quantizer.get_codebook_entry(index)
        rec_pose = self.decoder(z_q)
        return rec_pose

class VQVAEConvZero2(nn.Module):
    def __init__(self, args):
        super(VQVAEConvZero2, self).__init__()
        self.encoder = VQEncoderV5(args)
        self.quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        self.decoder = VQDecoderV7(args)
        
    def forward(self, inputs):
        pre_latent = self.encoder(inputs)
        # print(pre_latent.shape)
        embedding_loss, vq_latent, _, perplexity = self.quantizer(pre_latent)
        rec_pose = self.decoder(vq_latent)
        return {
            "poses_feat":vq_latent,
            "embedding_loss":embedding_loss,
            "perplexity":perplexity,
            "rec_pose": rec_pose
            }
    
    def map2index(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        return index
    
    def map2latent(self, inputs):
        pre_latent = self.encoder(inputs)
        index = self.quantizer.map2index(pre_latent)
        z_q = self.quantizer.get_codebook_entry(index)
        return z_q
    
    def decode(self, index):
        z_q = self.quantizer.get_codebook_entry(index)
        rec_pose = self.decoder(z_q)
        return rec_pose

class VQVAE2(nn.Module):
    def __init__(self, args):
        super(VQVAE2, self).__init__()
        # Bottom-level encoder and decoder
        args_bottom = copy.deepcopy(args)
        args_bottom.vae_layer = 2
        self.bottom_encoder = VQEncoderV6(args_bottom)
        self.bottom_quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        args_bottom.vae_test_dim = args.vae_test_dim
        self.bottom_decoder = VQDecoderV6(args_bottom)
        
        # Top-level encoder and decoder
        args_top = copy.deepcopy(args)
        args_top.vae_layer = 3
        args_top.vae_test_dim = args.vae_length
        self.top_encoder = VQEncoderV3(args_top)  # Adjust according to the top level's design
        self.quantize_conv_t = nn.Conv1d(args.vae_length+args.vae_length, args.vae_length, 1)
        self.top_quantizer = Quantizer(args.vae_codebook_size, args.vae_length, args.vae_quantizer_lambda)
        # self.upsample_t_up = nn.Upsample(scale_factor=2, mode='nearest')
        layers = [
                nn.Upsample(scale_factor=2, mode='nearest'),
                nn.Conv1d(args.vae_length, args.vae_length, kernel_size=3, stride=1, padding=1),
                nn.LeakyReLU(0.2, inplace=True),
                nn.Upsample(scale_factor=2, mode='nearest'),
                nn.Conv1d(args.vae_length, args.vae_length, kernel_size=3, stride=1, padding=1),
                nn.LeakyReLU(0.2, inplace=True),
                nn.Upsample(scale_factor=2, mode='nearest'),
                nn.Conv1d(args.vae_length, args.vae_length, kernel_size=3, stride=1, padding=1),
                nn.LeakyReLU(0.2, inplace=True)
            ]
        self.upsample_t= nn.Sequential(*layers)
        self.top_decoder = VQDecoderV3(args_top)  # Adjust to handle top level features appropriately

    def forward(self, inputs):
        # Bottom-level processing
        enc_b = self.bottom_encoder(inputs)
        enc_t = self.top_encoder(enc_b)
        #print(enc_b.shape, enc_t.shape)
        top_embedding_loss, quant_t, _, top_perplexity = self.top_quantizer(enc_t)
        #print(quant_t.shape)
        dec_t = self.top_decoder(quant_t)
        #print(dec_t.shape)
        enc_b = torch.cat([dec_t, enc_b], dim=2).permute(0,2,1)
        #print(enc_b.shape)
        quant_b = self.quantize_conv_t(enc_b).permute(0,2,1)
        #print("5",quant_b.shape)
        bottom_embedding_loss, quant_b, _, bottom_perplexity = self.bottom_quantizer(quant_b)
        #print("6",quant_b.shape)
        upsample_t = self.upsample_t(quant_t.permute(0,2,1)).permute(0,2,1)
        #print("7",upsample_t.shape)
        quant = torch.cat([upsample_t, quant_b], 2)
        rec_pose = self.bottom_decoder(quant)
        # print(quant_t.shape, quant_b.shape, rec_pose.shape)
        return {
            "poses_feat_top": quant_t,
            "pose_feat_bottom": quant_b,
            "embedding_loss":top_embedding_loss+bottom_embedding_loss,
            #"perplexity":perplexity,
            "rec_pose": rec_pose
            }
    
    def map2index(self, inputs):
        enc_b = self.bottom_encoder(inputs)
        enc_t = self.top_encoder(enc_b)
        
        _, quant_t, _, _ = self.top_quantizer(enc_t)
        top_index = self.top_quantizer.map2index(enc_t)
        dec_t = self.top_decoder(quant_t)

        enc_b = torch.cat([dec_t, enc_b], dim=2).permute(0,2,1)
        #print(enc_b.shape)
        quant_b = self.quantize_conv_t(enc_b).permute(0,2,1)
        # quant_b = self.quantize_conv_t(enc_b)
        bottom_index = self.bottom_quantizer.map2index(quant_b)
        return top_index, bottom_index
    
    def get_top_laent(self, top_index):
        z_q_top = self.top_quantizer.get_codebook_entry(top_index)
        return z_q_top
    
    def map2latent(self, inputs):
        enc_b = self.bottom_encoder(inputs)
        enc_t = self.top_encoder(enc_b)
        
        _, quant_t, _, _ = self.top_quantizer(enc_t)
        top_index = self.top_quantizer.map2index(enc_t)
        dec_t = self.top_decoder(quant_t)

        enc_b = torch.cat([dec_t, enc_b], dim=2).permute(0,2,1)
        #print(enc_b.shape)
        quant_b = self.quantize_conv_t(enc_b).permute(0,2,1)
        # quant_b = self.quantize_conv_t(enc_b)
        bottom_index = self.bottom_quantizer.map2index(quant_b)
        z_q_top = self.top_quantizer.get_codebook_entry(top_index)
        z_q_bottom = self.bottom_quantizer.get_codebook_entry(bottom_index)
        return z_q_top, z_q_bottom
    
    def map2latent_top(self, inputs):
        enc_b = self.bottom_encoder(inputs)
        enc_t = self.top_encoder(enc_b)
        top_index = self.top_quantizer.map2index(enc_t)
        z_q_top = self.top_quantizer.get_codebook_entry(top_index)
        return z_q_top
    
    def decode(self, top_index, bottom_index):
        quant_t = self.top_quantizer.get_codebook_entry(top_index)
        quant_b = self.bottom_quantizer.get_codebook_entry(bottom_index)
        upsample_t = self.upsample_t(quant_t.permute(0,2,1)).permute(0,2,1)
        #print("7",upsample_t.shape)
        quant = torch.cat([upsample_t, quant_b], 2)
        rec_pose = self.bottom_decoder(quant)      
        return rec_pose