-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_4.py
272 lines (237 loc) · 8.47 KB
/
model_4.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# self.x = x
self.block0 = nn.Sequential(
# input image 96x96
nn.Conv2d(
in_channels=3, out_channels=128, kernel_size=5, stride=1, padding=2
),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1), # parameters
# image 96x96
)
self.block1 = nn.Sequential(
# image 96x96
nn.Conv2d(
in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1), # parameters
nn.Conv2d(
in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1), # parameters
# image 96x96
)
self.info1 = nn.Sequential(
# 96x96
nn.Conv2d(
in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1), # parameters
# 96x96
)
self.block2 = nn.Sequential(
# image 96x96
nn.Conv2d(
in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1), # parameters
nn.Conv2d(
in_channels=128, out_channels=256, kernel_size=4, stride=2, padding=1
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
# image 48x48
)
self.side2 = nn.Sequential(
# image 96x96
nn.Conv2d(
in_channels=128, out_channels=256, kernel_size=2, stride=2
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
# image 48x48
)
self.block3 = nn.Sequential(
# image 48x48
nn.Conv2d(
in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
# image 48x48
nn.Conv2d(
in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
# image 48x48
)
self.info2 = nn.Sequential(
# 48x48
nn.Conv2d(
in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
)
self.side3 = nn.Sequential(
# image 48x48
nn.Conv2d(
in_channels=256, out_channels=256, kernel_size=1, stride=1
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
# image 48x48
)
self.block4 = nn.Sequential(
# image 48x48
nn.Conv2d(
in_channels=256, out_channels=512, kernel_size=4, stride=2, padding=1
),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.1), # parameters
# image 24x24
nn.Conv2d(
in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.1), # parameters
# image 24x24
)
self.side4 = nn.Sequential(
# image 48x48
nn.Conv2d(
in_channels=256, out_channels=512, kernel_size=2, stride=2
),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.1), # parameters
# image 24x24
# padding till here is all correct
)
self.block5 = nn.Sequential(
# image 24x24
nn.ConvTranspose2d(
in_channels=512, out_channels=256, kernel_size=3, stride=2, padding=1, output_padding=1
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
# image 48x48
nn.Conv2d(
in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
# image 48x48
)
self.side5 = nn.Sequential(
# image 24x24
nn.ConvTranspose2d(
in_channels=512, out_channels=256, kernel_size=2, stride=2
),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.1), # parameters
# image 48x48
)
self.block6 = nn.Sequential(
# image 48x48
nn.ConvTranspose2d(
in_channels=256, out_channels=128, kernel_size=3, stride=2, padding=1, output_padding=1
),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1), # parameters
# image 96x96
nn.Conv2d(
in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1), # parameters
# image 96x96
)
self.side6 = nn.Sequential(
# image 48x48
nn.ConvTranspose2d(
in_channels=256, out_channels=128, kernel_size=2, stride=2
),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.1), # parameters
# image 96x96
)
self.block7 = nn.Sequential(
# image 96x96
nn.Conv2d(
in_channels=128, out_channels=32, kernel_size=3, stride=1, padding=1
),
nn.BatchNorm2d(32),
nn.LeakyReLU(0.1), # parameters
# image 96x96
)
self.block8 = nn.Sequential(
# image 96x96
nn.Conv2d(
in_channels=32, out_channels=4, kernel_size=3, stride=2, padding=1
),
nn.BatchNorm2d(4),
nn.LeakyReLU(0.1), # parameters
# image 48x48
)
self.block9 = nn.Sequential(
# image 48x48
nn.PixelShuffle(2)
# image 96x96
)
self.fc = nn.Sequential(
# nn.PixelShuffle(96/89)
nn.Sigmoid()
)
def forward(self, x):
x=x.float()
out = self.block0(x) # save block0 output, 64 channels, 48x48 image
residual1 = out # save block0 output as residual, 64 channels, 48x48 image
out = self.block1(out) # run block1, 64 channels, 48x48 image
info_1 = self.info1(out) # transfer information to save level decoder
out = out + residual1 # add residuak to output
residual2 = out #
residual2 = self.side2(out)
out = self.block2(out) # 64 channels, 96x96 image
out = out + residual2 #
residual3 = out
residual3 = self.side3(residual3) # residual need to be conv in order to add
out = self.block3(out) # 128 channels, 24x24 image
info_2 = self.info2(out)
out = out + residual3
residual4 = out
residual4 = self.side4(residual4)
out = self.block4(out)
out = out + residual4
residual5 = out
residual5 = self.side5(residual5)
out = self.block5(out)
out = out + residual5 + info_2 # recieve info_2
residual6 = out
residual6 = self.side6(residual6)
out = self.block6(out)
out = out + residual6 + info_1 # recieve info_1
out = self.block7(out)
out = self.block8(out) # image 96x96, 4D tensor [batch, channel, h, w]
# batchsize = out.size()[0]
# print(batchsize)
# out = out.view(batchsize, -1)
out = self.block9(out) # image 96x96, 2D tensor [batch, features]
out = self.fc(out)
# out = nn.functional.interpolate(out, [96, 96])
# out = F.relu(out) # fully connect sigmoid, image 96x96
# print(out.size())
# out = out.view(-1, 1, 96, 96)
return out
def _initialize_weights(self):
pass