pyramid pipeline ready
This commit is contained in:
+116
-124
@@ -1,176 +1,168 @@
|
||||
import os
|
||||
import time
|
||||
import random
|
||||
import tensorflow as tf
|
||||
import torch
|
||||
from torch import nn
|
||||
import numpy as np
|
||||
import scipy as sp
|
||||
# import plotly.graph_objects as go
|
||||
# import plotly.express as ex
|
||||
|
||||
from custom_utils.custom_utils import load_model
|
||||
from models.base import AEEncoder, VariationalEncoder, Decoder, VariationalEncoderNew, DecoderNew
|
||||
from models.upscaler import UpscalerBasic, UpscalerBasicConv, UpscalerBasicNew
|
||||
from utils.checkpoints import load_model
|
||||
from models.base import AEEncoder, VariationalEncoder, Decoder
|
||||
from models.upscaler import Upscaler
|
||||
|
||||
|
||||
class PyramidLevel(tf.keras.Model):
|
||||
|
||||
# class PyramidLevel(nn.Module):
|
||||
|
||||
# def __init__(self, config):
|
||||
# super(PyramidLevel, self).__init__()
|
||||
# self.latent_dim = config['latent_dim']
|
||||
# self.size = config['resolution']
|
||||
|
||||
# self.encoder: VariationalEncoder = VariationalEncoder(config['encoder_block_settings'], config['input_features'], kernel_size=config['kernel_size'])
|
||||
# self.decoder: Decoder = Decoder(config['decoder_block_settings'], config['kernel_size'])
|
||||
|
||||
# self.upscale_enhancer = None
|
||||
# if config['upscale_enhancer'] != '':
|
||||
# k_size = config['upscale_enhancer_kernel_size']
|
||||
# self.upscale_enhancer = Upscaler(config['resolution'], kernel_size=k_size)
|
||||
# load_model(self.upscale_enhancer, config['upscale_enhancer'])
|
||||
# for param in self.upscale_enhancer.parameters(): param.requires_grad = False
|
||||
|
||||
# if config['avg_garment_path'] != '':
|
||||
# self.avg_garment = np.load(config['avg_garment_path'])
|
||||
# else:
|
||||
# self.avg_garment = None
|
||||
|
||||
|
||||
# def enhance(self, low, mask, training=None):
|
||||
# x = self.upscale_enhancer([low, mask], training=training)
|
||||
# return x
|
||||
|
||||
|
||||
# def sample(self, z_cond, uv_mask, noise=None):
|
||||
# if noise is None:
|
||||
# eps = torch.normal(shape=(uv_mask.shape[0], self.latent_dim))
|
||||
# else:
|
||||
# eps = noise
|
||||
|
||||
# z = torch.concat([eps, z_cond], dim=-1)
|
||||
|
||||
# x = self.decoder(z)
|
||||
|
||||
# if self.avg_garment is not None:
|
||||
# x = x + self.avg_garment[None,:,:,:]
|
||||
|
||||
# x = x * uv_mask[:,None,:,:]
|
||||
|
||||
# return x
|
||||
|
||||
|
||||
# def reparameterize(self, mean, logvar, training, fix_noise=False):
|
||||
# if training == True:
|
||||
# if not fix_noise:
|
||||
# eps = torch.normal(mean=0.0, std=1.0, size=mean.shape)
|
||||
# else:
|
||||
# eps = torch.normal(mean=0.0, std=1.0, size=mean.shape)
|
||||
# eps = torch.tile(eps, [mean.shape[0],1])
|
||||
# out = eps * torch.exp(logvar * .5) + mean
|
||||
# else:
|
||||
# out = torch.exp(logvar * .5) + mean
|
||||
# return out
|
||||
|
||||
|
||||
# def call(self, inputs, z_cond, training=None):
|
||||
# uv_unposed, uv_template, uv_body_posed, uv_curv, uv_mask, uv_mask_high = inputs
|
||||
|
||||
# x = torch.concat([uv_unposed, uv_template, uv_body_posed, uv_curv], dim=1)
|
||||
# mean, logvar = self.encoder(x, training=training)
|
||||
# z = self.reparameterize(mean, logvar, training)
|
||||
|
||||
# z = torch.concat([z, z_cond], dim=-1)
|
||||
# x = self.decoder(z, training=training)
|
||||
|
||||
# if self.avg_garment is not None:
|
||||
# x = x + self.avg_garment[None,:,:,:]
|
||||
|
||||
# x = x * uv_mask[:,None,:,:]
|
||||
|
||||
# # if self.upscale_enhancer is not None:
|
||||
# # pred_low = self.enhance(x, uv_mask, training=training)
|
||||
# # x = tf.image.resize(pred_low, (512, 256), tf.image.ResizeMethod.LANCZOS5)
|
||||
|
||||
# # x = x * uv_mask_high[:,:,:,tf.newaxis]
|
||||
|
||||
# return x, mean, logvar
|
||||
|
||||
|
||||
|
||||
class PyramidLevel(nn.Module):
|
||||
|
||||
def __init__(self, config):
|
||||
super(PyramidLevel, self).__init__()
|
||||
self.latent_dim = config['latent_dim']
|
||||
self.size = config['resolution']
|
||||
|
||||
self.encoder = VariationalEncoder(config['latent_dim'], config['encoder_depths'], config['encoder_dims'], kernel_size=config['kernel_size'])
|
||||
self.decoder = Decoder(config['decoder_input'], config['decoder_depths'], config['decoder_dims'], config['kernel_size'])
|
||||
self.encoder: VariationalEncoder = VariationalEncoder(config['encoder_block_settings'], input_features=config['input_features'], kernel_size=config['kernel_size'])
|
||||
self.decoder: Decoder = Decoder(config['decoder_block_settings'], config['kernel_size'])
|
||||
|
||||
self.upscale_enhancer = None
|
||||
if config['upscale_enhancer'] != '':
|
||||
k_size = config['upscale_enhancer_kernel_size'] if 'upscale_enhancer_kernel_size' in config.keys() else ((3 if config['resolution'] <= 64 else 5))
|
||||
self.upscale_enhancer = UpscalerBasicConv(config['resolution'], kernel_size=k_size)
|
||||
k_size = config['kernel_size']
|
||||
self.upscale_enhancer = Upscaler(config['resolution'], kernel_size=k_size).to('cuda')
|
||||
load_model(self.upscale_enhancer, config['upscale_enhancer'])
|
||||
self.upscale_enhancer.trainable = not config['upscale_enhancer_freeze']
|
||||
|
||||
if config['avg_garment_path'] != '':
|
||||
self.avg_garment = np.load(config['avg_garment_path'])
|
||||
else:
|
||||
self.avg_garment = None
|
||||
for param in self.upscale_enhancer.parameters(): param.requires_grad = False
|
||||
|
||||
|
||||
def enhance(self, low, mask, training=None):
|
||||
x = self.upscale_enhancer([low, mask], training=training)
|
||||
def enhance(self, low, mask):
|
||||
x = self.upscale_enhancer([low, mask])
|
||||
return x
|
||||
|
||||
|
||||
def sample(self, z_cond, uv_mask, noise=None):
|
||||
if noise is None:
|
||||
eps = tf.random.normal(shape=(uv_mask.shape[0], self.latent_dim))
|
||||
eps = torch.normal(shape=(uv_mask.shape[0], self.latent_dim))
|
||||
else:
|
||||
eps = noise
|
||||
|
||||
z = tf.concat([eps, z_cond], axis=-1)
|
||||
z = torch.concat([eps, z_cond], dim=-1)
|
||||
|
||||
x = self.decoder(z)
|
||||
|
||||
if self.avg_garment is not None:
|
||||
x = x + self.avg_garment[tf.newaxis,:,:,:]
|
||||
x = x + self.avg_garment[None,:,:,:]
|
||||
|
||||
x = x * uv_mask[:,:,:,tf.newaxis]
|
||||
x = x * uv_mask[:,None:,:]
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def reparameterize(self, mean, logvar, training, fix_noise=False):
|
||||
if training == True:
|
||||
def reparameterize(self, mean, logvar, fix_noise=False):
|
||||
if self.training == True:
|
||||
if not fix_noise:
|
||||
eps = tf.random.normal(shape=mean.shape)
|
||||
eps = torch.normal(mean=0.0, std=1.0, size=mean.shape).to(mean.device)
|
||||
else:
|
||||
eps = tf.random.normal(shape=(1, mean.shape[1]))
|
||||
eps = tf.tile(eps, [mean.shape[0],1])
|
||||
out = eps * tf.exp(logvar * .5) + mean
|
||||
eps = torch.normal(mean=0.0, std=1.0, size=mean.shape).to(mean.device)
|
||||
eps = torch.tile(eps, [mean.shape[0],1])
|
||||
out = eps * torch.exp(logvar * .5) + mean
|
||||
else:
|
||||
out = tf.exp(logvar * .5) + mean
|
||||
out = torch.exp(logvar * .5) + mean
|
||||
return out
|
||||
|
||||
|
||||
def call(self, inputs, z_cond, training=None):
|
||||
def forward(self, inputs, z_cond):
|
||||
uv_unposed, uv_template, uv_body_posed, uv_curv, uv_mask, uv_mask_high = inputs
|
||||
|
||||
x = tf.concat([uv_unposed, uv_template, uv_body_posed, uv_curv], axis=-1)
|
||||
mean, logvar = self.encoder(x, training=training)
|
||||
z = self.reparameterize(mean, logvar, training)
|
||||
|
||||
z = tf.concat([z, z_cond], axis=-1)
|
||||
x = self.decoder(z, training=training)
|
||||
|
||||
if self.avg_garment is not None:
|
||||
x = x + self.avg_garment[tf.newaxis,:,:,:]
|
||||
|
||||
x = x * uv_mask[:,:,:,tf.newaxis]
|
||||
|
||||
# if self.upscale_enhancer is not None:
|
||||
# pred_low = self.enhance(x, uv_mask, training=training)
|
||||
# x = tf.image.resize(pred_low, (512, 256), tf.image.ResizeMethod.LANCZOS5)
|
||||
|
||||
# x = x * uv_mask_high[:,:,:,tf.newaxis]
|
||||
|
||||
return x, mean, logvar
|
||||
|
||||
|
||||
|
||||
class PyramidLevelNew(tf.keras.Model):
|
||||
|
||||
def __init__(self, config):
|
||||
super(PyramidLevelNew, self).__init__()
|
||||
self.size = config['resolution']
|
||||
|
||||
self.encoder = VariationalEncoderNew(config['encoder_depths'], config['encoder_dims'], kernel_size=config['kernel_size'])
|
||||
self.decoder = DecoderNew(config['decoder_depths'], config['decoder_dims'], config['kernel_size'])
|
||||
|
||||
self.upscale_enhancer = None
|
||||
if config['upscale_enhancer'] != '':
|
||||
k_size = config['upscale_enhancer_kernel_size'] if 'upscale_enhancer_kernel_size' in config.keys() else ((3 if config['resolution'] <= 64 else 5))
|
||||
self.upscale_enhancer = UpscalerBasicConv(config['resolution'], kernel_size=k_size)
|
||||
load_model(self.upscale_enhancer, config['upscale_enhancer'])
|
||||
self.upscale_enhancer.trainable = not config['upscale_enhancer_freeze']
|
||||
|
||||
if config['avg_garment_path'] != '':
|
||||
self.avg_garment = np.load(config['avg_garment_path'])
|
||||
else:
|
||||
self.avg_garment = None
|
||||
|
||||
self.reshape = tf.keras.layers.Reshape((8, 4, 64))
|
||||
|
||||
|
||||
def enhance(self, low, mask, training=None):
|
||||
x = self.upscale_enhancer([low, mask], training=training)
|
||||
return x
|
||||
|
||||
|
||||
def sample(self, z_cond, uv_mask, noise=None):
|
||||
if noise is None:
|
||||
eps = tf.random.normal(shape=(uv_mask.shape[0], self.latent_dim))
|
||||
else:
|
||||
eps = noise
|
||||
|
||||
z = tf.concat([eps, z_cond], axis=-1)
|
||||
x = torch.concat([uv_unposed, uv_template, uv_body_posed, uv_curv], dim=1)
|
||||
mean, logvar = self.encoder(x)
|
||||
z = self.reparameterize(mean, logvar)
|
||||
z = torch.reshape(z, (mean.shape[0], -1, 8, 4))
|
||||
|
||||
z = torch.concat([z, z_cond], dim=1)
|
||||
x = self.decoder(z)
|
||||
|
||||
if self.avg_garment is not None:
|
||||
x = x + self.avg_garment[tf.newaxis,:,:,:]
|
||||
|
||||
x = x * uv_mask[:,:,:,tf.newaxis]
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def reparameterize(self, mean, logvar, training, fix_noise=False):
|
||||
if training == True:
|
||||
if not fix_noise:
|
||||
eps = tf.random.normal(shape=mean.shape)
|
||||
else:
|
||||
eps = tf.random.normal(shape=(1, mean.shape[1]))
|
||||
eps = tf.tile(eps, [mean.shape[0],1])
|
||||
out = eps * tf.exp(logvar * .5) + mean
|
||||
else:
|
||||
out = tf.exp(logvar * .5) + mean
|
||||
return out
|
||||
|
||||
|
||||
def call(self, inputs, z_cond, training=None):
|
||||
uv_unposed, uv_template, uv_body_posed, uv_curv, uv_mask, uv_mask_high = inputs
|
||||
|
||||
x = tf.concat([uv_unposed, uv_template, uv_body_posed, uv_curv], axis=-1)
|
||||
mean, logvar = self.encoder(x, training=training)
|
||||
z = self.reparameterize(mean, logvar, training)
|
||||
z = self.reshape(z)
|
||||
|
||||
z = tf.concat([z, z_cond], axis=-1)
|
||||
x = self.decoder(z, training=training)
|
||||
|
||||
if self.avg_garment is not None:
|
||||
x = x + self.avg_garment[tf.newaxis,:,:,:]
|
||||
|
||||
x = x * uv_mask[:,:,:,tf.newaxis]
|
||||
x = x * uv_mask[:,None,:,:]
|
||||
|
||||
# if self.upscale_enhancer is not None:
|
||||
# pred_low = self.enhance(x, uv_mask, training=training)
|
||||
|
||||
Reference in New Issue
Block a user