Face-Upscale / model.py
DonkeySmall's picture
Upload model.py
eebbce4 verified
Raw History Blame Contribute Delete
10.1 kB
import torch
import torch.nn as nn
import torch.nn.functional as F
class DropPath(nn.Module):
def __init__(self, drop_prob: float = 0.0):
super().__init__()
self.drop_prob = drop_prob
def forward(self, x):
if self.drop_prob == 0.0 or not self.training:
return x
keep_prob = 1.0 - self.drop_prob
shape = (x.shape[0],) + (1,) * (x.ndim - 1)
random_tensor = torch.rand(shape, dtype=x.dtype, device=x.device)
random_tensor = torch.floor(random_tensor + keep_prob)
return x * random_tensor / keep_prob
def window_partition(x, window_size):
# x: [B, H, W, C]
B, H, W, C = x.shape
x = x.view(
B,
H // window_size,
window_size,
W // window_size,
window_size,
C,
)
windows = x.permute(0, 1, 3, 2, 4, 5).contiguous()
windows = windows.view(-1, window_size, window_size, C)
return windows
def window_reverse(windows, window_size, H, W):
num_windows = (H // window_size) * (W // window_size)
B = windows.shape[0] // num_windows
x = windows.view(
B,
H // window_size,
W // window_size,
window_size,
window_size,
-1,
)
x = x.permute(0, 1, 3, 2, 4, 5).contiguous()
x = x.view(B, H, W, -1)
return x
class Mlp(nn.Module):
def __init__(self, dim, mlp_ratio=4.0):
super().__init__()
hidden = int(dim * mlp_ratio)
self.fc1 = nn.Linear(dim, hidden)
self.act = nn.GELU()
self.fc2 = nn.Linear(hidden, dim)
def forward(self, x):
return self.fc2(self.act(self.fc1(x)))
class WindowAttention(nn.Module):
def __init__(self, dim, window_size, num_heads):
super().__init__()
assert dim % num_heads == 0, "dim должен делиться на num_heads"
self.dim = dim
self.window_size = window_size
self.num_heads = num_heads
head_dim = dim // num_heads
self.scale = head_dim ** -0.5
self.relative_position_bias_table = nn.Parameter(
torch.zeros((2 * window_size - 1) * (2 * window_size - 1), num_heads)
)
nn.init.trunc_normal_(self.relative_position_bias_table, std=0.02)
coords_h = torch.arange(window_size)
coords_w = torch.arange(window_size)
coords = torch.stack(torch.meshgrid(coords_h, coords_w, indexing="ij"))
coords_flatten = torch.flatten(coords, 1)
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :]
relative_coords = relative_coords.permute(1, 2, 0).contiguous()
relative_coords[:, :, 0] += window_size - 1
relative_coords[:, :, 1] += window_size - 1
relative_coords[:, :, 0] *= 2 * window_size - 1
relative_position_index = relative_coords.sum(-1)
self.register_buffer("relative_position_index", relative_position_index)
self.qkv = nn.Linear(dim, dim * 3)
self.proj = nn.Linear(dim, dim)
self.softmax = nn.Softmax(dim=-1)
def forward(self, x, mask=None):
# x: [B_windows, N, C]
B_, N, C = x.shape
qkv = self.qkv(x)
qkv = qkv.reshape(B_, N, 3, self.num_heads, C // self.num_heads)
qkv = qkv.permute(2, 0, 3, 1, 4)
q, k, v = qkv.unbind(0)
attn = (q * self.scale) @ k.transpose(-2, -1)
relative_position_bias = self.relative_position_bias_table[
self.relative_position_index.view(-1)
].view(
self.window_size * self.window_size,
self.window_size * self.window_size,
-1,
)
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous()
attn = attn + relative_position_bias.unsqueeze(0)
if mask is not None:
nW = mask.shape[0]
attn = attn.view(B_ // nW, nW, self.num_heads, N, N)
attn = attn + mask.unsqueeze(1).unsqueeze(0)
attn = attn.view(-1, self.num_heads, N, N)
attn = self.softmax(attn)
x = (attn @ v).transpose(1, 2).reshape(B_, N, C)
x = self.proj(x)
return x
class SwinBlock(nn.Module):
def __init__(
self,
dim,
num_heads,
window_size,
shift_size=0,
mlp_ratio=4.0,
drop_path=0.0,
):
super().__init__()
self.window_size = window_size
self.shift_size = shift_size
self.norm1 = nn.LayerNorm(dim)
self.attn = WindowAttention(
dim=dim,
window_size=window_size,
num_heads=num_heads,
)
self.drop_path1 = DropPath(drop_path)
self.drop_path2 = DropPath(drop_path)
self.norm2 = nn.LayerNorm(dim)
self.mlp = Mlp(dim=dim, mlp_ratio=mlp_ratio)
def _make_mask(self, H, W, shift_size, device):
img_mask = torch.zeros((1, H, W, 1), device=device)
h_slices = (
slice(0, -self.window_size),
slice(-self.window_size, -shift_size),
slice(-shift_size, None),
)
w_slices = (
slice(0, -self.window_size),
slice(-self.window_size, -shift_size),
slice(-shift_size, None),
)
cnt = 0
for h in h_slices:
for w in w_slices:
img_mask[:, h, w, :] = cnt
cnt += 1
mask_windows = window_partition(img_mask, self.window_size)
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
attn_mask = attn_mask.masked_fill(attn_mask != 0, -100.0)
attn_mask = attn_mask.masked_fill(attn_mask == 0, 0.0)
return attn_mask
def forward(self, x, H, W):
# x: [B, H*W, C]
B, L, C = x.shape
shortcut = x
x = self.norm1(x)
x = x.view(B, H, W, C)
pad_r = (self.window_size - W % self.window_size) % self.window_size
pad_b = (self.window_size - H % self.window_size) % self.window_size
x = F.pad(x, (0, 0, 0, pad_r, 0, pad_b))
_, Hp, Wp, _ = x.shape
shift_size = self.shift_size if min(Hp, Wp) > self.window_size else 0
if shift_size > 0:
shifted = torch.roll(x, shifts=(-shift_size, -shift_size), dims=(1, 2))
attn_mask = self._make_mask(Hp, Wp, shift_size, x.device)
else:
shifted = x
attn_mask = None
x_windows = window_partition(shifted, self.window_size)
x_windows = x_windows.view(-1, self.window_size * self.window_size, C)
attn_windows = self.attn(x_windows, mask=attn_mask)
attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C)
shifted = window_reverse(attn_windows, self.window_size, Hp, Wp)
if shift_size > 0:
x = torch.roll(shifted, shifts=(shift_size, shift_size), dims=(1, 2))
else:
x = shifted
if pad_r > 0 or pad_b > 0:
x = x[:, :H, :W, :].contiguous()
x = x.view(B, H * W, C)
x = shortcut + self.drop_path1(x)
x = x + self.drop_path2(self.mlp(self.norm2(x)))
return x
class UpscalerTransformer(nn.Module):
"""
Лёгкая трансформерная сеть для апскейла x2.
Вход: [B, 3, 128, 128]
Выход: [B, 3, 256, 256]
"""
def __init__(
self,
dim=256,
depth=8,
num_heads=8,
window_size=8,
mlp_ratio=4.0,
):
super().__init__()
assert dim % num_heads == 0, "dim должен делиться на num_heads"
self.conv_first = nn.Conv2d(3, dim, kernel_size=3, padding=1)
self.layers = nn.ModuleList(
[
SwinBlock(
dim=dim,
num_heads=num_heads,
window_size=window_size,
shift_size=0 if i % 2 == 0 else window_size // 2,
mlp_ratio=mlp_ratio,
)
for i in range(depth)
]
)
self.norm = nn.LayerNorm(dim)
self.conv_feat = nn.Conv2d(dim, dim, kernel_size=3, padding=1)
# PixelShuffle x2:
# conv делает 3 * 2 * 2 = 12 каналов,
# PixelShuffle собирает из них RGB x2 размера.
self.up = nn.Sequential(
nn.Conv2d(dim, 3 * 4, kernel_size=3, padding=1),
nn.PixelShuffle(2),
)
self.conv_last = nn.Conv2d(3, 3, kernel_size=3, padding=1)
self.apply(self._init_weights)
def _init_weights(self, m):
if isinstance(m, nn.Linear):
nn.init.trunc_normal_(m.weight, std=0.02)
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0.0)
nn.init.constant_(m.weight, 1.0)
elif isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
def forward(self, x):
base = F.interpolate(x, scale_factor=2, mode="bicubic", align_corners=False)
f = self.conv_first(x)
shortcut = f
B, C, H, W = f.shape
f = f.flatten(2).transpose(1, 2)
for layer in self.layers:
f = layer(f, H, W)
f = self.norm(f)
f = f.transpose(1, 2).reshape(B, C, H, W)
f = self.conv_feat(f + shortcut)
out = self.up(f)
out = self.conv_last(out)
return base + out