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
| import torch from torch import nn, einsum import numpy as np from einops import rearrange, repeat
class CyclicShift(nn.Module): def __init__(self, displacement): super().__init__() self.displacement = displacement
def forward(self, x): return torch.roll(x, shifts=(self.displacement, self.displacement), dims=(1, 2))
class Residual(nn.Module): def __init__(self, fn): super().__init__() self.fn = fn
def forward(self, x, **kwargs): return self.fn(x, **kwargs) + x
class PreNorm(nn.Module): def __init__(self, dim, fn): super().__init__() self.norm = nn.LayerNorm(dim) self.fn = fn
def forward(self, x, **kwargs): return self.fn(self.norm(x), **kwargs)
class FeedForward(nn.Module): def __init__(self, dim, hidden_dim): super().__init__() self.net = nn.Sequential( nn.Linear(dim, hidden_dim), nn.GELU(), nn.Linear(hidden_dim, dim), )
def forward(self, x): return self.net(x)
def create_mask(window_size, displacement, upper_lower, left_right): mask = torch.zeros(window_size ** 2, window_size ** 2)
if upper_lower: mask[-displacement * window_size:, :-displacement * window_size] = float('-inf') mask[:-displacement * window_size, -displacement * window_size:] = float('-inf')
if left_right: mask = rearrange(mask, '(h1 w1) (h2 w2) -> h1 w1 h2 w2', h1=window_size, h2=window_size) mask[:, -displacement:, :, :-displacement] = float('-inf') mask[:, :-displacement, :, -displacement:] = float('-inf') mask = rearrange(mask, 'h1 w1 h2 w2 -> (h1 w1) (h2 w2)')
return mask
def get_relative_distances(window_size): indices = torch.tensor(np.array([[x, y] for x in range(window_size) for y in range(window_size)])) distances = indices[None, :, :] - indices[:, None, :] return distances
class WindowAttention(nn.Module): def __init__(self, dim, heads, head_dim, shifted, window_size, relative_pos_embedding): super().__init__() inner_dim = head_dim * heads
self.heads = heads self.scale = head_dim ** -0.5 self.window_size = window_size self.relative_pos_embedding = relative_pos_embedding self.shifted = shifted
if self.shifted: displacement = window_size // 2 self.cyclic_shift = CyclicShift(-displacement) self.cyclic_back_shift = CyclicShift(displacement) self.upper_lower_mask = nn.Parameter(create_mask(window_size=window_size, displacement=displacement, upper_lower=True, left_right=False), requires_grad=False) self.left_right_mask = nn.Parameter(create_mask(window_size=window_size, displacement=displacement, upper_lower=False, left_right=True), requires_grad=False)
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias=False)
if self.relative_pos_embedding: self.relative_indices = get_relative_distances(window_size) + window_size - 1 self.pos_embedding = nn.Parameter(torch.randn(2 * window_size - 1, 2 * window_size - 1)) else: self.pos_embedding = nn.Parameter(torch.randn(window_size ** 2, window_size ** 2))
self.to_out = nn.Linear(inner_dim, dim)
def forward(self, x): if self.shifted: x = self.cyclic_shift(x)
b, n_h, n_w, _, h = *x.shape, self.heads
qkv = self.to_qkv(x).chunk(3, dim=-1) nw_h = n_h // self.window_size nw_w = n_w // self.window_size
q, k, v = map( lambda t: rearrange(t, 'b (nw_h w_h) (nw_w w_w) (h d) -> b h (nw_h nw_w) (w_h w_w) d', h=h, w_h=self.window_size, w_w=self.window_size), qkv)
dots = einsum('b h w i d, b h w j d -> b h w i j', q, k) * self.scale
if self.relative_pos_embedding: dots += self.pos_embedding[self.relative_indices[:, :, 0], self.relative_indices[:, :, 1]] else: dots += self.pos_embedding
if self.shifted: dots[:, :, -nw_w:] += self.upper_lower_mask dots[:, :, nw_w - 1::nw_w] += self.left_right_mask
attn = dots.softmax(dim=-1)
out = einsum('b h w i j, b h w j d -> b h w i d', attn, v) out = rearrange(out, 'b h (nw_h nw_w) (w_h w_w) d -> b (nw_h w_h) (nw_w w_w) (h d)', h=h, w_h=self.window_size, w_w=self.window_size, nw_h=nw_h, nw_w=nw_w) out = self.to_out(out)
if self.shifted: out = self.cyclic_back_shift(out) return out
class SwinBlock(nn.Module): def __init__(self, dim, heads, head_dim, mlp_dim, shifted, window_size, relative_pos_embedding): super().__init__() self.attention_block = Residual(PreNorm(dim, WindowAttention(dim=dim, heads=heads, head_dim=head_dim, shifted=shifted, window_size=window_size, relative_pos_embedding=relative_pos_embedding))) self.mlp_block = Residual(PreNorm(dim, FeedForward(dim=dim, hidden_dim=mlp_dim)))
def forward(self, x): x = self.attention_block(x) x = self.mlp_block(x) return x
class PatchMerging(nn.Module): def __init__(self, in_channels, out_channels, downscaling_factor): super().__init__() self.downscaling_factor = downscaling_factor self.patch_merge = nn.Unfold(kernel_size=downscaling_factor, stride=downscaling_factor, padding=0) self.linear = nn.Linear(in_channels * downscaling_factor ** 2, out_channels)
def forward(self, x): b, c, h, w = x.shape new_h, new_w = h // self.downscaling_factor, w // self.downscaling_factor x = self.patch_merge(x).view(b, -1, new_h, new_w).permute(0, 2, 3, 1) x = self.linear(x) return x
class StageModule(nn.Module): def __init__(self, in_channels, hidden_dimension, layers, downscaling_factor, num_heads, head_dim, window_size, relative_pos_embedding): super().__init__() assert layers % 2 == 0, 'Stage layers need to be divisible by 2 for regular and shifted block.'
self.patch_partition = PatchMerging(in_channels=in_channels, out_channels=hidden_dimension, downscaling_factor=downscaling_factor)
self.layers = nn.ModuleList([]) for _ in range(layers // 2): self.layers.append(nn.ModuleList([ SwinBlock(dim=hidden_dimension, heads=num_heads, head_dim=head_dim, mlp_dim=hidden_dimension * 4, shifted=False, window_size=window_size, relative_pos_embedding=relative_pos_embedding), SwinBlock(dim=hidden_dimension, heads=num_heads, head_dim=head_dim, mlp_dim=hidden_dimension * 4, shifted=True, window_size=window_size, relative_pos_embedding=relative_pos_embedding), ]))
def forward(self, x): x = self.patch_partition(x) for regular_block, shifted_block in self.layers: x = regular_block(x) x = shifted_block(x) return x.permute(0, 3, 1, 2)
class SwinTransformer(nn.Module): def __init__(self, *, hidden_dim, layers, heads, channels=3, num_classes=1000, head_dim=32, window_size=7, downscaling_factors=(4, 2, 2, 2), relative_pos_embedding=True): super().__init__()
self.stage1 = StageModule(in_channels=channels, hidden_dimension=hidden_dim, layers=layers[0], downscaling_factor=downscaling_factors[0], num_heads=heads[0], head_dim=head_dim, window_size=window_size, relative_pos_embedding=relative_pos_embedding) self.stage2 = StageModule(in_channels=hidden_dim, hidden_dimension=hidden_dim * 2, layers=layers[1], downscaling_factor=downscaling_factors[1], num_heads=heads[1], head_dim=head_dim, window_size=window_size, relative_pos_embedding=relative_pos_embedding) self.stage3 = StageModule(in_channels=hidden_dim * 2, hidden_dimension=hidden_dim * 4, layers=layers[2], downscaling_factor=downscaling_factors[2], num_heads=heads[2], head_dim=head_dim, window_size=window_size, relative_pos_embedding=relative_pos_embedding) self.stage4 = StageModule(in_channels=hidden_dim * 4, hidden_dimension=hidden_dim * 8, layers=layers[3], downscaling_factor=downscaling_factors[3], num_heads=heads[3], head_dim=head_dim, window_size=window_size, relative_pos_embedding=relative_pos_embedding)
self.mlp_head = nn.Sequential( nn.LayerNorm(hidden_dim * 8), nn.Linear(hidden_dim * 8, num_classes) )
def forward(self, img): x = self.stage1(img) x = self.stage2(x) x = self.stage3(x) x = self.stage4(x) x = x.mean(dim=[2, 3]) return self.mlp_head(x)
|