-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.py
127 lines (116 loc) · 7.22 KB
/
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
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
import torch
import torch.nn as nn
class UNet(nn.Module):
def __init__(self, num):
super(UNet, self).__init__()
self.down1 = nn.Sequential(nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(64),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(64),
nn.LeakyReLU(0.01, inplace=True)
)
self.down2 = nn.Sequential(nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1, bias=True),
nn.Dropout(0.1),
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.01, inplace=True)
)
self.down3 = nn.Sequential(nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=1, bias=True),
nn.Dropout(0.1),
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(256),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(256),
nn.LeakyReLU(0.01, inplace=True)
)
self.down4 = nn.Sequential(nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1, bias=True),
nn.Dropout(0.1),
nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(512),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(512),
nn.LeakyReLU(0.01, inplace=True)
)
self.center = nn.Sequential(nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1, bias=True),
nn.Dropout(0.1),
nn.Conv2d(512, 1024, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(1024),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(1024, 1024, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(1024),
nn.LeakyReLU(0.01, inplace=True),
nn.ConvTranspose2d(1024, 512, kernel_size=2, stride=2, bias=True),
nn.Dropout(0.1)
)
self.up1 = nn.Sequential(nn.Conv2d(1024, 512, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(512),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.2),
nn.InstanceNorm2d(512),
nn.LeakyReLU(0.01, inplace=True),
nn.ConvTranspose2d(512, 256, kernel_size=2, stride=2, bias=True),
nn.Dropout(0.1)
)
self.up2 = nn.Sequential(nn.Conv2d(512, 256, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(256),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(256),
nn.LeakyReLU(0.01, inplace=True),
nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2, bias=True),
nn.Dropout(0.1)
)
self.up3 = nn.Sequential(nn.Conv2d(256, 128, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(128),
nn.LeakyReLU(0.01, inplace=True),
nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2, bias=True),
nn.Dropout(0.1)
)
self.up4 = nn.Sequential(nn.Conv2d(128, 64, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(64),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=True),
nn.Dropout(0.1),
nn.InstanceNorm2d(64),
nn.LeakyReLU(0.01, inplace=True),
nn.Conv2d(64, num, kernel_size=1, bias=True),
nn.Dropout(0.1),
nn.Softmax(dim=1)
)
def forward(self, img):
x1 = self.down1(img)
x2 = self.down2(x1)
x3 = self.down3(x2)
x4 = self.down4(x3)
x = self.center(x4)
x = self.up1(torch.cat([x, x4], dim=1))
x = self.up2(torch.cat([x, x3], dim=1))
x = self.up3(torch.cat([x, x2], dim=1))
x = self.up4(torch.cat([x, x1], dim=1))
return x