This commit is contained in:
2023-03-10 17:25:44 +00:00
parent cb76d36c4f
commit 4b1cb0b06d
27 changed files with 1890 additions and 207 deletions
+78 -59
View File
@@ -1,7 +1,8 @@
import os
import sys
from configs.iccv.config_abl_pyr_32_64_128_256_512 import config
from configs.iccv.config_abl_pyr_32_tshirt import config
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = config['gpu']
import tensorflow as tf
@@ -417,6 +418,7 @@ def test_step(batch):
def plot_everything(batch: dict, type: str):
nr_to_plot = 4
if config['use_skinning']:
mesh_orig, uv_orig = batch['mesh_unposed'].numpy(), batch['uv_unposed'].numpy()
else:
@@ -426,8 +428,8 @@ def plot_everything(batch: dict, type: str):
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_posed = batch['mesh_posed'].numpy()
body = batch['mesh_body_posed'].numpy()
mesh_orig_posed = batch['mesh_posed'].numpy()[:nr_to_plot]
body = batch['mesh_body_posed'].numpy()[:nr_to_plot]
orig_img = (uv_orig + 1) / 2
uv_static_img = (batch['uv_static'].numpy() + 1) / 2
@@ -436,10 +438,10 @@ def plot_everything(batch: dict, type: str):
pred_img = np.concatenate([tf.image.resize(output_levels[i][0], (512, 256), tf.image.ResizeMethod.LANCZOS5) for i in range(len(output_levels))], axis=2)
pred_img = (pred_img - pred_img.min(axis=(1,2,3))[:,np.newaxis,np.newaxis,np.newaxis]) / (pred_img.max(axis=(1,2,3))[:,np.newaxis,np.newaxis,np.newaxis] - pred_img.min(axis=(1,2,3))[:,np.newaxis,np.newaxis,np.newaxis])
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)]
imgs = [wandb.Image(img[i]) for i in range(nr_to_plot)]
F = np.load("custom_utils/smpl_faces" + skirt_modifier + ("_orig" if config['is_cape'] else "") + ".npy")
mask = batch['mesh_mask'].numpy().astype(bool)
mask = batch['mesh_mask'].numpy().astype(bool)[:nr_to_plot]
_, faces = maskFacesBatch(np.ones_like(mesh_orig), F, mask)
if config['use_skinning']:
@@ -448,30 +450,30 @@ def plot_everything(batch: dict, type: str):
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])]
gt = [mesh_orig[i][mask[i], :] + shape_offsets[i, mask[i], :] for i in range(nr_to_plot)]
mesh_unposed_shaped_levels = [[] for _ in range(mask.shape[0])]
mesh_unposed_shaped_levels = [[] for _ in range(nr_to_plot)]
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 = [mesh_pred_levels[i][j, mask[j], :] + shape_offsets[j, mask[j]] for j in range(nr_to_plot)]
for k in range(nr_to_plot):
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 = np.zeros((nr_to_plot, config['smpl_res'], 3), dtype=np.float32)
for i in range(nr_to_plot):
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 = smpl.run_garment(batch['pose_shape'][:nr_to_plot, :72], batch['pose_shape'][:nr_to_plot, 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 = [mesh_orig[i][mask[i], :] for i in range(nr_to_plot)]
mesh_unposed_shaped_levels = [[mesh_pred_levels[i][j, mask[j], :] for i in range(len(output_levels))] for j in range(nr_to_plot)]
gt_posed = [mesh_orig_posed[i, mask[i], :] for i in range(mask.shape[0])]
gt_posed = [mesh_orig_posed[i, mask[i], :] for i in range(nr_to_plot)]
mesh_posed_fixed = fix_collision(mesh_posed, body, mask).numpy()
mesh_posed = [mesh_posed[j, mask[j], :] for j in range(mask.shape[0])]
mesh_posed = [mesh_posed[j, mask[j], :] for j in range(nr_to_plot)]
if config['is_cape']:
collision = mesh_posed
@@ -487,11 +489,11 @@ def plot_everything(batch: dict, type: str):
if config['is_cape']:
figs_coll = dict()
else:
garment_posed_fixed = [mesh_posed_fixed[i, mask[i], :] for i in range(body.shape[0])]
garment_posed_fixed = [mesh_posed_fixed[i, mask[i], :] for i in range(nr_to_plot)]
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])]
gt_posed = [batch['mesh_posed'].numpy()[i, mask[i], :] for i in range(nr_to_plot)]
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) }
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(nr_to_plot) }
return { **figs, **figs_coll, **{ type + '/images': imgs, } }
@@ -728,14 +730,23 @@ def generate_results():
pass
@tf.function
def speed_step(levels_input, conds):
def speed_step(levels_input, conds, pose_shape, mask):
shape = 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])])
return model.sample([levels_input, conds])
# pred = model.sample([levels_input, conds])
out_levels = model([levels_input, conds], training=False)
pred = out_levels[-1][0]
mesh_pred = uv2mesh(pred)
mesh_pred_shaped = mesh_pred + shape_offsets * mask[:,:,tf.newaxis]
mesh_pred_posed = smpl.run_garment(pose_shape[:, :72], pose_shape[:, 72:], mesh_pred_shaped)
return mesh_pred_posed
if __name__ == '__main__':
# init_wandb()
init_wandb()
parser = argparse.ArgumentParser(description='')
parser.add_argument('--name', type=str, default='lhunor')
args = vars(parser.parse_args())
@@ -760,8 +771,12 @@ if __name__ == '__main__':
resolutions=dataset_resolutions,
in_memory=config['dataset_in_memory'],
is_cape=config['is_cape'],
frames_file=config['frames_file'] if 'frames_file' in config.keys() else None)
frames_file=config['frames_file'] if 'frames_file' in config.keys() else None,
frames_file_skirt=config['frames_file'] if 'frames_file' in config.keys() else '',
folder_skirt=config['train_dir_skirt'] if 'train_dir_skirt' in config.keys() else '',
)
static_dataset_train = StaticDataset(folder=config['train_dir'],
folder_skirt=config['train_dir_skirt'] if 'train_dir_skirt' in config.keys() else '',
signature=config['signature_static'],
resolutions=dataset_resolutions,
in_memory=True,
@@ -772,8 +787,12 @@ if __name__ == '__main__':
resolutions=dataset_resolutions,
in_memory=config['dataset_in_memory'],
is_cape=config['is_cape'],
frames_file=config['frames_file_valid'] if 'frames_file_valid' in config.keys() else None)
frames_file=config['frames_file_valid'] if 'frames_file_valid' in config.keys() else None,
frames_file_skirt=config['frames_file_valid'] if 'frames_file_valid' in config.keys() else '',
folder_skirt=config['valid_dir_skirt'] if 'valid_dir_skirt' in config.keys() else '',
)
static_dataset_valid = StaticDataset(folder=config['valid_dir'],
folder_skirt=config['valid_dir_skirt'] if 'valid_dir_skirt' in config.keys() else '',
signature=config['signature_static'],
resolutions=dataset_resolutions,
in_memory=True,
@@ -848,50 +867,50 @@ if __name__ == '__main__':
levels_input, conds = group_inputs_for_levels(batch, config)
start = time.perf_counter()
out = speed_step(levels_input, conds)
t = time.perf_counter() - start
print(t)
times.append(t)
# start = time.perf_counter()
# out = speed_step(levels_input, conds, batch['pose_shape'] ,batch['mesh_mask'])
# t = time.perf_counter() - start
# print(t)
# times.append(t)
if step > 200:
break
# if step > 200:
# break
# train_loss = train_step(batch)
# train_accumulator.update(train_loss)
train_loss = train_step(batch)
train_accumulator.update(train_loss)
# if step % 25 == 0:
# data_valid = valid_iterator.get_next()
# data_valid = static_dataset_valid.expand_batch(batch_to_dict(data_valid, config['signature_dynamic']))
# valid_loss = valid_step(data_valid)
# valid_accumulator.update(valid_loss)
if step % 25 == 0:
data_valid = valid_iterator.get_next()
data_valid = static_dataset_valid.expand_batch(batch_to_dict(data_valid, config['signature_dynamic']))
valid_loss = valid_step(data_valid)
valid_accumulator.update(valid_loss)
# if train_loss['train/euclid_error'] < 0.015:
# if balancing_activated_step == 0:
# balancing_activated_step = step
# balancing_coeff = balancing_coeff + 1 / 20000
if train_loss['train/euclid_error'] < 0.015:
if balancing_activated_step == 0:
balancing_activated_step = step
balancing_coeff = balancing_coeff + 1 / 20000
# if step % 1000 == 0 or (step < 1000 and step % 100 == 0):
# to_log = dict({ **train_loss, **valid_loss, **{ 'current_step': step } })
# if step % 5000 == 0 or (step < 5000 and step % 1000 == 0):
# train_figs = plot_everything(batch, 'train')
# valid_figs = plot_everything(data_valid, 'valid')
# to_log = { **to_log, **train_figs, **valid_figs }
# if step == 0:
# to_log = { **to_log, **{ 'model/summary': wandb.Html(get_summary_html(model)) } }
if step % 1000 == 0 or (step < 1000 and step % 100 == 0):
to_log = dict({ **train_loss, **valid_loss, **{ 'current_step': step } })
if step % 5000 == 0 or (step < 5000 and step % 1000 == 0):
train_figs = plot_everything(batch, 'train')
valid_figs = plot_everything(data_valid, 'valid')
to_log = { **to_log, **train_figs, **valid_figs }
if step == 0:
to_log = { **to_log, **{ 'model/summary': wandb.Html(get_summary_html(model)) } }
# to_log = { **to_log, **{ 'train/balancing_coeff': balancing_coeff, 'train/gamma': gamma } }
to_log = { **to_log, **{ 'train/balancing_coeff': balancing_coeff, 'train/gamma': gamma } }
# wandb.log(to_log, step=step)
# train_accumulator.reset()
# valid_accumulator.reset()
wandb.log(to_log, step=step)
train_accumulator.reset()
valid_accumulator.reset()
# if step % 5000 == 0:
# manager.save(checkpoint_number=step)
if step % 5000 == 0:
manager.save(checkpoint_number=step)
# print(step)
print(step)
step += 1
print('mean:')
print(np.mean(times[10:]))
# print('mean:')
# print(np.mean(times[10:]))