mirror of
https://github.com/SkyworkAI/Matrix-3D
synced 2026-11-25 01:50:39 +08:00
920 lines
37 KiB
Python
920 lines
37 KiB
Python
import torch
|
|
import pytorch_lightning as pl
|
|
import torch.nn.functional as F
|
|
from contextlib import contextmanager
|
|
|
|
from taming.modules.vqvae.quantize import VectorQuantizer2 as VectorQuantizer
|
|
|
|
from ldm.modules.diffusionmodules.model import Encoder, Decoder, Decoder_Mix
|
|
from ldm.modules.distributions.distributions import DiagonalGaussianDistribution
|
|
|
|
from ldm.util import instantiate_from_config
|
|
|
|
from basicsr.utils import DiffJPEG, USMSharp
|
|
from basicsr.utils.img_process_util import filter2D
|
|
from basicsr.data.transforms import paired_random_crop, triplet_random_crop
|
|
from basicsr.data.degradations import random_add_gaussian_noise_pt, random_add_poisson_noise_pt, random_add_speckle_noise_pt, random_add_saltpepper_noise_pt
|
|
import random
|
|
|
|
import torchvision.transforms as transforms
|
|
|
|
|
|
class VQModel(pl.LightningModule):
|
|
def __init__(self,
|
|
ddconfig,
|
|
lossconfig,
|
|
n_embed,
|
|
embed_dim,
|
|
ckpt_path=None,
|
|
ignore_keys=[],
|
|
image_key="image",
|
|
colorize_nlabels=None,
|
|
monitor=None,
|
|
batch_resize_range=None,
|
|
scheduler_config=None,
|
|
lr_g_factor=1.0,
|
|
remap=None,
|
|
sane_index_shape=False, # tell vector quantizer to return indices as bhw
|
|
use_ema=False
|
|
):
|
|
super().__init__()
|
|
self.embed_dim = embed_dim
|
|
self.n_embed = n_embed
|
|
self.image_key = image_key
|
|
self.encoder = Encoder(**ddconfig)
|
|
self.decoder = Decoder(**ddconfig)
|
|
self.loss = instantiate_from_config(lossconfig)
|
|
self.quantize = VectorQuantizer(n_embed, embed_dim, beta=0.25,
|
|
remap=remap,
|
|
sane_index_shape=sane_index_shape)
|
|
self.quant_conv = torch.nn.Conv2d(ddconfig["z_channels"], embed_dim, 1)
|
|
self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1)
|
|
if colorize_nlabels is not None:
|
|
assert type(colorize_nlabels)==int
|
|
self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1))
|
|
if monitor is not None:
|
|
self.monitor = monitor
|
|
self.batch_resize_range = batch_resize_range
|
|
if self.batch_resize_range is not None:
|
|
print(f"{self.__class__.__name__}: Using per-batch resizing in range {batch_resize_range}.")
|
|
|
|
self.use_ema = use_ema
|
|
if self.use_ema:
|
|
self.model_ema = LitEma(self)
|
|
print(f"Keeping EMAs of {len(list(self.model_ema.buffers()))}.")
|
|
|
|
if ckpt_path is not None:
|
|
self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys)
|
|
self.scheduler_config = scheduler_config
|
|
self.lr_g_factor = lr_g_factor
|
|
|
|
@contextmanager
|
|
def ema_scope(self, context=None):
|
|
if self.use_ema:
|
|
self.model_ema.store(self.parameters())
|
|
self.model_ema.copy_to(self)
|
|
if context is not None:
|
|
print(f"{context}: Switched to EMA weights")
|
|
try:
|
|
yield None
|
|
finally:
|
|
if self.use_ema:
|
|
self.model_ema.restore(self.parameters())
|
|
if context is not None:
|
|
print(f"{context}: Restored training weights")
|
|
|
|
def init_from_ckpt(self, path, ignore_keys=list()):
|
|
sd = torch.load(path, map_location="cpu")["state_dict"]
|
|
keys = list(sd.keys())
|
|
for k in keys:
|
|
for ik in ignore_keys:
|
|
if k.startswith(ik):
|
|
print("Deleting key {} from state_dict.".format(k))
|
|
del sd[k]
|
|
missing, unexpected = self.load_state_dict(sd, strict=False)
|
|
print(f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys")
|
|
if len(missing) > 0:
|
|
print(f"Missing Keys: {missing}")
|
|
print(f"Unexpected Keys: {unexpected}")
|
|
|
|
def on_train_batch_end(self, *args, **kwargs):
|
|
if self.use_ema:
|
|
self.model_ema(self)
|
|
|
|
def encode(self, x):
|
|
h = self.encoder(x)
|
|
h = self.quant_conv(h)
|
|
quant, emb_loss, info = self.quantize(h)
|
|
return quant, emb_loss, info
|
|
|
|
def encode_to_prequant(self, x):
|
|
h = self.encoder(x)
|
|
h = self.quant_conv(h)
|
|
return h
|
|
|
|
def decode(self, quant):
|
|
quant = self.post_quant_conv(quant)
|
|
dec = self.decoder(quant)
|
|
return dec
|
|
|
|
def decode_code(self, code_b):
|
|
quant_b = self.quantize.embed_code(code_b)
|
|
dec = self.decode(quant_b)
|
|
return dec
|
|
|
|
def forward(self, input, return_pred_indices=False):
|
|
quant, diff, (_,_,ind) = self.encode(input)
|
|
dec = self.decode(quant)
|
|
if return_pred_indices:
|
|
return dec, diff, ind
|
|
return dec, diff
|
|
|
|
def get_input(self, batch, k):
|
|
x = batch[k]
|
|
if len(x.shape) == 3:
|
|
x = x[..., None]
|
|
x = x.permute(0, 3, 1, 2).to(memory_format=torch.contiguous_format).float()
|
|
if self.batch_resize_range is not None:
|
|
lower_size = self.batch_resize_range[0]
|
|
upper_size = self.batch_resize_range[1]
|
|
if self.global_step <= 4:
|
|
# do the first few batches with max size to avoid later oom
|
|
new_resize = upper_size
|
|
else:
|
|
new_resize = np.random.choice(np.arange(lower_size, upper_size+16, 16))
|
|
if new_resize != x.shape[2]:
|
|
x = F.interpolate(x, size=new_resize, mode="bicubic")
|
|
x = x.detach()
|
|
return x
|
|
|
|
def training_step(self, batch, batch_idx, optimizer_idx):
|
|
# https://github.com/pytorch/pytorch/issues/37142
|
|
# try not to fool the heuristics
|
|
x = self.get_input(batch, self.image_key)
|
|
xrec, qloss, ind = self(x, return_pred_indices=True)
|
|
|
|
if optimizer_idx == 0:
|
|
# autoencode
|
|
aeloss, log_dict_ae = self.loss(qloss, x, xrec, optimizer_idx, self.global_step,
|
|
last_layer=self.get_last_layer(), split="train",
|
|
predicted_indices=ind)
|
|
|
|
self.log_dict(log_dict_ae, prog_bar=False, logger=True, on_step=True, on_epoch=True)
|
|
return aeloss
|
|
|
|
if optimizer_idx == 1:
|
|
# discriminator
|
|
discloss, log_dict_disc = self.loss(qloss, x, xrec, optimizer_idx, self.global_step,
|
|
last_layer=self.get_last_layer(), split="train")
|
|
self.log_dict(log_dict_disc, prog_bar=False, logger=True, on_step=True, on_epoch=True)
|
|
return discloss
|
|
|
|
def validation_step(self, batch, batch_idx):
|
|
log_dict = self._validation_step(batch, batch_idx)
|
|
with self.ema_scope():
|
|
log_dict_ema = self._validation_step(batch, batch_idx, suffix="_ema")
|
|
return log_dict
|
|
|
|
def _validation_step(self, batch, batch_idx, suffix=""):
|
|
x = self.get_input(batch, self.image_key)
|
|
xrec, qloss, ind = self(x, return_pred_indices=True)
|
|
aeloss, log_dict_ae = self.loss(qloss, x, xrec, 0,
|
|
self.global_step,
|
|
last_layer=self.get_last_layer(),
|
|
split="val"+suffix,
|
|
predicted_indices=ind
|
|
)
|
|
|
|
discloss, log_dict_disc = self.loss(qloss, x, xrec, 1,
|
|
self.global_step,
|
|
last_layer=self.get_last_layer(),
|
|
split="val"+suffix,
|
|
predicted_indices=ind
|
|
)
|
|
rec_loss = log_dict_ae[f"val{suffix}/rec_loss"]
|
|
self.log(f"val{suffix}/rec_loss", rec_loss,
|
|
prog_bar=True, logger=True, on_step=False, on_epoch=True, sync_dist=True)
|
|
self.log(f"val{suffix}/aeloss", aeloss,
|
|
prog_bar=True, logger=True, on_step=False, on_epoch=True, sync_dist=True)
|
|
if version.parse(pl.__version__) >= version.parse('1.4.0'):
|
|
del log_dict_ae[f"val{suffix}/rec_loss"]
|
|
self.log_dict(log_dict_ae)
|
|
self.log_dict(log_dict_disc)
|
|
return self.log_dict
|
|
|
|
def configure_optimizers(self):
|
|
lr_d = self.learning_rate
|
|
lr_g = self.lr_g_factor*self.learning_rate
|
|
print("lr_d", lr_d)
|
|
print("lr_g", lr_g)
|
|
opt_ae = torch.optim.Adam(list(self.encoder.parameters())+
|
|
list(self.decoder.parameters())+
|
|
list(self.quantize.parameters())+
|
|
list(self.quant_conv.parameters())+
|
|
list(self.post_quant_conv.parameters()),
|
|
lr=lr_g, betas=(0.5, 0.9))
|
|
opt_disc = torch.optim.Adam(self.loss.discriminator.parameters(),
|
|
lr=lr_d, betas=(0.5, 0.9))
|
|
|
|
if self.scheduler_config is not None:
|
|
scheduler = instantiate_from_config(self.scheduler_config)
|
|
|
|
print("Setting up LambdaLR scheduler...")
|
|
scheduler = [
|
|
{
|
|
'scheduler': LambdaLR(opt_ae, lr_lambda=scheduler.schedule),
|
|
'interval': 'step',
|
|
'frequency': 1
|
|
},
|
|
{
|
|
'scheduler': LambdaLR(opt_disc, lr_lambda=scheduler.schedule),
|
|
'interval': 'step',
|
|
'frequency': 1
|
|
},
|
|
]
|
|
return [opt_ae, opt_disc], scheduler
|
|
return [opt_ae, opt_disc], []
|
|
|
|
def get_last_layer(self):
|
|
return self.decoder.conv_out.weight
|
|
|
|
def log_images(self, batch, only_inputs=False, plot_ema=False, **kwargs):
|
|
log = dict()
|
|
x = self.get_input(batch, self.image_key)
|
|
x = x.to(self.device)
|
|
if only_inputs:
|
|
log["inputs"] = x
|
|
return log
|
|
xrec, _ = self(x)
|
|
if x.shape[1] > 3:
|
|
# colorize with random projection
|
|
assert xrec.shape[1] > 3
|
|
x = self.to_rgb(x)
|
|
xrec = self.to_rgb(xrec)
|
|
log["inputs"] = x
|
|
log["reconstructions"] = xrec
|
|
if plot_ema:
|
|
with self.ema_scope():
|
|
xrec_ema, _ = self(x)
|
|
if x.shape[1] > 3: xrec_ema = self.to_rgb(xrec_ema)
|
|
log["reconstructions_ema"] = xrec_ema
|
|
return log
|
|
|
|
def to_rgb(self, x):
|
|
assert self.image_key == "segmentation"
|
|
if not hasattr(self, "colorize"):
|
|
self.register_buffer("colorize", torch.randn(3, x.shape[1], 1, 1).to(x))
|
|
x = F.conv2d(x, weight=self.colorize)
|
|
x = 2.*(x-x.min())/(x.max()-x.min()) - 1.
|
|
return x
|
|
|
|
class VQModelInterface(VQModel):
|
|
def __init__(self, embed_dim, *args, **kwargs):
|
|
super().__init__(embed_dim=embed_dim, *args, **kwargs)
|
|
self.embed_dim = embed_dim
|
|
|
|
def encode(self, x):
|
|
h = self.encoder(x)
|
|
h = self.quant_conv(h)
|
|
return h
|
|
|
|
def decode(self, h, force_not_quantize=False):
|
|
# also go through quantization layer
|
|
if not force_not_quantize:
|
|
quant, emb_loss, info = self.quantize(h)
|
|
else:
|
|
quant = h
|
|
quant = self.post_quant_conv(quant)
|
|
dec = self.decoder(quant)
|
|
return dec
|
|
|
|
class AutoencoderKL(pl.LightningModule):
|
|
def __init__(self,
|
|
ddconfig,
|
|
lossconfig,
|
|
embed_dim,
|
|
ckpt_path=None,
|
|
ignore_keys=[],
|
|
image_key="image",
|
|
colorize_nlabels=None,
|
|
monitor=None,
|
|
):
|
|
super().__init__()
|
|
self.image_key = image_key
|
|
self.encoder = Encoder(**ddconfig)
|
|
self.decoder = Decoder(**ddconfig)
|
|
self.loss = instantiate_from_config(lossconfig)
|
|
assert ddconfig["double_z"]
|
|
self.quant_conv = torch.nn.Conv2d(2*ddconfig["z_channels"], 2*embed_dim, 1)
|
|
self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1)
|
|
self.embed_dim = embed_dim
|
|
if colorize_nlabels is not None:
|
|
assert type(colorize_nlabels)==int
|
|
self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1))
|
|
if monitor is not None:
|
|
self.monitor = monitor
|
|
if ckpt_path is not None:
|
|
self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys)
|
|
|
|
def init_from_ckpt(self, path, ignore_keys=list(), only_model=False):
|
|
sd = torch.load(path, map_location="cpu")
|
|
if "state_dict" in list(sd.keys()):
|
|
sd = sd["state_dict"]
|
|
keys = list(sd.keys())
|
|
for k in keys:
|
|
if 'first_stage_model' in k:
|
|
sd[k[18:]] = sd[k]
|
|
for ik in ignore_keys:
|
|
if k.startswith(ik):
|
|
print("Deleting key {} from state_dict.".format(k))
|
|
del sd[k]
|
|
missing, unexpected = self.load_state_dict(sd, strict=False) if not only_model else self.model.load_state_dict(
|
|
sd, strict=False)
|
|
print(f"Encoder Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys")
|
|
if len(missing) > 0:
|
|
print(f"Missing Keys: {missing}")
|
|
# if len(unexpected) > 0:
|
|
# print(f"Unexpected Keys: {unexpected}")
|
|
|
|
def encode(self, x, return_encfea=False):
|
|
h = self.encoder(x)
|
|
moments = self.quant_conv(h)
|
|
posterior = DiagonalGaussianDistribution(moments)
|
|
if return_encfea:
|
|
return posterior, moments
|
|
return posterior
|
|
|
|
def encode_gt(self, x, new_encoder):
|
|
h = new_encoder(x)
|
|
moments = self.quant_conv(h)
|
|
posterior = DiagonalGaussianDistribution(moments)
|
|
return posterior, moments
|
|
|
|
def decode(self, z):
|
|
z = self.post_quant_conv(z)
|
|
dec = self.decoder(z)
|
|
return dec
|
|
|
|
def forward(self, input, sample_posterior=True):
|
|
posterior = self.encode(input)
|
|
if sample_posterior:
|
|
z = posterior.sample()
|
|
else:
|
|
z = posterior.mode()
|
|
dec = self.decode(z)
|
|
return dec, posterior
|
|
|
|
def get_input(self, batch, k):
|
|
x = batch[k]
|
|
if len(x.shape) == 3:
|
|
x = x[..., None]
|
|
# x = x.permute(0, 3, 1, 2).to(memory_format=torch.contiguous_format).float()
|
|
x = x.to(memory_format=torch.contiguous_format).float()
|
|
# x = x*2.0-1.0
|
|
return x
|
|
|
|
def training_step(self, batch, batch_idx, optimizer_idx):
|
|
inputs = self.get_input(batch, self.image_key)
|
|
reconstructions, posterior = self(inputs)
|
|
|
|
if optimizer_idx == 0:
|
|
# train encoder+decoder+logvar
|
|
aeloss, log_dict_ae = self.loss(inputs, reconstructions, posterior, optimizer_idx, self.global_step,
|
|
last_layer=self.get_last_layer(), split="train")
|
|
self.log("aeloss", aeloss, prog_bar=True, logger=True, on_step=True, on_epoch=True)
|
|
self.log_dict(log_dict_ae, prog_bar=False, logger=True, on_step=True, on_epoch=False)
|
|
return aeloss
|
|
|
|
if optimizer_idx == 1:
|
|
# train the discriminator
|
|
discloss, log_dict_disc = self.loss(inputs, reconstructions, posterior, optimizer_idx, self.global_step,
|
|
last_layer=self.get_last_layer(), split="train")
|
|
|
|
self.log("discloss", discloss, prog_bar=True, logger=True, on_step=True, on_epoch=True)
|
|
self.log_dict(log_dict_disc, prog_bar=False, logger=True, on_step=True, on_epoch=False)
|
|
return discloss
|
|
|
|
def validation_step(self, batch, batch_idx):
|
|
inputs = self.get_input(batch, self.image_key)
|
|
reconstructions, posterior = self(inputs)
|
|
aeloss, log_dict_ae = self.loss(inputs, reconstructions, posterior, 0, self.global_step,
|
|
last_layer=self.get_last_layer(), split="val")
|
|
|
|
discloss, log_dict_disc = self.loss(inputs, reconstructions, posterior, 1, self.global_step,
|
|
last_layer=self.get_last_layer(), split="val")
|
|
|
|
self.log("val/rec_loss", log_dict_ae["val/rec_loss"])
|
|
self.log_dict(log_dict_ae)
|
|
self.log_dict(log_dict_disc)
|
|
return self.log_dict
|
|
|
|
def configure_optimizers(self):
|
|
lr = self.learning_rate
|
|
opt_ae = torch.optim.Adam(list(self.encoder.parameters())+
|
|
list(self.decoder.parameters())+
|
|
list(self.quant_conv.parameters())+
|
|
list(self.post_quant_conv.parameters()),
|
|
lr=lr, betas=(0.5, 0.9))
|
|
opt_disc = torch.optim.Adam(self.loss.discriminator.parameters(),
|
|
lr=lr, betas=(0.5, 0.9))
|
|
return [opt_ae, opt_disc], []
|
|
|
|
def get_last_layer(self):
|
|
return self.decoder.conv_out.weight
|
|
|
|
@torch.no_grad()
|
|
def log_images(self, batch, only_inputs=False, **kwargs):
|
|
log = dict()
|
|
x = self.get_input(batch, self.image_key)
|
|
x = x.to(self.device)
|
|
if not only_inputs:
|
|
xrec, posterior = self(x)
|
|
if x.shape[1] > 3:
|
|
# colorize with random projection
|
|
assert xrec.shape[1] > 3
|
|
x = self.to_rgb(x)
|
|
xrec = self.to_rgb(xrec)
|
|
# log["samples"] = self.decode(torch.randn_like(posterior.sample()))
|
|
log["reconstructions"] = xrec
|
|
log["inputs"] = x
|
|
return log
|
|
|
|
def to_rgb(self, x):
|
|
assert self.image_key == "segmentation"
|
|
if not hasattr(self, "colorize"):
|
|
self.register_buffer("colorize", torch.randn(3, x.shape[1], 1, 1).to(x))
|
|
x = F.conv2d(x, weight=self.colorize)
|
|
x = 2.*(x-x.min())/(x.max()-x.min()) - 1.
|
|
return x
|
|
|
|
class IdentityFirstStage(torch.nn.Module):
|
|
def __init__(self, *args, vq_interface=False, **kwargs):
|
|
self.vq_interface = vq_interface # TODO: Should be true by default but check to not break older stuff
|
|
super().__init__()
|
|
|
|
def encode(self, x, *args, **kwargs):
|
|
return x
|
|
|
|
def decode(self, x, *args, **kwargs):
|
|
return x
|
|
|
|
def quantize(self, x, *args, **kwargs):
|
|
if self.vq_interface:
|
|
return x, None, [None, None, None]
|
|
return x
|
|
|
|
def forward(self, x, *args, **kwargs):
|
|
return x
|
|
|
|
class AutoencoderKLResi(pl.LightningModule):
|
|
def __init__(self,
|
|
ddconfig,
|
|
lossconfig,
|
|
embed_dim,
|
|
ckpt_path=None,
|
|
ignore_keys=[],
|
|
image_key="image",
|
|
colorize_nlabels=None,
|
|
monitor=None,
|
|
fusion_w=1.0,
|
|
freeze_dec=True,
|
|
synthesis_data=False,
|
|
use_usm=False,
|
|
test_gt=False,
|
|
):
|
|
super().__init__()
|
|
self.image_key = image_key
|
|
self.encoder = Encoder(**ddconfig)
|
|
self.decoder = Decoder_Mix(**ddconfig)
|
|
self.decoder.fusion_w = fusion_w
|
|
self.loss = instantiate_from_config(lossconfig)
|
|
self.quant_conv = torch.nn.Conv2d(2*ddconfig["z_channels"], 2*embed_dim, 1)
|
|
self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1)
|
|
self.embed_dim = embed_dim
|
|
if colorize_nlabels is not None:
|
|
assert type(colorize_nlabels)==int
|
|
self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1))
|
|
if monitor is not None:
|
|
self.monitor = monitor
|
|
if ckpt_path is not None:
|
|
missing_list = self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys)
|
|
else:
|
|
missing_list = []
|
|
|
|
print('>>>>>>>>>>>>>>>>>missing>>>>>>>>>>>>>>>>>>>')
|
|
print(missing_list)
|
|
self.synthesis_data = synthesis_data
|
|
self.use_usm = use_usm
|
|
self.test_gt = test_gt
|
|
|
|
if freeze_dec:
|
|
for name, param in self.named_parameters():
|
|
if 'fusion_layer' in name:
|
|
param.requires_grad = True
|
|
# elif 'encoder' in name:
|
|
# param.requires_grad = True
|
|
# elif 'quant_conv' in name and 'post_quant_conv' not in name:
|
|
# param.requires_grad = True
|
|
elif 'loss.discriminator' in name:
|
|
param.requires_grad = True
|
|
else:
|
|
param.requires_grad = False
|
|
|
|
print('>>>>>>>>>>>>>>>>>trainable_list>>>>>>>>>>>>>>>>>>>')
|
|
trainable_list = []
|
|
for name, params in self.named_parameters():
|
|
if params.requires_grad:
|
|
trainable_list.append(name)
|
|
print(trainable_list)
|
|
|
|
print('>>>>>>>>>>>>>>>>>Untrainable_list>>>>>>>>>>>>>>>>>>>')
|
|
untrainable_list = []
|
|
for name, params in self.named_parameters():
|
|
if not params.requires_grad:
|
|
untrainable_list.append(name)
|
|
print(untrainable_list)
|
|
# untrainable_list = list(set(trainable_list).difference(set(missing_list)))
|
|
# print('>>>>>>>>>>>>>>>>>untrainable_list>>>>>>>>>>>>>>>>>>>')
|
|
# print(untrainable_list)
|
|
|
|
# def init_from_ckpt(self, path, ignore_keys=list()):
|
|
# sd = torch.load(path, map_location="cpu")["state_dict"]
|
|
# keys = list(sd.keys())
|
|
# for k in keys:
|
|
# for ik in ignore_keys:
|
|
# if k.startswith(ik):
|
|
# print("Deleting key {} from state_dict.".format(k))
|
|
# del sd[k]
|
|
# self.load_state_dict(sd, strict=False)
|
|
# print(f"Restored from {path}")
|
|
|
|
def init_from_ckpt(self, path, ignore_keys=list(), only_model=False):
|
|
sd = torch.load(path, map_location="cpu")
|
|
if "state_dict" in list(sd.keys()):
|
|
sd = sd["state_dict"]
|
|
keys = list(sd.keys())
|
|
for k in keys:
|
|
if 'first_stage_model' in k:
|
|
sd[k[18:]] = sd[k]
|
|
del sd[k]
|
|
for ik in ignore_keys:
|
|
if k.startswith(ik):
|
|
print("Deleting key {} from state_dict.".format(k))
|
|
del sd[k]
|
|
missing, unexpected = self.load_state_dict(sd, strict=False) if not only_model else self.model.load_state_dict(
|
|
sd, strict=False)
|
|
print(f"Encoder Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys")
|
|
if len(missing) > 0:
|
|
print(f"Missing Keys: {missing}")
|
|
if len(unexpected) > 0:
|
|
print(f"Unexpected Keys: {unexpected}")
|
|
return missing
|
|
|
|
def encode(self, x):
|
|
h, enc_fea = self.encoder(x, return_fea=True)
|
|
moments = self.quant_conv(h)
|
|
posterior = DiagonalGaussianDistribution(moments)
|
|
# posterior = h
|
|
return posterior, enc_fea
|
|
|
|
def encode_gt(self, x, new_encoder):
|
|
h = new_encoder(x)
|
|
moments = self.quant_conv(h)
|
|
posterior = DiagonalGaussianDistribution(moments)
|
|
return posterior, moments
|
|
|
|
def decode(self, z, enc_fea):
|
|
z = self.post_quant_conv(z)
|
|
dec = self.decoder(z, enc_fea)
|
|
return dec
|
|
|
|
def forward(self, input, latent, sample_posterior=True):
|
|
posterior, enc_fea_lq = self.encode(input)
|
|
dec = self.decode(latent, enc_fea_lq)
|
|
return dec, posterior
|
|
|
|
@torch.no_grad()
|
|
def _dequeue_and_enqueue(self):
|
|
"""It is the training pair pool for increasing the diversity in a batch.
|
|
|
|
Batch processing limits the diversity of synthetic degradations in a batch. For example, samples in a
|
|
batch could not have different resize scaling factors. Therefore, we employ this training pair pool
|
|
to increase the degradation diversity in a batch.
|
|
"""
|
|
# initialize
|
|
b, c, h, w = self.lq.size()
|
|
_, c_, h_, w_ = self.latent.size()
|
|
if b == self.configs.data.params.batch_size:
|
|
if not hasattr(self, 'queue_size'):
|
|
self.queue_size = self.configs.data.params.train.params.get('queue_size', b*50)
|
|
if not hasattr(self, 'queue_lr'):
|
|
assert self.queue_size % b == 0, f'queue size {self.queue_size} should be divisible by batch size {b}'
|
|
self.queue_lr = torch.zeros(self.queue_size, c, h, w).cuda()
|
|
_, c, h, w = self.gt.size()
|
|
self.queue_gt = torch.zeros(self.queue_size, c, h, w).cuda()
|
|
self.queue_sample = torch.zeros(self.queue_size, c, h, w).cuda()
|
|
self.queue_latent = torch.zeros(self.queue_size, c_, h_, w_).cuda()
|
|
self.queue_ptr = 0
|
|
if self.queue_ptr == self.queue_size: # the pool is full
|
|
# do dequeue and enqueue
|
|
# shuffle
|
|
idx = torch.randperm(self.queue_size)
|
|
self.queue_lr = self.queue_lr[idx]
|
|
self.queue_gt = self.queue_gt[idx]
|
|
self.queue_sample = self.queue_sample[idx]
|
|
self.queue_latent = self.queue_latent[idx]
|
|
# get first b samples
|
|
lq_dequeue = self.queue_lr[0:b, :, :, :].clone()
|
|
gt_dequeue = self.queue_gt[0:b, :, :, :].clone()
|
|
sample_dequeue = self.queue_sample[0:b, :, :, :].clone()
|
|
latent_dequeue = self.queue_latent[0:b, :, :, :].clone()
|
|
# update the queue
|
|
self.queue_lr[0:b, :, :, :] = self.lq.clone()
|
|
self.queue_gt[0:b, :, :, :] = self.gt.clone()
|
|
self.queue_sample[0:b, :, :, :] = self.sample.clone()
|
|
self.queue_latent[0:b, :, :, :] = self.latent.clone()
|
|
|
|
self.lq = lq_dequeue
|
|
self.gt = gt_dequeue
|
|
self.sample = sample_dequeue
|
|
self.latent = latent_dequeue
|
|
else:
|
|
# only do enqueue
|
|
self.queue_lr[self.queue_ptr:self.queue_ptr + b, :, :, :] = self.lq.clone()
|
|
self.queue_gt[self.queue_ptr:self.queue_ptr + b, :, :, :] = self.gt.clone()
|
|
self.queue_sample[self.queue_ptr:self.queue_ptr + b, :, :, :] = self.sample.clone()
|
|
self.queue_latent[self.queue_ptr:self.queue_ptr + b, :, :, :] = self.latent.clone()
|
|
self.queue_ptr = self.queue_ptr + b
|
|
|
|
def get_input(self, batch):
|
|
input = batch['lq']
|
|
gt = batch['gt']
|
|
latent = batch['latent']
|
|
sample = batch['sample']
|
|
|
|
assert not torch.isnan(latent).any()
|
|
|
|
input = input.to(memory_format=torch.contiguous_format).float()
|
|
gt = gt.to(memory_format=torch.contiguous_format).float()
|
|
latent = latent.to(memory_format=torch.contiguous_format).float() / 0.18215
|
|
|
|
gt = gt * 2.0 - 1.0
|
|
input = input * 2.0 - 1.0
|
|
sample = sample * 2.0 -1.0
|
|
|
|
return input, gt, latent, sample
|
|
|
|
@torch.no_grad()
|
|
def get_input_synthesis(self, batch, val=False, test_gt=False):
|
|
|
|
jpeger = DiffJPEG(differentiable=False).cuda() # simulate JPEG compression artifacts
|
|
im_gt = batch['gt'].cuda()
|
|
if self.use_usm:
|
|
usm_sharpener = USMSharp().cuda() # do usm sharpening
|
|
im_gt = usm_sharpener(im_gt)
|
|
im_gt = im_gt.to(memory_format=torch.contiguous_format).float()
|
|
kernel1 = batch['kernel1'].cuda()
|
|
kernel2 = batch['kernel2'].cuda()
|
|
sinc_kernel = batch['sinc_kernel'].cuda()
|
|
|
|
ori_h, ori_w = im_gt.size()[2:4]
|
|
|
|
# ----------------------- The first degradation process ----------------------- #
|
|
# blur
|
|
out = filter2D(im_gt, kernel1)
|
|
# random resize
|
|
updown_type = random.choices(
|
|
['up', 'down', 'keep'],
|
|
self.configs.degradation['resize_prob'],
|
|
)[0]
|
|
if updown_type == 'up':
|
|
scale = random.uniform(1, self.configs.degradation['resize_range'][1])
|
|
elif updown_type == 'down':
|
|
scale = random.uniform(self.configs.degradation['resize_range'][0], 1)
|
|
else:
|
|
scale = 1
|
|
mode = random.choice(['area', 'bilinear', 'bicubic'])
|
|
out = F.interpolate(out, scale_factor=scale, mode=mode)
|
|
# add noise
|
|
gray_noise_prob = self.configs.degradation['gray_noise_prob']
|
|
if random.random() < self.configs.degradation['gaussian_noise_prob']:
|
|
out = random_add_gaussian_noise_pt(
|
|
out,
|
|
sigma_range=self.configs.degradation['noise_range'],
|
|
clip=True,
|
|
rounds=False,
|
|
gray_prob=gray_noise_prob,
|
|
)
|
|
else:
|
|
out = random_add_poisson_noise_pt(
|
|
out,
|
|
scale_range=self.configs.degradation['poisson_scale_range'],
|
|
gray_prob=gray_noise_prob,
|
|
clip=True,
|
|
rounds=False)
|
|
# JPEG compression
|
|
jpeg_p = out.new_zeros(out.size(0)).uniform_(*self.configs.degradation['jpeg_range'])
|
|
out = torch.clamp(out, 0, 1) # clamp to [0, 1], otherwise JPEGer will result in unpleasant artifacts
|
|
out = jpeger(out, quality=jpeg_p)
|
|
|
|
# ----------------------- The second degradation process ----------------------- #
|
|
# blur
|
|
if random.random() < self.configs.degradation['second_blur_prob']:
|
|
out = filter2D(out, kernel2)
|
|
# random resize
|
|
updown_type = random.choices(
|
|
['up', 'down', 'keep'],
|
|
self.configs.degradation['resize_prob2'],
|
|
)[0]
|
|
if updown_type == 'up':
|
|
scale = random.uniform(1, self.configs.degradation['resize_range2'][1])
|
|
elif updown_type == 'down':
|
|
scale = random.uniform(self.configs.degradation['resize_range2'][0], 1)
|
|
else:
|
|
scale = 1
|
|
mode = random.choice(['area', 'bilinear', 'bicubic'])
|
|
out = F.interpolate(
|
|
out,
|
|
size=(int(ori_h / self.configs.sf * scale),
|
|
int(ori_w / self.configs.sf * scale)),
|
|
mode=mode,
|
|
)
|
|
# add noise
|
|
gray_noise_prob = self.configs.degradation['gray_noise_prob2']
|
|
if random.random() < self.configs.degradation['gaussian_noise_prob2']:
|
|
out = random_add_gaussian_noise_pt(
|
|
out,
|
|
sigma_range=self.configs.degradation['noise_range2'],
|
|
clip=True,
|
|
rounds=False,
|
|
gray_prob=gray_noise_prob,
|
|
)
|
|
else:
|
|
out = random_add_poisson_noise_pt(
|
|
out,
|
|
scale_range=self.configs.degradation['poisson_scale_range2'],
|
|
gray_prob=gray_noise_prob,
|
|
clip=True,
|
|
rounds=False,
|
|
)
|
|
|
|
# JPEG compression + the final sinc filter
|
|
# We also need to resize images to desired sizes. We group [resize back + sinc filter] together
|
|
# as one operation.
|
|
# We consider two orders:
|
|
# 1. [resize back + sinc filter] + JPEG compression
|
|
# 2. JPEG compression + [resize back + sinc filter]
|
|
# Empirically, we find other combinations (sinc + JPEG + Resize) will introduce twisted lines.
|
|
if random.random() < 0.5:
|
|
# resize back + the final sinc filter
|
|
mode = random.choice(['area', 'bilinear', 'bicubic'])
|
|
out = F.interpolate(
|
|
out,
|
|
size=(ori_h // self.configs.sf,
|
|
ori_w // self.configs.sf),
|
|
mode=mode,
|
|
)
|
|
out = filter2D(out, sinc_kernel)
|
|
# JPEG compression
|
|
jpeg_p = out.new_zeros(out.size(0)).uniform_(*self.configs.degradation['jpeg_range2'])
|
|
out = torch.clamp(out, 0, 1)
|
|
out = jpeger(out, quality=jpeg_p)
|
|
else:
|
|
# JPEG compression
|
|
jpeg_p = out.new_zeros(out.size(0)).uniform_(*self.configs.degradation['jpeg_range2'])
|
|
out = torch.clamp(out, 0, 1)
|
|
out = jpeger(out, quality=jpeg_p)
|
|
# resize back + the final sinc filter
|
|
mode = random.choice(['area', 'bilinear', 'bicubic'])
|
|
out = F.interpolate(
|
|
out,
|
|
size=(ori_h // self.configs.sf,
|
|
ori_w // self.configs.sf),
|
|
mode=mode,
|
|
)
|
|
out = filter2D(out, sinc_kernel)
|
|
|
|
# clamp and round
|
|
im_lq = torch.clamp(out, 0, 1.0)
|
|
|
|
# random crop
|
|
gt_size = self.configs.degradation['gt_size']
|
|
im_gt, im_lq = paired_random_crop(im_gt, im_lq, gt_size, self.configs.sf)
|
|
self.lq, self.gt = im_lq, im_gt
|
|
|
|
self.lq = F.interpolate(
|
|
self.lq,
|
|
size=(self.gt.size(-2),
|
|
self.gt.size(-1)),
|
|
mode='bicubic',
|
|
)
|
|
|
|
self.latent = batch['latent'] / 0.18215
|
|
self.sample = batch['sample'] * 2 - 1.0
|
|
# training pair pool
|
|
if not val:
|
|
self._dequeue_and_enqueue()
|
|
# sharpen self.gt again, as we have changed the self.gt with self._dequeue_and_enqueue
|
|
self.lq = self.lq.contiguous() # for the warning: grad and param do not obey the gradient layout contract
|
|
self.lq = self.lq*2 - 1.0
|
|
self.gt = self.gt*2 - 1.0
|
|
|
|
self.lq = torch.clamp(self.lq, -1.0, 1.0)
|
|
|
|
x = self.lq
|
|
y = self.gt
|
|
x = x.to(self.device)
|
|
y = y.to(self.device)
|
|
|
|
if self.test_gt:
|
|
return y, y, self.latent.to(self.device), self.sample.to(self.device)
|
|
else:
|
|
return x, y, self.latent.to(self.device), self.sample.to(self.device)
|
|
|
|
def training_step(self, batch, batch_idx, optimizer_idx):
|
|
if self.synthesis_data:
|
|
inputs, gts, latents, _ = self.get_input_synthesis(batch, val=False)
|
|
else:
|
|
inputs, gts, latents, _ = self.get_input(batch)
|
|
reconstructions, posterior = self(inputs, latents)
|
|
|
|
if optimizer_idx == 0:
|
|
# train encoder+decoder+logvar
|
|
aeloss, log_dict_ae = self.loss(gts, reconstructions, posterior, optimizer_idx, self.global_step,
|
|
last_layer=self.get_last_layer(), split="train")
|
|
self.log("aeloss", aeloss, prog_bar=True, logger=True, on_step=True, on_epoch=True)
|
|
self.log_dict(log_dict_ae, prog_bar=False, logger=True, on_step=True, on_epoch=False)
|
|
return aeloss
|
|
|
|
if optimizer_idx == 1:
|
|
# train the discriminator
|
|
discloss, log_dict_disc = self.loss(gts, reconstructions, posterior, optimizer_idx, self.global_step,
|
|
last_layer=self.get_last_layer(), split="train")
|
|
|
|
self.log("discloss", discloss, prog_bar=True, logger=True, on_step=True, on_epoch=True)
|
|
self.log_dict(log_dict_disc, prog_bar=False, logger=True, on_step=True, on_epoch=False)
|
|
return discloss
|
|
|
|
def validation_step(self, batch, batch_idx):
|
|
inputs, gts, latents, _ = self.get_input(batch)
|
|
|
|
reconstructions, posterior = self(inputs, latents)
|
|
aeloss, log_dict_ae = self.loss(gts, reconstructions, posterior, 0, self.global_step,
|
|
last_layer=self.get_last_layer(), split="val")
|
|
|
|
discloss, log_dict_disc = self.loss(gts, reconstructions, posterior, 1, self.global_step,
|
|
last_layer=self.get_last_layer(), split="val")
|
|
|
|
self.log("val/rec_loss", log_dict_ae["val/rec_loss"])
|
|
self.log_dict(log_dict_ae)
|
|
self.log_dict(log_dict_disc)
|
|
return self.log_dict
|
|
|
|
def configure_optimizers(self):
|
|
lr = self.learning_rate
|
|
opt_ae = torch.optim.Adam(list(self.encoder.parameters())+
|
|
list(self.decoder.parameters())+
|
|
# list(self.quant_conv.parameters())+
|
|
list(self.post_quant_conv.parameters()),
|
|
lr=lr, betas=(0.5, 0.9))
|
|
opt_disc = torch.optim.Adam(self.loss.discriminator.parameters(),
|
|
lr=lr, betas=(0.5, 0.9))
|
|
return [opt_ae, opt_disc], []
|
|
|
|
def get_last_layer(self):
|
|
return self.decoder.conv_out.weight
|
|
|
|
@torch.no_grad()
|
|
def log_images(self, batch, only_inputs=False, **kwargs):
|
|
log = dict()
|
|
if self.synthesis_data:
|
|
x, gts, latents, samples = self.get_input_synthesis(batch, val=False)
|
|
else:
|
|
x, gts, latents, samples = self.get_input(batch)
|
|
x = x.to(self.device)
|
|
latents = latents.to(self.device)
|
|
samples = samples.to(self.device)
|
|
if not only_inputs:
|
|
xrec, posterior = self(x, latents)
|
|
if x.shape[1] > 3:
|
|
# colorize with random projection
|
|
assert xrec.shape[1] > 3
|
|
x = self.to_rgb(x)
|
|
gts = self.to_rgb(gts)
|
|
samples = self.to_rgb(samples)
|
|
xrec = self.to_rgb(xrec)
|
|
# log["samples"] = self.decode(torch.randn_like(posterior.sample()))
|
|
log["reconstructions"] = xrec
|
|
log["inputs"] = x
|
|
log["gts"] = gts
|
|
log["samples"] = samples
|
|
return log
|
|
|
|
def to_rgb(self, x):
|
|
assert self.image_key == "segmentation"
|
|
if not hasattr(self, "colorize"):
|
|
self.register_buffer("colorize", torch.randn(3, x.shape[1], 1, 1).to(x))
|
|
x = F.conv2d(x, weight=self.colorize)
|
|
x = 2.*(x-x.min())/(x.max()-x.min()) - 1.
|
|
return x
|