large refactor
This commit is contained in:
+38
-114
@@ -1,5 +1,7 @@
|
||||
import imp
|
||||
import os
|
||||
from configs.config_ae_static import config
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = config['gpu']
|
||||
|
||||
import time
|
||||
import argparse
|
||||
import wandb
|
||||
@@ -13,24 +15,22 @@ import plotly.express as ex
|
||||
from tf_utils import check_tf_version, allow_memory_growth
|
||||
from losses import d_logistic, d_logistic_r1_reg, g_logistic_non_saturating, g_logistic_ns_pathreg, edge_loss, uv_mask_loss
|
||||
from dataset import DatasetFactory, StaticDataset
|
||||
from custom_utils.custom_utils import batch_to_dict
|
||||
from custom_utils.smpl import SMPL
|
||||
from models.ae_static_model import AutoEncoder
|
||||
from custom_utils.custom_utils import batch_to_dict, plot_3d
|
||||
from models.cond_encoder import AutoEncoder
|
||||
|
||||
|
||||
name='ae_static_5'
|
||||
def init_wandb():
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
||||
run = wandb.init(project='vae', name=name,
|
||||
notes='convnext + convnext, add dense with tanh at the end of reverse convnext, larger features. add lr scheduler. fft3d instead of rfft3d. \
|
||||
fft2d per channel and separate for top and bottom. fft_loss, all data. added convnext block as bottleneck, same as before, added extra metric. \
|
||||
. lowered learning rate because it goes to nan. shallow third stage. slower lr decay. with validation. higher lr rate.\
|
||||
convnext first stage down/upscale by factor fo 2 instead of 4, and 5 stages instead of 4 so bottlneck same size\
|
||||
run = wandb.init(project='vae', name=config['name'],
|
||||
notes='convnext + convnext, add dense with tanh at the end of reverse convnext,\
|
||||
flattened latent dim to 128\
|
||||
trai on 100 garments')
|
||||
train on 100 garments')
|
||||
artifact = wandb.Artifact('code', type='code')
|
||||
artifact.add_file('auto_encoder_static.py')
|
||||
artifact.add_file('losses.py')
|
||||
artifact.add_file('configs/config_base.py')
|
||||
artifact.add_file('configs/config_ae_static.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')
|
||||
run.log_artifact(artifact)
|
||||
@@ -39,86 +39,24 @@ uv2mesh = UV2Mesh(res=256)
|
||||
|
||||
|
||||
|
||||
def plot_3d(mesh_gt, mesh_pred_unposed, faces, collision, collision_faces, collision_color):
|
||||
mesh_pred_unposed[:,1] += 1
|
||||
collision[:,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_unposed[:,0],
|
||||
y=mesh_pred_unposed[:,1],
|
||||
z=mesh_pred_unposed[:,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=collision[:,0],
|
||||
y=collision[:,1],
|
||||
z=collision[:,2],
|
||||
# i, j and k give the vertices of triangles
|
||||
i = collision_faces[:,0],
|
||||
j = collision_faces[:,1],
|
||||
k = collision_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
|
||||
|
||||
|
||||
|
||||
@tf.function
|
||||
def train_step(batch):
|
||||
|
||||
with tf.GradientTape() as tape:
|
||||
pred = model(batch['uv_static'])
|
||||
|
||||
uv_mask = batch['uv_mask']
|
||||
pred = model([batch['uv_static'], batch['uv_mask']])
|
||||
|
||||
mesh_pred = uv2mesh(pred)
|
||||
mesh_orig = batch['mesh_static']
|
||||
|
||||
e_loss = 1e-2 * tf.reduce_mean(edge_loss(mesh_pred, mesh_orig, batch['mesh_mask']))
|
||||
c_loss = 0 #1e-2 * tf.reduce_mean(collision_penalty(mesh_pred, batch['mesh_body_posed'], batch['mesh_mask']))
|
||||
# l1_loss = tf.reduce_mean(tf.reduce_sum((tf.math.abs(batch['mesh_unposed'] - pred)), axis=-1) * batch['mesh_mask'])
|
||||
l1_loss = tf.reduce_mean(tf.reduce_sum((tf.math.abs(batch['uv_static'] - pred)), axis=1) * uv_mask)
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(tf.transpose(pred, [0,2,3,1]) * uv_mask[:,:,:,tf.newaxis] + 1, tf.transpose(batch['uv_static'], [0,2,3,1]) * uv_mask[:,:,:,tf.newaxis] + 1, max_val=2))
|
||||
fft_loss = 0 #tf.reduce_mean(tf.reduce_sum(tf.math.abs(tf.signal.fft3d(tf.complex(pred, tf.zeros_like(pred))) - tf.signal.fft3d(tf.complex(batch['uv_static'], tf.zeros_like(batch['uv_static'])))), axis=[1,2,3]))
|
||||
l1_loss = tf.reduce_mean(tf.reduce_sum((tf.math.abs(batch['uv_static'] - pred)), axis=1))
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(pred + 1, batch['uv_static'] + 1, max_val=2, power_factors=(0.5, 0.2, 0.1, 0.1, 0.1)))
|
||||
loss = 1e3 * (1 - ms_ssim)
|
||||
|
||||
mesh_orig_2 = uv2mesh(batch['uv_static'])
|
||||
# mesh_skinned = smpl.run_garment(batch['pose_shape'][:, :72], batch['pose_shape'][:, 72:], mesh_pred)
|
||||
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'])
|
||||
metric_2 = tf.reduce_sum(tf.math.sqrt(tf.reduce_sum(batch['mesh_mask'][:,:,tf.newaxis] * (mesh_orig_2 - mesh_pred) ** 2, axis=-1))) / tf.reduce_sum(batch['mesh_mask'])
|
||||
euclid_posed = 0 # tf.reduce_sum(tf.math.sqrt(tf.reduce_sum(batch['mesh_mask'][:,:,tf.newaxis] * (mesh_skinned - batch['mesh_posed']) ** 2, axis=-1))) / tf.reduce_sum(batch['mesh_mask'])
|
||||
gradients = tape.gradient(loss, model.trainable_variables)
|
||||
# gradients = [tf.clip_by_norm(g, 1.0) for g in gradients]
|
||||
# gradients = [tf.clip_by_value(g, -1.0, 1.0) for g in gradients]
|
||||
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
|
||||
return { 'train/loss': loss, 'train/edge_loss': e_loss, 'train/collision_loss': c_loss, 'train/l1_loss': l1_loss, 'train/euclid_error': metric, 'train/ms_ssim': ms_ssim,
|
||||
'train/fft_loss': fft_loss, 'train/euclid_error_reconst': metric_2, 'train/euclid_error_posed': euclid_posed }
|
||||
return { 'train/loss': loss, 'train/edge_loss': e_loss, 'train/l1_loss': l1_loss, 'train/euclid_error': metric, 'train/ms_ssim': ms_ssim, }
|
||||
|
||||
|
||||
|
||||
@@ -145,14 +83,13 @@ def valid_step(batch):
|
||||
|
||||
|
||||
def plot_everything(batch: dict, type: str):
|
||||
|
||||
pred = model(batch['uv_static'])
|
||||
pred = model([batch['uv_static'], batch['uv_mask']])
|
||||
|
||||
mesh_pred = uv2mesh(pred)
|
||||
mesh_orig = batch['mesh_static']
|
||||
|
||||
orig_img = tf.transpose(batch['uv_static'], [0,2,3,1])
|
||||
pred_img = tf.transpose(pred, [0,2,3,1])
|
||||
orig_img = batch['uv_static']
|
||||
pred_img = pred
|
||||
img = tf.concat((orig_img, pred_img), axis=2)
|
||||
imgs = [wandb.Image((img[i] + 1) / 2.0) for i in range(4)]
|
||||
|
||||
@@ -175,20 +112,23 @@ def plot_everything(batch: dict, type: str):
|
||||
def train():
|
||||
step = 0
|
||||
for batch in dataset_train:
|
||||
batch = static_dataset_train.expand_batch(batch_to_dict(batch, signature_dynamic))
|
||||
batch = static_dataset_train.expand_batch(batch_to_dict(batch, config['signature_dynamic']))
|
||||
train_loss = train_step(batch)
|
||||
|
||||
# data_valid = valid_iterator.get_next()
|
||||
# data_valid = static_dataset_valid.expand_batch(batch_to_dict(data_valid, signature_dynamic))
|
||||
valid_loss = {}#valid_step(data_valid)
|
||||
|
||||
if step % 1000 == 0:
|
||||
if step % 2500 == 0:
|
||||
train_figs = plot_everything(batch, 'train')
|
||||
valid_figs = {}#plot_everything(data_valid, 'valid')
|
||||
wandb.log({ **train_loss, **valid_loss, **train_figs, **valid_figs }, step=step)
|
||||
else:
|
||||
wandb.log({ **train_loss, **valid_loss }, step=step)
|
||||
|
||||
if step % 10000 == 0:
|
||||
manager_enc.save(checkpoint_number=step)
|
||||
manager_dec.save(checkpoint_number=step)
|
||||
|
||||
print(step)
|
||||
|
||||
@@ -201,41 +141,25 @@ if __name__ == '__main__':
|
||||
parser.add_argument('--name', type=str)
|
||||
args = vars(parser.parse_args())
|
||||
|
||||
smpl = SMPL()
|
||||
# TODO: different resolutions not working yet, model should be ready though
|
||||
|
||||
resolution_modifier = '_32'
|
||||
resolutions = [32]
|
||||
resolution_modifier = ''
|
||||
if config['resolution'] != 512:
|
||||
resolution_modifier = '_' + str(config['resolution'])
|
||||
|
||||
factory = DatasetFactory()
|
||||
signature_dynamic = { 'id': (),
|
||||
# 'pose_shape': (72 + 11,),
|
||||
# 'uv_posed': (3, 512, 256,),
|
||||
# 'uv_body_posed': (3, 512, 256,),
|
||||
# 'mesh_body_posed': (14475, 3,),
|
||||
# 'mesh_posed': (14475, 3,),
|
||||
# 'mesh_unposed': (14475, 3,),
|
||||
# 'uv_unposed': (3, 512, 256,)
|
||||
}
|
||||
signature_static = { 'uv_static': (3, 512, 256,),
|
||||
'mesh_static': (14475, 3,),
|
||||
'mesh_mask': (14475,),
|
||||
'uv_mask': (512, 256,),
|
||||
# 'uv_shaped_body': (3, 512, 256,),
|
||||
}
|
||||
|
||||
dataset_train = factory.get_dataset(folder='/home/workspace/local-data/train_100', batch_size=8, signature=signature_dynamic)
|
||||
static_dataset_train = StaticDataset(folder='/home/workspace/local-data/train_100', signature=signature_static)
|
||||
dataset_valid = factory.get_dataset(folder='/home/workspace/local-data/jumpsuit_20/valid', batch_size=8, signature=signature_dynamic)
|
||||
static_dataset_valid = StaticDataset(folder='/home/workspace/local-data/jumpsuit_20/valid', signature=signature_static)
|
||||
dataset_train = factory.get_dataset(folder=config['train_dir'], batch_size=config['batch_size'], signature=config['signature_dynamic'], use_filled=config['use_filled'], resolutions=[config['resolution']])
|
||||
static_dataset_train = StaticDataset(folder=config['train_dir'], signature=config['signature_static'], use_filled=config['use_filled'], resolutions=[config['resolution']])
|
||||
dataset_valid = factory.get_dataset(folder=config['valid_dir'], batch_size=config['batch_size'], signature=config['signature_dynamic'])
|
||||
static_dataset_valid = StaticDataset(folder=config['valid_dir'], signature=config['signature_static'])
|
||||
|
||||
model : AutoEncoder = AutoEncoder(config['cond_ae'])
|
||||
optimizer = tf.optimizers.Adam(learning_rate=config['lr'])
|
||||
|
||||
model = AutoEncoder()
|
||||
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(0.0005, decay_steps=30000, decay_rate=0.1, staircase=False)
|
||||
optimizer = tf.optimizers.Adam(learning_rate=lr_schedule)
|
||||
folder = 'models/ae_logs/'
|
||||
valid_iterator = iter(dataset_valid)
|
||||
|
||||
ckpt = tf.train.Checkpoint(model=model)
|
||||
manager = tf.train.CheckpointManager(ckpt, 'checkpoints/' + name, max_to_keep=5)
|
||||
ckpt_enc = tf.train.Checkpoint(model=model.encoder)
|
||||
manager_enc = tf.train.CheckpointManager(ckpt_enc, 'checkpoints/' + config['name'] + '_enc', max_to_keep=3)
|
||||
ckpt_dec = tf.train.Checkpoint(model=model.decoder)
|
||||
manager_dec = tf.train.CheckpointManager(ckpt_dec, 'checkpoints/' + config['name'] + '_dec', max_to_keep=3)
|
||||
|
||||
train()
|
||||
Reference in New Issue
Block a user