|
| 1 | +import torch |
| 2 | +from torch import nn |
| 3 | + |
| 4 | +from einops import rearrange |
| 5 | +from einops.layers.torch import Rearrange |
| 6 | + |
| 7 | +# helpers |
| 8 | + |
| 9 | +def pair(t): |
| 10 | + return t if isinstance(t, tuple) else (t, t) |
| 11 | + |
| 12 | +def posemb_sincos_2d(patches, temperature = 10000, dtype = torch.float32): |
| 13 | + _, h, w, dim, device, dtype = *patches.shape, patches.device, patches.dtype |
| 14 | + |
| 15 | + y, x = torch.meshgrid(torch.arange(h, device = device), torch.arange(w, device = device), indexing = 'ij') |
| 16 | + assert (dim % 4) == 0, 'feature dimension must be multiple of 4 for sincos emb' |
| 17 | + omega = torch.arange(dim // 4, device = device) / (dim // 4 - 1) |
| 18 | + omega = 1. / (temperature ** omega) |
| 19 | + |
| 20 | + y = y.flatten()[:, None] * omega[None, :] |
| 21 | + x = x.flatten()[:, None] * omega[None, :] |
| 22 | + pe = torch.cat((x.sin(), x.cos(), y.sin(), y.cos()), dim = 1) |
| 23 | + return pe.type(dtype) |
| 24 | + |
| 25 | +# classes |
| 26 | + |
| 27 | +class FeedForward(nn.Module): |
| 28 | + def __init__(self, dim, hidden_dim): |
| 29 | + super().__init__() |
| 30 | + self.net = nn.Sequential( |
| 31 | + nn.LayerNorm(dim), |
| 32 | + nn.Linear(dim, hidden_dim), |
| 33 | + nn.GELU(), |
| 34 | + nn.Linear(hidden_dim, dim), |
| 35 | + ) |
| 36 | + def forward(self, x): |
| 37 | + return self.net(x) |
| 38 | + |
| 39 | +class Attention(nn.Module): |
| 40 | + def __init__(self, dim, heads = 8, dim_head = 64): |
| 41 | + super().__init__() |
| 42 | + inner_dim = dim_head * heads |
| 43 | + self.heads = heads |
| 44 | + self.scale = dim_head ** -0.5 |
| 45 | + self.norm = nn.LayerNorm(dim) |
| 46 | + |
| 47 | + self.attend = nn.Softmax(dim = -1) |
| 48 | + |
| 49 | + self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False) |
| 50 | + self.to_out = nn.Linear(inner_dim, dim, bias = False) |
| 51 | + |
| 52 | + def forward(self, x): |
| 53 | + x = self.norm(x) |
| 54 | + |
| 55 | + qkv = self.to_qkv(x).chunk(3, dim = -1) |
| 56 | + q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), qkv) |
| 57 | + |
| 58 | + dots = torch.matmul(q, k.transpose(-1, -2)) * self.scale |
| 59 | + |
| 60 | + attn = self.attend(dots) |
| 61 | + |
| 62 | + out = torch.matmul(attn, v) |
| 63 | + out = rearrange(out, 'b h n d -> b n (h d)') |
| 64 | + return self.to_out(out) |
| 65 | + |
| 66 | +class Transformer(nn.Module): |
| 67 | + def __init__(self, dim, depth, heads, dim_head, mlp_dim): |
| 68 | + super().__init__() |
| 69 | + self.layers = nn.ModuleList([]) |
| 70 | + for _ in range(depth): |
| 71 | + self.layers.append(nn.ModuleList([ |
| 72 | + Attention(dim, heads = heads, dim_head = dim_head), |
| 73 | + FeedForward(dim, mlp_dim) |
| 74 | + ])) |
| 75 | + def forward(self, x): |
| 76 | + for attn, ff in self.layers: |
| 77 | + x = attn(x) + x |
| 78 | + x = ff(x) + x |
| 79 | + return x |
| 80 | + |
| 81 | +class SimpleViT(nn.Module): |
| 82 | + def __init__(self, *, image_size, patch_size, num_classes, dim, depth, heads, mlp_dim, channels = 3, dim_head = 64): |
| 83 | + super().__init__() |
| 84 | + image_height, image_width = pair(image_size) |
| 85 | + patch_height, patch_width = pair(patch_size) |
| 86 | + |
| 87 | + assert image_height % patch_height == 0 and image_width % patch_width == 0, 'Image dimensions must be divisible by the patch size.' |
| 88 | + |
| 89 | + num_patches = (image_height // patch_height) * (image_width // patch_width) |
| 90 | + patch_dim = channels * patch_height * patch_width |
| 91 | + |
| 92 | + self.to_patch_embedding = nn.Sequential( |
| 93 | + Rearrange('b c (h p1) (w p2) -> b h w (p1 p2 c)', p1 = patch_height, p2 = patch_width), |
| 94 | + nn.Linear(patch_dim, dim), |
| 95 | + ) |
| 96 | + |
| 97 | + self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim) |
| 98 | + |
| 99 | + self.to_latent = nn.Identity() |
| 100 | + self.linear_head = nn.Sequential( |
| 101 | + nn.LayerNorm(dim), |
| 102 | + nn.Linear(dim, num_classes) |
| 103 | + ) |
| 104 | + |
| 105 | + def forward(self, img): |
| 106 | + *_, h, w, dtype = *img.shape, img.dtype |
| 107 | + |
| 108 | + x = self.to_patch_embedding(img) |
| 109 | + pe = posemb_sincos_2d(x) |
| 110 | + x = rearrange(x, 'b ... d -> b (...) d') + pe |
| 111 | + |
| 112 | + x = self.transformer(x) |
| 113 | + x = x.mean(dim = 1) |
| 114 | + |
| 115 | + x = self.to_latent(x) |
| 116 | + return self.linear_head(x) |
0 commit comments