Spaces:
Sleeping
Sleeping
import math | |
import random | |
import torch | |
from torch import nn | |
from torch.nn import functional as F | |
import numpy as np | |
from models.stylegan2.op import FusedLeakyReLU, fused_leaky_relu, upfirdn2d | |
class PixelNorm(nn.Module): | |
def __init__(self): | |
super().__init__() | |
def forward(self, input): | |
return input * torch.rsqrt(torch.mean(input ** 2, dim=1, keepdim=True) + 1e-8) | |
def make_kernel(k): | |
k = torch.tensor(k, dtype=torch.float32) | |
if k.ndim == 1: | |
k = k[None, :] * k[:, None] | |
k /= k.sum() | |
return k | |
class Upsample(nn.Module): | |
def __init__(self, kernel, factor=2): | |
super().__init__() | |
self.factor = factor | |
kernel = make_kernel(kernel) * (factor ** 2) | |
self.register_buffer('kernel', kernel) | |
p = kernel.shape[0] - factor | |
pad0 = (p + 1) // 2 + factor - 1 | |
pad1 = p // 2 | |
self.pad = (pad0, pad1) | |
def forward(self, input): | |
out = upfirdn2d(input, self.kernel, up=self.factor, down=1, pad=self.pad) | |
return out | |
class Downsample(nn.Module): | |
def __init__(self, kernel, factor=2): | |
super().__init__() | |
self.factor = factor | |
kernel = make_kernel(kernel) | |
self.register_buffer('kernel', kernel) | |
p = kernel.shape[0] - factor | |
pad0 = (p + 1) // 2 | |
pad1 = p // 2 | |
self.pad = (pad0, pad1) | |
def forward(self, input): | |
out = upfirdn2d(input, self.kernel, up=1, down=self.factor, pad=self.pad) | |
return out | |
class Blur(nn.Module): | |
def __init__(self, kernel, pad, upsample_factor=1): | |
super().__init__() | |
kernel = make_kernel(kernel) | |
if upsample_factor > 1: | |
kernel = kernel * (upsample_factor ** 2) | |
self.register_buffer('kernel', kernel) | |
self.pad = pad | |
def forward(self, input): | |
out = upfirdn2d(input, self.kernel, pad=self.pad) | |
return out | |
class EqualConv2d(nn.Module): | |
def __init__( | |
self, in_channel, out_channel, kernel_size, stride=1, padding=0, bias=True, dilation=1 ## modified | |
): | |
super().__init__() | |
self.weight = nn.Parameter( | |
torch.randn(out_channel, in_channel, kernel_size, kernel_size) | |
) | |
self.scale = 1 / math.sqrt(in_channel * kernel_size ** 2) | |
self.stride = stride | |
self.padding = padding | |
self.dilation = dilation ## modified | |
if bias: | |
self.bias = nn.Parameter(torch.zeros(out_channel)) | |
else: | |
self.bias = None | |
def forward(self, input): | |
out = F.conv2d( | |
input, | |
self.weight * self.scale, | |
bias=self.bias, | |
stride=self.stride, | |
padding=self.padding, | |
dilation=self.dilation, ## modified | |
) | |
return out | |
def __repr__(self): | |
return ( | |
f"{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]}," | |
f" {self.weight.shape[2]}, stride={self.stride}, padding={self.padding}, dilation={self.dilation})" ## modified | |
) | |
class EqualLinear(nn.Module): | |
def __init__( | |
self, in_dim, out_dim, bias=True, bias_init=0, lr_mul=1, activation=None | |
): | |
super().__init__() | |
self.weight = nn.Parameter(torch.randn(out_dim, in_dim).div_(lr_mul)) | |
if bias: | |
self.bias = nn.Parameter(torch.zeros(out_dim).fill_(bias_init)) | |
else: | |
self.bias = None | |
self.activation = activation | |
self.scale = (1 / math.sqrt(in_dim)) * lr_mul | |
self.lr_mul = lr_mul | |
def forward(self, input): | |
if self.activation: | |
out = F.linear(input, self.weight * self.scale) | |
out = fused_leaky_relu(out, self.bias * self.lr_mul) | |
else: | |
out = F.linear( | |
input, self.weight * self.scale, bias=self.bias * self.lr_mul | |
) | |
return out | |
def __repr__(self): | |
return ( | |
f'{self.__class__.__name__}({self.weight.shape[1]}, {self.weight.shape[0]})' | |
) | |
class ScaledLeakyReLU(nn.Module): | |
def __init__(self, negative_slope=0.2): | |
super().__init__() | |
self.negative_slope = negative_slope | |
def forward(self, input): | |
out = F.leaky_relu(input, negative_slope=self.negative_slope) | |
return out * math.sqrt(2) | |
class ModulatedConv2d(nn.Module): | |
def __init__( | |
self, | |
in_channel, | |
out_channel, | |
kernel_size, | |
style_dim, | |
demodulate=True, | |
upsample=False, | |
downsample=False, | |
blur_kernel=[1, 3, 3, 1], | |
dilation=1, ##### modified | |
): | |
super().__init__() | |
self.eps = 1e-8 | |
self.kernel_size = kernel_size | |
self.in_channel = in_channel | |
self.out_channel = out_channel | |
self.upsample = upsample | |
self.downsample = downsample | |
self.dilation = dilation ##### modified | |
if upsample: | |
factor = 2 | |
p = (len(blur_kernel) - factor) - (kernel_size - 1) | |
pad0 = (p + 1) // 2 + factor - 1 | |
pad1 = p // 2 + 1 | |
self.blur = Blur(blur_kernel, pad=(pad0, pad1), upsample_factor=factor) | |
# to simulate transconv + blur | |
# we use dilated transposed conv with blur kernel as weight + dilated transconv | |
if dilation > 1: ##### modified | |
blur_weight = torch.randn(1, 1, 3, 3) * 0 + 1 | |
blur_weight[:,:,0,1] = 2 | |
blur_weight[:,:,1,0] = 2 | |
blur_weight[:,:,1,2] = 2 | |
blur_weight[:,:,2,1] = 2 | |
blur_weight[:,:,1,1] = 4 | |
blur_weight = blur_weight / 16.0 | |
self.register_buffer("blur_weight", blur_weight) | |
if downsample: | |
factor = 2 | |
p = (len(blur_kernel) - factor) + (kernel_size - 1) | |
pad0 = (p + 1) // 2 | |
pad1 = p // 2 | |
self.blur = Blur(blur_kernel, pad=(pad0, pad1)) | |
fan_in = in_channel * kernel_size ** 2 | |
self.scale = 1 / math.sqrt(fan_in) | |
self.padding = kernel_size // 2 + dilation - 1 ##### modified | |
self.weight = nn.Parameter( | |
torch.randn(1, out_channel, in_channel, kernel_size, kernel_size) | |
) | |
self.modulation = EqualLinear(style_dim, in_channel, bias_init=1) | |
self.demodulate = demodulate | |
def __repr__(self): | |
return ( | |
f'{self.__class__.__name__}({self.in_channel}, {self.out_channel}, {self.kernel_size}, ' | |
f'upsample={self.upsample}, downsample={self.downsample})' | |
) | |
def forward(self, input, style): | |
batch, in_channel, height, width = input.shape | |
style = self.modulation(style).view(batch, 1, in_channel, 1, 1) | |
weight = self.scale * self.weight * style | |
if self.demodulate: | |
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + 1e-8) | |
weight = weight * demod.view(batch, self.out_channel, 1, 1, 1) | |
weight = weight.view( | |
batch * self.out_channel, in_channel, self.kernel_size, self.kernel_size | |
) | |
if self.upsample: | |
input = input.view(1, batch * in_channel, height, width) | |
weight = weight.view( | |
batch, self.out_channel, in_channel, self.kernel_size, self.kernel_size | |
) | |
weight = weight.transpose(1, 2).reshape( | |
batch * in_channel, self.out_channel, self.kernel_size, self.kernel_size | |
) | |
if self.dilation > 1: ##### modified | |
# to simulate out = self.blur(out) | |
out = F.conv_transpose2d( | |
input, self.blur_weight.repeat(batch*in_channel,1,1,1), padding=0, groups=batch*in_channel, dilation=self.dilation//2) | |
# to simulate the next line | |
out = F.conv_transpose2d( | |
out, weight, padding=self.dilation, groups=batch, dilation=self.dilation//2) | |
_, _, height, width = out.shape | |
out = out.view(batch, self.out_channel, height, width) | |
return out | |
out = F.conv_transpose2d(input, weight, padding=0, stride=2, groups=batch) | |
_, _, height, width = out.shape | |
out = out.view(batch, self.out_channel, height, width) | |
out = self.blur(out) | |
elif self.downsample: | |
input = self.blur(input) | |
_, _, height, width = input.shape | |
input = input.view(1, batch * in_channel, height, width) | |
out = F.conv2d(input, weight, padding=0, stride=2, groups=batch) | |
_, _, height, width = out.shape | |
out = out.view(batch, self.out_channel, height, width) | |
else: | |
input = input.view(1, batch * in_channel, height, width) | |
out = F.conv2d(input, weight, padding=self.padding, groups=batch, dilation=self.dilation) ##### modified | |
_, _, height, width = out.shape | |
out = out.view(batch, self.out_channel, height, width) | |
return out | |
class NoiseInjection(nn.Module): | |
def __init__(self): | |
super().__init__() | |
self.weight = nn.Parameter(torch.zeros(1)) | |
def forward(self, image, noise=None): | |
if noise is None: | |
batch, _, height, width = image.shape | |
noise = image.new_empty(batch, 1, height, width).normal_() | |
else: ##### modified, to make the resolution matches | |
batch, _, height, width = image.shape | |
_, _, height1, width1 = noise.shape | |
if height != height1 or width != width1: | |
noise = F.adaptive_avg_pool2d(noise, (height, width)) | |
return image + self.weight * noise | |
class ConstantInput(nn.Module): | |
def __init__(self, channel, size=4): | |
super().__init__() | |
self.input = nn.Parameter(torch.randn(1, channel, size, size)) | |
def forward(self, input): | |
batch = input.shape[0] | |
out = self.input.repeat(batch, 1, 1, 1) | |
return out | |
class StyledConv(nn.Module): | |
def __init__( | |
self, | |
in_channel, | |
out_channel, | |
kernel_size, | |
style_dim, | |
upsample=False, | |
blur_kernel=[1, 3, 3, 1], | |
demodulate=True, | |
dilation=1, ##### modified | |
): | |
super().__init__() | |
self.conv = ModulatedConv2d( | |
in_channel, | |
out_channel, | |
kernel_size, | |
style_dim, | |
upsample=upsample, | |
blur_kernel=blur_kernel, | |
demodulate=demodulate, | |
dilation=dilation, ##### modified | |
) | |
self.noise = NoiseInjection() | |
self.activate = FusedLeakyReLU(out_channel) | |
def forward(self, input, style, noise=None): | |
out = self.conv(input, style) | |
out = self.noise(out, noise=noise) | |
out = self.activate(out) | |
return out | |
class ToRGB(nn.Module): | |
def __init__(self, in_channel, style_dim, upsample=True, blur_kernel=[1, 3, 3, 1], dilation=1): ##### modified | |
super().__init__() | |
if upsample: | |
self.upsample = Upsample(blur_kernel) | |
self.conv = ModulatedConv2d(in_channel, 3, 1, style_dim, demodulate=False) | |
self.bias = nn.Parameter(torch.zeros(1, 3, 1, 1)) | |
self.dilation = dilation ##### modified | |
if dilation > 1: ##### modified | |
blur_weight = torch.randn(1, 1, 3, 3) * 0 + 1 | |
blur_weight[:,:,0,1] = 2 | |
blur_weight[:,:,1,0] = 2 | |
blur_weight[:,:,1,2] = 2 | |
blur_weight[:,:,2,1] = 2 | |
blur_weight[:,:,1,1] = 4 | |
blur_weight = blur_weight / 16.0 | |
self.register_buffer("blur_weight", blur_weight) | |
def forward(self, input, style, skip=None): | |
out = self.conv(input, style) | |
out = out + self.bias | |
if skip is not None: | |
if self.dilation == 1: | |
skip = self.upsample(skip) | |
else: ##### modified, to simulate skip = self.upsample(skip) | |
batch, in_channel, _, _ = skip.shape | |
skip = F.conv2d(skip, self.blur_weight.repeat(in_channel,1,1,1), | |
padding=self.dilation//2, groups=in_channel, dilation=self.dilation//2) | |
out = out + skip | |
return out | |
class Generator(nn.Module): | |
def __init__( | |
self, | |
size, | |
style_dim, | |
n_mlp, | |
channel_multiplier=2, | |
blur_kernel=[1, 3, 3, 1], | |
lr_mlp=0.01, | |
): | |
super().__init__() | |
self.size = size | |
self.style_dim = style_dim | |
layers = [PixelNorm()] | |
for i in range(n_mlp): | |
layers.append( | |
EqualLinear( | |
style_dim, style_dim, lr_mul=lr_mlp, activation='fused_lrelu' | |
) | |
) | |
self.style = nn.Sequential(*layers) | |
self.channels = { | |
4: 512, | |
8: 512, | |
16: 512, | |
32: 512, | |
64: 256 * channel_multiplier, | |
128: 128 * channel_multiplier, | |
256: 64 * channel_multiplier, | |
512: 32 * channel_multiplier, | |
1024: 16 * channel_multiplier, | |
} | |
self.input = ConstantInput(self.channels[4]) | |
self.conv1 = StyledConv( | |
self.channels[4], self.channels[4], 3, style_dim, blur_kernel=blur_kernel, dilation=8 ##### modified | |
) | |
self.to_rgb1 = ToRGB(self.channels[4], style_dim, upsample=False) | |
self.log_size = int(math.log(size, 2)) | |
self.num_layers = (self.log_size - 2) * 2 + 1 | |
self.convs = nn.ModuleList() | |
self.upsamples = nn.ModuleList() | |
self.to_rgbs = nn.ModuleList() | |
self.noises = nn.Module() | |
in_channel = self.channels[4] | |
for layer_idx in range(self.num_layers): | |
res = (layer_idx + 5) // 2 | |
shape = [1, 1, 2 ** res, 2 ** res] | |
self.noises.register_buffer(f'noise_{layer_idx}', torch.randn(*shape)) | |
for i in range(3, self.log_size + 1): | |
out_channel = self.channels[2 ** i] | |
self.convs.append( | |
StyledConv( | |
in_channel, | |
out_channel, | |
3, | |
style_dim, | |
upsample=True, | |
blur_kernel=blur_kernel, | |
dilation=max(1, 32 // (2**(i-1))) ##### modified | |
) | |
) | |
self.convs.append( | |
StyledConv( | |
out_channel, out_channel, 3, style_dim, blur_kernel=blur_kernel, dilation=max(1, 32 // (2**i)) ##### modified | |
) | |
) | |
self.to_rgbs.append(ToRGB(out_channel, style_dim, dilation=max(1, 32 // (2**(i-1))))) ##### modified | |
in_channel = out_channel | |
self.n_latent = self.log_size * 2 - 2 | |
def make_noise(self): | |
device = self.input.input.device | |
noises = [torch.randn(1, 1, 2 ** 2, 2 ** 2, device=device)] | |
for i in range(3, self.log_size + 1): | |
for _ in range(2): | |
noises.append(torch.randn(1, 1, 2 ** i, 2 ** i, device=device)) | |
return noises | |
def mean_latent(self, n_latent): | |
latent_in = torch.randn( | |
n_latent, self.style_dim, device=self.input.input.device | |
) | |
latent = self.style(latent_in).mean(0, keepdim=True) | |
return latent | |
def get_latent(self, input): | |
return self.style(input) | |
# styles is the latent code w+ | |
# first_layer_feature is the first-layer input feature f | |
# first_layer_feature_ind indicate which layer of G accepts f (should always=0, the first layer) | |
# skip_layer_feature is the encoder features sent by skip connection | |
# fusion_block is the network to fuse the encoder feature and decoder feature | |
# zero_noise is to force the noise to be zero (to avoid flickers for videos) | |
# editing_w is the editing vector v used in video face editing | |
def forward( | |
self, | |
styles, | |
return_latents=False, | |
return_features=False, | |
inject_index=None, | |
truncation=1, | |
truncation_latent=None, | |
input_is_latent=False, | |
noise=None, | |
randomize_noise=True, | |
first_layer_feature = None, ##### modified | |
first_layer_feature_ind = 0, ##### modified | |
skip_layer_feature = None, ##### modified | |
fusion_block = None, ##### modified | |
zero_noise = False, ##### modified | |
editing_w = None, ##### modified | |
): | |
if not input_is_latent: | |
styles = [self.style(s) for s in styles] | |
if zero_noise: | |
noise = [ | |
getattr(self.noises, f'noise_{i}') * 0.0 for i in range(self.num_layers) | |
] | |
elif noise is None: | |
if randomize_noise: | |
noise = [None] * self.num_layers | |
else: | |
noise = [ | |
getattr(self.noises, f'noise_{i}') for i in range(self.num_layers) | |
] | |
if truncation < 1: | |
style_t = [] | |
for style in styles: | |
style_t.append( | |
truncation_latent + truncation * (style - truncation_latent) | |
) | |
styles = style_t | |
if len(styles) < 2: | |
inject_index = self.n_latent | |
if styles[0].ndim < 3: | |
latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1) | |
else: | |
latent = styles[0] | |
else: | |
if inject_index is None: | |
inject_index = random.randint(1, self.n_latent - 1) | |
latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1) | |
latent2 = styles[1].unsqueeze(1).repeat(1, self.n_latent - inject_index, 1) | |
latent = torch.cat([latent, latent2], 1) | |
# w+ + v for video face editing | |
if editing_w is not None: ##### modified | |
latent = latent + editing_w | |
# the original StyleGAN | |
if first_layer_feature is None: ##### modified | |
out = self.input(latent) | |
out = F.adaptive_avg_pool2d(out, 32) ##### modified | |
out = self.conv1(out, latent[:, 0], noise=noise[0]) | |
skip = self.to_rgb1(out, latent[:, 1]) | |
# the default StyleGANEX, replacing the first layer of G | |
elif first_layer_feature_ind == 0: ##### modified | |
out = first_layer_feature[0] ##### modified | |
out = self.conv1(out, latent[:, 0], noise=noise[0]) | |
skip = self.to_rgb1(out, latent[:, 1]) | |
# maybe we can also use the second layer of G to accept f? | |
else: ##### modified | |
out = first_layer_feature[0] ##### modified | |
skip = first_layer_feature[1] ##### modified | |
i = 1 | |
for conv1, conv2, noise1, noise2, to_rgb in zip( | |
self.convs[::2], self.convs[1::2], noise[1::2], noise[2::2], self.to_rgbs | |
): | |
# these layers accepts skipped encoder layer, use fusion block to fuse the encoder feature and decoder feature | |
if skip_layer_feature and fusion_block and i//2 < len(skip_layer_feature) and i//2 < len(fusion_block): | |
if editing_w is None: | |
out, skip = fusion_block[i//2](skip_layer_feature[i//2], out, skip) | |
else: | |
out, skip = fusion_block[i//2](skip_layer_feature[i//2], out, skip, editing_w[:,i]) | |
out = conv1(out, latent[:, i], noise=noise1) | |
out = conv2(out, latent[:, i + 1], noise=noise2) | |
skip = to_rgb(out, latent[:, i + 2], skip) | |
i += 2 | |
image = skip | |
if return_latents: | |
return image, latent | |
elif return_features: | |
return image, out | |
else: | |
return image, None | |
class ConvLayer(nn.Sequential): | |
def __init__( | |
self, | |
in_channel, | |
out_channel, | |
kernel_size, | |
downsample=False, | |
blur_kernel=[1, 3, 3, 1], | |
bias=True, | |
activate=True, | |
dilation=1, ## modified | |
): | |
layers = [] | |
if downsample: | |
factor = 2 | |
p = (len(blur_kernel) - factor) + (kernel_size - 1) | |
pad0 = (p + 1) // 2 | |
pad1 = p // 2 | |
layers.append(Blur(blur_kernel, pad=(pad0, pad1))) | |
stride = 2 | |
self.padding = 0 | |
else: | |
stride = 1 | |
self.padding = kernel_size // 2 + dilation-1 ## modified | |
layers.append( | |
EqualConv2d( | |
in_channel, | |
out_channel, | |
kernel_size, | |
padding=self.padding, | |
stride=stride, | |
bias=bias and not activate, | |
dilation=dilation, ## modified | |
) | |
) | |
if activate: | |
if bias: | |
layers.append(FusedLeakyReLU(out_channel)) | |
else: | |
layers.append(ScaledLeakyReLU(0.2)) | |
super().__init__(*layers) | |
class ResBlock(nn.Module): | |
def __init__(self, in_channel, out_channel, blur_kernel=[1, 3, 3, 1]): | |
super().__init__() | |
self.conv1 = ConvLayer(in_channel, in_channel, 3) | |
self.conv2 = ConvLayer(in_channel, out_channel, 3, downsample=True) | |
self.skip = ConvLayer( | |
in_channel, out_channel, 1, downsample=True, activate=False, bias=False | |
) | |
def forward(self, input): | |
out = self.conv1(input) | |
out = self.conv2(out) | |
skip = self.skip(input) | |
out = (out + skip) / math.sqrt(2) | |
return out | |
class Discriminator(nn.Module): | |
def __init__(self, size, channel_multiplier=2, blur_kernel=[1, 3, 3, 1], img_channel=3): | |
super().__init__() | |
channels = { | |
4: 512, | |
8: 512, | |
16: 512, | |
32: 512, | |
64: 256 * channel_multiplier, | |
128: 128 * channel_multiplier, | |
256: 64 * channel_multiplier, | |
512: 32 * channel_multiplier, | |
1024: 16 * channel_multiplier, | |
} | |
convs = [ConvLayer(img_channel, channels[size], 1)] | |
log_size = int(math.log(size, 2)) | |
in_channel = channels[size] | |
for i in range(log_size, 2, -1): | |
out_channel = channels[2 ** (i - 1)] | |
convs.append(ResBlock(in_channel, out_channel, blur_kernel)) | |
in_channel = out_channel | |
self.convs = nn.Sequential(*convs) | |
self.stddev_group = 4 | |
self.stddev_feat = 1 | |
self.final_conv = ConvLayer(in_channel + 1, channels[4], 3) | |
self.final_linear = nn.Sequential( | |
EqualLinear(channels[4] * 4 * 4, channels[4], activation='fused_lrelu'), | |
EqualLinear(channels[4], 1), | |
) | |
self.size = size ##### modified | |
def forward(self, input): | |
# for input that not satisfies the target size, we crop it to extract a small image of the target size. | |
_, _, h, w = input.shape ##### modified | |
i, j = torch.randint(0, h+1-self.size, size=(1,)).item(), torch.randint(0, w+1-self.size, size=(1,)).item() ##### modified | |
out = self.convs(input[:,:,i:i+self.size,j:j+self.size]) ##### modified | |
batch, channel, height, width = out.shape | |
group = min(batch, self.stddev_group) | |
stddev = out.view( | |
group, -1, self.stddev_feat, channel // self.stddev_feat, height, width | |
) | |
stddev = torch.sqrt(stddev.var(0, unbiased=False) + 1e-8) | |
stddev = stddev.mean([2, 3, 4], keepdims=True).squeeze(2) | |
stddev = stddev.repeat(group, 1, height, width) | |
out = torch.cat([out, stddev], 1) | |
out = self.final_conv(out) | |
out = out.view(batch, -1) | |
out = self.final_linear(out) | |
return out |