-
Notifications
You must be signed in to change notification settings - Fork 1
/
arch.py
43 lines (40 loc) · 1.48 KB
/
arch.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
class UnetBlock(nn.Module):
def __init__(self, up_in, x_in, n_out):
super().__init__()
up_out = x_out = n_out//2
self.x_conv = nn.Conv2d(x_in, x_out, 1)
self.tr_conv = nn.ConvTranspose2d(up_in, up_out, 2, stride=2)
self.bn = nn.BatchNorm2d(n_out)
def forward(self, up_p, x_p):
up_p = self.tr_conv(up_p)
x_p = self.x_conv(x_p)
cat_p = torch.cat([up_p,x_p], dim=1)
return self.bn(F.relu(cat_p, inplace=True))
class Unet34Mod(nn.Module):
def __init__(self, out=3, f=resnet34):
super().__init__()
m_base, lr_cut = get_base(f)
self.rn = m_base
self.lr_cut = lr_cut
self.sfs = [SaveFeatures(self.rn[i]) for i in [2,4,5,6]]
self.up1 = UnetBlock(512,256,256)
self.up2 = UnetBlock(256,128,256)
self.up3 = UnetBlock(256,64,128)
self.up4 = UnetBlock(128,64,64)
self.up5 = UnetBlock(64,32,32)
self.up6 = nn.ConvTranspose2d(32, out, 1)
self.x_skip = nn.Sequential(
nn.Conv2d(out,32,1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True)
)
def forward(self,x):
x_skip = self.x_skip(x)
x = self.rn(x)
x = self.up1(x, self.sfs[3].features)
x = self.up2(x, self.sfs[2].features)
x = self.up3(x, self.sfs[1].features)
x = self.up4(x, self.sfs[0].features)
x = self.up5(x, x_skip)
x = self.up6(x)
return torch.squeeze(x)