prepare cvae_curv

This commit is contained in:
2023-03-02 16:13:18 +00:00
parent dcb4ade0ca
commit d93678ee1a
4 changed files with 181 additions and 38 deletions
+31 -20
View File
@@ -1,6 +1,6 @@
import os
import sys
from configs.config_cvae_curv import config
from configs.iccv.config_cvae_curv import config
os.environ["CUDA_VISIBLE_DEVICES"] = config['gpu']
import tensorflow as tf
@@ -30,7 +30,6 @@ import plotly.express as ex
from plotly.subplots import make_subplots
from PIL import Image
from tf_utils import check_tf_version, allow_memory_growth
# from losses import collision_penalty as collision_penalty
from losses import collision_loss as collision_penalty
from dataset import DatasetFactory, StaticDataset
@@ -38,15 +37,17 @@ from custom_utils.custom_utils import batch_to_dict
from custom_utils.smpl import SMPL
from models.cvae_curv_model import CVAE
from losses import kl_loss
from custom_utils.custom_utils import Accumulator
def init_wandb():
run = wandb.init(project='ablation', name=config['name'], notes=config['notes'], dir='/wandb')
run = wandb.init(project='iccv', name=config['name'], notes=config['notes'], dir='/tmp', tags=config['tags'])
artifact = wandb.Artifact('code', type='code')
artifact.add_file('cvae_curv.py')
artifact.add_file('models/cvae_curv_model.py')
artifact.add_file('models/cvae_model.py')
artifact.add_file('dataset.py')
artifact.add_file('losses.py')
artifact.add_file('models/base.py')
@@ -55,7 +56,9 @@ def init_wandb():
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')
file_name = '/'.join(config_file_name.split('.')) + '.py'
if os.path.exists(file_name):
artifact.add_file(file_name)
run.log_artifact(artifact)
uv2mesh = UV2MeshLabel(res=256)
@@ -150,7 +153,7 @@ def dice_dense(prediction, ground_truth, weight_map=None):
return tf.reduce_mean(dice_score)
@tf.function
# @tf.function
def train_step(batch):
with tf.GradientTape() as tape:
@@ -376,7 +379,9 @@ if __name__ == '__main__':
valid_iterator = iter(dataset_valid)
ckpt = tf.train.Checkpoint(model=model)
manager = tf.train.CheckpointManager(ckpt, 'checkpoints/' + config['name'], max_to_keep=20)
manager = tf.train.CheckpointManager(ckpt, 'checkpoints/iccv/' + config['name'], max_to_keep=20)
train_accumulator = Accumulator()
valid_accumulator = Accumulator()
if config['pretrained_model'] != '':
load_model(model, config['pretrained_model'], iteration=config['pretrained_model_iteration'])
@@ -386,29 +391,35 @@ if __name__ == '__main__':
batch = static_dataset_train.expand_batch(batch_to_dict(batch, config['signature_dynamic']))
train_loss = train_step(batch)
train_accumulator.update(train_loss)
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)
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)
train_accumulator.update(train_loss)
to_log = { **train_loss, **valid_loss, **{ 'current_step': step} }
if step % 5000 == 0 or (step < 5000 and step % 500 == 0):
train_figs = plot_everything(batch, 'train')
valid_figs = plot_everything(data_valid, 'valid')
to_log = { **to_log, **train_figs, **valid_figs }
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': g_balancing_coeff } }
wandb.log(to_log, step=step)
train_accumulator.reset()
valid_accumulator.reset()
if step % 10000 == 0:
manager.save(checkpoint_number=step)
if step == 0:
to_log = { **to_log, **{ 'model/summary': wandb.Html(get_summary_html(model)) } }
if step > 40000:
g_balancing_coeff = g_balancing_coeff + 1 / 20000
to_log = { **to_log, **{ 'train/g_balancing_coeff': g_balancing_coeff } }
wandb.log(to_log, step = step)
print(step)
step += 1