-
Notifications
You must be signed in to change notification settings - Fork 9
/
Blocks.py
220 lines (177 loc) · 7.18 KB
/
Blocks.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import torch
import torch.nn as nn
'''def conv_block(in_dim, out_dim, act_fn):
model = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_dim),
act_fn,
)
return model
'''
def conv(nin, nout, kernel_size=3, stride=1, padding=1, bias=False, layer=nn.Conv2d,
BN=False, ws=False, activ=nn.LeakyReLU(0.2), gainWS=2):
convlayer = layer(nin, nout, kernel_size, stride=stride, padding=padding, bias=bias)
layers = []
if ws:
layers.append(WScaleLayer(convlayer, gain=gainWS))
if BN:
layers.append(nn.BatchNorm2d(nout))
if activ is not None:
if activ == nn.PReLU:
# to avoid sharing the same parameter, activ must be set to nn.PReLU (without '()')
layers.append(activ(num_parameters=1))
else:
# if activ == nn.PReLU(), the parameter will be shared for the whole network !
layers.append(activ)
layers.insert(ws, convlayer)
return nn.Sequential(*layers)
class ResidualConv(nn.Module):
def __init__(self, nin, nout, bias=False, BN=False, ws=False, activ=nn.LeakyReLU(0.2)):
super(ResidualConv, self).__init__()
convs = [conv(nin, nout, bias=bias, BN=BN, ws=ws, activ=activ),
conv(nout, nout, bias=bias, BN=BN, ws=ws, activ=None)]
self.convs = nn.Sequential(*convs)
res = []
if nin != nout:
res.append(conv(nin, nout, kernel_size=1, padding=0, bias=False, BN=BN, ws=ws, activ=None))
self.res = nn.Sequential(*res)
activation = []
if activ is not None:
if activ == nn.PReLU:
# to avoid sharing the same parameter, activ must be set to nn.PReLU (without '()')
activation.append(activ(num_parameters=1))
else:
# if activ == nn.PReLU(), the parameter will be shared for the whole network !
activation.append(activ)
self.activation = nn.Sequential(*activation)
def forward(self, input):
out = self.convs(input)
return self.activation(out + self.res(input))
def upSampleConv_Res(nin, nout, upscale=2, bias=False, BN=False, ws=False, activ=nn.LeakyReLU(0.2)):
return nn.Sequential(
nn.Upsample(scale_factor=upscale),
ResidualConv(nin, nout, bias=bias, BN=BN, ws=ws, activ=activ)
)
def conv_block(in_dim, out_dim, act_fn, kernel_size=3, stride=1, padding=1, dilation=1 ):
model = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size = kernel_size, stride = stride, padding = padding, dilation = dilation ),
nn.BatchNorm2d(out_dim),
act_fn,
)
return model
def conv_block_1(in_dim, out_dim):
model = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=1),
nn.BatchNorm2d(out_dim),
nn.PReLU(),
)
return model
def conv_block_Asym(in_dim, out_dim, kernelSize):
model = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=[kernelSize,1], padding=tuple([2,0])),
nn.Conv2d(out_dim, out_dim, kernel_size=[1, kernelSize], padding=tuple([0,2])),
nn.BatchNorm2d(out_dim),
nn.PReLU(),
)
return model
def conv_block_Asym_Inception(in_dim, out_dim, kernel_size, padding, dilation=1):
model = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=[kernel_size,1], padding=tuple([padding*dilation,0]), dilation = (dilation,1)),
nn.BatchNorm2d(out_dim),
nn.ReLU(),
nn.Conv2d(out_dim, out_dim, kernel_size=[1, kernel_size], padding=tuple([0,padding*dilation]), dilation = (dilation,1)),
nn.BatchNorm2d(out_dim),
nn.ReLU(),
)
return model
def conv_block_Asym_Inception_WithIncreasedFeatMaps(in_dim, mid_dim, out_dim, kernel_size, padding, dilation=1):
model = nn.Sequential(
nn.Conv2d(in_dim, mid_dim, kernel_size=[kernel_size,1], padding=tuple([padding*dilation,0]), dilation = (dilation,1)),
nn.BatchNorm2d(mid_dim),
nn.ReLU(),
nn.Conv2d(mid_dim, out_dim, kernel_size=[1, kernel_size], padding=tuple([0,padding*dilation]), dilation = (dilation,1)),
nn.BatchNorm2d(out_dim),
nn.ReLU(),
)
return model
def conv_block_Asym_ERFNet(in_dim, out_dim, kernelSize, padding, drop, dilation):
model = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=[kernelSize,1], padding=tuple([padding,0]), bias = True),
nn.ReLU(),
nn.Conv2d(out_dim, out_dim, kernel_size=[1, kernelSize], padding=tuple([0,padding]), bias = True),
nn.BatchNorm2d(out_dim, eps=1e-03),
nn.ReLU(),
nn.Conv2d(in_dim, out_dim, kernel_size=[kernelSize,1], padding=tuple([padding*dilation,0]), bias=True, dilation = (dilation,1)),
nn.ReLU(),
nn.Conv2d(out_dim, out_dim, kernel_size=[1, kernelSize], padding=tuple([0,padding*dilation]), bias=True, dilation = (1, dilation)),
nn.BatchNorm2d(out_dim, eps=1e-03),
nn.Dropout2d(drop),
)
return model
def conv_block_3_3(in_dim, out_dim):
model = nn.Sequential(
nn.Conv2d(in_dim, out_dim, kernel_size=3, padding=1),
nn.BatchNorm2d(out_dim),
nn.PReLU(),
)
return model
# TODO: Change order of block: BN + Activation + Conv
def conv_decod_block(in_dim, out_dim, act_fn):
model = nn.Sequential(
nn.ConvTranspose2d(in_dim, out_dim, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(out_dim),
act_fn,
)
return model
def dilation_conv_block(in_dim,out_dim,act_fn,stride_val,dil_val):
model = nn.Sequential(
nn.Conv2d(in_dim,out_dim, kernel_size=3, stride=stride_val, padding=1, dilation=dil_val),
nn.BatchNorm2d(out_dim),
act_fn,
)
return model
def maxpool():
pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
return pool
def avrgpool05():
pool = nn.AvgPool2d(kernel_size=2, stride=2, padding=0)
return pool
def avrgpool025():
pool = nn.AvgPool2d(kernel_size=2, stride=4, padding=0)
return pool
def avrgpool0125():
pool = nn.AvgPool2d(kernel_size=2, stride=8, padding=0)
return pool
def maxpool():
pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
return pool
def maxpool_1_4():
pool = nn.MaxPool2d(kernel_size=2, stride=4, padding=0)
return pool
def maxpool_1_8():
pool = nn.MaxPool2d(kernel_size=2, stride=8, padding=0)
return pool
def maxpool_1_16():
pool = nn.MaxPool2d(kernel_size=2, stride=16, padding=0)
return pool
def maxpool_1_32():
pool = nn.MaxPool2d(kernel_size=2, stride=32, padding=0)
def conv_block_3(in_dim, out_dim, act_fn):
model = nn.Sequential(
conv_block(in_dim, out_dim, act_fn),
conv_block(out_dim, out_dim, act_fn),
nn.Conv2d(out_dim, out_dim, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_dim),
)
return model
def classificationNet(D_in):
H = 400
D_out = 1
model = torch.nn.Sequential(
torch.nn.Linear(D_in, H),
torch.nn.ReLU(),
torch.nn.Linear(H, int(H / 4)),
torch.nn.ReLU(),
torch.nn.Linear(int(H / 4), D_out)
)
return model