Spaces:
Paused
Paused
File size: 10,743 Bytes
cee5099 |
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 |
import torch
import torch.nn as nn
import numpy as np
from einops import rearrange, repeat
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, dropout = 0.):
super().__init__()
self.net = nn.Sequential(
nn.Linear(dim, hidden_dim),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, dim),
nn.Dropout(dropout)
)
def forward(self, x):
return self.net(x)
class Attention(nn.Module):
def __init__(self, dim, heads, dim_head, dropout):
super().__init__()
inner_dim = dim_head * heads
self.heads = heads
self.scale = dim_head ** -0.5
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False)
self.to_out = nn.Sequential(
nn.Linear(inner_dim, dim),
nn.Dropout(dropout)
)
def forward(self, x, mask = None):
# x:[b,n,dim]
b, n, _, h = *x.shape, self.heads
# get qkv tuple:([b,n,head_num*head_dim],[...],[...])
qkv = self.to_qkv(x).chunk(3, dim = -1)
# split q,k,v from [b,n,head_num*head_dim] -> [b,head_num,n,head_dim]
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), qkv)
# transpose(k) * q / sqrt(head_dim) -> [b,head_num,n,n]
dots = torch.einsum('bhid,bhjd->bhij', q, k) * self.scale
mask_value = -torch.finfo(dots.dtype).max
# mask value: -inf
if mask is not None:
mask = F.pad(mask.flatten(1), (1, 0), value = True)
assert mask.shape[-1] == dots.shape[-1], 'mask has incorrect dimensions'
mask = mask[:, None, :] * mask[:, :, None]
dots.masked_fill_(~mask, mask_value)
del mask
# softmax normalization -> attention matrix
attn = dots.softmax(dim=-1)
# value * attention matrix -> output
out = torch.einsum('bhij,bhjd->bhid', attn, v)
# cat all output -> [b, n, head_num*head_dim]
out = rearrange(out, 'b h n d -> b n (h d)')
out = self.to_out(out)
return out
class CrossAttention(nn.Module):
def __init__(self, dim, heads, dim_head, dropout):
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.to_k = nn.Linear(dim, inner_dim , bias=False)
self.to_v = nn.Linear(dim, inner_dim , bias = False)
self.to_q = nn.Linear(dim, inner_dim, bias = False)
self.to_out = nn.Sequential(
nn.Linear(inner_dim, dim),
nn.Dropout(dropout)
) if project_out else nn.Identity()
def forward(self, x_qkv):
b, n, _, h = *x_qkv.shape, self.heads
k = self.to_k(x_qkv)
k = rearrange(k, 'b n (h d) -> b h n d', h = h)
v = self.to_v(x_qkv)
v = rearrange(v, 'b n (h d) -> b h n d', h = h)
q = self.to_q(x_qkv[:, 0].unsqueeze(1))
q = rearrange(q, 'b n (h d) -> b h n d', h = h)
dots = torch.einsum('b h i d, b h j d -> b h i j', q, k) * self.scale
attn = dots.softmax(dim=-1)
out = torch.einsum('b h i j, b h j d -> b h i d', attn, v)
out = rearrange(out, 'b h n d -> b n (h d)')
out = self.to_out(out)
return out
class Transformer(nn.Module):
def __init__(self, dim, depth, heads, dim_head, mlp_head, dropout, num_channel):
super().__init__()
self.layers = nn.ModuleList([])
for _ in range(depth):
self.layers.append(nn.ModuleList([
Residual(PreNorm(dim, Attention(dim, heads = heads, dim_head = dim_head, dropout = dropout))),
Residual(PreNorm(dim, FeedForward(dim, mlp_head, dropout = dropout)))
]))
self.skipcat = nn.ModuleList([])
for _ in range(depth-2):
self.skipcat.append(nn.Conv2d(num_channel+1, num_channel+1, [1, 2], 1, 0))
def forward(self, x, mask = None):
for attn, ff in self.layers:
x = attn(x, mask = mask)
x = ff(x)
return x
class SSTransformer(nn.Module):
def __init__(self, dim, depth, heads, dim_head, mlp_head, b_dim, b_depth, b_heads, b_dim_head, b_mlp_head, num_patches, dropout):
super().__init__()
self.layers = nn.ModuleList([])
self.k_layers = nn.ModuleList([])
self.channels_to_embedding = nn.Linear(num_patches, b_dim)
self.cls_token = nn.Parameter(torch.randn(1, 1, b_dim))
for _ in range(depth):
self.layers.append(nn.ModuleList([
Residual(PreNorm(dim, Attention(dim, heads = heads, dim_head = dim_head, dropout = dropout))),
Residual(PreNorm(dim, FeedForward(dim, mlp_head, dropout = dropout)))
]))
for _ in range(b_depth):
self.k_layers.append(nn.ModuleList([
Residual(PreNorm(b_dim, Attention(dim=b_dim, heads=b_heads, dim_head=b_dim_head, dropout = dropout))),
Residual(PreNorm(b_dim, FeedForward(b_dim, b_mlp_head, dropout = dropout)))
]))
def forward(self, x, mask = None):
for attn, ff in self.layers:
x = attn(x, mask = mask)
x = ff(x)
x = rearrange(x, 'b n d -> b d n')
x = self.channels_to_embedding(x)
b, d, n = x.shape
cls_tokens = repeat(self.cls_token, '() n d -> b n d', b = b)
x = torch.cat((cls_tokens, x), dim = 1)
for attn, ff in self.k_layers:
x = attn(x, mask = mask)
x = ff(x)
return x
class SSTransformer_pyramid(nn.Module):
def __init__(self, dim, depth, heads, dim_head, mlp_head, b_dim, b_depth, b_heads, b_dim_head, b_mlp_head, num_patches, dropout):
super().__init__()
self.layers = nn.ModuleList([])
self.k_layers = nn.ModuleList([])
self.channels_to_embedding = nn.Linear(num_patches, b_dim)
self.cls_token = nn.Parameter(torch.randn(1, 1, b_dim))
for _ in range(depth):
self.layers.append(nn.ModuleList([
Residual(PreNorm(dim, Attention(dim, heads = heads, dim_head = dim_head, dropout = dropout))),
Residual(PreNorm(dim, FeedForward(dim, mlp_head, dropout = dropout)))
]))
for _ in range(b_depth):
self.k_layers.append(nn.ModuleList([
Residual(PreNorm(b_dim, Attention(dim=b_dim, heads=b_heads, dim_head=b_dim_head, dropout = dropout))),
Residual(PreNorm(b_dim, FeedForward(b_dim, b_mlp_head, dropout = dropout)))
]))
def forward(self, x, mask = None):
for attn, ff in self.layers:
x = attn(x, mask = mask)
x = ff(x)
out_feature = x
x = rearrange(x, 'b n d -> b d n')
x = self.channels_to_embedding(x)
b, d, n = x.shape
cls_tokens = repeat(self.cls_token, '() n d -> b n d', b = b)
x = torch.cat((cls_tokens, x), dim = 1)
for attn, ff in self.k_layers:
x = attn(x, mask = mask)
x = ff(x)
return x, out_feature
class ViT(nn.Module):
def __init__(self, image_size, near_band, num_patches, num_classes, dim, depth, heads, mlp_dim, pool='cls', channel_dim=1, dim_head = 16, dropout=0., emb_dropout=0., mode='ViT'):
super().__init__()
patch_dim = image_size ** 2 * near_band
self.pos_embedding = nn.Parameter(torch.randn(1, num_patches + 1, dim))
self.patch_to_embedding = nn.Linear(channel_dim, dim)
self.cls_token = nn.Parameter(torch.randn(1, 1, dim))
self.dropout = nn.Dropout(emb_dropout)
self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim, dropout, num_patches, mode)
self.pool = pool
self.to_latent = nn.Identity()
self.mlp_head = nn.Sequential(
nn.LayerNorm(dim),
nn.Linear(dim, num_classes)
)
def forward(self, x, mask = None):
# patchs[batch, patch_num, patch_size*patch_size*c] [batch,200,145*145]
# x = rearrange(x, 'b c h w -> b c (h w)')
## embedding every patch vector to embedding size: [batch, patch_num, embedding_size]
x = self.patch_to_embedding(x) #[b,n,dim]
b, n, _ = x.shape
# add position embedding
cls_tokens = repeat(self.cls_token, '() n d -> b n d', b = b) #[b,1,dim]
x = torch.cat((cls_tokens, x), dim = 1) #[b,n+1,dim]
x += self.pos_embedding[:, :(n + 1)]
x = self.dropout(x)
# transformer: x[b,n + 1,dim] -> x[b,n + 1,dim]
x = self.transformer(x, mask)
# classification: using cls_token output
x = self.to_latent(x[:,0])
# MLP classification layer
return self.mlp_head(x)
class SSFormer_v4(nn.Module):
def __init__(self, dim, depth, heads, dim_head, mlp_head, b_dim, b_depth, b_heads, b_dim_head, b_mlp_head, num_patches, dropout, mode):
super().__init__()
self.layers = nn.ModuleList([])
self.k_layers = nn.ModuleList([])
self.channels_to_embedding = nn.Linear(num_patches, b_dim)
self.cls_token = nn.Parameter(torch.randn(1, 1, b_dim))
for _ in range(depth):
self.layers.append(nn.ModuleList([
Residual(PreNorm(dim, Attention(dim, heads = heads, dim_head = dim_head, dropout = dropout))),
Residual(PreNorm(dim, FeedForward(dim, mlp_head, dropout = dropout)))
]))
for _ in range(b_depth):
self.k_layers.append(nn.ModuleList([
Residual(PreNorm(b_dim, Attention(dim=b_dim, heads=b_heads, dim_head=b_dim_head, dropout = dropout))),
Residual(PreNorm(b_dim, FeedForward(b_dim, b_mlp_head, dropout = dropout)))
]))
self.mode = mode
def forward(self, x, c, mask = None):
for attn, ff in self.layers:
x = attn(x, mask = mask)
x = ff(x)
x = rearrange(x, 'b n d -> b d n')
x = self.channels_to_embedding(x)
b, d, n = x.shape
cls_tokens = repeat(c, '() n d -> b n d', b = b)
x = torch.cat((cls_tokens, x), dim = 1)
for attn, ff in self.k_layers:
x = attn(x, mask = mask)
x = ff(x)
return x
|