Exemplo n.º 1
0
    def __init__(self,
                 config,
                 latent_code_garms_sz=1024,
                 garmparams_sz=config.PCA_,
                 name=None):
        super(PoseShapeOffsetModel, self).__init__(name=name)

        self.config = config
        self.latent_code_garms_sz = latent_code_garms_sz
        self.garmparams_sz = garmparams_sz
        self.latent_code_betas_sz = 128

        # ToDo: Minor: Remove hard coded colors. Should be same as rendered colors in input
        self.colormap = tf.cast([
            np.array([255, 255, 255]),
            np.array([65, 0, 65]),
            np.array([0, 65, 65]),
            np.array([145, 65, 0]),
            np.array([145, 0, 65]),
            np.array([0, 145, 65])
        ], tf.float32) / 255.
        with open('assets/hresMapping.pkl', 'rb') as f:
            _, self.faces = pkl.load(f, encoding="latin1")
        self.faces = np.int32(self.faces)

        # Define network layers
        self.top_ = SingleImageNet(self.latent_code_garms_sz,
                                   self.latent_code_betas_sz)

        for n in self.config.garmentKeys:
            gn = GarmentNet(self.config.PCA_, n, self.garmparams_sz)
            self.garmentModels.append(gn)
        self.smpl = SMPL('assets/neutral_smpl.pkl',
                         theta_in_rodrigues=False,
                         theta_is_perfect_rotmtx=False,
                         isHres=True,
                         scale=True)
        self.smpl_J = SmplBody25Layer(theta_in_rodrigues=False,
                                      theta_is_perfect_rotmtx=False,
                                      isHres=True)
        self.J_layers = [NameLayer('J_{}'.format(i)) for i in range(NUM)]

        self.lat_betas = Dense(self.latent_code_betas_sz,
                               kernel_initializer=initializers.RandomNormal(
                                   0, 0.00005),
                               activation='relu')
        self.betas = Dense(10,
                           kernel_initializer=initializers.RandomNormal(
                               0, 0.000005),
                           name='betas')

        init_trans = np.array([0, 0.2, -2.])
        init_pose = np.load('assets/mean_a_pose.npy')
        init_pose[:3] = 0
        init_pose = tf.reshape(
            batch_rodrigues(init_pose.reshape(-1, 3).astype(np.float32)),
            (-1, ))
        self.pose_trans = tf.concat((init_pose, init_trans), axis=0)

        self.lat_pose = Dense(self.latent_code_betas_sz,
                              kernel_initializer=initializers.RandomNormal(
                                  0, 0.000005),
                              activation='relu')
        self.lat_pose_layer = Dense(
            24 * 3 * 3 + 3,
            kernel_initializer=initializers.RandomNormal(0, 0.000005),
            name='pose_trans')
        self.cut_trans = Lambda(lambda z: z[:, -3:])
        self.trans_layers = [
            NameLayer('trans_{}'.format(i)) for i in range(NUM)
        ]

        self.cut_poses = Lambda(lambda z: z[:, :-3])
        self.reshape_pose = Reshape((24, 3, 3))
        self.pose_layers = [NameLayer('pose_{}'.format(i)) for i in range(NUM)]

        # Optional: Condition garment on betas, probably not
        self.latent_code_offset_ShapeMerged = Dense(self.latent_code_garms_sz +
                                                    self.latent_code_betas_sz,
                                                    activation='relu')
        self.latent_code_offset_ShapeMerged_2 = Dense(
            self.latent_code_garms_sz + self.latent_code_betas_sz,
            activation='relu',
            name='latent_code_offset_ShapeMerged')

        self.avg = Average()
        self.flatten = Flatten()
        self.concat = Concatenate()

        self.scatters = []
        for vs in self.vertSpread:
            self.scatters.append(Scatter_(vs, self.config.NVERTS))
Exemplo n.º 2
0
import os
import pickle

import numpy as np
import torch
from block_timer.timer import Timer

from smpl.batch_smpl import SMPL

with open('../mpi/data/mano/MANO_RIGHT_py3.pkl', 'rb') as f:
    mano_data = pickle.load(f, encoding='latin1')

mano = SMPL(mano_data).cuda()  # not good


def get_mano_vertices(shape, pose, device=torch.device('cpu')):
    """
    :param shape: mano shape params [batch_size, 10]
    :param pose: mano pose params including global rotation (joint axis-angle representation) [batch_size, 45+3]
    :return:
    """
    # check if not tensor: wrap
    if not isinstance(shape, torch.Tensor):
        shape = torch.tensor(shape, dtype=torch.float).cuda()

    if not isinstance(pose, torch.Tensor):
        pose = torch.tensor(pose, dtype=torch.float).cuda()

    verts, joints, Rs = mano(shape, pose, get_skin=True)
    return verts.cpu().detach().numpy()