-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
143 lines (111 loc) · 3.75 KB
/
models.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from torch import nn
from blocks import *
class UNet(nn.Module):
def __init__(self, in_c=3, out_c=1):
super().__init__()
""" Encoder """
self.e1 = UNetEncoder(in_c, 64)
self.e2 = UNetEncoder(64, 128)
self.e3 = UNetEncoder(128, 256)
self.e4 = UNetEncoder(256, 512)
""" Bottleneck """
self.b = Conv(512, 1024)
""" UNetDecoder """
self.d1 = UNetDecoder(1024, 512)
self.d2 = UNetDecoder(512, 256)
self.d3 = UNetDecoder(256, 128)
self.d4 = UNetDecoder(128, 64)
""" Classifier """
self.outputs = nn.Conv2d(64, out_c, kernel_size=1, padding=0)
def forward(self, inputs):
"""Encoder"""
s1, p1 = self.e1(inputs)
s2, p2 = self.e2(p1)
s3, p3 = self.e3(p2)
s4, p4 = self.e4(p3)
""" Bottleneck """
b = self.b(p4)
""" UNetDecoder """
d1 = self.d1(b, s4)
d2 = self.d2(d1, s3)
d3 = self.d3(d2, s2)
d4 = self.d4(d3, s1)
outputs = self.outputs(d4)
return outputs
class SegNet(nn.Module):
def __init__(self, in_c=3, out_c=1):
super().__init__()
""" Encoder """
self.e1 = SegNetEncoder(in_c, 64)
self.e2 = SegNetEncoder(64, 128)
self.e3 = SegNetEncoder(128, 256)
self.e4 = SegNetEncoder(256, 512)
""" Bottleneck """
self.b = nn.Sequential(
nn.Conv2d(512, 1024, kernel_size=3, padding=1),
nn.BatchNorm2d(1024),
nn.ReLU(),
nn.Conv2d(1024, 1024, kernel_size=3, padding=1),
nn.BatchNorm2d(1024),
nn.ReLU(),
)
""" Decoder """
self.d4 = SegNetDecoder(1024, 512)
self.d3 = SegNetDecoder(512, 256)
self.d2 = SegNetDecoder(256, 128)
self.d1 = SegNetDecoder(128, 64)
""" Classifier """
self.outputs = nn.Conv2d(64, out_c, kernel_size=1, padding=0)
def forward(self, inputs):
"""Encoder"""
s1, indices1 = self.e1(inputs)
s2, indices2 = self.e2(s1)
s3, indices3 = self.e3(s2)
s4, indices4 = self.e4(s3)
""" Bottleneck """
b = self.b(s4)
""" Decoder """
d4 = self.d4(b, indices4)
d3 = self.d3(d4, indices3)
d2 = self.d2(d3, indices2)
d1 = self.d1(d2, indices1)
outputs = self.outputs(d1)
return outputs
class AttentionUNet(nn.Module):
def __init__(self, in_c=3, out_c=1):
super().__init__()
""" Encoder """
self.e1 = Conv(in_c, 64)
self.e2 = Conv(64, 128)
self.e3 = Conv(128, 256)
self.e4 = Conv(256, 512)
""" Bottleneck """
self.b = Conv(512, 1024)
""" Decoder """
self.d1 = UpConv(1024, 512)
self.a1 = AttentionBlock(512, 256)
self.d2 = UpConv(512, 256)
self.a2 = AttentionBlock(256, 128)
self.d3 = UpConv(256, 128)
self.a3 = AttentionBlock(128, 64)
self.d4 = UpConv(128, 64)
""" Classifier """
self.outputs = nn.Conv2d(64, out_c, kernel_size=1, padding=0)
def forward(self, x):
"""Encoder"""
s1 = self.e1(x)
s2 = self.e2(nn.functional.max_pool2d(s1, kernel_size=2, stride=2))
s3 = self.e3(nn.functional.max_pool2d(s2, kernel_size=2, stride=2))
s4 = self.e4(nn.functional.max_pool2d(s3, kernel_size=2, stride=2))
""" Bottleneck """
b = self.b(nn.functional.max_pool2d(s4, kernel_size=2, stride=2))
""" Decoder """
d1 = self.d1(b)
a1 = self.a1(d1, s4)
d2 = self.d2(a1)
a2 = self.a2(d2, s3)
d3 = self.d3(a2)
a3 = self.a3(d3, s2)
d4 = self.d4(a3)
outputs = self.outputs(d4)
return outputs