-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
174 lines (135 loc) · 6.23 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
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
164
165
166
167
168
169
170
171
172
173
174
import torch
import torch.nn as nn
from config import Config
config = Config()
''' Each module consists of two 3 x 3 conv layers, each followed by a ReLU
non-linearity and batch normalization.
In the expansive path, modules are connected via transposed 2D convolution
layers.
The input of each module in the contracting path is also bypassed to the
output of its corresponding module in the expansive path
we double the number of feature channels at each downsampling step
We halve the number of feature channels at each upsampling step
'''
# NOTE: batch norm is up for debate
# We want batch norm if possible, but the batch size is too low to benefit
# So instead we do instancenorm
# Note: Normalization should go before ReLU
# Padding=1 because (3x3) conv leaves of 2pixels in each dimension, 1 on each side
# Do we want non-linearity between pointwise and depthwise (separable) conv?
# Do we want non-linearity after upconv?
class ConvModule(nn.Module):
def __init__(self, input_dim, output_dim):
super(ConvModule, self).__init__()
layers = [
nn.Conv2d(input_dim, output_dim, 1), # Pointwise (1x1) through all channels
nn.Conv2d(output_dim, output_dim, 3, padding=1, groups=output_dim), # Depthwise (3x3) through each channel
nn.InstanceNorm2d(output_dim),
nn.BatchNorm2d(output_dim),
nn.ReLU(),
nn.Dropout(config.drop),
nn.Conv2d(output_dim, output_dim, 1),
nn.Conv2d(output_dim, output_dim, 3, padding=1, groups=output_dim),
nn.InstanceNorm2d(output_dim),
nn.BatchNorm2d(output_dim),
nn.ReLU(),
nn.Dropout(config.drop),
]
if not config.useInstanceNorm:
layers = [layer for layer in layers if not isinstance(layer, nn.InstanceNorm2d)]
if not config.useBatchNorm:
layers = [layer for layer in layers if not isinstance(layer, nn.BatchNorm2d)]
if not config.useDropout:
layers = [layer for layer in layers if not isinstance(layer, nn.Dropout)]
self.module = nn.Sequential(*layers)
def forward(self, x):
return self.module(x)
class BaseNet(nn.Module): # 1 U-net
def __init__(self, input_channels=3,
encoder=[64, 128, 256, 512], decoder=[1024, 512, 256], output_channels=config.k):
super(BaseNet, self).__init__()
layers = [
nn.Conv2d(input_channels, 64, 3, padding=1),
nn.InstanceNorm2d(64),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Dropout(config.drop),
nn.Conv2d(64, 64, 3, padding=1),
nn.InstanceNorm2d(64),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Dropout(config.drop),
]
if not config.useInstanceNorm:
layers = [layer for layer in layers if not isinstance(layer, nn.InstanceNorm2d)]
if not config.useBatchNorm:
layers = [layer for layer in layers if not isinstance(layer, nn.BatchNorm2d)]
if not config.useDropout:
layers = [layer for layer in layers if not isinstance(layer, nn.Dropout)]
self.first_module = nn.Sequential(*layers)
self.pool = nn.MaxPool2d(2, 2)
self.enc_modules = nn.ModuleList(
[ConvModule(channels, 2*channels) for channels in encoder])
decoder_out_sizes = [int(x/2) for x in decoder]
self.dec_transpose_layers = nn.ModuleList(
[nn.ConvTranspose2d(channels, channels, 2, stride=2) for channels in decoder]) # Stride of 2 makes it right size
self.dec_modules = nn.ModuleList(
[ConvModule(3*channels_out, channels_out) for channels_out in decoder_out_sizes])
self.last_dec_transpose_layer = nn.ConvTranspose2d(128, 128, 2, stride=2)
layers = [
nn.Conv2d(128+64, 64, 3, padding=1),
nn.InstanceNorm2d(64),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Dropout(config.drop),
nn.Conv2d(64, 64, 3, padding=1),
nn.InstanceNorm2d(64),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Dropout(config.drop),
nn.Conv2d(64, output_channels, 1), # No padding on pointwise
nn.ReLU(),
]
if not config.useInstanceNorm:
layers = [layer for layer in layers if not isinstance(layer, nn.InstanceNorm2d)]
if not config.useBatchNorm:
layers = [layer for layer in layers if not isinstance(layer, nn.BatchNorm2d)]
if not config.useDropout:
layers = [layer for layer in layers if not isinstance(layer, nn.Dropout)]
self.last_module = nn.Sequential(*layers)
def forward(self, x):
x1 = self.first_module(x)
activations = [x1]
for module in self.enc_modules:
activations.append(module(self.pool(activations[-1])))
x_ = activations.pop(-1)
for conv, upconv in zip(self.dec_modules, self.dec_transpose_layers):
skip_connection = activations.pop(-1)
x_ = conv(
torch.cat((skip_connection, upconv(x_)), 1)
)
segmentations = self.last_module(
torch.cat((activations[-1], self.last_dec_transpose_layer(x_)), 1)
)
return segmentations
class WNet(nn.Module):
def __init__(self):
super(WNet, self).__init__()
self.U_encoder = BaseNet(input_channels=3, encoder=config.encoderLayerSizes,
decoder=config.decoderLayerSizes, output_channels=config.k)
self.softmax = nn.Softmax2d()
self.U_decoder = BaseNet(input_channels=config.k, encoder=config.encoderLayerSizes,
decoder=config.decoderLayerSizes, output_channels=3)
#self.sigmoid = nn.Sigmoid()
def forward_encoder(self, x):
x9 = self.U_encoder(x)
segmentations = self.softmax(x9)
return segmentations
def forward_decoder(self, segmentations):
x18 = self.U_decoder(segmentations)
reconstructions = x18 #self.sigmoid(x18)
return reconstructions
def forward(self, x): # x is (3 channels 224x224)
segmentations = self.forward_encoder(x)
x_prime = self.forward_decoder(segmentations)
return segmentations, x_prime