-
Notifications
You must be signed in to change notification settings - Fork 7
/
module.py
71 lines (59 loc) · 2.12 KB
/
module.py
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
import torch
from torch import nn, einsum
import torch.nn.functional as F
class conv_block(nn.Module):
def __init__(self,ch_in,ch_out,residual=False):
super(conv_block,self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(ch_in, ch_out, kernel_size=3,stride=1,padding=1,bias=True),
nn.BatchNorm2d(ch_out),
nn.ReLU(inplace=True),
nn.Conv2d(ch_out, ch_out, kernel_size=3,stride=1,padding=1,bias=True),
nn.BatchNorm2d(ch_out),
nn.ReLU(inplace=True)
)
self.residual = residual
def forward(self,x):
if self.residual:
return x + self.conv(x)
return self.conv(x)
class up_conv(nn.Module):
def __init__(self,ch_in,ch_out):
super(up_conv,self).__init__()
self.up = nn.Sequential(
nn.Upsample(scale_factor=2),
nn.Conv2d(ch_in,ch_out,kernel_size=3,stride=1,padding=1,bias=True),
nn.BatchNorm2d(ch_out),
nn.ReLU(inplace=True)
)
def forward(self,x):
x = self.up(x)
return x
class OffsetScale(nn.Module):
def __init__(self, dim, heads = 1):
super().__init__()
self.gamma = nn.Parameter(torch.ones(heads, dim))
self.beta = nn.Parameter(torch.zeros(heads, dim))
nn.init.normal_(self.gamma, std = 0.02)
def forward(self, x):
out = einsum('... d, h d -> ... h d', x, self.gamma) + self.beta
return out.unbind(dim = -2)
class Attn(nn.Module):
def __init__(self, *, dim, query_key_dim=128, expansion_factor=2.,
dropout=0.1):
super().__init__()
self.norm = nn.LayerNorm(dim)
self.dropout = nn.Dropout(dropout)
self.to_qk = nn.Sequential(
nn.Linear(dim, query_key_dim),
nn.SiLU()
)
self.offsetscale = OffsetScale(query_key_dim, heads=2)
def forward(self, x):
seq_len = x.shape[-2]
normed_x = self.norm(x)
qk = self.to_qk(normed_x)
q, k = self.offsetscale(qk)
sim = einsum('b i d, b j d -> b i j', q, k) / seq_len
attn = F.relu(sim) ** 2
return attn