-
Notifications
You must be signed in to change notification settings - Fork 0
/
SegNet.py
165 lines (143 loc) · 6.19 KB
/
SegNet.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import torch
import torch.nn as nn
import torchvision.transforms.functional as TF
from torchsummary import summary
# since UNET uses a lot of double convolution, create one to be used
class DoubleConvEncoder(nn.Module):
def __init__(self, in_channels, out_channels):
super(DoubleConvEncoder, self).__init__()
self.conv = nn.Sequential(
# kernel size = 3, stride = 1, pad = 1 (basically same convolution)
nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False),
# normalize in response to bias = false
nn.BatchNorm2d(out_channels),
# inplace=True means that it will modify the input directly, without allocating
# any additional output. It can sometimes slightly decrease the memory usage
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.conv(x)
class DoubleConvDecoder(nn.Module):
def __init__(self, in_channels, out_channels):
super(DoubleConvDecoder, self).__init__()
self.conv = nn.Sequential(
# kernel size = 3, stride = 1, pad = 1 (basically same convolution)
nn.Conv2d(in_channels, in_channels, 3, 1, 1, bias=False),
# normalize in response to bias = false
nn.BatchNorm2d(in_channels),
# inplace=True means that it will modify the input directly, without allocating
# any additional output. It can sometimes slightly decrease the memory usage
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.conv(x)
class TripleConvDecoder(nn.Module):
def __init__(self, in_channels, out_channels):
super(TripleConvDecoder, self).__init__()
self.conv = nn.Sequential(
# kernel size = 3, stride = 1, pad = 1 (basically same convolution)
nn.Conv2d(in_channels, in_channels, 3, 1, 1, bias=False),
# normalize in response to bias = false
nn.BatchNorm2d(in_channels),
# inplace=True means that it will modify the input directly, without allocating
# any additional output. It can sometimes slightly decrease the memory usage
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, in_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.conv(x)
class TripleConvEncoder(nn.Module):
def __init__(self, in_channels, out_channels):
super(TripleConvEncoder, self).__init__()
self.conv = nn.Sequential(
# kernel size = 3, stride = 1, pad = 1 (basically same convolution)
nn.Conv2d(in_channels, in_channels, 3, 1, 1, bias=False),
# normalize in response to bias = false
nn.BatchNorm2d(in_channels),
# inplace=True means that it will modify the input directly, without allocating
# any additional output. It can sometimes slightly decrease the memory usage
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, in_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(in_channels),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.conv(x)
class SegNet(nn.Module):
#def __init__(self, in_channels=3, out_channels=1, features=[64,128,256,512]):
def __init__(self, in_channels=3, out_channels=1, features=[32,64, 128 , 256, 512]):
super(SegNet, self).__init__()
self.downs = nn.ModuleList()
self.ups = nn.ModuleList()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
# downs
for feature in features:
if feature > 64:
self.downs.append(TripleConvEncoder(in_channels, feature))
#print(feature)
else:
self.downs.append(DoubleConvEncoder(in_channels, feature))
#print(feature)
in_channels = feature
first_512 = True
# ups
for feature in reversed(features):
# upsampling
self.ups.append(nn.ConvTranspose2d(feature, feature, kernel_size=2, stride=2))
if feature == 32:
self.ups.append(DoubleConvDecoder(feature, out_channels))
#print(out_channels)
elif feature == 64:
self.ups.append(DoubleConvDecoder(feature, int(feature/2)))
#print(feature/2)
# elif feature == 512:
# self.ups.append(TripleConvDecoder(feature, feature))
#print(feature / 2)
else:
self.ups.append(TripleConvDecoder(feature, int(feature / 2)))
#print(feature)
# bottom middle
# features[-1] = feature at the end of array
self.final_conv = nn.Conv2d(features[0], out_channels,kernel_size=1)
def forward(self, x):
skip_connections = []
# down part of UNET
for down in self.downs:
# add each down
x = down(x)
#print(f"Conv: {x.size()}")
# pooling
x = self.pool(x)
#print(f"Pool: {x.size()}")
# up part of UNET
# in steps of 2 b/c if you remember, up has ConvTranspose and DoubleConv
#print("Down")
for up in self.ups:
# add upsampling
x = up(x)
#print(x.size())
return x
def test():
x = torch.randn((1, 3, 512, 512))
model = SegNet(in_channels=3, out_channels=1)
#print(model.parameters())
preds = model(x)
print(f"pred shape: {preds.shape}")
#print(x.shape)
#assert preds.shape == x.shape
if __name__ == "__main__":
test()