backup
This commit is contained in:
+155
-98
@@ -1,5 +1,7 @@
|
||||
import os
|
||||
from configs.config_pyramid import config
|
||||
import sys
|
||||
from configs.config_abl_pyr_64_128_cape_m import config
|
||||
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = config['gpu']
|
||||
import tensorflow as tf
|
||||
|
||||
@@ -22,7 +24,7 @@ import wandb
|
||||
import random
|
||||
import numpy as np
|
||||
import scipy as sp
|
||||
from custom_utils.custom_utils import UV2Mesh, get_summary_html, maskFacesBatch, _maskFaces, get_summary_str
|
||||
from custom_utils.custom_utils import UV2Mesh, get_summary_html, maskFacesBatch, _maskFaces, get_summary_str, plot_3d_collision, group_inputs_for_levels
|
||||
import plotly.graph_objects as go
|
||||
import plotly.express as ex
|
||||
from plotly.subplots import make_subplots
|
||||
@@ -33,7 +35,6 @@ if config['is_skirt']:
|
||||
else:
|
||||
from losses import edge_loss
|
||||
|
||||
from tf_utils import check_tf_version, allow_memory_growth, split_gpu_for_testing
|
||||
from losses import uv_mask_loss, bend_loss, kl_loss, collision_ratio
|
||||
# from losses import collision_penalty as collision_penalty
|
||||
from losses import collision_loss as collision_penalty
|
||||
@@ -41,24 +42,26 @@ from custom_utils.custom_utils import batch_to_dict, load_model
|
||||
from dataset import DatasetFactory, StaticDataset
|
||||
from custom_utils.smpl import SMPL
|
||||
from models.pyramid_model_optimized import Pyramid
|
||||
from postprocessing import fix_collision
|
||||
|
||||
|
||||
def init_wandb():
|
||||
run = wandb.init(project='cvae', name=config['name'], notes=config['notes'], dir='/home/workspace/local-data/temp')
|
||||
run = wandb.init(project='ablation', name=config['name'], notes=config['notes'], dir='/wandb')
|
||||
|
||||
artifact = wandb.Artifact('code', type='code')
|
||||
artifact.add_file('cvae_pyramid.py')
|
||||
artifact.add_file('models/cvae_model.py')
|
||||
artifact.add_file('models/pyramid_model.py')
|
||||
artifact.add_file('models/pyramid_model_optimized.py')
|
||||
artifact.add_file('models/pyramid_level.py')
|
||||
artifact.add_file('models/upscaler.py')
|
||||
artifact.add_file('losses.py')
|
||||
artifact.add_file('configs/config_base.py')
|
||||
artifact.add_file('configs/config_pyramid.py')
|
||||
artifact.add_file('models/base.py')
|
||||
artifact.add_file('models/cond_encoder.py')
|
||||
artifact.add_file('dataset.py')
|
||||
artifact.add_file('convnext/models/convnext_tf.py')
|
||||
config_file_names = [key for key in sys.modules.keys() if key.startswith('configs.')]
|
||||
for config_file_name in config_file_names:
|
||||
artifact.add_file('/'.join(config_file_name.split('.')) + '.py')
|
||||
run.log_artifact(artifact)
|
||||
|
||||
uv2mesh = UV2Mesh(res=256, is_skirt=config['is_skirt'], is_smpl_orig=config['is_cape'])
|
||||
@@ -205,26 +208,18 @@ def plot_3d(mesh_gt, mesh_pred_unposed_levels, faces, mesh_posed, collision, col
|
||||
return fig
|
||||
|
||||
|
||||
def group_inputs_for_levels(batch):
|
||||
levels_input = []
|
||||
for res in config['pyramid']['resolutions']:
|
||||
res_modifier = '_' + str(res) if res != 512 else ''
|
||||
levels_input.append([batch['uv_unposed' + res_modifier], batch['uv_static' + res_modifier], batch['uv_body_posed' + res_modifier], batch['uv_curvature' + res_modifier], batch['uv_mask' + res_modifier], batch['uv_mask']])
|
||||
conds = [batch['uv_static'], batch['uv_body_posed'], batch['uv_curvature']]
|
||||
return levels_input, conds
|
||||
|
||||
|
||||
@tf.function
|
||||
def train_step(batch):
|
||||
levels_input, conds = group_inputs_for_levels(batch)
|
||||
levels_input, conds = group_inputs_for_levels(batch, config)
|
||||
|
||||
with tf.GradientTape() as tape:
|
||||
out_levels = model([levels_input, conds], training=True)
|
||||
|
||||
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_orig = batch['mesh_unposed']
|
||||
if config['use_skinning']:
|
||||
mesh_orig, uv_orig = batch['mesh_unposed'], batch['uv_unposed']
|
||||
else:
|
||||
mesh_orig, uv_orig = batch['mesh_posed'], batch['uv_posed']
|
||||
|
||||
output = dict()
|
||||
reconstr_loss_total = 0
|
||||
@@ -237,21 +232,36 @@ def train_step(batch):
|
||||
pred, mean, logvar = out_levels[i]
|
||||
|
||||
mesh_pred = uv2mesh(pred)
|
||||
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)
|
||||
|
||||
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
|
||||
|
||||
# is the output already masked?
|
||||
|
||||
e_loss = tf.reduce_mean(edge_loss(mesh_pred, mesh_orig, batch['mesh_mask']))
|
||||
c_loss = tf.reduce_mean(collision_penalty(mesh_pred_posed, batch['mesh_body_posed'], batch['mesh_mask']))
|
||||
l2_loss = tf.reduce_mean(1e3 * tf.reduce_mean(tf.reduce_sum(((batch['uv_unposed'] - pred) ** 2), axis=-1), axis=[1, 2]))
|
||||
c_loss, c_ratio = collision_penalty(mesh_pred_posed, batch['mesh_body_posed'], batch['mesh_mask'])
|
||||
c_loss, c_ratio = tf.reduce_mean(c_loss), tf.reduce_mean(c_ratio)
|
||||
l2_loss = tf.reduce_mean(1e3 * tf.reduce_mean(tf.reduce_sum(((uv_orig - pred) ** 2), axis=-1), axis=[1, 2]))
|
||||
# ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(pred + 5, batch['uv_unposed'] + 5, max_val=10, power_factors=(0.5, 0.2, 0.1, 0.1, 0.1)))
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim(pred + 1, batch['uv_unposed'] + 1, max_val=3))
|
||||
# ms_ssim = tf.reduce_mean(tf.image.ssim(pred + 1, batch['uv_unposed'] + 1, max_val=3))
|
||||
l2_mesh_loss = tf.reduce_mean(1e3 * tf.reduce_mean(tf.reduce_sum(batch['mesh_mask'][:,:,tf.newaxis] * (mesh_orig - mesh_pred) ** 2, axis=-1), axis=-1))
|
||||
kl = kl_loss(mean, logvar)
|
||||
|
||||
reconstr_loss_total += config['pyramid']['loss_weights'][i] * (l2_loss + l2_mesh_loss + (e_loss + c_loss) * config['reg_loss_coeff'])
|
||||
# reconstr_loss_total += config['pyramid']['loss_weights'][i] * (l2_loss + (e_loss + c_loss) * config['reg_loss_coeff'])
|
||||
if config['use_extra_losses']:
|
||||
reconstr_loss_total = reconstr_loss_total + config['pyramid']['loss_weights'][i] * (l2_loss + l2_mesh_loss)
|
||||
if not config['is_cape']:
|
||||
reconstr_loss_total = reconstr_loss_total + config['pyramid']['loss_weights'][i] * (e_loss + c_loss) * config['reg_loss_coeff']
|
||||
else:
|
||||
reconstr_loss_total = reconstr_loss_total + config['pyramid']['loss_weights'][i] * (l2_loss)
|
||||
|
||||
|
||||
kl_total += kl
|
||||
|
||||
mean = tf.reduce_mean(mean)
|
||||
@@ -260,9 +270,9 @@ def train_step(batch):
|
||||
metric = tf.reduce_sum(tf.math.sqrt(tf.reduce_sum(batch['mesh_mask'][:,:,tf.newaxis] * (mesh_orig - mesh_pred) ** 2, axis=-1))) / tf.reduce_sum(batch['mesh_mask'])
|
||||
|
||||
output = { **output, **{ 'train/edge_loss' + res_modifier: e_loss, 'train/collision_loss' + res_modifier: c_loss,'train/euclid_error' + res_modifier: metric,
|
||||
'train/ms_ssim' + res_modifier: ms_ssim, 'train/mean' + res_modifier: mean, 'train/logvar' + res_modifier: logvar, 'train/kl_loss' + res_modifier: kl,
|
||||
'train/var' + res_modifier: np.e ** logvar, 'train/l2_loss' + res_modifier: l2_loss,
|
||||
'train/euclid_error': metric, 'train/l2_mesh_loss': l2_mesh_loss } }
|
||||
'train/mean' + res_modifier: mean, 'train/logvar' + res_modifier: logvar, 'train/kl_loss' + res_modifier: kl,
|
||||
'train/var' + res_modifier: np.e ** logvar, 'train/l2_loss' + res_modifier: l2_loss,
|
||||
'train/euclid_error': metric, 'train/l2_mesh_loss': l2_mesh_loss } } # 'train/ms_ssim' + res_modifier: ms_ssim,
|
||||
|
||||
loss = reconstr_loss_total + balancing_coeff * kl_total
|
||||
|
||||
@@ -273,13 +283,14 @@ def train_step(batch):
|
||||
|
||||
@tf.function
|
||||
def valid_step(batch):
|
||||
levels_input, conds = group_inputs_for_levels(batch)
|
||||
levels_input, conds = group_inputs_for_levels(batch, config)
|
||||
out_levels = model([levels_input, conds], training=False)
|
||||
|
||||
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_orig = batch['mesh_unposed']
|
||||
|
||||
if config['use_skinning']:
|
||||
mesh_orig, uv_orig = batch['mesh_unposed'], batch['uv_unposed']
|
||||
else:
|
||||
mesh_orig, uv_orig = batch['mesh_posed'], batch['uv_posed']
|
||||
|
||||
output = dict()
|
||||
reconstr_loss_total = 0
|
||||
@@ -292,21 +303,34 @@ def valid_step(batch):
|
||||
pred, mean, logvar = out_levels[i]
|
||||
|
||||
mesh_pred = uv2mesh(pred)
|
||||
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)
|
||||
|
||||
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
|
||||
# is the output already masked?
|
||||
|
||||
e_loss = tf.reduce_mean(edge_loss(mesh_pred, mesh_orig, batch['mesh_mask']))
|
||||
c_loss = tf.reduce_mean(collision_penalty(mesh_pred_posed, batch['mesh_body_posed'], batch['mesh_mask']))
|
||||
l2_loss = tf.reduce_mean(1e3 * tf.reduce_mean(tf.reduce_sum(((batch['uv_unposed'] - pred) ** 2), axis=-1), axis=[1, 2]))
|
||||
c_loss, c_ratio = collision_penalty(mesh_pred_posed, batch['mesh_body_posed'], batch['mesh_mask'])
|
||||
c_loss, c_ratio = tf.reduce_mean(c_loss), tf.reduce_mean(c_ratio)
|
||||
l2_loss = tf.reduce_mean(1e3 * tf.reduce_mean(tf.reduce_sum(((uv_orig - pred) ** 2), axis=-1), axis=[1, 2]))
|
||||
# ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(pred + 5, batch['uv_unposed'] + 5, max_val=10, power_factors=(0.5, 0.2, 0.1, 0.1, 0.1)))
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim(pred + 1, batch['uv_unposed'] + 1, max_val=3))
|
||||
# ms_ssim = tf.reduce_mean(tf.image.ssim(pred + 1, batch['uv_unposed'] + 1, max_val=3))
|
||||
l2_mesh_loss = tf.reduce_mean(1e3 * tf.reduce_mean(tf.reduce_sum(batch['mesh_mask'][:,:,tf.newaxis] * (mesh_orig - mesh_pred) ** 2, axis=-1), axis=-1))
|
||||
kl = kl_loss(mean, logvar)
|
||||
|
||||
reconstr_loss_total += config['pyramid']['loss_weights'][i] * (l2_loss + l2_mesh_loss + (e_loss + c_loss) * config['reg_loss_coeff'])
|
||||
# reconstr_loss_total += config['pyramid']['loss_weights'][i] * (l2_loss + (e_loss + c_loss) * config['reg_loss_coeff'])
|
||||
if config['use_extra_losses']:
|
||||
reconstr_loss_total = reconstr_loss_total + config['pyramid']['loss_weights'][i] * (l2_loss + l2_mesh_loss)
|
||||
if not config['is_cape']:
|
||||
reconstr_loss_total = reconstr_loss_total + config['pyramid']['loss_weights'][i] * (e_loss + c_loss) * config['reg_loss_coeff']
|
||||
else:
|
||||
reconstr_loss_total = reconstr_loss_total + config['pyramid']['loss_weights'][i] * (l2_loss)
|
||||
|
||||
kl_total += kl
|
||||
|
||||
mean = tf.reduce_mean(mean)
|
||||
@@ -315,9 +339,9 @@ def valid_step(batch):
|
||||
metric = tf.reduce_sum(tf.math.sqrt(tf.reduce_sum(batch['mesh_mask'][:,:,tf.newaxis] * (mesh_orig - mesh_pred) ** 2, axis=-1))) / tf.reduce_sum(batch['mesh_mask'])
|
||||
|
||||
output = { **output, **{ 'valid/edge_loss' + res_modifier: e_loss, 'valid/collision_loss' + res_modifier: c_loss,'valid/euclid_error' + res_modifier: metric,
|
||||
'valid/ms_ssim' + res_modifier: ms_ssim, 'valid/mean' + res_modifier: mean, 'valid/logvar' + res_modifier: logvar, 'valid/kl_loss' + res_modifier: kl,
|
||||
'valid/mean' + res_modifier: mean, 'valid/logvar' + res_modifier: logvar, 'valid/kl_loss' + res_modifier: kl,
|
||||
'valid/var' + res_modifier: np.e ** logvar, 'valid/l2_loss' + res_modifier: l2_loss,
|
||||
'valid/euclid_error': metric, 'valid/l2_mesh_loss': l2_mesh_loss } }
|
||||
'valid/euclid_error': metric, 'valid/l2_mesh_loss': l2_mesh_loss } } # 'valid/ms_ssim' + res_modifier: ms_ssim,
|
||||
|
||||
loss = reconstr_loss_total + balancing_coeff * kl_total
|
||||
|
||||
@@ -326,7 +350,7 @@ def valid_step(batch):
|
||||
|
||||
@tf.function
|
||||
def test_step(batch):
|
||||
levels_input, conds = group_inputs_for_levels(batch)
|
||||
levels_input, conds = group_inputs_for_levels(batch, config)
|
||||
out_levels = model([levels_input, conds], training=False)
|
||||
|
||||
shape = batch['pose_shape'][:, 72:]
|
||||
@@ -351,14 +375,17 @@ def test_step(batch):
|
||||
# is the output already masked?
|
||||
|
||||
e_loss = tf.reduce_mean(edge_loss(mesh_pred, mesh_orig, batch['mesh_mask']))
|
||||
c_loss = tf.reduce_mean(collision_penalty(mesh_pred_posed, batch['mesh_body_posed'], batch['mesh_mask']))
|
||||
c_ratio = tf.reduce_mean(collision_ratio(mesh_pred_posed, batch['mesh_body_posed'], batch['mesh_mask']))
|
||||
c_loss, c_ratio = collision_penalty(mesh_pred_posed, batch['mesh_body_posed'], batch['mesh_mask'])
|
||||
c_loss, c_ratio = tf.reduce_mean(c_loss), tf.reduce_mean(c_ratio)
|
||||
l2_loss = tf.reduce_mean(1e3 * tf.reduce_mean(tf.reduce_sum(((batch['uv_unposed'] - pred) ** 2), axis=-1), axis=[1, 2]))
|
||||
l2_mesh_loss = tf.reduce_mean(1e3 * tf.reduce_mean(tf.reduce_sum(batch['mesh_mask'][:,:,tf.newaxis] * (mesh_orig - mesh_pred) ** 2, axis=-1), axis=-1))
|
||||
# ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(pred + 5, batch['uv_unposed'] + 5, max_val=10, power_factors=(0.5, 0.2, 0.1, 0.1, 0.1)))
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim(pred + 1, batch['uv_unposed'] + 1, max_val=3))
|
||||
# ms_ssim = tf.reduce_mean(tf.image.ssim(pred + 1, batch['uv_unposed'] + 1, max_val=3))
|
||||
kl = kl_loss(mean, logvar)
|
||||
|
||||
reconstr_loss_total += config['pyramid']['loss_weights'][i] * (l2_loss + (e_loss + c_loss) * config['reg_loss_coeff'])
|
||||
reconstr_loss_total = reconstr_loss_total + config['pyramid']['loss_weights'][i] * (l2_loss + l2_mesh_loss)
|
||||
if not config['is_cape']:
|
||||
reconstr_loss_total = reconstr_loss_total + config['pyramid']['loss_weights'][i] * (e_loss + c_loss) * config['reg_loss_coeff']
|
||||
# reconstr_loss_total += config['pyramid']['loss_weights'][i] * (l2_loss + (e_loss + c_loss) * config['reg_loss_coeff'])
|
||||
kl_total += kl
|
||||
|
||||
@@ -369,7 +396,7 @@ def test_step(batch):
|
||||
euclid_error_per_garment = tf.reduce_sum(tf.math.sqrt(tf.reduce_sum(batch['mesh_mask'][:,:,tf.newaxis] * (mesh_orig - mesh_pred) ** 2, axis=-1)), axis=1) / tf.reduce_sum(batch['mesh_mask'], axis=1)
|
||||
|
||||
output = { **output, **{ 'test/edge_loss' + res_modifier: e_loss, 'test/collision_loss' + res_modifier: c_loss,'test/euclid_error' + res_modifier: metric,
|
||||
'test/ms_ssim' + res_modifier: ms_ssim, 'test/mean' + res_modifier: mean, 'test/logvar' + res_modifier: logvar, 'test/kl_loss' + res_modifier: kl,
|
||||
'test/mean' + res_modifier: mean, 'test/logvar' + res_modifier: logvar, 'test/kl_loss' + res_modifier: kl,
|
||||
'test/var' + res_modifier: np.e ** logvar, 'test/l2_loss' + res_modifier: l2_loss,
|
||||
'test/euclid_error': metric, 'test/collision_ratio' + res_modifier: c_ratio } }
|
||||
|
||||
@@ -380,15 +407,19 @@ def test_step(batch):
|
||||
|
||||
|
||||
def plot_everything(batch: dict, type: str):
|
||||
levels_input = group_inputs_for_levels(batch)
|
||||
if config['use_skinning']:
|
||||
mesh_orig, uv_orig = batch['mesh_unposed'].numpy(), batch['uv_unposed'].numpy()
|
||||
else:
|
||||
mesh_orig, uv_orig = batch['mesh_posed'].numpy(), batch['uv_posed'].numpy()
|
||||
|
||||
levels_input = group_inputs_for_levels(batch, config)
|
||||
output_levels = model(levels_input, training=False)
|
||||
|
||||
mesh_pred_levels = [uv2mesh(output_levels[i][0]).numpy() for i in range(len(output_levels))]
|
||||
mesh_orig = batch['mesh_unposed'].numpy()
|
||||
mesh_orig_posed = batch['mesh_posed'].numpy()
|
||||
body = batch['mesh_body_posed'].numpy()
|
||||
|
||||
orig_img = (batch['uv_unposed'].numpy() + 1) / 2
|
||||
orig_img = (uv_orig + 1) / 2
|
||||
uv_static_img = (batch['uv_static'].numpy() + 1) / 2
|
||||
uv_body_posed_img = (batch['uv_body_posed'].numpy() + 1) / 2
|
||||
uv_curvature = (np.tile(batch['uv_curvature'].numpy().argmax(-1)[:,:,:,np.newaxis], [1,1,1,3]) / 6)
|
||||
@@ -397,43 +428,62 @@ def plot_everything(batch: dict, type: str):
|
||||
img = tf.concat((uv_static_img, uv_body_posed_img, uv_curvature, orig_img, pred_img), axis=2)
|
||||
imgs = [wandb.Image(img[i]) for i in range(4)]
|
||||
|
||||
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])]).numpy()
|
||||
|
||||
F = np.load("custom_utils/smpl_faces" + skirt_modifier + ("_orig" if config['is_cape'] else "") + ".npy")
|
||||
mask = batch['mesh_mask'].numpy().astype(bool)
|
||||
|
||||
_, faces = maskFacesBatch(np.ones_like(mesh_orig), F, mask)
|
||||
gt = [mesh_orig[i][mask[i], :] + shape_offsets[i, mask[i], :] for i in range(mask.shape[0])]
|
||||
|
||||
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])]).numpy()
|
||||
|
||||
|
||||
gt = [mesh_orig[i][mask[i], :] + shape_offsets[i, mask[i], :] for i in range(mask.shape[0])]
|
||||
|
||||
mesh_unposed_shaped_levels = [[] for _ in range(mask.shape[0])]
|
||||
for i in range(len(output_levels)):
|
||||
mesh_unposed_shaped = [mesh_pred_levels[i][j, mask[j], :] + shape_offsets[j, mask[j]] for j in range(mask.shape[0])]
|
||||
for k in range(mask.shape[0]):
|
||||
mesh_unposed_shaped_levels[k].append(mesh_unposed_shaped[k])
|
||||
|
||||
# full res version
|
||||
mesh_unposed_shaped_full = np.zeros((mask.shape[0], config['smpl_res'], 3), dtype=np.float32)
|
||||
for i in range(mask.shape[0]):
|
||||
mesh_unposed_shaped_full[i, mask[i], :] = mesh_unposed_shaped[i]
|
||||
|
||||
mesh_posed = smpl.run_garment(batch['pose_shape'][:, :72], batch['pose_shape'][:, 72:], mesh_unposed_shaped_full).numpy()
|
||||
else:
|
||||
mesh_posed = mesh_pred_levels[-1]
|
||||
gt = [mesh_orig[i][mask[i], :] for i in range(mesh_posed.shape[0])]
|
||||
mesh_unposed_shaped_levels = [[mesh_pred_levels[i][j, mask[j], :] for i in range(len(output_levels))] for j in range(mask.shape[0])]
|
||||
|
||||
|
||||
gt_posed = [mesh_orig_posed[i, mask[i], :] for i in range(mask.shape[0])]
|
||||
|
||||
mesh_unposed_shaped_levels = [[] for _ in range(mask.shape[0])]
|
||||
for i in range(len(output_levels)):
|
||||
mesh_unposed_shaped = [mesh_pred_levels[i][j, mask[j], :] + shape_offsets[j, mask[j]] for j in range(mask.shape[0])]
|
||||
for k in range(mask.shape[0]):
|
||||
mesh_unposed_shaped_levels[k].append(mesh_unposed_shaped[k])
|
||||
|
||||
# full res version
|
||||
mesh_unposed_shaped_full = np.zeros((mask.shape[0], config['smpl_res'], 3), dtype=np.float32)
|
||||
for i in range(mask.shape[0]):
|
||||
mesh_unposed_shaped_full[i, mask[i], :] = mesh_unposed_shaped[i]
|
||||
mesh_posed = smpl.run_garment(batch['pose_shape'][:, :72], batch['pose_shape'][:, 72:], mesh_unposed_shaped_full).numpy()
|
||||
mesh_posed_fixed = fix_collision(mesh_posed, body, mask).numpy()
|
||||
|
||||
mesh_posed = [mesh_posed[j, mask[j], :] for j in range(mask.shape[0])]
|
||||
|
||||
collision = [np.concatenate((body[i], mesh_posed[i]), axis=0) for i in range(len(mesh_posed))]
|
||||
collision_faces = [np.concatenate((F, faces[i] + config['smpl_res']), axis=0) for i in range(len(mesh_posed))]
|
||||
collision_color = [np.concatenate((np.ones_like(body[i], dtype=np.int32)*100, np.ones_like(mesh_posed[i], dtype=np.int32)*230), axis=0) for i in range(len(mesh_posed))]
|
||||
|
||||
# body_2 = [b.copy() for b in body]
|
||||
# for i in range(len(gt_posed)):
|
||||
# body_2[i][:, 1] = body[i][:,2].copy()
|
||||
# body_2[i][:, 2] = body[i][:,1].copy()
|
||||
# gt_posed_with_body = [np.concatenate((body_2[i], gt_posed[i]), axis=0) for i in range(len(gt_posed))]
|
||||
|
||||
if config['is_cape']:
|
||||
collision = mesh_posed
|
||||
collision_faces = faces
|
||||
collision_color = [np.ones_like(mesh_posed[i], dtype=np.int32)*230 for i in range(len(mesh_posed))]
|
||||
else:
|
||||
collision = [np.concatenate((body[i], mesh_posed[i]), axis=0) for i in range(len(mesh_posed))]
|
||||
collision_faces = [np.concatenate((F, faces[i] + config['smpl_res']), axis=0) for i in range(len(mesh_posed))]
|
||||
collision_color = [np.concatenate((np.ones_like(body[i], dtype=np.int32)*100, np.ones_like(mesh_posed[i], dtype=np.int32)*230), axis=0) for i in range(len(mesh_posed))]
|
||||
|
||||
figs = { type + "/garment_" + str(i): plot_3d(gt[i], mesh_unposed_shaped_levels[i], faces[i], gt_posed[i], collision[i], collision_faces[i], collision_color[i]) for i in range(4) }
|
||||
|
||||
if config['is_cape']:
|
||||
figs_coll = dict()
|
||||
else:
|
||||
garment_posed_fixed = [mesh_posed_fixed[i, mask[i], :] for i in range(body.shape[0])]
|
||||
collision_fixed = [np.concatenate((body[i], garment_posed_fixed[i]), axis=0) for i in range(len(mesh_posed))]
|
||||
gt_posed = [batch['mesh_posed'].numpy()[i, mask[i], :] for i in range(mask.shape[0])]
|
||||
collision_gt = [np.concatenate((body[i], gt_posed[i]), axis=0) for i in range(len(gt_posed))]
|
||||
figs_coll = { type + "/garment_" + str(i) + '_collision': plot_3d_collision(collision_gt[i], collision[i], collision_fixed[i], collision_faces[i], collision_color[i]) for i in range(4) }
|
||||
|
||||
return { **figs, **{ type + '/images': imgs, } }
|
||||
return { **figs, **figs_coll, **{ type + '/images': imgs, } }
|
||||
|
||||
|
||||
def sample(folder):
|
||||
@@ -622,7 +672,6 @@ def test():
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_wandb()
|
||||
allow_memory_growth()
|
||||
parser = argparse.ArgumentParser(description='')
|
||||
parser.add_argument('--name', type=str)
|
||||
args = vars(parser.parse_args())
|
||||
@@ -634,10 +683,10 @@ if __name__ == '__main__':
|
||||
dataset_resolutions.append(512)
|
||||
|
||||
factory = DatasetFactory()
|
||||
dataset_train = factory.get_dataset(folder=config['train_dir'], batch_size=config['batch_size'], signature=config['signature_dynamic'], use_filled=config['use_filled'], resolutions=dataset_resolutions, in_memory=config['dataset_in_memory'], is_cape=config['is_cape'])
|
||||
static_dataset_train = StaticDataset(folder=config['train_dir'], signature=config['signature_static'], use_filled=config['use_filled'], resolutions=dataset_resolutions, is_cape=config['is_cape'])
|
||||
dataset_valid = factory.get_dataset(folder=config['valid_dir'], batch_size=config['batch_size'], signature=config['signature_dynamic'], use_filled=config['use_filled'], resolutions=dataset_resolutions, in_memory=config['dataset_in_memory'], is_cape=config['is_cape'])
|
||||
static_dataset_valid = StaticDataset(folder=config['valid_dir'], signature=config['signature_static'], use_filled=config['use_filled'], resolutions=dataset_resolutions, is_cape=config['is_cape'])
|
||||
dataset_train = factory.get_dataset(folder=config['train_dir'], batch_size=config['batch_size'], signature=config['signature_dynamic'], resolutions=dataset_resolutions, in_memory=config['dataset_in_memory'], is_cape=config['is_cape'])
|
||||
static_dataset_train = StaticDataset(folder=config['train_dir'], signature=config['signature_static'], resolutions=dataset_resolutions, is_cape=config['is_cape'])
|
||||
dataset_valid = factory.get_dataset(folder=config['valid_dir'], batch_size=config['batch_size'], signature=config['signature_dynamic'], resolutions=dataset_resolutions, in_memory=config['dataset_in_memory'], is_cape=config['is_cape'])
|
||||
static_dataset_valid = StaticDataset(folder=config['valid_dir'], signature=config['signature_static'], resolutions=dataset_resolutions, is_cape=config['is_cape'])
|
||||
|
||||
model: Pyramid = Pyramid(config)
|
||||
|
||||
@@ -654,11 +703,18 @@ if __name__ == '__main__':
|
||||
ckpt = tf.train.Checkpoint(model=model)
|
||||
manager = tf.train.CheckpointManager(ckpt, 'checkpoints/' + config['name'], max_to_keep=20)
|
||||
|
||||
# load_model(model, config['pyramid']['pretrained_pyramid'])
|
||||
model.static_enc.trainable = False
|
||||
model.body_enc.trainable = False
|
||||
model.curv_enc.trainable = False
|
||||
# model.cond_encoder.trainable = False
|
||||
# load_model(model, 'checkpoints/pyr_abl_32_skirt', iteration='180000')
|
||||
|
||||
if len(config['pyramid']['resolutions']) == 1:
|
||||
model.static_enc.trainable = False
|
||||
model.body_enc.trainable = False
|
||||
model.curv_enc.trainable = False
|
||||
else:
|
||||
load_model(model, config['pyramid']['pretrained_pyramid'])
|
||||
model.static_enc.trainable = False
|
||||
model.body_enc.trainable = False
|
||||
model.curv_enc.trainable = False
|
||||
model.cond_encoder.trainable = False
|
||||
|
||||
|
||||
# load_model(model.levels[0].upscale_enhancer, config['pyramid']['levels'][0]['upscale_enhancer'])
|
||||
@@ -669,14 +725,15 @@ if __name__ == '__main__':
|
||||
for i in range(len(model.levels)-1):
|
||||
load_model(model.levels[i].upscale_enhancer, config['pyramid']['levels'][i]['upscale_enhancer'])
|
||||
model.levels[i].upscale_enhancer.trainable = False
|
||||
load_model(model.upscale_enhancer, config['pyramid']['final_upscale_enhancer'])
|
||||
model.upscale_enhancer.trainable = False
|
||||
if config['pyramid']['resolutions'][-1] != 512:
|
||||
load_model(model.upscale_enhancer, config['pyramid']['final_upscale_enhancer'])
|
||||
model.upscale_enhancer.trainable = False
|
||||
|
||||
for i in range(len(model.levels)-1):
|
||||
model.levels[i].trainable = False
|
||||
|
||||
# load_model(model.cond_encoder, 'checkpoints/cvae_low_4_3_d32')
|
||||
# load_model(model.levels[0], 'checkpoints/cvae_low_4_3_d32')
|
||||
# load_model(model.cond_encoder, config['pyramid']['pretrained_pyramid'])
|
||||
# load_model(model.levels[0], config['pyramid']['pretrained_pyramid'])
|
||||
# load_model(model.levels[-1], 'checkpoints/cvae_19')
|
||||
|
||||
|
||||
@@ -701,10 +758,10 @@ if __name__ == '__main__':
|
||||
if step == 0:
|
||||
to_log = { **to_log, **{ 'model/summary': wandb.Html(get_summary_html(model)) } }
|
||||
|
||||
if train_loss['train/euclid_error'] < 0.01:
|
||||
if train_loss['train/euclid_error'] < 0.004:
|
||||
if balancing_activated_step == 0:
|
||||
balancing_activated_step = step
|
||||
balancing_coeff = balancing_coeff + 1 / 15000
|
||||
balancing_coeff = balancing_coeff + 1 / 20000
|
||||
|
||||
|
||||
to_log = { **to_log, **{ 'train/balancing_coeff': balancing_coeff, 'train/gamma': gamma } }
|
||||
|
||||
Reference in New Issue
Block a user