274 lines
11 KiB
Python
274 lines
11 KiB
Python
import os
|
|
import sys
|
|
from configs.config_abl_all_skirt import config as config_a
|
|
from configs.config_abl_extra_loss_skirt import config as config_b
|
|
from configs.config_abl_norm_skirt import config as config_c
|
|
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = '6'
|
|
import tensorflow as tf
|
|
|
|
gpus = tf.config.experimental.list_physical_devices('GPU')
|
|
if gpus:
|
|
try:
|
|
# Currently, memory growth needs to be the same across GPUs
|
|
for gpu in gpus:
|
|
tf.config.experimental.set_memory_growth(gpu, True)
|
|
logical_gpus = tf.config.experimental.list_logical_devices('GPU')
|
|
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
|
|
except RuntimeError as e:
|
|
# Memory growth must be set before GPUs have been initialized
|
|
print(e)
|
|
|
|
import glob
|
|
import time
|
|
import argparse
|
|
import wandb
|
|
import tqdm
|
|
import random
|
|
import numpy as np
|
|
import plotly.graph_objects as go
|
|
import plotly.express as ex
|
|
from PIL import Image
|
|
from postprocessing import fix_collision
|
|
|
|
if config_a['is_skirt']:
|
|
from losses import edge_loss_skirt as edge_loss
|
|
else:
|
|
from losses import edge_loss
|
|
|
|
|
|
|
|
from losses import collision_ratio, kl_loss #, concept_loss
|
|
# from losses import collision_penalty as collision_penalty
|
|
from losses import collision_loss as collision_penalty
|
|
from custom_utils.custom_utils import batch_to_dict, load_model, plot_3d, UV2Mesh, get_summary_html, maskFacesBatch, _maskFaces, plot_3d_collision
|
|
from dataset import DatasetFactory, StaticDataset
|
|
from custom_utils.smpl import SMPL
|
|
from models.cvae_model import CVAE
|
|
from custom_utils.supersmpl_io import writeOBJ
|
|
from plotly.subplots import make_subplots
|
|
|
|
|
|
uv2mesh = UV2Mesh(res=256, is_skirt=config_a['is_skirt'], is_adaptive=False)
|
|
|
|
|
|
|
|
def plot(mesh_gt, pred_a, pred_b, pred_c, body, mask):
|
|
fig = make_subplots(rows=1, cols=4, start_cell="top-left", specs = [ [{ 'type': 'scene' } for _ in range(4)] ])
|
|
|
|
_, faces_garment = _maskFaces(np.zeros((14475, 3), np.float32), F, mask)
|
|
faces = np.concatenate((F, faces_garment + 14475), axis=0)
|
|
mesh_gt = np.concatenate((body, mesh_gt[mask, :]), axis=0)
|
|
pred_a = np.concatenate((body, pred_a[mask, :]), axis=0)
|
|
pred_b = np.concatenate((body, pred_b[mask, :]), axis=0)
|
|
pred_c = np.concatenate((body, pred_c[mask, :]), axis=0)
|
|
|
|
colors = np.concatenate((np.ones_like(body, dtype=np.int32)*100, np.ones_like(mesh_gt, dtype=np.int32)*230), axis=0)
|
|
|
|
fig.add_trace(go.Mesh3d(
|
|
x=mesh_gt[:,0],
|
|
y=mesh_gt[:,2],
|
|
z=mesh_gt[:,1],
|
|
i = faces[:,0],
|
|
j = faces[:,1],
|
|
k = faces[:,2],
|
|
text="Ground Truth",
|
|
vertexcolor=colors,
|
|
),
|
|
row=1, col=1)
|
|
|
|
fig.add_trace(go.Mesh3d(
|
|
x=pred_a[:,0],
|
|
y=pred_a[:,2],
|
|
z=pred_a[:,1],
|
|
i = faces[:,0],
|
|
j = faces[:,1],
|
|
k = faces[:,2],
|
|
text="Pred A",
|
|
vertexcolor=colors,
|
|
),
|
|
row=1, col=2)
|
|
|
|
fig.add_trace(go.Mesh3d(
|
|
x=pred_b[:,0],
|
|
y=pred_b[:,2],
|
|
z=pred_b[:,1],
|
|
i = faces[:,0],
|
|
j = faces[:,1],
|
|
k = faces[:,2],
|
|
text="Pred B",
|
|
vertexcolor=colors,
|
|
),
|
|
row=1, col=3)
|
|
|
|
fig.add_trace(go.Mesh3d(
|
|
x=pred_c[:,0],
|
|
y=pred_c[:,2],
|
|
z=pred_c[:,1],
|
|
i = faces[:,0],
|
|
j = faces[:,1],
|
|
k = faces[:,2],
|
|
text="Pred C",
|
|
vertexcolor=colors,
|
|
),
|
|
row=1, col=4)
|
|
|
|
|
|
fig.update_layout(scene_aspectmode='manual', scene_aspectratio=dict(x=1, y=1, z=1),
|
|
scene2_aspectmode='manual', scene2_aspectratio=dict(x=1, y=1, z=1),
|
|
scene3_aspectmode='manual', scene3_aspectratio=dict(x=1, y=1, z=1),
|
|
scene4_aspectmode='manual', scene4_aspectratio=dict(x=1, y=1, z=1),
|
|
)
|
|
helper = dict(xaxis = dict(nticks=4, range=[-1,1],),
|
|
yaxis = dict(nticks=4, range=[-1,1],),
|
|
zaxis = dict(nticks=4, range=[-1,1],),)
|
|
fig.update_layout(scene = helper,
|
|
scene2 = helper,
|
|
scene3 = helper,
|
|
scene4 = helper,
|
|
)
|
|
|
|
return fig
|
|
|
|
def step(model, config, batch):
|
|
if config['use_skinning']:
|
|
uv_orig = batch['uv_unposed']
|
|
else:
|
|
uv_orig = batch['uv_posed']
|
|
|
|
pred, _, _ = model([uv_orig, batch['uv_static'], batch['uv_body_posed'], batch['uv_curvature'], batch['uv_mask']], training=False)
|
|
|
|
mesh_pred = uv2mesh(pred)
|
|
|
|
if config['use_skinning']:
|
|
shape = batch['pose_shape'][:, 72:]
|
|
gender = tf.cast(tf.greater_equal(shape[:, 10] , 0.5), tf.int32)
|
|
shape_offsets = tf.stack([tf.gather(tf.tensordot(shape[i,:10][tf.newaxis, :], smpl.shapedirs[gender[i]], axes=[[1], [2]])[0,:,:], smpl.to_keep, axis=0) for i in range(shape.shape[0])])
|
|
|
|
mesh_pred_shaped = mesh_pred + shape_offsets * batch['mesh_mask'][:,:,tf.newaxis]
|
|
mesh_pred_posed = smpl.run_garment(batch['pose_shape'][:, :72], batch['pose_shape'][:, 72:], mesh_pred_shaped)
|
|
else:
|
|
mesh_pred_posed = mesh_pred
|
|
|
|
# mesh_pred_posed_fixed = fix_collision(mesh_pred_posed, batch['mesh_body_posed'], batch['mesh_mask'])
|
|
|
|
return mesh_pred_posed, None #mesh_pred_posed_fixed
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# init_wandb()
|
|
parser = argparse.ArgumentParser(description='')
|
|
parser.add_argument('--name', type=str)
|
|
args = vars(parser.parse_args())
|
|
|
|
skirt_modifier = '_skirt' if config_a['is_skirt'] else ''
|
|
|
|
folder = config_a['test_dir']
|
|
smpl = SMPL(use_one_hot=False)
|
|
|
|
model_a = CVAE(config_a)
|
|
model_b = CVAE(config_b)
|
|
model_c = CVAE(config_c)
|
|
|
|
|
|
F = np.load("custom_utils/smpl_faces" + skirt_modifier + ".npy").astype(np.int32)
|
|
|
|
iteration = '70000'
|
|
|
|
load_model(model_a, 'checkpoints/' + config_a['name'], iteration=iteration)
|
|
load_model(model_b, 'checkpoints/' + config_b['name'], iteration=iteration)
|
|
load_model(model_c, 'checkpoints/' + config_c['name'], iteration=iteration)
|
|
# load_model(model, 'checkpoints/' + 'abl_all_120k')
|
|
|
|
assert config_a['remove_global_orientation'] == config_b['remove_global_orientation'] == config_c['remove_global_orientation']
|
|
|
|
factory = DatasetFactory()
|
|
dataset_a = factory.get_dataset(folder=config_a['test_dir'],
|
|
batch_size=config_a['batch_size'],
|
|
signature=config_a['signature_dynamic'],
|
|
resolutions=[config_a['resolution']],
|
|
in_memory=False,
|
|
do_repeat=False,
|
|
use_one_hot=config_a['use_one_hot'],
|
|
use_no_orient=config_a['remove_global_orientation'],
|
|
shuffle=False,
|
|
testing=True,
|
|
# frames_file='frames_abl.txt',
|
|
)
|
|
static_dataset_a = StaticDataset(folder=config_a['test_dir'],
|
|
signature=config_a['signature_static'],
|
|
resolutions=[config_a['resolution']],
|
|
in_memory=True)
|
|
|
|
dataset_b = factory.get_dataset(folder=config_b['test_dir'],
|
|
batch_size=config_b['batch_size'],
|
|
signature=config_b['signature_dynamic'],
|
|
resolutions=[config_b['resolution']],
|
|
in_memory=False,
|
|
do_repeat=False,
|
|
use_one_hot=config_b['use_one_hot'],
|
|
use_no_orient=config_b['remove_global_orientation'],
|
|
shuffle=False,
|
|
testing=True,
|
|
# frames_file='frames_abl.txt',
|
|
)
|
|
static_dataset_b = StaticDataset(folder=config_b['test_dir'],
|
|
signature=config_b['signature_static'],
|
|
resolutions=[config_b['resolution']],
|
|
in_memory=True)
|
|
|
|
dataset_c = factory.get_dataset(folder=config_c['test_dir'],
|
|
batch_size=config_c['batch_size'],
|
|
signature=config_c['signature_dynamic'],
|
|
resolutions=[config_c['resolution']],
|
|
in_memory=False,
|
|
do_repeat=False,
|
|
use_one_hot=config_c['use_one_hot'],
|
|
use_no_orient=config_c['remove_global_orientation'],
|
|
shuffle=False,
|
|
testing=True,
|
|
# frames_file='frames_abl.txt',
|
|
)
|
|
static_dataset_c = StaticDataset(folder=config_c['test_dir'],
|
|
signature=config_c['signature_static'],
|
|
resolutions=[config_c['resolution']],
|
|
in_memory=True)
|
|
|
|
iterator_a = iter(dataset_a)
|
|
iterator_b = iter(dataset_b)
|
|
iterator_c = iter(dataset_c)
|
|
|
|
|
|
faces_folder = '/home/workspace/data/results/support/faces' + ('_skirt' if config_a['is_skirt'] else '')
|
|
ids = os.listdir(faces_folder)
|
|
ids = [id.split('.')[0] for id in ids]
|
|
faces = {id : np.load(faces_folder + '/' + id + '.npy') for id in ids }
|
|
|
|
|
|
while True:
|
|
batch_a = iterator_a.get_next()
|
|
batch_b = iterator_b.get_next()
|
|
batch_c = iterator_c.get_next()
|
|
|
|
batch_a = static_dataset_a.expand_batch(batch_to_dict(batch_a, config_a['signature_dynamic']))
|
|
batch_b = static_dataset_b.expand_batch(batch_to_dict(batch_b, config_b['signature_dynamic']))
|
|
batch_c = static_dataset_c.expand_batch(batch_to_dict(batch_c, config_c['signature_dynamic']))
|
|
|
|
mesh_pred_posed_a, mesh_pred_posed_fixed_a = step(model_a, config_a, batch_a)
|
|
mesh_pred_posed_b, mesh_pred_posed_fixed_b = step(model_b, config_b, batch_b)
|
|
mesh_pred_posed_c, mesh_pred_posed_fixed_c = step(model_c, config_c, batch_c)
|
|
|
|
mesh_pred_posed_a = mesh_pred_posed_a.numpy()
|
|
mesh_pred_posed_b = mesh_pred_posed_b.numpy()
|
|
mesh_pred_posed_c = mesh_pred_posed_c.numpy()
|
|
mask = batch_a['mesh_mask'].numpy().astype(bool)
|
|
gt = batch_a['mesh_posed'].numpy()
|
|
body = batch_a['mesh_body_posed'].numpy()
|
|
|
|
for i in range(mask.shape[0]):
|
|
id, frame_nr = batch_a['id'][i].numpy().decode("utf-8"), batch_a['frame_nr'][i].numpy().decode("utf-8")
|
|
plot(gt[i], mesh_pred_posed_a[i], mesh_pred_posed_b[i], mesh_pred_posed_c[i], body[i], mask[i]).show()
|
|
|
|
print()
|
|
|