commit all
This commit is contained in:
+44
-106
@@ -8,96 +8,43 @@ import scipy as sp
|
||||
# import plotly.express as ex
|
||||
|
||||
from custom_utils.custom_utils import load_model
|
||||
from convnext.models.convnext_tf import ConvNeXt, Block, ConvNeXtReverseLabels
|
||||
from auto_encoder_body import AutoEncoder as BodyAE
|
||||
from auto_encoder_static import AutoEncoder as StaticAE
|
||||
from models.base import AEEncoder, VariationalEncoder, Decoder
|
||||
|
||||
|
||||
|
||||
class Bottleneck(tf.keras.Model):
|
||||
|
||||
def __init__(self, dim):
|
||||
super(Bottleneck, self).__init__()
|
||||
|
||||
self.bottleneck = tf.keras.Sequential(
|
||||
[Block(dim=dim, drop_path=0,
|
||||
layer_scale_init_value=1e-6, prefix=f'bottleneck{j}') for j in range(5)]
|
||||
)
|
||||
|
||||
def call(self, inputs, training=None):
|
||||
x = inputs
|
||||
|
||||
x = self.bottleneck(x, training=training)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class Encoder(tf.keras.Model):
|
||||
|
||||
def __init__(self, latent_dim):
|
||||
super(Encoder, self).__init__()
|
||||
|
||||
self.encoder = ConvNeXt(depths=[3, 3, 9, 3], dims=[32, 64, 128, 256], include_top=False)
|
||||
self.flatten = tf.keras.layers.Flatten()
|
||||
self.mean = tf.keras.layers.Dense(latent_dim)
|
||||
self.log_var = tf.keras.layers.Dense(latent_dim)
|
||||
|
||||
def call(self, inputs, training=None):
|
||||
x = inputs
|
||||
|
||||
x = self.encoder(x)
|
||||
x = self.flatten(x)
|
||||
mean = self.mean(x)
|
||||
log_var = self.log_var(x)
|
||||
|
||||
return mean, log_var
|
||||
|
||||
|
||||
class Decoder(tf.keras.Model):
|
||||
|
||||
def __init__(self):
|
||||
super(Decoder, self).__init__()
|
||||
|
||||
self.dense = tf.keras.layers.Dense(16 * 8 * 80)
|
||||
self.reshape = tf.keras.layers.Reshape((16, 8, 80))
|
||||
self.decoder = ConvNeXtReverseLabels(depths=list(reversed([3, 3, 9, 3])), dims=list(reversed([256, 128, 64, 16])))
|
||||
|
||||
|
||||
def call(self, inputs, training=None):
|
||||
x = inputs
|
||||
|
||||
x = self.dense(x)
|
||||
x = self.reshape(x)
|
||||
x = self.decoder(x)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class CVAE(tf.keras.Model):
|
||||
|
||||
def __init__(self, latent_dim=128, static_model='checkpoints/ae_static_3',
|
||||
body_model='checpoints/ae_body_6',):
|
||||
def __init__(self, config):
|
||||
super(CVAE, self).__init__()
|
||||
self.latent_dim = latent_dim
|
||||
self.latent_dim = config['latent_dim']
|
||||
self.latent_dim = config['cvae']['latent_dim']
|
||||
self.size = config['resolution']
|
||||
|
||||
model : StaticAE = StaticAE(size=512)
|
||||
load_model(model, static_model)
|
||||
self.static_enc = tf.keras.Sequential([model.encoder, model.bottleneck])
|
||||
self.static_enc.trainable = False
|
||||
self.static_enc : AEEncoder = AEEncoder(config['cond_ae']['latent_dim'], config['cond_ae']['encoder_depths'], config['cond_ae']['encoder_dims'], config['cond_ae']['bottleneck_size'], config['cond_ae']['kernel_size'])
|
||||
if config['static_model'] != '':
|
||||
load_model(self.static_enc, config['static_model'] + '_enc')
|
||||
|
||||
model : BodyAE = BodyAE(size=512)
|
||||
load_model(model, body_model)
|
||||
self.body_enc = tf.keras.Sequential([model.encoder, model.bottleneck])
|
||||
self.body_enc.trainable = False
|
||||
self.body_enc : AEEncoder = AEEncoder(config['cond_ae_body']['latent_dim'], config['cond_ae_body']['encoder_depths'], config['cond_ae_body']['encoder_dims'], config['cond_ae_body']['bottleneck_size'], config['cond_ae_body']['kernel_size'])
|
||||
if config['body_model'] != '':
|
||||
load_model(self.body_enc, config['body_model'] + '_enc')
|
||||
|
||||
self.cond_encoder = tf.keras.Sequential([tf.keras.layers.Dense(256, activation='relu'),
|
||||
if config['freeze_encoders']:
|
||||
self.static_enc.trainable = False
|
||||
self.body_enc.trainable = False
|
||||
|
||||
self.cond_encoder = tf.keras.Sequential([tf.keras.layers.Dense(512, activation='relu'),
|
||||
tf.keras.layers.Dense(256, activation='relu'),
|
||||
tf.keras.layers.Dense(128, activation='relu'),
|
||||
tf.keras.layers.Dense(latent_dim),])
|
||||
tf.keras.layers.Dense(self.latent_dim, activation='tanh'),])
|
||||
|
||||
self.encoder = Encoder(latent_dim)
|
||||
self.decoder = Decoder()
|
||||
self.encoder = VariationalEncoder(config['cvae']['latent_dim'], config['cvae']['encoder_depths'], config['cvae']['encoder_dims'], kernel_size=config['cvae']['kernel_size'])
|
||||
self.decoder = Decoder(config['cvae']['decoder_input'], config['cvae']['decoder_depths'], config['cvae']['decoder_dims'], config['cvae']['kernel_size'], is_label=config['cvae']['is_label'])
|
||||
|
||||
self.add_noise = config['cvae']['add_noise']
|
||||
self.noise_amount = config['cvae']['noise_amount']
|
||||
|
||||
# @tf.function
|
||||
def sample_single(self, uv_template, uv_body_posed, uv_mask, eps=None, nr_of_samples=4):
|
||||
@@ -122,33 +69,32 @@ class CVAE(tf.keras.Model):
|
||||
return x
|
||||
|
||||
|
||||
def encode_conds(self, uv_template, uv_body_posed):
|
||||
if uv_template.shape[1] == 3:
|
||||
uv_template = tf.transpose(uv_template, [0,2,3,1])
|
||||
if uv_body_posed.shape[1] == 3:
|
||||
uv_body_posed = tf.transpose(uv_body_posed, [0,2,3,1])
|
||||
|
||||
z_static = self.static_enc(uv_template)
|
||||
z_body = self.body_enc(uv_body_posed)
|
||||
def encode_conds(self, uv_template, uv_body_posed, training):
|
||||
z_static = self.static_enc(uv_template, training=training)
|
||||
z_body = self.body_enc(uv_body_posed, training=training)
|
||||
|
||||
z_cond = tf.concat([z_static, z_body], axis=-1)
|
||||
z_cond = self.cond_encoder(z_cond)
|
||||
z_cond = self.cond_encoder(z_cond, training=training)
|
||||
|
||||
return z_cond
|
||||
|
||||
def sample(self, uv_template, uv_body_posed, uv_mask=None, noise=None):
|
||||
def sample(self, uv_template, uv_body_posed, uv_mask, noise=None):
|
||||
if noise is None:
|
||||
eps = tf.random.normal(shape=(uv_template.shape[0], self.latent_dim))
|
||||
else:
|
||||
eps = noise
|
||||
|
||||
z_cond = self.encode_conds(uv_template, uv_body_posed)
|
||||
z_cond = self.encode_conds(uv_template, uv_body_posed, training=False)
|
||||
z = tf.concat([eps, z_cond], axis=-1)
|
||||
|
||||
x = self.decoder(z)
|
||||
x = self.decoder(z, training=False)
|
||||
|
||||
if uv_mask is not None:
|
||||
x = x * uv_mask[:,:,:,tf.newaxis]
|
||||
x = x * uv_mask[:,:,:,tf.newaxis]
|
||||
|
||||
background = tf.ones((uv_template.shape[0],512,256,1)) * (1 - uv_mask[:,:,:,tf.newaxis])
|
||||
x = tf.concat((background, x), axis=-1)
|
||||
|
||||
x = tf.math.softmax(x)
|
||||
|
||||
return x
|
||||
|
||||
@@ -171,32 +117,24 @@ class CVAE(tf.keras.Model):
|
||||
return eps * tf.exp(logvar * .5) + mean
|
||||
|
||||
|
||||
def decode(self, z, apply_sigmoid=False):
|
||||
logits = self.decoder(z)
|
||||
if apply_sigmoid:
|
||||
probs = tf.sigmoid(logits)
|
||||
return probs
|
||||
return logits
|
||||
|
||||
|
||||
def call(self, inputs, training=None):
|
||||
def call(self, inputs, training):
|
||||
uv_template, uv_body_posed, uv_curv, uv_mask = inputs
|
||||
if uv_template.shape[1] == 3:
|
||||
uv_template = tf.transpose(uv_template, [0,2,3,1])
|
||||
if uv_body_posed.shape[1] == 3:
|
||||
uv_body_posed = tf.transpose(uv_body_posed, [0,2,3,1])
|
||||
if uv_curv.shape[1] == 3:
|
||||
uv_curv = tf.transpose(uv_curv, [0,2,3,1])
|
||||
|
||||
z_cond = self.encode_conds(uv_template, uv_body_posed)
|
||||
|
||||
x = tf.concat([uv_template, uv_body_posed, uv_curv], axis=-1)
|
||||
mean, logvar = self.encoder(x)
|
||||
z = self.reparameterize(mean, logvar)
|
||||
|
||||
z_cond = self.encode_conds(uv_template, uv_body_posed, training=training)
|
||||
|
||||
z = tf.concat([z, z_cond], axis=-1)
|
||||
x = self.decoder(z)
|
||||
x = self.decoder(z, training=training)
|
||||
|
||||
x = x * uv_mask[:,:,:,tf.newaxis]
|
||||
|
||||
return x, mean, logvar, z
|
||||
background = tf.ones((4,512,256,1)) * (1 - uv_mask[:,:,:,tf.newaxis])
|
||||
x = tf.concat((background, x), axis=-1)
|
||||
|
||||
x = tf.math.softmax(x)
|
||||
|
||||
return x, mean, logvar
|
||||
|
||||
Reference in New Issue
Block a user