|
|
|
import math |
|
import torch |
|
import torch.nn as nn |
|
import numpy as np |
|
from einops import rearrange |
|
|
|
from utility.initialize import instantiate_from_config |
|
from .nn_2d import LinearAttention |
|
|
|
|
|
def get_timestep_embedding(timesteps, embedding_dim): |
|
""" |
|
This matches the implementation in Denoising Diffusion Probabilistic Models: |
|
From Fairseq. |
|
Build sinusoidal embeddings. |
|
This matches the implementation in tensor2tensor, but differs slightly |
|
from the description in Section 3.5 of "Attention Is All You Need". |
|
""" |
|
assert len(timesteps.shape) == 1 |
|
|
|
half_dim = embedding_dim // 2 |
|
emb = math.log(10000) / (half_dim - 1) |
|
emb = torch.exp(torch.arange(half_dim, dtype=torch.float32) * -emb) |
|
emb = emb.to(device=timesteps.device) |
|
emb = timesteps.float()[:, None] * emb[None, :] |
|
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1) |
|
if embedding_dim % 2 == 1: |
|
emb = torch.nn.functional.pad(emb, (0,1,0,0)) |
|
return emb |
|
|
|
|
|
def nonlinearity(x): |
|
|
|
return x*torch.sigmoid(x) |
|
|
|
|
|
def Normalize(in_channels, num_groups=32): |
|
return torch.nn.GroupNorm(num_groups=num_groups, num_channels=in_channels, eps=1e-6, affine=True) |
|
|
|
|
|
class Upsample(nn.Module): |
|
def __init__(self, in_channels, with_conv): |
|
super().__init__() |
|
self.with_conv = with_conv |
|
if self.with_conv: |
|
self.conv = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
def forward(self, x): |
|
x = torch.nn.functional.interpolate(x, scale_factor=2.0, mode="nearest") |
|
if self.with_conv: |
|
x = self.conv(x) |
|
return x |
|
|
|
|
|
class Downsample(nn.Module): |
|
def __init__(self, in_channels, with_conv): |
|
super().__init__() |
|
self.with_conv = with_conv |
|
if self.with_conv: |
|
|
|
self.conv = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=3, |
|
stride=2, |
|
padding=0) |
|
|
|
def forward(self, x): |
|
if self.with_conv: |
|
pad = (0,1,0,1) |
|
x = torch.nn.functional.pad(x, pad, mode="constant", value=0) |
|
x = self.conv(x) |
|
else: |
|
x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) |
|
return x |
|
|
|
|
|
class ResnetBlock(nn.Module): |
|
def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False, |
|
dropout, temb_channels=512): |
|
super().__init__() |
|
self.in_channels = in_channels |
|
out_channels = in_channels if out_channels is None else out_channels |
|
self.out_channels = out_channels |
|
self.use_conv_shortcut = conv_shortcut |
|
|
|
self.norm1 = Normalize(in_channels) |
|
self.conv1 = torch.nn.Conv2d(in_channels, |
|
out_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
if temb_channels > 0: |
|
self.temb_proj = torch.nn.Linear(temb_channels, |
|
out_channels) |
|
self.norm2 = Normalize(out_channels) |
|
self.dropout = torch.nn.Dropout(dropout) |
|
self.conv2 = torch.nn.Conv2d(out_channels, |
|
out_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
if self.in_channels != self.out_channels: |
|
if self.use_conv_shortcut: |
|
self.conv_shortcut = torch.nn.Conv2d(in_channels, |
|
out_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
else: |
|
self.nin_shortcut = torch.nn.Conv2d(in_channels, |
|
out_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
def forward(self, x, temb): |
|
h = x |
|
h = self.norm1(h) |
|
h = nonlinearity(h) |
|
h = self.conv1(h) |
|
|
|
if temb is not None: |
|
h = h + self.temb_proj(nonlinearity(temb))[:,:,None,None] |
|
|
|
h = self.norm2(h) |
|
h = nonlinearity(h) |
|
h = self.dropout(h) |
|
h = self.conv2(h) |
|
|
|
if self.in_channels != self.out_channels: |
|
if self.use_conv_shortcut: |
|
x = self.conv_shortcut(x) |
|
else: |
|
x = self.nin_shortcut(x) |
|
|
|
return x+h |
|
|
|
|
|
class LinAttnBlock(LinearAttention): |
|
"""to match AttnBlock usage""" |
|
def __init__(self, in_channels): |
|
super().__init__(dim=in_channels, heads=1, dim_head=in_channels) |
|
|
|
|
|
class AttnBlock(nn.Module): |
|
def __init__(self, in_channels): |
|
super().__init__() |
|
self.in_channels = in_channels |
|
|
|
self.norm = Normalize(in_channels) |
|
self.q = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.k = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.v = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.proj_out = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
|
|
def forward(self, x): |
|
h_ = x |
|
h_ = self.norm(h_) |
|
q = self.q(h_) |
|
k = self.k(h_) |
|
v = self.v(h_) |
|
|
|
|
|
b,c,h,w = q.shape |
|
q = q.reshape(b,c,h*w) |
|
q = q.permute(0,2,1) |
|
k = k.reshape(b,c,h*w) |
|
w_ = torch.bmm(q,k) |
|
w_ = w_ * (int(c)**(-0.5)) |
|
w_ = torch.nn.functional.softmax(w_, dim=2) |
|
|
|
|
|
v = v.reshape(b,c,h*w) |
|
w_ = w_.permute(0,2,1) |
|
h_ = torch.bmm(v,w_) |
|
h_ = h_.reshape(b,c,h,w) |
|
|
|
h_ = self.proj_out(h_) |
|
|
|
return x+h_ |
|
|
|
|
|
def make_attn(in_channels, attn_type="vanilla"): |
|
assert attn_type in ["vanilla", "linear", "none", "vanilla_groupconv", "crossattention"], f'attn_type {attn_type} unknown' |
|
|
|
if attn_type == "vanilla": |
|
return AttnBlock(in_channels) |
|
elif attn_type == 'vanilla_groupconv': |
|
return AttnBlock_GroupConv(in_channels) |
|
elif attn_type == 'crossattention': |
|
num_heads = 8 |
|
return TriplaneAttentionBlock(in_channels, num_heads, in_channels // num_heads, True) |
|
elif attn_type == "none": |
|
return nn.Identity(in_channels) |
|
else: |
|
return LinAttnBlock(in_channels) |
|
|
|
|
|
class Model(nn.Module): |
|
def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
|
attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
|
resolution, use_timestep=True, use_linear_attn=False, attn_type="vanilla"): |
|
super().__init__() |
|
if use_linear_attn: attn_type = "linear" |
|
self.ch = ch |
|
self.temb_ch = self.ch*4 |
|
self.num_resolutions = len(ch_mult) |
|
self.num_res_blocks = num_res_blocks |
|
self.resolution = resolution |
|
self.in_channels = in_channels |
|
|
|
self.use_timestep = use_timestep |
|
if self.use_timestep: |
|
|
|
self.temb = nn.Module() |
|
self.temb.dense = nn.ModuleList([ |
|
torch.nn.Linear(self.ch, |
|
self.temb_ch), |
|
torch.nn.Linear(self.temb_ch, |
|
self.temb_ch), |
|
]) |
|
|
|
|
|
self.conv_in = torch.nn.Conv2d(in_channels, |
|
self.ch, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
curr_res = resolution |
|
in_ch_mult = (1,)+tuple(ch_mult) |
|
self.down = nn.ModuleList() |
|
for i_level in range(self.num_resolutions): |
|
block = nn.ModuleList() |
|
attn = nn.ModuleList() |
|
block_in = ch*in_ch_mult[i_level] |
|
block_out = ch*ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks): |
|
block.append(ResnetBlock(in_channels=block_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
if curr_res in attn_resolutions: |
|
attn.append(make_attn(block_in, attn_type=attn_type)) |
|
down = nn.Module() |
|
down.block = block |
|
down.attn = attn |
|
if i_level != self.num_resolutions-1: |
|
down.downsample = Downsample(block_in, resamp_with_conv) |
|
curr_res = curr_res // 2 |
|
self.down.append(down) |
|
|
|
|
|
self.mid = nn.Module() |
|
self.mid.block_1 = ResnetBlock(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
|
self.mid.block_2 = ResnetBlock(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
|
|
|
|
self.up = nn.ModuleList() |
|
for i_level in reversed(range(self.num_resolutions)): |
|
block = nn.ModuleList() |
|
attn = nn.ModuleList() |
|
block_out = ch*ch_mult[i_level] |
|
skip_in = ch*ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks+1): |
|
if i_block == self.num_res_blocks: |
|
skip_in = ch*in_ch_mult[i_level] |
|
block.append(ResnetBlock(in_channels=block_in+skip_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
if curr_res in attn_resolutions: |
|
attn.append(make_attn(block_in, attn_type=attn_type)) |
|
up = nn.Module() |
|
up.block = block |
|
up.attn = attn |
|
if i_level != 0: |
|
up.upsample = Upsample(block_in, resamp_with_conv) |
|
curr_res = curr_res * 2 |
|
self.up.insert(0, up) |
|
|
|
|
|
self.norm_out = Normalize(block_in) |
|
self.conv_out = torch.nn.Conv2d(block_in, |
|
out_ch, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
def forward(self, x, t=None, context=None): |
|
|
|
if context is not None: |
|
|
|
x = torch.cat((x, context), dim=1) |
|
if self.use_timestep: |
|
|
|
assert t is not None |
|
temb = get_timestep_embedding(t, self.ch) |
|
temb = self.temb.dense[0](temb) |
|
temb = nonlinearity(temb) |
|
temb = self.temb.dense[1](temb) |
|
else: |
|
temb = None |
|
|
|
|
|
hs = [self.conv_in(x)] |
|
for i_level in range(self.num_resolutions): |
|
for i_block in range(self.num_res_blocks): |
|
h = self.down[i_level].block[i_block](hs[-1], temb) |
|
if len(self.down[i_level].attn) > 0: |
|
h = self.down[i_level].attn[i_block](h) |
|
hs.append(h) |
|
if i_level != self.num_resolutions-1: |
|
hs.append(self.down[i_level].downsample(hs[-1])) |
|
|
|
|
|
h = hs[-1] |
|
h = self.mid.block_1(h, temb) |
|
h = self.mid.attn_1(h) |
|
h = self.mid.block_2(h, temb) |
|
|
|
|
|
for i_level in reversed(range(self.num_resolutions)): |
|
for i_block in range(self.num_res_blocks+1): |
|
h = self.up[i_level].block[i_block]( |
|
torch.cat([h, hs.pop()], dim=1), temb) |
|
if len(self.up[i_level].attn) > 0: |
|
h = self.up[i_level].attn[i_block](h) |
|
if i_level != 0: |
|
h = self.up[i_level].upsample(h) |
|
|
|
|
|
h = self.norm_out(h) |
|
h = nonlinearity(h) |
|
h = self.conv_out(h) |
|
return h |
|
|
|
def get_last_layer(self): |
|
return self.conv_out.weight |
|
|
|
|
|
class Encoder(nn.Module): |
|
def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
|
attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
|
resolution, z_channels, double_z=True, use_linear_attn=False, attn_type="vanilla", |
|
**ignore_kwargs): |
|
super().__init__() |
|
if use_linear_attn: attn_type = "linear" |
|
self.ch = ch |
|
self.temb_ch = 0 |
|
self.num_resolutions = len(ch_mult) |
|
self.num_res_blocks = num_res_blocks |
|
self.resolution = resolution |
|
self.in_channels = in_channels |
|
|
|
|
|
self.conv_in = torch.nn.Conv2d(in_channels, |
|
self.ch, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
curr_res = resolution |
|
in_ch_mult = (1,)+tuple(ch_mult) |
|
self.in_ch_mult = in_ch_mult |
|
self.down = nn.ModuleList() |
|
for i_level in range(self.num_resolutions): |
|
block = nn.ModuleList() |
|
attn = nn.ModuleList() |
|
block_in = ch*in_ch_mult[i_level] |
|
block_out = ch*ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks): |
|
block.append(ResnetBlock(in_channels=block_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
if curr_res in attn_resolutions: |
|
attn.append(make_attn(block_in, attn_type=attn_type)) |
|
down = nn.Module() |
|
down.block = block |
|
down.attn = attn |
|
if i_level != self.num_resolutions-1: |
|
down.downsample = Downsample(block_in, resamp_with_conv) |
|
curr_res = curr_res // 2 |
|
self.down.append(down) |
|
|
|
|
|
self.mid = nn.Module() |
|
self.mid.block_1 = ResnetBlock(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
|
self.mid.block_2 = ResnetBlock(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
|
|
|
|
self.norm_out = Normalize(block_in) |
|
self.conv_out = torch.nn.Conv2d(block_in, |
|
2*z_channels if double_z else z_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
def forward(self, x): |
|
|
|
temb = None |
|
|
|
|
|
hs = [self.conv_in(x)] |
|
for i_level in range(self.num_resolutions): |
|
for i_block in range(self.num_res_blocks): |
|
h = self.down[i_level].block[i_block](hs[-1], temb) |
|
if len(self.down[i_level].attn) > 0: |
|
h = self.down[i_level].attn[i_block](h) |
|
hs.append(h) |
|
if i_level != self.num_resolutions-1: |
|
hs.append(self.down[i_level].downsample(hs[-1])) |
|
|
|
|
|
h = hs[-1] |
|
h = self.mid.block_1(h, temb) |
|
h = self.mid.attn_1(h) |
|
h = self.mid.block_2(h, temb) |
|
|
|
|
|
h = self.norm_out(h) |
|
h = nonlinearity(h) |
|
h = self.conv_out(h) |
|
return h |
|
|
|
|
|
class Decoder(nn.Module): |
|
def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
|
attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
|
resolution, z_channels, give_pre_end=False, tanh_out=False, use_linear_attn=False, |
|
attn_type="vanilla", **ignorekwargs): |
|
super().__init__() |
|
if use_linear_attn: attn_type = "linear" |
|
self.ch = ch |
|
self.temb_ch = 0 |
|
self.num_resolutions = len(ch_mult) |
|
self.num_res_blocks = num_res_blocks |
|
self.resolution = resolution |
|
self.in_channels = in_channels |
|
self.give_pre_end = give_pre_end |
|
self.tanh_out = tanh_out |
|
|
|
|
|
in_ch_mult = (1,)+tuple(ch_mult) |
|
block_in = ch*ch_mult[self.num_resolutions-1] |
|
curr_res = resolution // 2**(self.num_resolutions-1) |
|
self.z_shape = (1,z_channels,curr_res,curr_res) |
|
|
|
|
|
|
|
|
|
self.conv_in = torch.nn.Conv2d(z_channels, |
|
block_in, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
|
|
self.mid = nn.Module() |
|
self.mid.block_1 = ResnetBlock(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
|
self.mid.block_2 = ResnetBlock(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
|
|
|
|
self.up = nn.ModuleList() |
|
for i_level in reversed(range(self.num_resolutions)): |
|
block = nn.ModuleList() |
|
attn = nn.ModuleList() |
|
block_out = ch*ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks+1): |
|
block.append(ResnetBlock(in_channels=block_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
if curr_res in attn_resolutions: |
|
attn.append(make_attn(block_in, attn_type=attn_type)) |
|
up = nn.Module() |
|
up.block = block |
|
up.attn = attn |
|
if i_level != 0: |
|
up.upsample = Upsample(block_in, resamp_with_conv) |
|
curr_res = curr_res * 2 |
|
self.up.insert(0, up) |
|
|
|
|
|
self.norm_out = Normalize(block_in) |
|
self.conv_out = torch.nn.Conv2d(block_in, |
|
out_ch, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
def forward(self, z): |
|
|
|
self.last_z_shape = z.shape |
|
|
|
|
|
temb = None |
|
|
|
|
|
h = self.conv_in(z) |
|
|
|
|
|
h = self.mid.block_1(h, temb) |
|
h = self.mid.attn_1(h) |
|
h = self.mid.block_2(h, temb) |
|
|
|
|
|
for i_level in reversed(range(self.num_resolutions)): |
|
for i_block in range(self.num_res_blocks+1): |
|
h = self.up[i_level].block[i_block](h, temb) |
|
if len(self.up[i_level].attn) > 0: |
|
h = self.up[i_level].attn[i_block](h) |
|
if i_level != 0: |
|
h = self.up[i_level].upsample(h) |
|
|
|
|
|
if self.give_pre_end: |
|
return h |
|
|
|
h = self.norm_out(h) |
|
h = nonlinearity(h) |
|
h = self.conv_out(h) |
|
if self.tanh_out: |
|
h = torch.tanh(h) |
|
return h |
|
|
|
|
|
class SimpleDecoder(nn.Module): |
|
def __init__(self, in_channels, out_channels, *args, **kwargs): |
|
super().__init__() |
|
self.model = nn.ModuleList([nn.Conv2d(in_channels, in_channels, 1), |
|
ResnetBlock(in_channels=in_channels, |
|
out_channels=2 * in_channels, |
|
temb_channels=0, dropout=0.0), |
|
ResnetBlock(in_channels=2 * in_channels, |
|
out_channels=4 * in_channels, |
|
temb_channels=0, dropout=0.0), |
|
ResnetBlock(in_channels=4 * in_channels, |
|
out_channels=2 * in_channels, |
|
temb_channels=0, dropout=0.0), |
|
nn.Conv2d(2*in_channels, in_channels, 1), |
|
Upsample(in_channels, with_conv=True)]) |
|
|
|
self.norm_out = Normalize(in_channels) |
|
self.conv_out = torch.nn.Conv2d(in_channels, |
|
out_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
def forward(self, x): |
|
for i, layer in enumerate(self.model): |
|
if i in [1,2,3]: |
|
x = layer(x, None) |
|
else: |
|
x = layer(x) |
|
|
|
h = self.norm_out(x) |
|
h = nonlinearity(h) |
|
x = self.conv_out(h) |
|
return x |
|
|
|
|
|
class UpsampleDecoder(nn.Module): |
|
def __init__(self, in_channels, out_channels, ch, num_res_blocks, resolution, |
|
ch_mult=(2,2), dropout=0.0): |
|
super().__init__() |
|
|
|
self.temb_ch = 0 |
|
self.num_resolutions = len(ch_mult) |
|
self.num_res_blocks = num_res_blocks |
|
block_in = in_channels |
|
curr_res = resolution // 2 ** (self.num_resolutions - 1) |
|
self.res_blocks = nn.ModuleList() |
|
self.upsample_blocks = nn.ModuleList() |
|
for i_level in range(self.num_resolutions): |
|
res_block = [] |
|
block_out = ch * ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks + 1): |
|
res_block.append(ResnetBlock(in_channels=block_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
self.res_blocks.append(nn.ModuleList(res_block)) |
|
if i_level != self.num_resolutions - 1: |
|
self.upsample_blocks.append(Upsample(block_in, True)) |
|
curr_res = curr_res * 2 |
|
|
|
|
|
self.norm_out = Normalize(block_in) |
|
self.conv_out = torch.nn.Conv2d(block_in, |
|
out_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
def forward(self, x): |
|
|
|
h = x |
|
for k, i_level in enumerate(range(self.num_resolutions)): |
|
for i_block in range(self.num_res_blocks + 1): |
|
h = self.res_blocks[i_level][i_block](h, None) |
|
if i_level != self.num_resolutions - 1: |
|
h = self.upsample_blocks[k](h) |
|
h = self.norm_out(h) |
|
h = nonlinearity(h) |
|
h = self.conv_out(h) |
|
return h |
|
|
|
|
|
class LatentRescaler(nn.Module): |
|
def __init__(self, factor, in_channels, mid_channels, out_channels, depth=2): |
|
super().__init__() |
|
|
|
self.factor = factor |
|
self.conv_in = nn.Conv2d(in_channels, |
|
mid_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
self.res_block1 = nn.ModuleList([ResnetBlock(in_channels=mid_channels, |
|
out_channels=mid_channels, |
|
temb_channels=0, |
|
dropout=0.0) for _ in range(depth)]) |
|
self.attn = AttnBlock(mid_channels) |
|
self.res_block2 = nn.ModuleList([ResnetBlock(in_channels=mid_channels, |
|
out_channels=mid_channels, |
|
temb_channels=0, |
|
dropout=0.0) for _ in range(depth)]) |
|
|
|
self.conv_out = nn.Conv2d(mid_channels, |
|
out_channels, |
|
kernel_size=1, |
|
) |
|
|
|
def forward(self, x): |
|
x = self.conv_in(x) |
|
for block in self.res_block1: |
|
x = block(x, None) |
|
x = torch.nn.functional.interpolate(x, size=(int(round(x.shape[2]*self.factor)), int(round(x.shape[3]*self.factor)))) |
|
x = self.attn(x) |
|
for block in self.res_block2: |
|
x = block(x, None) |
|
x = self.conv_out(x) |
|
return x |
|
|
|
|
|
class MergedRescaleEncoder(nn.Module): |
|
def __init__(self, in_channels, ch, resolution, out_ch, num_res_blocks, |
|
attn_resolutions, dropout=0.0, resamp_with_conv=True, |
|
ch_mult=(1,2,4,8), rescale_factor=1.0, rescale_module_depth=1): |
|
super().__init__() |
|
intermediate_chn = ch * ch_mult[-1] |
|
self.encoder = Encoder(in_channels=in_channels, num_res_blocks=num_res_blocks, ch=ch, ch_mult=ch_mult, |
|
z_channels=intermediate_chn, double_z=False, resolution=resolution, |
|
attn_resolutions=attn_resolutions, dropout=dropout, resamp_with_conv=resamp_with_conv, |
|
out_ch=None) |
|
self.rescaler = LatentRescaler(factor=rescale_factor, in_channels=intermediate_chn, |
|
mid_channels=intermediate_chn, out_channels=out_ch, depth=rescale_module_depth) |
|
|
|
def forward(self, x): |
|
x = self.encoder(x) |
|
x = self.rescaler(x) |
|
return x |
|
|
|
|
|
class MergedRescaleDecoder(nn.Module): |
|
def __init__(self, z_channels, out_ch, resolution, num_res_blocks, attn_resolutions, ch, ch_mult=(1,2,4,8), |
|
dropout=0.0, resamp_with_conv=True, rescale_factor=1.0, rescale_module_depth=1): |
|
super().__init__() |
|
tmp_chn = z_channels*ch_mult[-1] |
|
self.decoder = Decoder(out_ch=out_ch, z_channels=tmp_chn, attn_resolutions=attn_resolutions, dropout=dropout, |
|
resamp_with_conv=resamp_with_conv, in_channels=None, num_res_blocks=num_res_blocks, |
|
ch_mult=ch_mult, resolution=resolution, ch=ch) |
|
self.rescaler = LatentRescaler(factor=rescale_factor, in_channels=z_channels, mid_channels=tmp_chn, |
|
out_channels=tmp_chn, depth=rescale_module_depth) |
|
|
|
def forward(self, x): |
|
x = self.rescaler(x) |
|
x = self.decoder(x) |
|
return x |
|
|
|
|
|
class Upsampler(nn.Module): |
|
def __init__(self, in_size, out_size, in_channels, out_channels, ch_mult=2): |
|
super().__init__() |
|
assert out_size >= in_size |
|
num_blocks = int(np.log2(out_size//in_size))+1 |
|
factor_up = 1.+ (out_size % in_size) |
|
print(f"Building {self.__class__.__name__} with in_size: {in_size} --> out_size {out_size} and factor {factor_up}") |
|
self.rescaler = LatentRescaler(factor=factor_up, in_channels=in_channels, mid_channels=2*in_channels, |
|
out_channels=in_channels) |
|
self.decoder = Decoder(out_ch=out_channels, resolution=out_size, z_channels=in_channels, num_res_blocks=2, |
|
attn_resolutions=[], in_channels=None, ch=in_channels, |
|
ch_mult=[ch_mult for _ in range(num_blocks)]) |
|
|
|
def forward(self, x): |
|
x = self.rescaler(x) |
|
x = self.decoder(x) |
|
return x |
|
|
|
|
|
class Resize(nn.Module): |
|
def __init__(self, in_channels=None, learned=False, mode="bilinear"): |
|
super().__init__() |
|
self.with_conv = learned |
|
self.mode = mode |
|
if self.with_conv: |
|
print(f"Note: {self.__class__.__name} uses learned downsampling and will ignore the fixed {mode} mode") |
|
raise NotImplementedError() |
|
assert in_channels is not None |
|
|
|
self.conv = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=4, |
|
stride=2, |
|
padding=1) |
|
|
|
def forward(self, x, scale_factor=1.0): |
|
if scale_factor==1.0: |
|
return x |
|
else: |
|
x = torch.nn.functional.interpolate(x, mode=self.mode, align_corners=False, scale_factor=scale_factor) |
|
return x |
|
|
|
class FirstStagePostProcessor(nn.Module): |
|
|
|
def __init__(self, ch_mult:list, in_channels, |
|
pretrained_model:nn.Module=None, |
|
reshape=False, |
|
n_channels=None, |
|
dropout=0., |
|
pretrained_config=None): |
|
super().__init__() |
|
if pretrained_config is None: |
|
assert pretrained_model is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' |
|
self.pretrained_model = pretrained_model |
|
else: |
|
assert pretrained_config is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' |
|
self.instantiate_pretrained(pretrained_config) |
|
|
|
self.do_reshape = reshape |
|
|
|
if n_channels is None: |
|
n_channels = self.pretrained_model.encoder.ch |
|
|
|
self.proj_norm = Normalize(in_channels,num_groups=in_channels//2) |
|
self.proj = nn.Conv2d(in_channels,n_channels,kernel_size=3, |
|
stride=1,padding=1) |
|
|
|
blocks = [] |
|
downs = [] |
|
ch_in = n_channels |
|
for m in ch_mult: |
|
blocks.append(ResnetBlock(in_channels=ch_in,out_channels=m*n_channels,dropout=dropout)) |
|
ch_in = m * n_channels |
|
downs.append(Downsample(ch_in, with_conv=False)) |
|
|
|
self.model = nn.ModuleList(blocks) |
|
self.downsampler = nn.ModuleList(downs) |
|
|
|
|
|
def instantiate_pretrained(self, config): |
|
model = instantiate_from_config(config) |
|
self.pretrained_model = model.eval() |
|
|
|
for param in self.pretrained_model.parameters(): |
|
param.requires_grad = False |
|
|
|
|
|
@torch.no_grad() |
|
def encode_with_pretrained(self,x): |
|
c = self.pretrained_model.encode(x) |
|
if isinstance(c, DiagonalGaussianDistribution): |
|
c = c.mode() |
|
return c |
|
|
|
def forward(self,x): |
|
z_fs = self.encode_with_pretrained(x) |
|
z = self.proj_norm(z_fs) |
|
z = self.proj(z) |
|
z = nonlinearity(z) |
|
|
|
for submodel, downmodel in zip(self.model,self.downsampler): |
|
z = submodel(z,temb=None) |
|
z = downmodel(z) |
|
|
|
if self.do_reshape: |
|
z = rearrange(z,'b c h w -> b (h w) c') |
|
return z |
|
|
|
|
|
class DiagonalGaussianDistribution(object): |
|
def __init__(self, parameters, deterministic=False): |
|
self.parameters = parameters |
|
self.mean, self.logvar = torch.chunk(parameters, 2, dim=1) |
|
self.logvar = torch.clamp(self.logvar, -30.0, 20.0) |
|
self.deterministic = deterministic |
|
self.std = torch.exp(0.5 * self.logvar) |
|
self.var = torch.exp(self.logvar) |
|
if self.deterministic: |
|
self.var = self.std = torch.zeros_like(self.mean).to(device=self.parameters.device) |
|
|
|
def sample(self): |
|
x = self.mean + self.std * torch.randn(self.mean.shape).to(device=self.parameters.device) |
|
return x |
|
|
|
def kl(self, other=None): |
|
if self.deterministic: |
|
return torch.Tensor([0.]) |
|
else: |
|
if other is None: |
|
return 0.5 * torch.sum(torch.pow(self.mean, 2) |
|
+ self.var - 1.0 - self.logvar, |
|
dim=[1, 2, 3]) |
|
else: |
|
return 0.5 * torch.sum( |
|
torch.pow(self.mean - other.mean, 2) / other.var |
|
+ self.var / other.var - 1.0 - self.logvar + other.logvar, |
|
dim=[1, 2, 3]) |
|
|
|
def nll(self, sample, dims=[1,2,3]): |
|
if self.deterministic: |
|
return torch.Tensor([0.]) |
|
logtwopi = np.log(2.0 * np.pi) |
|
return 0.5 * torch.sum( |
|
logtwopi + self.logvar + torch.pow(sample - self.mean, 2) / self.var, |
|
dim=dims) |
|
|
|
def mode(self): |
|
return self.mean |
|
|
|
|
|
class ResnetBlock_GroupConv(nn.Module): |
|
def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False, |
|
dropout, temb_channels=512): |
|
super().__init__() |
|
self.in_channels = in_channels |
|
out_channels = in_channels if out_channels is None else out_channels |
|
self.out_channels = out_channels |
|
self.use_conv_shortcut = conv_shortcut |
|
|
|
self.norm1 = Normalize(in_channels * 3, 32 * 3) |
|
self.conv1 = torch.nn.Conv2d(in_channels * 3, |
|
out_channels * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
if temb_channels > 0: |
|
self.temb_proj = torch.nn.Linear(temb_channels, |
|
out_channels) |
|
self.norm2 = Normalize(out_channels * 3, 32 * 3) |
|
self.dropout = torch.nn.Dropout(dropout) |
|
self.conv2 = torch.nn.Conv2d(out_channels * 3, |
|
out_channels * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
if self.in_channels != self.out_channels: |
|
if self.use_conv_shortcut: |
|
self.conv_shortcut = torch.nn.Conv2d(in_channels * 3, |
|
out_channels * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
else: |
|
self.nin_shortcut = torch.nn.Conv2d(in_channels * 3, |
|
out_channels * 3, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0, |
|
groups=3) |
|
|
|
def forward(self, x, temb): |
|
h = x |
|
h = self.norm1(h) |
|
h = nonlinearity(h) |
|
h = self.conv1(h) |
|
|
|
assert temb is None |
|
if temb is not None: |
|
h = h + self.temb_proj(nonlinearity(temb))[:,:,None,None] |
|
|
|
h = self.norm2(h) |
|
h = nonlinearity(h) |
|
h = self.dropout(h) |
|
h = self.conv2(h) |
|
|
|
if self.in_channels != self.out_channels: |
|
if self.use_conv_shortcut: |
|
x = self.conv_shortcut(x) |
|
else: |
|
x = self.nin_shortcut(x) |
|
|
|
return x+h |
|
|
|
|
|
def rollout(triplane): |
|
res = triplane.shape[-1] |
|
ch = triplane.shape[1] |
|
triplane = triplane.reshape(-1, 3, ch//3, res, res).permute(0, 2, 3, 1, 4).reshape(-1, ch//3, res, 3 * res) |
|
return triplane |
|
|
|
def unrollout(triplane): |
|
res = triplane.shape[-2] |
|
ch = 3 * triplane.shape[1] |
|
triplane = triplane.reshape(-1, ch//3, res, 3, res).permute(0, 3, 1, 2, 4).reshape(-1, ch, res, res) |
|
return triplane |
|
|
|
class Upsample_GroupConv(nn.Module): |
|
def __init__(self, in_channels, with_conv): |
|
super().__init__() |
|
self.with_conv = with_conv |
|
if self.with_conv: |
|
self.conv = torch.nn.Conv2d(in_channels * 3, |
|
in_channels * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
|
|
def forward(self, x): |
|
x = torch.nn.functional.interpolate(x, scale_factor=2.0, mode="nearest") |
|
if self.with_conv: |
|
x = self.conv(x) |
|
return x |
|
|
|
|
|
class Downsample_GroupConv(nn.Module): |
|
def __init__(self, in_channels, with_conv): |
|
super().__init__() |
|
self.with_conv = with_conv |
|
if self.with_conv: |
|
|
|
self.conv = torch.nn.Conv2d(in_channels * 3, |
|
in_channels * 3, |
|
kernel_size=3, |
|
stride=2, |
|
padding=0, |
|
groups=3) |
|
|
|
def forward(self, x): |
|
if self.with_conv: |
|
pad = (0,1,0,1) |
|
x = torch.nn.functional.pad(x, pad, mode="constant", value=0) |
|
x = self.conv(x) |
|
else: |
|
x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) |
|
return x |
|
|
|
class AttnBlock_GroupConv(nn.Module): |
|
def __init__(self, in_channels): |
|
super().__init__() |
|
self.in_channels = in_channels |
|
|
|
self.norm = Normalize(in_channels) |
|
self.q = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.k = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.v = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.proj_out = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
|
|
def forward(self, x, temp=None): |
|
x = rollout(x) |
|
h_ = x |
|
h_ = self.norm(h_) |
|
q = self.q(h_) |
|
k = self.k(h_) |
|
v = self.v(h_) |
|
|
|
|
|
b,c,h,w = q.shape |
|
q = q.reshape(b,c,h*w) |
|
q = q.permute(0,2,1) |
|
k = k.reshape(b,c,h*w) |
|
w_ = torch.bmm(q,k) |
|
w_ = w_ * (int(c)**(-0.5)) |
|
w_ = torch.nn.functional.softmax(w_, dim=2) |
|
|
|
|
|
v = v.reshape(b,c,h*w) |
|
w_ = w_.permute(0,2,1) |
|
h_ = torch.bmm(v,w_) |
|
h_ = h_.reshape(b,c,h,w) |
|
|
|
h_ = self.proj_out(h_) |
|
|
|
return unrollout(x+h_) |
|
|
|
|
|
from torch import nn, einsum |
|
from inspect import isfunction |
|
from einops import rearrange, repeat |
|
|
|
def exists(val): |
|
return val is not None |
|
|
|
def default(val, d): |
|
if exists(val): |
|
return val |
|
return d() if isfunction(d) else d |
|
|
|
def checkpoint(func, inputs, params, flag): |
|
""" |
|
Evaluate a function without caching intermediate activations, allowing for |
|
reduced memory at the expense of extra compute in the backward pass. |
|
:param func: the function to evaluate. |
|
:param inputs: the argument sequence to pass to `func`. |
|
:param params: a sequence of parameters `func` depends on but does not |
|
explicitly take as arguments. |
|
:param flag: if False, disable gradient checkpointing. |
|
""" |
|
if flag: |
|
args = tuple(inputs) + tuple(params) |
|
return CheckpointFunction.apply(func, len(inputs), *args) |
|
else: |
|
return func(*inputs) |
|
|
|
class CheckpointFunction(torch.autograd.Function): |
|
@staticmethod |
|
def forward(ctx, run_function, length, *args): |
|
ctx.run_function = run_function |
|
ctx.input_tensors = list(args[:length]) |
|
ctx.input_params = list(args[length:]) |
|
|
|
with torch.no_grad(): |
|
output_tensors = ctx.run_function(*ctx.input_tensors) |
|
return output_tensors |
|
|
|
@staticmethod |
|
def backward(ctx, *output_grads): |
|
ctx.input_tensors = [x.detach().requires_grad_(True) for x in ctx.input_tensors] |
|
with torch.enable_grad(): |
|
|
|
|
|
|
|
shallow_copies = [x.view_as(x) for x in ctx.input_tensors] |
|
output_tensors = ctx.run_function(*shallow_copies) |
|
input_grads = torch.autograd.grad( |
|
output_tensors, |
|
ctx.input_tensors + ctx.input_params, |
|
output_grads, |
|
allow_unused=True, |
|
) |
|
del ctx.input_tensors |
|
del ctx.input_params |
|
del output_tensors |
|
return (None, None) + input_grads |
|
|
|
class CrossAttention(nn.Module): |
|
def __init__(self, query_dim, context_dim=None, heads=8, dim_head=64, dropout=0.): |
|
super().__init__() |
|
inner_dim = dim_head * heads |
|
context_dim = default(context_dim, query_dim) |
|
|
|
self.scale = dim_head ** -0.5 |
|
self.heads = heads |
|
|
|
self.to_q = nn.Linear(query_dim, inner_dim, bias=False) |
|
self.to_k = nn.Linear(context_dim, inner_dim, bias=False) |
|
self.to_v = nn.Linear(context_dim, inner_dim, bias=False) |
|
|
|
self.to_out = nn.Sequential( |
|
nn.Linear(inner_dim, query_dim), |
|
nn.Dropout(dropout) |
|
) |
|
|
|
def forward(self, x, context=None, mask=None): |
|
h = self.heads |
|
|
|
x = x.permute(0, 2, 1) |
|
context = context.permute(0, 2, 1) |
|
|
|
q = self.to_q(x) |
|
context = default(context, x) |
|
k = self.to_k(context) |
|
v = self.to_v(context) |
|
|
|
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v)) |
|
|
|
sim = einsum('b i d, b j d -> b i j', q, k) * self.scale |
|
|
|
if exists(mask): |
|
mask = rearrange(mask, 'b ... -> b (...)') |
|
max_neg_value = -torch.finfo(sim.dtype).max |
|
mask = repeat(mask, 'b j -> (b h) () j', h=h) |
|
sim.masked_fill_(~mask, max_neg_value) |
|
|
|
|
|
attn = sim.softmax(dim=-1) |
|
|
|
out = einsum('b i j, b j d -> b i d', attn, v) |
|
out = rearrange(out, '(b h) n d -> b n (h d)', h=h) |
|
return self.to_out(out).permute(0, 2, 1) |
|
|
|
def normalization(channels): |
|
""" |
|
Make a standard normalization layer. |
|
:param channels: number of input channels. |
|
:return: an nn.Module for normalization. |
|
""" |
|
return GroupNorm32(32, channels) |
|
|
|
class GroupNorm32(nn.GroupNorm): |
|
def forward(self, x): |
|
return super().forward(x.float()).type(x.dtype) |
|
|
|
class TriplaneAttentionBlock(nn.Module): |
|
def __init__( |
|
self, |
|
channels, |
|
num_heads=1, |
|
num_head_channels=-1, |
|
use_checkpoint=False, |
|
use_new_attention_order=False, |
|
): |
|
super().__init__() |
|
self.channels = channels |
|
if num_head_channels == -1: |
|
self.num_heads = num_heads |
|
else: |
|
assert ( |
|
channels % num_head_channels == 0 |
|
), f"q,k,v channels {channels} is not divisible by num_head_channels {num_head_channels}" |
|
self.num_heads = channels // num_head_channels |
|
self.use_checkpoint = use_checkpoint |
|
self.norm = normalization(channels) |
|
|
|
self.plane1_ca = CrossAttention(channels, channels, self.num_heads, num_head_channels) |
|
self.plane2_ca = CrossAttention(channels, channels, self.num_heads, num_head_channels) |
|
self.plane3_ca = CrossAttention(channels, channels, self.num_heads, num_head_channels) |
|
|
|
def forward(self, x, temp=None): |
|
return checkpoint(self._forward, (x,), self.parameters(), True) |
|
|
|
|
|
def _forward(self, x): |
|
x = rollout(x) |
|
|
|
b, c, *spatial = x.shape |
|
res = x.shape[-2] |
|
plane1 = x[..., :res].reshape(b, c, -1) |
|
plane2 = x[..., res:res*2].reshape(b, c, -1) |
|
plane3 = x[..., 2*res:3*res].reshape(b, c, -1) |
|
x = x.reshape(b, c, -1) |
|
|
|
plane1_output = self.plane1_ca(self.norm(plane1), self.norm(x)) |
|
plane2_output = self.plane2_ca(self.norm(plane2), self.norm(x)) |
|
plane3_output = self.plane3_ca(self.norm(plane3), self.norm(x)) |
|
|
|
h = torch.cat([plane1_output, plane2_output, plane3_output], -1) |
|
|
|
x = (x + h).reshape(b, c, *spatial) |
|
|
|
return unrollout(x) |
|
|
|
|
|
class Encoder_GroupConv(nn.Module): |
|
def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
|
attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
|
resolution, z_channels, double_z=True, use_linear_attn=False, |
|
attn_type="vanilla_groupconv", mid_layers=1, |
|
**ignore_kwargs): |
|
super().__init__() |
|
assert not use_linear_attn |
|
self.ch = ch |
|
self.temb_ch = 0 |
|
self.num_resolutions = len(ch_mult) |
|
self.num_res_blocks = num_res_blocks |
|
self.resolution = resolution |
|
self.in_channels = in_channels |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.conv_in = torch.nn.Conv2d(in_channels * 3, |
|
self.ch * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
|
|
curr_res = resolution |
|
in_ch_mult = (1,)+tuple(ch_mult) |
|
self.in_ch_mult = in_ch_mult |
|
self.down = nn.ModuleList() |
|
for i_level in range(self.num_resolutions): |
|
block = nn.ModuleList() |
|
attn = nn.ModuleList() |
|
block_in = ch*in_ch_mult[i_level] |
|
block_out = ch*ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks): |
|
block.append(ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
if curr_res in attn_resolutions: |
|
attn.append(make_attn(block_in, attn_type=attn_type)) |
|
down = nn.Module() |
|
down.block = block |
|
down.attn = attn |
|
if i_level != self.num_resolutions-1: |
|
down.downsample = Downsample_GroupConv(block_in, resamp_with_conv) |
|
curr_res = curr_res // 2 |
|
self.down.append(down) |
|
|
|
|
|
self.attn_type = attn_type |
|
self.mid = nn.Module() |
|
if attn_type == 'crossattention': |
|
self.mid.block_1 = nn.ModuleList() |
|
for _ in range(mid_layers): |
|
self.mid.block_1.append( |
|
ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
) |
|
self.mid.block_1.append( |
|
make_attn(block_in, attn_type=attn_type) |
|
) |
|
else: |
|
self.mid.block_1 = ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
|
self.mid.block_2 = ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
|
|
|
|
self.norm_out = Normalize(block_in * 3, 32 * 3) |
|
self.conv_out = torch.nn.Conv2d(block_in * 3, |
|
2*z_channels * 3 if double_z else z_channels * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
def forward(self, x): |
|
|
|
temb = None |
|
|
|
x = unrollout(x) |
|
|
|
|
|
hs = [self.conv_in(x)] |
|
for i_level in range(self.num_resolutions): |
|
for i_block in range(self.num_res_blocks): |
|
h = self.down[i_level].block[i_block](hs[-1], temb) |
|
if len(self.down[i_level].attn) > 0: |
|
h = self.down[i_level].attn[i_block](h) |
|
hs.append(h) |
|
if i_level != self.num_resolutions-1: |
|
hs.append(self.down[i_level].downsample(hs[-1])) |
|
|
|
|
|
h = hs[-1] |
|
if self.attn_type == 'crossattention': |
|
for m in self.mid.block_1: |
|
h = m(h, temb) |
|
else: |
|
h = self.mid.block_1(h, temb) |
|
h = self.mid.attn_1(h) |
|
h = self.mid.block_2(h, temb) |
|
|
|
|
|
h = self.norm_out(h) |
|
h = nonlinearity(h) |
|
h = self.conv_out(h) |
|
|
|
h = rollout(h) |
|
|
|
return h |
|
|
|
class Decoder_GroupConv(nn.Module): |
|
def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
|
attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
|
resolution, z_channels, give_pre_end=False, tanh_out=False, use_linear_attn=False, |
|
attn_type="vanilla_groupconv", mid_layers=1, **ignorekwargs): |
|
super().__init__() |
|
assert not use_linear_attn |
|
self.ch = ch |
|
self.temb_ch = 0 |
|
self.num_resolutions = len(ch_mult) |
|
self.num_res_blocks = num_res_blocks |
|
self.resolution = resolution |
|
self.in_channels = in_channels |
|
self.give_pre_end = give_pre_end |
|
self.tanh_out = tanh_out |
|
|
|
|
|
in_ch_mult = (1,)+tuple(ch_mult) |
|
block_in = ch*ch_mult[self.num_resolutions-1] |
|
curr_res = resolution // 2**(self.num_resolutions-1) |
|
self.z_shape = (1,z_channels,curr_res,curr_res) |
|
|
|
|
|
|
|
|
|
self.conv_in = torch.nn.Conv2d(z_channels * 3, |
|
block_in * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
|
|
|
|
self.mid = nn.Module() |
|
self.attn_type = attn_type |
|
if attn_type == 'crossattention': |
|
self.mid.block_1 = nn.ModuleList() |
|
for _ in range(mid_layers): |
|
self.mid.block_1.append( |
|
ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
) |
|
self.mid.block_1.append( |
|
make_attn(block_in, attn_type=attn_type) |
|
) |
|
else: |
|
self.mid.block_1 = ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
|
self.mid.block_2 = ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
|
|
|
|
self.up = nn.ModuleList() |
|
for i_level in reversed(range(self.num_resolutions)): |
|
block = nn.ModuleList() |
|
attn = nn.ModuleList() |
|
block_out = ch*ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks+1): |
|
block.append(ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
if curr_res in attn_resolutions: |
|
attn.append(make_attn(block_in, attn_type=attn_type)) |
|
up = nn.Module() |
|
up.block = block |
|
up.attn = attn |
|
if i_level != 0: |
|
up.upsample = Upsample_GroupConv(block_in, resamp_with_conv) |
|
curr_res = curr_res * 2 |
|
self.up.insert(0, up) |
|
|
|
|
|
self.norm_out = Normalize(block_in * 3, 32 * 3) |
|
self.conv_out = torch.nn.Conv2d(block_in * 3, |
|
out_ch * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
|
|
def forward(self, z): |
|
|
|
self.last_z_shape = z.shape |
|
|
|
z = unrollout(z) |
|
|
|
|
|
temb = None |
|
|
|
|
|
h = self.conv_in(z) |
|
|
|
|
|
if self.attn_type == 'crossattention': |
|
for m in self.mid.block_1: |
|
h = m(h, temb) |
|
else: |
|
h = self.mid.block_1(h, temb) |
|
h = self.mid.attn_1(h) |
|
h = self.mid.block_2(h, temb) |
|
|
|
|
|
for i_level in reversed(range(self.num_resolutions)): |
|
for i_block in range(self.num_res_blocks+1): |
|
h = self.up[i_level].block[i_block](h, temb) |
|
if len(self.up[i_level].attn) > 0: |
|
h = self.up[i_level].attn[i_block](h) |
|
if i_level != 0: |
|
h = self.up[i_level].upsample(h) |
|
|
|
|
|
if self.give_pre_end: |
|
return h |
|
|
|
h = self.norm_out(h) |
|
h = nonlinearity(h) |
|
h = self.conv_out(h) |
|
if self.tanh_out: |
|
h = torch.tanh(h) |
|
|
|
h = rollout(h) |
|
|
|
return h |
|
|
|
|
|
|
|
|
|
class CrossAttnFuseBlock_GroupConv(nn.Module): |
|
def __init__(self, in_channels): |
|
super().__init__() |
|
self.in_channels = in_channels |
|
|
|
self.norm = Normalize(in_channels) |
|
self.q0 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.k0 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.v0 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.q1 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.k1 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.v1 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.q2 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.k2 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.v2 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.proj_out0 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.proj_out1 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.proj_out2 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
self.fuse_out = torch.nn.Conv2d(in_channels * 3, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
def forward(self, x): |
|
x = rollout(x) |
|
|
|
b, c, *spatial = x.shape |
|
res = x.shape[-2] |
|
plane1 = x[..., :res].reshape(b, c, res, res) |
|
plane2 = x[..., res:res*2].reshape(b, c, res, res) |
|
plane3 = x[..., 2*res:3*res].reshape(b, c, res, res) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
q0 = self.q0(self.norm(plane2)) |
|
k0 = self.k0(self.norm(plane2)) |
|
v0 = self.v0(self.norm(plane2)) |
|
|
|
q1 = self.q1(self.norm(plane2)) |
|
k1 = self.k1(self.norm(plane1)) |
|
v1 = self.v1(self.norm(plane1)) |
|
|
|
q2 = self.q2(self.norm(plane2)) |
|
k2 = self.k2(self.norm(plane3)) |
|
v2 = self.v2(self.norm(plane3)) |
|
|
|
def compute_attention(q, k, v): |
|
|
|
b,c,h,w = q.shape |
|
q = q.reshape(b,c,h*w) |
|
q = q.permute(0,2,1) |
|
k = k.reshape(b,c,h*w) |
|
w_ = torch.bmm(q,k) |
|
w_ = w_ * (int(c)**(-0.5)) |
|
w_ = torch.nn.functional.softmax(w_, dim=2) |
|
|
|
v = v.reshape(b,c,h*w) |
|
w_ = w_.permute(0,2,1) |
|
h_ = torch.bmm(v,w_) |
|
h_ = h_.reshape(b,c,h,w) |
|
|
|
return h_ |
|
|
|
h0 = compute_attention(q0, k0, v0) |
|
h0 = self.proj_out0(h0) |
|
|
|
h1 = compute_attention(q1, k1, v1) |
|
h1 = self.proj_out1(h1) |
|
|
|
h2 = compute_attention(q2, k2, v2) |
|
h2 = self.proj_out2(h2) |
|
|
|
fuse_out = self.fuse_out( |
|
torch.cat([h0, h1, h2], 1) |
|
) |
|
|
|
return fuse_out |
|
|
|
class CrossAttnDecodeBlock_GroupConv(nn.Module): |
|
def __init__(self, in_channels, h, w): |
|
super().__init__() |
|
self.in_channels = in_channels |
|
self.h = h |
|
self.w = w |
|
|
|
self.norm = Normalize(in_channels) |
|
self.q0 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.k0 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.v0 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
self.q1 = torch.nn.Parameter(torch.randn(1, self.in_channels, h, w)) |
|
self.q1.requires_grad = True |
|
|
|
self.k1 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.v1 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
self.q2 = torch.nn.Parameter(torch.randn(1, self.in_channels, h, w)) |
|
self.q2.requires_grad = True |
|
|
|
self.k2 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.v2 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.proj_out0 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.proj_out1 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
self.proj_out2 = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
self.fuse_out = torch.nn.Conv2d(in_channels, |
|
in_channels, |
|
kernel_size=1, |
|
stride=1, |
|
padding=0) |
|
|
|
def forward(self, x): |
|
|
|
|
|
b, c, *spatial = x.shape |
|
res = x.shape[-2] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
q0 = self.q0(self.norm(x)) |
|
k0 = self.k0(self.norm(x)) |
|
v0 = self.v0(self.norm(x)) |
|
|
|
q1 = self.q1.repeat(b, 1, 1, 1) |
|
k1 = self.k1(self.norm(x)) |
|
v1 = self.v1(self.norm(x)) |
|
|
|
q2 = self.q2.repeat(b, 1, 1, 1) |
|
k2 = self.k2(self.norm(x)) |
|
v2 = self.v2(self.norm(x)) |
|
|
|
def compute_attention(q, k, v): |
|
|
|
b,c,h,w = q.shape |
|
q = q.reshape(b,c,h*w) |
|
q = q.permute(0,2,1) |
|
k = k.reshape(b,c,h*w) |
|
w_ = torch.bmm(q,k) |
|
w_ = w_ * (int(c)**(-0.5)) |
|
w_ = torch.nn.functional.softmax(w_, dim=2) |
|
|
|
v = v.reshape(b,c,h*w) |
|
w_ = w_.permute(0,2,1) |
|
h_ = torch.bmm(v,w_) |
|
h_ = h_.reshape(b,c,h,w) |
|
return h_ |
|
|
|
h0 = compute_attention(q0, k0, v0) |
|
h0 = self.proj_out0(h0) |
|
|
|
h1 = compute_attention(q1, k1, v1) |
|
h1 = self.proj_out1(h1) |
|
|
|
h2 = compute_attention(q2, k2, v2) |
|
h2 = self.proj_out2(h2) |
|
|
|
fuse_out = self.fuse_out( |
|
torch.cat([h1, h0, h2], -1) |
|
) |
|
|
|
fuse_out = unrollout(fuse_out) |
|
|
|
return fuse_out |
|
|
|
class Encoder_GroupConv_LateFusion(nn.Module): |
|
def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
|
attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
|
resolution, z_channels, double_z=True, use_linear_attn=False, attn_type="vanilla_groupconv", |
|
**ignore_kwargs): |
|
super().__init__() |
|
assert not use_linear_attn |
|
self.ch = ch |
|
self.temb_ch = 0 |
|
self.num_resolutions = len(ch_mult) |
|
self.num_res_blocks = num_res_blocks |
|
self.resolution = resolution |
|
self.in_channels = in_channels |
|
|
|
|
|
self.conv_in = torch.nn.Conv2d(in_channels * 3, |
|
self.ch * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
|
|
curr_res = resolution |
|
in_ch_mult = (1,)+tuple(ch_mult) |
|
self.in_ch_mult = in_ch_mult |
|
self.down = nn.ModuleList() |
|
for i_level in range(self.num_resolutions): |
|
block = nn.ModuleList() |
|
attn = nn.ModuleList() |
|
block_in = ch*in_ch_mult[i_level] |
|
block_out = ch*ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks): |
|
block.append(ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
if curr_res in attn_resolutions: |
|
attn.append(make_attn(block_in, attn_type=attn_type)) |
|
down = nn.Module() |
|
down.block = block |
|
down.attn = attn |
|
if i_level != self.num_resolutions-1: |
|
down.downsample = Downsample_GroupConv(block_in, resamp_with_conv) |
|
curr_res = curr_res // 2 |
|
self.down.append(down) |
|
|
|
|
|
self.mid = nn.Module() |
|
self.mid.block_1 = ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
|
self.mid.block_2 = ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
|
|
|
|
self.fuse = CrossAttnFuseBlock_GroupConv(block_in) |
|
|
|
|
|
self.norm_out = Normalize(block_in, 32) |
|
self.conv_out = torch.nn.Conv2d(block_in, |
|
2*z_channels if double_z else z_channels, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
def forward(self, x): |
|
|
|
temb = None |
|
|
|
x = unrollout(x) |
|
|
|
|
|
hs = [self.conv_in(x)] |
|
for i_level in range(self.num_resolutions): |
|
for i_block in range(self.num_res_blocks): |
|
h = self.down[i_level].block[i_block](hs[-1], temb) |
|
if len(self.down[i_level].attn) > 0: |
|
h = self.down[i_level].attn[i_block](h) |
|
hs.append(h) |
|
if i_level != self.num_resolutions-1: |
|
hs.append(self.down[i_level].downsample(hs[-1])) |
|
|
|
|
|
h = hs[-1] |
|
h = self.mid.block_1(h, temb) |
|
h = self.mid.attn_1(h) |
|
h = self.mid.block_2(h, temb) |
|
|
|
h = self.fuse(h) |
|
|
|
|
|
h = self.norm_out(h) |
|
h = nonlinearity(h) |
|
h = self.conv_out(h) |
|
|
|
|
|
|
|
return h |
|
|
|
class Decoder_GroupConv_LateFusion(nn.Module): |
|
def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, |
|
attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, |
|
resolution, z_channels, give_pre_end=False, tanh_out=False, use_linear_attn=False, |
|
attn_type="vanilla_groupconv", **ignorekwargs): |
|
super().__init__() |
|
assert not use_linear_attn |
|
self.ch = ch |
|
self.temb_ch = 0 |
|
self.num_resolutions = len(ch_mult) |
|
self.num_res_blocks = num_res_blocks |
|
self.resolution = resolution |
|
self.in_channels = in_channels |
|
self.give_pre_end = give_pre_end |
|
self.tanh_out = tanh_out |
|
|
|
|
|
in_ch_mult = (1,)+tuple(ch_mult) |
|
block_in = ch*ch_mult[self.num_resolutions-1] |
|
curr_res = resolution // 2**(self.num_resolutions-1) |
|
self.z_shape = (1,z_channels,curr_res,curr_res) |
|
|
|
|
|
|
|
|
|
self.conv_in = torch.nn.Conv2d(z_channels, |
|
block_in, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1) |
|
|
|
|
|
self.triplane_decoder = CrossAttnDecodeBlock_GroupConv(block_in, curr_res, curr_res) |
|
|
|
|
|
self.mid = nn.Module() |
|
self.mid.block_1 = ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) |
|
self.mid.block_2 = ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_in, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout) |
|
|
|
|
|
self.up = nn.ModuleList() |
|
for i_level in reversed(range(self.num_resolutions)): |
|
block = nn.ModuleList() |
|
attn = nn.ModuleList() |
|
block_out = ch*ch_mult[i_level] |
|
for i_block in range(self.num_res_blocks+1): |
|
block.append(ResnetBlock_GroupConv(in_channels=block_in, |
|
out_channels=block_out, |
|
temb_channels=self.temb_ch, |
|
dropout=dropout)) |
|
block_in = block_out |
|
if curr_res in attn_resolutions: |
|
attn.append(make_attn(block_in, attn_type=attn_type)) |
|
up = nn.Module() |
|
up.block = block |
|
up.attn = attn |
|
if i_level != 0: |
|
up.upsample = Upsample_GroupConv(block_in, resamp_with_conv) |
|
curr_res = curr_res * 2 |
|
self.up.insert(0, up) |
|
|
|
|
|
self.norm_out = Normalize(block_in * 3, 32 * 3) |
|
self.conv_out = torch.nn.Conv2d(block_in * 3, |
|
out_ch * 3, |
|
kernel_size=3, |
|
stride=1, |
|
padding=1, |
|
groups=3) |
|
|
|
def forward(self, z): |
|
|
|
self.last_z_shape = z.shape |
|
|
|
|
|
temb = None |
|
|
|
|
|
h = self.conv_in(z) |
|
|
|
h = self.triplane_decoder(h) |
|
|
|
|
|
h = self.mid.block_1(h, temb) |
|
h = self.mid.attn_1(h) |
|
h = self.mid.block_2(h, temb) |
|
|
|
|
|
for i_level in reversed(range(self.num_resolutions)): |
|
for i_block in range(self.num_res_blocks+1): |
|
h = self.up[i_level].block[i_block](h, temb) |
|
if len(self.up[i_level].attn) > 0: |
|
h = self.up[i_level].attn[i_block](h) |
|
if i_level != 0: |
|
h = self.up[i_level].upsample(h) |
|
|
|
|
|
if self.give_pre_end: |
|
return h |
|
|
|
h = self.norm_out(h) |
|
h = nonlinearity(h) |
|
h = self.conv_out(h) |
|
if self.tanh_out: |
|
h = torch.tanh(h) |
|
|
|
h = rollout(h) |
|
|
|
return h |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import math |
|
import numpy as np |
|
from typing import Union, Tuple, List |
|
from collections import OrderedDict |
|
|
|
import torch |
|
import torch.nn as nn |
|
import torch.nn.functional as F |
|
from einops import rearrange, repeat |
|
from einops.layers.torch import Rearrange |
|
|
|
def get_2d_sincos_pos_embed(embed_dim, grid_size): |
|
""" |
|
grid_size: int or (int, int) of the grid height and width |
|
return: |
|
pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token) |
|
""" |
|
grid_size = (grid_size, grid_size) if type(grid_size) != tuple else grid_size |
|
grid_h = np.arange(grid_size[0], dtype=np.float32) |
|
grid_w = np.arange(grid_size[1], dtype=np.float32) |
|
grid = np.meshgrid(grid_w, grid_h) |
|
grid = np.stack(grid, axis=0) |
|
|
|
grid = grid.reshape([2, 1, grid_size[0], grid_size[1]]) |
|
pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid) |
|
|
|
return pos_embed |
|
|
|
|
|
def get_2d_sincos_pos_embed_from_grid(embed_dim, grid): |
|
assert embed_dim % 2 == 0 |
|
|
|
|
|
emb_h = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[0]) |
|
emb_w = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[1]) |
|
|
|
emb = np.concatenate([emb_h, emb_w], axis=1) |
|
return emb |
|
|
|
|
|
def get_1d_sincos_pos_embed_from_grid(embed_dim, pos): |
|
""" |
|
embed_dim: output dimension for each position |
|
pos: a list of positions to be encoded: size (M,) |
|
out: (M, D) |
|
""" |
|
assert embed_dim % 2 == 0 |
|
omega = np.arange(embed_dim // 2, dtype=np.float32) |
|
omega /= embed_dim / 2. |
|
omega = 1. / 10000**omega |
|
|
|
pos = pos.reshape(-1) |
|
out = np.einsum('m,d->md', pos, omega) |
|
|
|
emb_sin = np.sin(out) |
|
emb_cos = np.cos(out) |
|
|
|
emb = np.concatenate([emb_sin, emb_cos], axis=1) |
|
return emb |
|
|
|
|
|
def init_weights(m): |
|
if isinstance(m, nn.Linear): |
|
|
|
torch.nn.init.xavier_uniform_(m.weight) |
|
if m.bias is not None: |
|
nn.init.constant_(m.bias, 0) |
|
elif isinstance(m, nn.LayerNorm): |
|
nn.init.constant_(m.bias, 0) |
|
nn.init.constant_(m.weight, 1.0) |
|
elif isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d): |
|
w = m.weight.data |
|
torch.nn.init.xavier_uniform_(w.view([w.shape[0], -1])) |
|
|
|
|
|
class PreNorm(nn.Module): |
|
def __init__(self, dim: int, fn: nn.Module) -> None: |
|
super().__init__() |
|
self.norm = nn.LayerNorm(dim) |
|
self.fn = fn |
|
|
|
def forward(self, x: torch.FloatTensor, **kwargs) -> torch.FloatTensor: |
|
return self.fn(self.norm(x), **kwargs) |
|
|
|
|
|
class FeedForward(nn.Module): |
|
def __init__(self, dim: int, hidden_dim: int) -> None: |
|
super().__init__() |
|
self.net = nn.Sequential( |
|
nn.Linear(dim, hidden_dim), |
|
nn.Tanh(), |
|
nn.Linear(hidden_dim, dim) |
|
) |
|
|
|
def forward(self, x: torch.FloatTensor) -> torch.FloatTensor: |
|
return self.net(x) |
|
|
|
|
|
class Attention(nn.Module): |
|
def __init__(self, dim: int, heads: int = 8, dim_head: int = 64) -> None: |
|
super().__init__() |
|
inner_dim = dim_head * heads |
|
project_out = not (heads == 1 and dim_head == dim) |
|
|
|
self.heads = heads |
|
self.scale = dim_head ** -0.5 |
|
|
|
self.attend = nn.Softmax(dim = -1) |
|
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False) |
|
|
|
self.to_out = nn.Linear(inner_dim, dim) if project_out else nn.Identity() |
|
|
|
def forward(self, x: torch.FloatTensor) -> torch.FloatTensor: |
|
qkv = self.to_qkv(x).chunk(3, dim = -1) |
|
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), qkv) |
|
|
|
attn = torch.matmul(q, k.transpose(-1, -2)) * self.scale |
|
attn = self.attend(attn) |
|
|
|
out = torch.matmul(attn, v) |
|
out = rearrange(out, 'b h n d -> b n (h d)') |
|
|
|
return self.to_out(out) |
|
|
|
|
|
class Transformer(nn.Module): |
|
def __init__(self, dim: int, depth: int, heads: int, dim_head: int, mlp_dim: int) -> None: |
|
super().__init__() |
|
self.layers = nn.ModuleList([]) |
|
for idx in range(depth): |
|
layer = nn.ModuleList([PreNorm(dim, Attention(dim, heads=heads, dim_head=dim_head)), |
|
PreNorm(dim, FeedForward(dim, mlp_dim))]) |
|
self.layers.append(layer) |
|
self.norm = nn.LayerNorm(dim) |
|
|
|
def forward(self, x: torch.FloatTensor) -> torch.FloatTensor: |
|
for attn, ff in self.layers: |
|
x = attn(x) + x |
|
x = ff(x) + x |
|
|
|
return self.norm(x) |
|
|
|
|
|
class ViTEncoder(nn.Module): |
|
def __init__(self, image_size: Union[Tuple[int, int], int], patch_size: Union[Tuple[int, int], int], |
|
dim: int, depth: int, heads: int, mlp_dim: int, channels: int = 3, dim_head: int = 64) -> None: |
|
super().__init__() |
|
image_height, image_width = image_size if isinstance(image_size, tuple) \ |
|
else (image_size, image_size) |
|
patch_height, patch_width = patch_size if isinstance(patch_size, tuple) \ |
|
else (patch_size, patch_size) |
|
|
|
assert image_height % patch_height == 0 and image_width % patch_width == 0, 'Image dimensions must be divisible by the patch size.' |
|
en_pos_embedding = get_2d_sincos_pos_embed(dim, (image_height // patch_height, image_width // patch_width)) |
|
|
|
self.num_patches = (image_height // patch_height) * (image_width // patch_width) |
|
self.patch_dim = channels * patch_height * patch_width |
|
|
|
self.to_patch_embedding = nn.Sequential( |
|
nn.Conv2d(channels, dim, kernel_size=patch_size, stride=patch_size), |
|
Rearrange('b c h w -> b (h w) c'), |
|
) |
|
|
|
self.patch_height = patch_height |
|
self.patch_width = patch_width |
|
self.image_height = image_height |
|
self.image_width = image_width |
|
self.dim = dim |
|
|
|
self.en_pos_embedding = nn.Parameter(torch.from_numpy(en_pos_embedding).float().unsqueeze(0), requires_grad=False) |
|
self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim) |
|
|
|
self.apply(init_weights) |
|
|
|
def forward(self, img: torch.FloatTensor) -> torch.FloatTensor: |
|
x = self.to_patch_embedding(img) |
|
x = x + self.en_pos_embedding |
|
x = self.transformer(x) |
|
|
|
x = Rearrange('b h w c -> b c h w')(x.reshape(-1, self.image_height // self.patch_height, self.image_width // self.patch_width, self.dim)) |
|
|
|
return x |
|
|
|
|
|
class ViTDecoder(nn.Module): |
|
def __init__(self, image_size: Union[Tuple[int, int], int], patch_size: Union[Tuple[int, int], int], |
|
dim: int, depth: int, heads: int, mlp_dim: int, channels: int = 3, dim_head: int = 64) -> None: |
|
super().__init__() |
|
image_height, image_width = image_size if isinstance(image_size, tuple) \ |
|
else (image_size, image_size) |
|
patch_height, patch_width = patch_size if isinstance(patch_size, tuple) \ |
|
else (patch_size, patch_size) |
|
|
|
assert image_height % patch_height == 0 and image_width % patch_width == 0, 'Image dimensions must be divisible by the patch size.' |
|
de_pos_embedding = get_2d_sincos_pos_embed(dim, (image_height // patch_height, image_width // patch_width)) |
|
|
|
self.num_patches = (image_height // patch_height) * (image_width // patch_width) |
|
self.patch_dim = channels * patch_height * patch_width |
|
|
|
self.patch_height = patch_height |
|
self.patch_width = patch_width |
|
self.image_height = image_height |
|
self.image_width = image_width |
|
self.dim = dim |
|
|
|
self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim) |
|
self.de_pos_embedding = nn.Parameter(torch.from_numpy(de_pos_embedding).float().unsqueeze(0), requires_grad=False) |
|
self.to_pixel = nn.Sequential( |
|
Rearrange('b (h w) c -> b c h w', h=image_height // patch_height), |
|
nn.ConvTranspose2d(dim, channels, kernel_size=patch_size, stride=patch_size) |
|
) |
|
|
|
self.apply(init_weights) |
|
|
|
def forward(self, token: torch.FloatTensor) -> torch.FloatTensor: |
|
token = Rearrange('b c h w -> b (h w) c')(token) |
|
|
|
x = token + self.de_pos_embedding |
|
x = self.transformer(x) |
|
x = self.to_pixel(x) |
|
|
|
return x |
|
|
|
def get_last_layer(self) -> nn.Parameter: |
|
return self.to_pixel[-1].weight |
|
|