File size: 10,056 Bytes
eebbce4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | 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
|