import os import sys import time import argparse import numpy as np import scipy.io as sio import scipy as sp import plotly.graph_objects as go from scipy.spatial.transform import Rotation from tqdm import tqdm os.environ["CUDA_VISIBLE_DEVICES"] = "1" import tensorflow as tf gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: try: for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) except RuntimeError as e: print(e) from custom_utils.smpl import SMPL from custom_utils.supersmpl_io import readPC2 # run = wandb.init(config=tf.compat.v1.flags.FLAGS, sync_tensorboard=True, project='unpose', name='new_smpl_2', notes='') # artifact = wandb.Artifact('code', type='code') # artifact.add_file('Data/unpose.py') # artifact.add_file('Data/verify_unposed.py') # artifact.add_file('FeatureLines/superSMPL/smpl.py') # artifact.add_file('FeatureLines/superSMPL/smpl_2.py') # run.log_artifact(artifact) def read_garment(folder, id): min_body = np.load(os.path.join(folder, id, id + '_minimal.npy')) # data = np.load(os.path.join('/home/workspace/data/dataset/cape_release', 'sequences', id)) return min_body def plot_3d(mesh_orig, mesh_pred, unposed, faces): _mesh_pred = mesh_pred.copy() _unposed = unposed.copy() _mesh_pred[:,1] += 1 _unposed[:,1] += 3 fig = go.Figure(data=[ go.Mesh3d( # 8 vertices of a cube x=mesh_orig[:,0], y=mesh_orig[:,1], z=mesh_orig[:,2], # i, j and k give the vertices of triangles i = faces[:,0], j = faces[:,1], k = faces[:,2] ), go.Mesh3d( # 8 vertices of a cube x=_mesh_pred[:,0], y=_mesh_pred[:,1], z=_mesh_pred[:,2], # i, j and k give the vertices of triangles i = faces[:,0], j = faces[:,1], k = faces[:,2], ), go.Mesh3d( # 8 vertices of a cube x=_unposed[:,0], y=_unposed[:,1], z=_unposed[:,2], # i, j and k give the vertices of triangles i = faces[:,0], j = faces[:,1], k = faces[:,2], # vertexcolor=collision_color ) ]) fig.update_layout(scene_aspectmode='manual', scene_aspectratio=dict(x=1, y=2, z=1)) fig.update_layout( scene = dict( xaxis = dict(nticks=4, range=[-1,1],), yaxis = dict(nticks=4, range=[-1,3],), zaxis = dict(nticks=4, range=[-1,1],),) ) return fig if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-f", "--folder", help = "end of range", default='/home/workspace/data/dataset/cape_release/minimal_body_shape') parser.add_argument("-s", "--start", help = "start of range", default=0) parser.add_argument("-e", "--end", help = "end of range", default=200) parser.add_argument("-n", "--name", help = "end of range", default='lhunor') args = parser.parse_args() F = np.load('/home/workspace/data/dataset/cape_release/misc/smpl_tris.npy') gender_table = np.load('/home/workspace/data/dataset/cape_release/misc/subj_genders.pkl', allow_pickle=True) folder = args.folder smpl = SMPL() iterations = 10000 ids = os.listdir(folder)[int(args.start):int(args.end)] for id in tqdm(ids): if not os.path.isdir(os.path.join(folder, id)): continue min_body = read_garment(folder, id) pose = tf.constant(np.zeros((1, 72)), dtype=np.float32) R = smpl.run_rodrigues(pose) # lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(10.0, decay_steps=40000, decay_rate=0.1, staircase=False) optimizer = tf.keras.optimizers.SGD(learning_rate=25.0) betas = tf.Variable(np.zeros((1, 10), np.float32)) gender = np.zeros((1,1), np.float32) if gender_table[id] == 'female' else np.ones((1,1), np.float32) @tf.function def shaping(shape): _shape = tf.concat((shape, gender), axis=-1) result = smpl.run(R, _shape, remove=False)[0][0, :6890, :] return result @tf.function def error(): pred = shaping(betas) loss = tf.reduce_mean(tf.math.abs(pred - min_body)) return loss start = time.perf_counter() euclid_error = 10000 while True: optimizer.minimize(error, [betas]) i = optimizer.iterations.numpy() if i % 100 == 0: pred = shaping(betas) # loss = tf.reduce_mean(tf.reduce_sum(tf.math.abs(pred - posed) ** 2, axis=-1)) loss = tf.reduce_mean(tf.math.abs(pred - min_body)) euclid_error = tf.reduce_mean(tf.math.sqrt(tf.reduce_sum((min_body - pred) ** 2, axis=-1))) print('id: ' + id + ' iteration: ' + str(i) + ' loss: ' + str(loss.numpy()) + ' error: ' + str(euclid_error.numpy()) + ' time: ' + str(time.perf_counter() - start)) start = time.perf_counter() if euclid_error < 0.0001: break if euclid_error < 0.0005 and i > 20000: break if euclid_error < 0.001 and i > 40000: break if i > 60000: break x = min_body y = pred faces = F fig = go.Figure(data=[ go.Mesh3d( # 8 vertices of a cube x=x[:,0], y=x[:,1], z=x[:,2], # i, j and k give the vertices of triangles i = faces[:,0], j = faces[:,1], k = faces[:,2] ), go.Mesh3d( # 8 vertices of a cube x=y[:,0], y=y[:,1], z=y[:,2], # i, j and k give the vertices of triangles i = faces[:,0], j = faces[:,1], k = faces[:,2] ), ]) fig.update_layout(scene_aspectmode='manual', scene_aspectratio=dict(x=1, y=1, z=1)) fig.update_layout( scene = dict( xaxis = dict(nticks=4, range=[-1.5,1.5],), yaxis = dict(nticks=4, range=[-1.5,1.5],), zaxis = dict(nticks=4, range=[-1.5,1.5],),) ) fig.show() continue offset = 1 for frame in range(posed.shape[0]): lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(10.0, decay_steps=10000, decay_rate=0.1, staircase=False) optimizer = tf.keras.optimizers.SGD(learning_rate=lr_schedule) unposed = tf.Variable(np.random.rand(offset, posed.shape[1], 3).astype(np.float32) * 2 - 1) # unposed = tf.Variable(init) @tf.function def skinning(verts): # verts = smpl.run(R[frame:frame+offset], tf.tile(shape[tf.newaxis,:], [offset,1]))[0].numpy()[:,mask,:] # start = time.perf_counter() result = smpl.run_garment(R[frame:frame+offset], tf.tile(shape[tf.newaxis,:], [offset,1]), verts, filter_idx=mask_idx) # print(time.perf_counter() - start) # start = time.perf_counter() # result_2 = smpl_2.run_garment(R[frame:frame+offset], shape[:10], gender, verts, filter_idx=mask_idx) # print(time.perf_counter() - start) # test = np.abs(result.numpy() - result_2.numpy()) > 1e-4 # x = np.sum(test, axis=-1).sum(-1).sum(-1) # euclid_error = tf.reduce_mean(tf.math.sqrt(tf.reduce_sum((result - result_2) ** 2, axis=-1))) # batch = 25 # _, faces = _maskFaces(np.zeros((14475,3)), F, mask) # go.Figure(data=[ # go.Mesh3d( # # 8 vertices of a cube # x=result[batch,:,0], # y=result[batch,:,1], # z=result[batch,:,2], # # i, j and k give the vertices of triangles # i = faces[:,0], # j = faces[:,1], # k = faces[:,2] # ), # go.Mesh3d( # # 8 vertices of a cube # x=result_2[batch,:,0], # y=result_2[batch,:,1], # z=result_2[batch,:,2], # # i, j and k give the vertices of triangles # i = faces[:,0], # j = faces[:,1], # k = faces[:,2], # )]).show() return result @tf.function def error(): pred = skinning(unposed) loss = tf.reduce_mean(tf.math.abs(pred - posed[frame])) return loss start = time.perf_counter() euclid_error = 10000 for i in range(iterations): optimizer.minimize(error, [unposed]) if i % 50 == 0: pred = skinning(unposed) # loss = tf.reduce_mean(tf.reduce_sum(tf.math.abs(pred - posed) ** 2, axis=-1)) loss = tf.reduce_mean(tf.math.abs(pred - posed[frame])) euclid_error = tf.reduce_mean(tf.math.sqrt(tf.reduce_sum((posed[frame] - pred) ** 2, axis=-1))) print('id: ' + id + ' frame: ' + str(frame) + ' iteration: ' + str(i) + ' loss: ' + str(loss.numpy()) + ' error: ' + str(euclid_error.numpy()) + ' time: ' + str(time.perf_counter() - start)) start = time.perf_counter() if euclid_error < 0.001: break result = np.zeros((14475, 3), dtype = np.float32) result[mask,:] = unposed.numpy() np.savez_compressed(os.path.join(folder, id, 'mesh_unposed', id + '_' + str(frame) + '.npz'), mesh_unposed=result) init = unposed # pred, unposed = pred.numpy(), unposed.numpy() # _, faces = _maskFaces(np.zeros((14475,3)), F, mask) # k = 0 # plot_3d(posed[k], pred[k], unposed[k], faces).show() # print()