-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhelper.py
98 lines (74 loc) · 2.83 KB
/
helper.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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(ResidualBlock, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.block = nn.Sequential(
GroupNorm(in_channels),
Swish(),
nn.Conv2d(in_channels, out_channels, 3, 1, 1),
GroupNorm(out_channels),
Swish(),
nn.Conv2d(out_channels, out_channels, 3, 1, 1)
)
if in_channels != out_channels:
self.channel_up = nn.Conv2d(in_channels, out_channels, 1, 1, 0)
def forward(self, x):
if self.in_channels != self.out_channels:
return self.block(x) + self.channel_up(x)
else:
return x + self.block(x)
class UpSampleBlock(nn.Module):
def __init__(self, channels):
super(UpSampleBlock, self).__init__()
self.conv = nn.Conv2d(channels, channels, 3, 1, 1)
def forward(self, x):
x = F.interpolate(x, scale_factor=2.)
return self.conv(x)
class DownSampleBlock(nn.Module):
def __init__(self, channels):
super(DownSampleBlock, self).__init__()
self.conv = nn.Conv2d(channels, channels, 3, 2, 0)
def forward(self, x):
pad = (0, 1, 0, 1)
x = F.pad(x, pad, mode="constant", value=0)
return self.conv(x)
class NonLocalBlock(nn.Module):
def __init__(self, in_channels):
super().__init__()
self.in_channels = in_channels
self.norm = GroupNorm(in_channels)
self.q = torch.nn.Conv2d(in_channels, in_channels, 1, 1, 0)
self.k = torch.nn.Conv2d(in_channels, in_channels, 1, 1, 0)
self.v = torch.nn.Conv2d(in_channels, in_channels, 1, 1, 0)
self.proj_out = torch.nn.Conv2d(in_channels, in_channels, 1, 1, 0)
def forward(self, x):
h_ = self.norm(x)
q = self.q(h_)
k = self.k(h_)
v = self.v(h_)
b, c, h, w = q.shape
q = q.reshape(b, c, h * w)
q = q.permute(0, 2, 1)
k = k.reshape(b, c, h * w)
v = v.reshape(b, c, h * w)
attn = torch.bmm(q, k)
attn = attn * (int(c) ** (-0.5))
attn = F.softmax(attn, dim=2)
attn = attn.permute(0, 2, 1)
A = torch.bmm(v, attn)
A = A.reshape(b, c, h, w)
A = self.proj_out(A)
return x + A
class GroupNorm(nn.Module):
def __init__(self, in_channels):
super(GroupNorm, self).__init__()
self.gn = nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True)
def forward(self, x):
return self.gn(x)
class Swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)