more training
This commit is contained in:
+34
-23
@@ -1,5 +1,5 @@
|
||||
import os
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = "6"
|
||||
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
|
||||
import tensorflow as tf
|
||||
|
||||
gpus = tf.config.experimental.list_physical_devices('GPU')
|
||||
@@ -35,7 +35,15 @@ from models.upscaler import UpscalerBasicConv as Upscaler
|
||||
# !!!!!!!!!! CHANGE SSIM BASED ON RES
|
||||
# !!!!!!!!!! CHANGE SKIRT OR NOT
|
||||
|
||||
name='256_512_skirt'
|
||||
|
||||
low_res = 64
|
||||
is_skirt = True
|
||||
is_cape = False
|
||||
skirt_modifier = '_skirt' if is_skirt else ''
|
||||
smpl_res = 3638 if is_cape else 14475
|
||||
|
||||
|
||||
name=f'iccv_{low_res}_skirt'
|
||||
def init_wandb():
|
||||
run = wandb.init(project='upscale', name=name,
|
||||
notes='\
|
||||
@@ -59,15 +67,10 @@ smaller features | \
|
||||
artifact.add_file('convnext/models/convnext_tf.py')
|
||||
run.log_artifact(artifact)
|
||||
|
||||
is_skirt = True
|
||||
is_cape = False
|
||||
skirt_modifier = '_skirt' if is_skirt else ''
|
||||
smpl_res = 3638 if is_cape else 14475
|
||||
|
||||
uv2mesh = UV2Mesh(res=256, is_skirt=is_skirt, is_smpl_orig=is_cape)
|
||||
|
||||
|
||||
|
||||
def plot_3d(mesh_gt, mesh_pred_unposed, faces):
|
||||
mesh_pred_unposed[:,2] += 1
|
||||
fig = go.Figure(data=[
|
||||
@@ -105,7 +108,7 @@ def plot_3d(mesh_gt, mesh_pred_unposed, faces):
|
||||
return fig
|
||||
|
||||
|
||||
# @tf.function
|
||||
@tf.function
|
||||
def train_step(batch):
|
||||
with tf.GradientTape() as tape:
|
||||
pred = model([batch['uv_unposed' + low_resolution_modifier], batch['uv_mask' + low_resolution_modifier]], training=True)
|
||||
@@ -115,8 +118,12 @@ def train_step(batch):
|
||||
mesh_orig = batch['mesh_unposed']
|
||||
|
||||
l1_loss = tf.reduce_mean(tf.reduce_sum(((tf.math.abs(batch['uv_unposed' + high_resolution_modifier] - pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis]) )), axis=-1))
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, batch['uv_unposed' + high_resolution_modifier] * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, max_val=2, power_factors=(0.5, 0.2, 0.2, 0.1)))
|
||||
# ms_ssim = tf.reduce_mean(tf.image.ssim(pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, batch['uv_unposed' + high_resolution_modifier] * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, max_val=2))
|
||||
|
||||
if low_res == 32:
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim(pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, batch['uv_unposed' + high_resolution_modifier] * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, max_val=2))
|
||||
else:
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, batch['uv_unposed' + high_resolution_modifier] * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, max_val=2, power_factors=(0.5, 0.2, 0.2, 0.1)))
|
||||
|
||||
loss = 1e3 * (1 - ms_ssim)
|
||||
|
||||
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'])
|
||||
@@ -125,7 +132,6 @@ def train_step(batch):
|
||||
return { 'train/loss': loss, 'train/l1_loss': l1_loss, 'train/euclid_error': metric, 'train/ms_ssim': ms_ssim }
|
||||
|
||||
|
||||
|
||||
@tf.function
|
||||
def valid_step(batch):
|
||||
pred = model([batch['uv_unposed' + low_resolution_modifier], batch['uv_mask' + low_resolution_modifier]], training=False)
|
||||
@@ -135,15 +141,18 @@ def valid_step(batch):
|
||||
mesh_orig = batch['mesh_unposed']
|
||||
|
||||
l1_loss = tf.reduce_mean(tf.reduce_sum(((tf.math.abs(batch['uv_unposed' + high_resolution_modifier] - pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis]) )), axis=-1))
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, batch['uv_unposed' + high_resolution_modifier] * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, max_val=2, power_factors=(0.5, 0.2, 0.2, 0.1)))
|
||||
# ms_ssim = tf.reduce_mean(tf.image.ssim(pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, batch['uv_unposed' + high_resolution_modifier] * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, max_val=2))
|
||||
|
||||
if low_res == 32:
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim(pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, batch['uv_unposed' + high_resolution_modifier] * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, max_val=2))
|
||||
else:
|
||||
ms_ssim = tf.reduce_mean(tf.image.ssim_multiscale(pred * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, batch['uv_unposed' + high_resolution_modifier] * batch['uv_mask' + high_resolution_modifier][:,:,:,tf.newaxis] + 1, max_val=2, power_factors=(0.5, 0.2, 0.2, 0.1)))
|
||||
|
||||
loss = 1e3 * (1 - ms_ssim)
|
||||
|
||||
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'])
|
||||
return { 'valid/loss': loss, 'valid/l1_loss': l1_loss, 'valid/euclid_error': metric, 'valid/ms_ssim': ms_ssim }
|
||||
|
||||
|
||||
|
||||
def plot_everything(batch: dict, type: str):
|
||||
|
||||
pred = model([batch['uv_unposed' + low_resolution_modifier], batch['uv_mask' + low_resolution_modifier]], training=False)
|
||||
@@ -168,7 +177,6 @@ def plot_everything(batch: dict, type: str):
|
||||
return { **figs, **{ type + '/images': imgs} }
|
||||
|
||||
|
||||
|
||||
def train():
|
||||
valid_iterator = iter(dataset_valid)
|
||||
|
||||
@@ -204,8 +212,8 @@ def train():
|
||||
step += 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
# init_wandb()
|
||||
allow_memory_growth()
|
||||
init_wandb()
|
||||
# allow_memory_growth()
|
||||
parser = argparse.ArgumentParser(description='')
|
||||
parser.add_argument('--name', type=str)
|
||||
args = vars(parser.parse_args())
|
||||
@@ -214,7 +222,6 @@ if __name__ == '__main__':
|
||||
# uv_mask_body = np.load('custom_utils/mask_256x512.npy')
|
||||
# uv_mask_body = cv2.dilate(uv_mask_body.astype(np.float32), cv2.getStructuringElement(cv2.MORPH_RECT, (2 * 2 + 1, 2 * 2 + 1), (2, 2)), iterations=2)
|
||||
|
||||
low_res = 32
|
||||
high_res = 512
|
||||
uv_mask_body = tf.ones((low_res, low_res//2))
|
||||
low_resolution_modifier = '_' + str(low_res)
|
||||
@@ -234,8 +241,8 @@ if __name__ == '__main__':
|
||||
'uv_mask' + low_resolution_modifier: (low_res, low_res//2,),
|
||||
}
|
||||
|
||||
dataset_train = factory.get_dataset(folder='/home/workspace/local-data/train', batch_size=8, signature=signature_dynamic, resolutions=[high_res, low_res], in_memory=False, is_cape=is_cape)
|
||||
static_dataset_train = StaticDataset(folder='/home/workspace/local-data/train', signature=signature_static, resolutions=[high_res, low_res], in_memory=True, is_cape=is_cape)
|
||||
dataset_train = factory.get_dataset(folder='/home/workspace/data/dataset/local-cache/train_skirt', batch_size=8, signature=signature_dynamic, resolutions=[high_res, low_res], in_memory=False, is_cape=is_cape)
|
||||
static_dataset_train = StaticDataset(folder='/home/workspace/data/dataset/local-cache/train_skirt', signature=signature_static, resolutions=[high_res, low_res], in_memory=True, is_cape=is_cape)
|
||||
dataset_valid = factory.get_dataset(folder='/home/workspace/local-data/valid_skirt', batch_size=8, signature=signature_dynamic,resolutions=[high_res, low_res], in_memory=False, is_cape=is_cape)
|
||||
static_dataset_valid = StaticDataset(folder='/home/workspace/local-data/valid_skirt', signature=signature_static, resolutions=[high_res, low_res], in_memory=True, is_cape=is_cape)
|
||||
|
||||
@@ -244,14 +251,18 @@ if __name__ == '__main__':
|
||||
# dataset_valid = factory.get_dataset(folder='/home/workspace/local-data/cape_processed/test/male', batch_size=8, signature=signature_dynamic, resolutions=[high_res, low_res], in_memory=False, is_cape=is_cape)
|
||||
# static_dataset_valid = StaticDataset(folder='/home/workspace/local-data/cape_processed/test/male', signature=signature_static, resolutions=[high_res, low_res], in_memory=True, is_cape=is_cape)
|
||||
|
||||
if low_res == 32 or low_res == 64:
|
||||
model = Upscaler(low_res=low_res, kernel_size=3)
|
||||
elif low_res == 128 or low_res == 256:
|
||||
model = Upscaler(low_res=low_res, kernel_size=5)
|
||||
else:
|
||||
model = Upscaler(low_res=low_res, kernel_size=7)
|
||||
|
||||
model = Upscaler(low_res=low_res, kernel_size=3)
|
||||
|
||||
optimizer = tf.optimizers.Adam(learning_rate=0.0001)
|
||||
folder = 'models/ae_logs/'
|
||||
|
||||
ckpt = tf.train.Checkpoint(model=model)
|
||||
manager = tf.train.CheckpointManager(ckpt, 'checkpoints/' + name, max_to_keep=20)
|
||||
load_model(model, 'checkpoints/32_512_4')
|
||||
manager = tf.train.CheckpointManager(ckpt, 'checkpoints/iccv/' + name, max_to_keep=20)
|
||||
|
||||
train()
|
||||
Reference in New Issue
Block a user