added skinning and gnn discriminator
This commit is contained in:
+95
-29
@@ -1,17 +1,18 @@
|
||||
import os
|
||||
import time
|
||||
import argparse
|
||||
from cv2 import polarToCart
|
||||
import wandb
|
||||
from cv2 import drawFrameAxes
|
||||
import random
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
from custom_utils.custom_utils import UV2Mesh, maskFacesBatch
|
||||
from tensorboardX import SummaryWriter
|
||||
import plotly.graph_objects as go
|
||||
|
||||
from utils import str_to_bool
|
||||
from tf_utils import check_tf_version, allow_memory_growth, split_gpu_for_testing
|
||||
from load_models import load_generator, load_discriminator
|
||||
from dataset import DatasetSimple
|
||||
from losses import d_logistic, d_logistic_r1_reg, g_logistic_non_saturating, g_logistic_ns_pathreg, edge_loss, uv_mask_loss
|
||||
# from losses import collision_penalty as collision_penalty
|
||||
from losses import collision_loss as collision_penalty
|
||||
@@ -19,8 +20,8 @@ from convnext.models.convnext_tf import ConvNeXt
|
||||
from stylegan2_ae.generator import Generator
|
||||
|
||||
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = "3"
|
||||
run = wandb.init(config=tf.compat.v1.flags.FLAGS, sync_tensorboard=True, project='stylegan', name='ae_1_2', notes='convnext + stylegan generator')
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
|
||||
run = wandb.init(config=tf.compat.v1.flags.FLAGS, sync_tensorboard=True, project='stylegan', name='ae_unposed_1', notes='convnext + stylegan generator')
|
||||
artifact = wandb.Artifact('code', type='code')
|
||||
artifact.add_file('auto_encoder.py')
|
||||
artifact.add_file('stylegan2_ae/layers/mapping_block.py')
|
||||
@@ -29,6 +30,35 @@ run.log_artifact(artifact)
|
||||
uv2mesh = UV2Mesh(res=256)
|
||||
|
||||
|
||||
class DatasetSimple(tf.data.Dataset):
|
||||
|
||||
def _generator(folder):
|
||||
# Opening the file
|
||||
folder = folder.decode("utf-8")
|
||||
uv_folder = os.path.join(folder, 'uv_posed')
|
||||
frames = os.listdir(uv_folder)
|
||||
random.shuffle(frames)
|
||||
|
||||
for frame in frames:
|
||||
id, frame_nr = frame.split('.')[0].split('_')
|
||||
mesh_mask = np.load(os.path.join(folder, 'mesh_mask', id + '.npy')); mesh_mask = mesh_mask.astype(np.float32)
|
||||
uv_mask = np.load(os.path.join(folder, 'uv_mask', id + '.npz')); uv_mask=uv_mask['uv_mask']; uv_mask = uv_mask.astype(np.float32)
|
||||
mesh_unposed = np.load(os.path.join(folder, 'mesh_unposed', id + '_' + frame_nr + '.npz')); mesh_unposed = mesh_unposed['mesh_unposed']
|
||||
uv_unposed = np.load(os.path.join(folder, 'uv_unposed', id + '_' + frame_nr + '.npz')); uv_unposed = uv_unposed['uv_unposed']; uv_unposed = np.transpose(uv_unposed, [2, 0, 1])
|
||||
|
||||
yield uv_unposed, mesh_unposed, uv_mask, mesh_mask
|
||||
|
||||
def __new__(cls, folder, res=256):
|
||||
return tf.data.Dataset.from_generator(
|
||||
cls._generator,
|
||||
output_signature = (tf.TensorSpec(shape = (3, res * 2, res,), dtype = tf.float32),
|
||||
tf.TensorSpec(shape = (14475, 3,), dtype = tf.float32),
|
||||
tf.TensorSpec(shape = (res * 2, res,), dtype = tf.float32),
|
||||
tf.TensorSpec(shape = (14475,), dtype = tf.float32), ),
|
||||
args=(folder, )
|
||||
)
|
||||
|
||||
|
||||
|
||||
def filter_resolutions_featuremaps(resolutions, featuremaps, res):
|
||||
index = resolutions.index(res)
|
||||
@@ -37,6 +67,43 @@ def filter_resolutions_featuremaps(resolutions, featuremaps, res):
|
||||
return filtered_resolutions, filtered_featuremaps
|
||||
|
||||
|
||||
def plot_3d(mesh_gt, mesh_pred, faces):
|
||||
mesh_pred[:,1] += 2
|
||||
fig = go.Figure(data=[
|
||||
go.Mesh3d(
|
||||
# 8 vertices of a cube
|
||||
x=mesh_gt[:,0],
|
||||
y=mesh_gt[:,1],
|
||||
z=mesh_gt[:,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],
|
||||
)
|
||||
])
|
||||
|
||||
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
|
||||
|
||||
|
||||
class AutoEncoder(tf.keras.Model):
|
||||
|
||||
def __init__(self):
|
||||
@@ -78,19 +145,18 @@ class AutoEncoder(tf.keras.Model):
|
||||
|
||||
@tf.function
|
||||
def train_step(batch):
|
||||
posed_uv, conditions,_, mesh_body_posed, mesh_mask = batch
|
||||
uv_mask = conditions[3]
|
||||
uv_unposed, mesh_unposed, uv_mask, mesh_mask = batch
|
||||
|
||||
with tf.GradientTape() as tape:
|
||||
|
||||
pred = model([posed_uv, uv_mask])
|
||||
pred = model([uv_unposed, uv_mask])
|
||||
|
||||
mesh_pred = uv2mesh(pred)
|
||||
mesh_orig = uv2mesh(posed_uv)
|
||||
mesh_orig = mesh_unposed
|
||||
|
||||
e_loss = 0.0#edge_loss(mesh_pred, mesh_orig, mesh_mask)
|
||||
c_loss = 0.0#collision_penalty(mesh_pred, mesh_body_posed, mesh_mask)
|
||||
l1_loss = tf.reduce_mean(tf.reduce_sum((tf.math.abs(posed_uv - pred)), axis=1) * uv_mask)
|
||||
l1_loss = tf.reduce_mean(tf.reduce_sum((tf.math.abs(uv_unposed - pred)), axis=1) * uv_mask)
|
||||
loss = l1_loss + e_loss + c_loss
|
||||
|
||||
metric = tf.reduce_sum(tf.math.sqrt(tf.reduce_sum(mesh_mask[:,:,tf.newaxis] * (mesh_orig - mesh_pred) ** 2, axis=-1))) / tf.reduce_sum(mesh_mask)
|
||||
@@ -102,16 +168,15 @@ def train_step(batch):
|
||||
|
||||
@tf.function
|
||||
def valid_step(batch):
|
||||
posed_uv, conditions,_, mesh_body_posed, mesh_mask = batch
|
||||
uv_mask = conditions[3]
|
||||
uv_unposed, mesh_unposed, uv_mask, mesh_mask = batch
|
||||
|
||||
pred = model([posed_uv, uv_mask])
|
||||
pred = model([uv_unposed, uv_mask])
|
||||
mesh_pred = uv2mesh(pred)
|
||||
mesh_orig = uv2mesh(posed_uv)
|
||||
mesh_orig = mesh_unposed
|
||||
|
||||
e_loss = 0.0#edge_loss(mesh_pred, mesh_orig, mesh_mask)
|
||||
c_loss = 0.0#collision_penalty(mesh_pred, mesh_body_posed, mesh_mask)
|
||||
l1_loss = tf.reduce_mean(tf.reduce_sum((tf.math.abs(posed_uv - pred)), axis=1) * uv_mask)
|
||||
l1_loss = tf.reduce_mean(tf.reduce_sum((tf.math.abs(uv_unposed - pred)), axis=1) * uv_mask)
|
||||
loss = l1_loss + e_loss + c_loss
|
||||
|
||||
metric = tf.reduce_sum(tf.math.sqrt(tf.reduce_sum(mesh_mask[:,:,tf.newaxis] * (mesh_orig - mesh_pred) ** 2, axis=-1))) / tf.reduce_sum(mesh_mask)
|
||||
@@ -122,7 +187,7 @@ if __name__ == '__main__':
|
||||
allow_memory_growth()
|
||||
|
||||
|
||||
name = 'test-9'
|
||||
name = 'ae_unposed_1'
|
||||
dataset_train = DatasetSimple('/home/workspace/data/dataset/stylegan_new/train').cache().batch(8).repeat()
|
||||
dataset_valid = DatasetSimple('/home/workspace/data/dataset/stylegan_new/valid').cache().batch(8).repeat()
|
||||
|
||||
@@ -131,8 +196,7 @@ if __name__ == '__main__':
|
||||
optimizer = tf.optimizers.Adam()
|
||||
folder = 'models/ae_logs/'
|
||||
train_summary_writer = tf.summary.create_file_writer(folder + name + '/train')
|
||||
valid_summary_writer = tf.summary.create_file_writer(folder + name + '/valid')
|
||||
mesh_writer = SummaryWriter(folder + name + '/mesh')
|
||||
# valid_summary_writer = tf.summary.create_file_writer(folder + name + '/valid')
|
||||
|
||||
step = 0
|
||||
for batch in dataset_train:
|
||||
@@ -142,18 +206,19 @@ if __name__ == '__main__':
|
||||
with train_summary_writer.as_default():
|
||||
tf.summary.scalar(key, train_loss[key], step=step)
|
||||
|
||||
data_valid = iter(dataset_valid).get_next()
|
||||
valid_loss = valid_step(data_valid)
|
||||
data_valid = batch
|
||||
# data_valid = iter(dataset_valid).get_next()
|
||||
# valid_loss = valid_step(data_valid)
|
||||
|
||||
for key in valid_loss.keys():
|
||||
with valid_summary_writer.as_default():
|
||||
tf.summary.scalar(key, valid_loss[key], step=step)
|
||||
# for key in valid_loss.keys():
|
||||
# with valid_summary_writer.as_default():
|
||||
# tf.summary.scalar(key, valid_loss[key], step=step)
|
||||
|
||||
if step % 250 == 0:
|
||||
pred = model([data_valid[0], data_valid[1][3]])
|
||||
pred = model([data_valid[0], data_valid[2]])
|
||||
|
||||
mesh_pred = uv2mesh(pred)
|
||||
mesh_orig = uv2mesh(data_valid[0])
|
||||
mesh_orig = data_valid[1]
|
||||
|
||||
orig_img = tf.transpose(data_valid[0], [0,2,3,1])
|
||||
pred_img = tf.transpose(pred, [0,2,3,1])
|
||||
@@ -162,12 +227,13 @@ if __name__ == '__main__':
|
||||
tf.summary.image('images', img, step=step)
|
||||
|
||||
F = np.load("custom_utils/smpl_faces.npy")
|
||||
garment, faces = maskFacesBatch(mesh_pred.numpy(), F, data_valid[4].numpy().astype(bool))
|
||||
gt, _ = maskFacesBatch(mesh_orig.numpy(), F, data_valid[4].numpy().astype(bool))
|
||||
garment, faces = maskFacesBatch(mesh_pred.numpy(), F, data_valid[3].numpy().astype(bool))
|
||||
gt = [mesh_orig[i].numpy()[data_valid[3][i].numpy().astype(bool), :] for i in range(pred.shape[0])]
|
||||
|
||||
for i in range(len(garment)):
|
||||
mesh_writer.add_mesh('garment_pred_' + str(i), vertices=garment[i][np.newaxis,:,:], faces=faces[i][np.newaxis,:,:], global_step=step)
|
||||
mesh_writer.add_mesh('garment_gt_' + str(i), vertices=gt[i][np.newaxis,:,:], faces=faces[i][np.newaxis,:,:], global_step=step)
|
||||
for i in range(pred.shape[0]):
|
||||
fig = plot_3d(gt[i], garment[i], faces[i])
|
||||
wandb.log({"garment_" + str(i): fig})
|
||||
|
||||
|
||||
print(step)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user