CoolFace
Apppublic

Kleinhe/SemanticBoost

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
Encode_Full.py80 linesDownload Raw Back to model
1from torch import nn2import torch3import torch.nn.functional as F4from motion.model.layer_norm_fp16 import RMSNorm, LayerNorm5 6class ResConv1DBlock(nn.Module):7    def __init__(self, n_in, n_state, bias, norm_type, activate_type):8        super().__init__()9 10        if activate_type.lower() == "silu":11            activate = nn.SiLU()12        elif activate_type.lower() == "relu":13            activate = nn.ReLU()14        elif activate_type.lower() == "gelu":15            activate = nn.GELU()16        elif activate_type.lower() == "mish":17            activate = nn.Mish()18        19        if norm_type.lower() == "rmsnorm":20            norm = RMSNorm21        elif norm_type.lower() == "layernorm":22            norm = LayerNorm23 24        self.norm1 = norm(n_state)25        self.norm2 = norm(n_in)26        self.relu1 = activate27        self.relu2 = activate28        self.conv1 = nn.Conv1d(n_in, n_state, 3, 1, 1, bias=bias)29        self.conv2 = nn.Conv1d(n_state, n_in, 1, 1, 0, bias=bias)     30 31    def forward(self, x):32        x_orig = x33        x = self.conv1(x)34        x = self.norm1(x.transpose(-2, -1))35        x = self.relu1(x.transpose(-2, -1))36 37        x = self.conv2(x)38        x = self.norm2(x.transpose(-2, -1))39        x = self.relu2(x.transpose(-2, -1))40        41        x = x + x_orig42        return x43 44class Encoder_Block(nn.Module):45    def __init__(self, begin_channel=263, latent_dim=512, num_layers=6, TN=1, bias=False, norm_type="rmsnorm", activate_type="silu"):46        super(Encoder_Block, self).__init__()47        self.layers = []48 49        begin_channel = begin_channel50        target_channel = latent_dim51 52        if activate_type.lower() == "silu":53            activate = nn.SiLU()54        elif activate_type.lower() == "relu":55            activate = nn.ReLU()56        elif activate_type.lower() == "gelu":57            activate = nn.GELU()58        elif activate_type.lower() == "mish":59            activate = nn.Mish()60        61        self.layers.append(nn.Conv1d(begin_channel, target_channel, 3, 2, 1, bias=bias))62        self.layers.append(activate)63 64        for _ in range(num_layers):      ### 196 -> 98 -> 49 -> 24 -> 12 -> 6 -> 365            self.layers.append(nn.Conv1d(target_channel, target_channel, 3, 2, 1, bias=bias))66            self.layers.append(activate)67            self.layers.append(ResConv1DBlock(target_channel, target_channel, bias, norm_type, activate_type))68 69        self.layers = nn.Sequential(*self.layers)70        self.maxpool = nn.AdaptiveMaxPool1d(TN)71 72    def forward(self, x): 73        bs, njoints, nfeats, nframes = x.shape74        reshaped_x = x.reshape(bs, njoints * nfeats, nframes)      ### [bs, 263, seq]75 76        res1 = self.layers(reshaped_x)      #### [bs, 512, 1]77        res2 = self.maxpool(res1)78 79        res3 = res2.permute(2, 0, 1)80        return res3