-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathskynet_Unet_model.py
102 lines (80 loc) · 2.86 KB
/
skynet_Unet_model.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
99
100
101
102
from torch import nn
import torch
import torch.nn.functional as F
class double_conv(nn.Module):
def __init__(self, in_ch, out_ch,bn=False):
super(double_conv, self).__init__()
self.conv= nn.Sequential(
nn.Conv2d(in_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=False),
nn.Conv2d(out_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=False),
)
def forward(self, x):
x=self.conv(x)
return x
class inputconv(nn.Module):
def __init__(self, in_ch, out_ch,bn=False):
super(inputconv, self).__init__()
self.conv = double_conv(in_ch, out_ch,bn)
def forward(self, x):
x = self.conv(x)
return x
class outputconv(nn.Module):
def __init__(self, in_ch, out_ch):
super(outputconv, self).__init__()
self.conv = nn.Conv2d(in_ch, out_ch, 1)
def forward(self, x):
x = self.conv(x)
return x
class down_layers(nn.Module):
def __init__(self, in_ch, out_ch,bn=False):
super(down_layers, self).__init__()
self.mpconv = nn.Sequential(
nn.MaxPool2d(2),
double_conv(in_ch, out_ch,bn),
)
def forward(self, x):
x = self.mpconv(x)
return x
class up_layers(nn.Module):
def __init__(self, in_ch, out_ch, bilinear=False,bn=False):
super(up_layers, self).__init__()
self.bilinear=bilinear
if self.bilinear:
self.up = nn.Sequential(nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
nn.Conv2d(in_ch,in_ch//2,1),)
else:
self.up = nn.ConvTranspose2d(in_ch, in_ch // 2, 2, stride=2)
self.conv = double_conv(in_ch, out_ch,bn)
def forward(self, x1, x2):
x1 = self.up(x1)
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, (diffX // 2, diffX - diffX // 2,diffY // 2, diffY - diffY // 2))
x = torch.cat([x2, x1], dim=1)
x = self.conv(x)
return x
class SkyNet_UNet(nn.Module):
def __init__(self,n_channels,layer_nums,features_root=64, output_channel=3, bn=False):
super(SkyNet_UNet,self).__init__()
self.inc = inputconv(n_channels, 64,bn)
self.down1 = down_layers(64, 128,bn)
self.down2 = down_layers(128, 256,bn)
self.down3 = down_layers(256, 512,bn)
self.up1 = up_layers(512, 256)
self.up2 = up_layers(256, 128)
self.up3 = up_layers(128, 64)
self.outc = outputconv(64, output_channel)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x = self.up1(x4, x3)
x = self.up2(x, x2)
x = self.up3(x, x1)
x = self.outc(x)
return x