File size: 1,447 Bytes
c3d0293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import numpy as np
import torch
import torch.nn as nn
from copy import deepcopy

# A wrapper model for Classifier-free guidance **SAMPLING** only
# https://arxiv.org/abs/2207.12598
class ClassifierFreeSampleModel(nn.Module):

    def __init__(self, model):
        super().__init__()
        self.model = model  # model is the actual model to run

        # assert self.model.cond_mask_prob > 0, 'Cannot run a guided diffusion on a model that has not been trained with no conditions'

        # pointers to inner model
        self.njoints = self.model.njoints
        self.nfeats = self.model.nfeats
        self.cond_mode = self.model.cond_mode

    def forward(self, x, timesteps, y=None):
        cond_mode = self.model.cond_mode
        assert cond_mode in ['text', 'action', "motion", "text-motion"]
        y_uncond = deepcopy(y)
        y_uncond['uncond'] = True

        out = self.model(x, timesteps, y)                   ###### 全部条件生成

        if "predict_length" in out.keys():
            y_uncond["predict_mask"] = out["predict_length"]

        out_uncond = self.model(x, timesteps, y_uncond)     ####### 全部无条件

        output = {}

        y['scale'] = y['scale'].to(out_uncond["output"].device)

        output["output"] = out_uncond["output"] + (y['scale'].view(-1, 1, 1, 1) * (out["output"] - out_uncond["output"]))
        
        return output       ##### 这里并不是生成 \epsilon,而是特征