-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_Adp.py
executable file
·211 lines (156 loc) · 6.37 KB
/
model_Adp.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
## New Modified model of my own. Time: Oct. 8th
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import pdb
kernel_sizes = [4,3,3]
strides = [2,2,1]
paddings=[0,0,1]
latent_dim = 300
## Style Discriminator
class StyleDiscriminator(nn.Module):
def __init__(
self, num_gpu
):
super(StyleDiscriminator, self).__init__()
self.num_gpu = num_gpu
self.main = nn.Sequential(
nn.Conv2d(1, 32, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(32, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 64 * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64 * 2, 64 * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 4),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64 * 4, 64 * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 8),
nn.LeakyReLU(0.2, inplace=True),
nn.AdaptiveMaxPool2d(1),
nn.Conv2d(512,64,kernel_size=(1,1)),
nn.Conv2d(64,2,kernel_size=(1,1))
)
def forward(self, input):
return self.main( input )
## output one value
class Discriminator(nn.Module):
def __init__(
self, num_gpu
):
super(Discriminator, self).__init__()
self.num_gpu = num_gpu
self.main = nn.Sequential(
nn.Conv2d(1, 32, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(32, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 64 * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 2),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64 * 2, 64 * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 4),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64 * 4, 64 * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 8),
nn.LeakyReLU(0.2, inplace=True),
nn.AdaptiveMaxPool2d(1),
nn.Conv2d(512,64,kernel_size=(1,1)),
nn.Conv2d(64,8,kernel_size=(1,1)),
nn.Conv2d(8,1,kernel_size=(1,1)),
nn.Sigmoid()
)
def forward(self, input):
return self.main( input )
# Varied Input Generator
class Generator(nn.Module):
def __init__(self, num_gpu):
super(Generator, self).__init__()
self.num_gpu = num_gpu
# Input 256X256 (DEFAULT) to 128x128
self.conv1 = nn.Conv2d(1, 64, 4, 2, 1, bias=False)
self.relu1 = nn.LeakyReLU(0.2, inplace=True)
# 128x128 to 64x64
self.conv2 = nn.Conv2d(64, 64 * 2, 4, 2, 1, bias=False)
self.bn2 = nn.BatchNorm2d(64 * 2)
self.relu2 = nn.LeakyReLU(0.2, inplace=True)
# 64x64 to 32x32
self.conv3 = nn.Conv2d(64 * 2, 64 * 4, 4, 2, 1, bias=False)
self.bn3 = nn.BatchNorm2d(64 * 4)
self.relu3 = nn.LeakyReLU(0.2, inplace=True)
# 32x32 to 16x16
self.conv4 = nn.Conv2d(64 * 4, 64 * 8, 4, 2, 1, bias=False)
self.bn4 = nn.BatchNorm2d(64 * 8)
self.relu4 = nn.LeakyReLU(0.2, inplace=True)
# 16x16 to 8x8
self.conv5 = nn.Conv2d(64 * 8, 64 * 8, 4, 2, 1, bias=False)
self.bn5 = nn.BatchNorm2d(64 * 8)
self.relu5 = nn.LeakyReLU(0.2, inplace=True)
# Varied length feature inside (8x8 to 4x4)
self.conv6 = nn.Conv2d(64 * 8, 64 * 8, 4, 2, 1, bias=False)
self.bn6 = nn.BatchNorm2d(64 * 8)
self.relu6 = nn.LeakyReLU(0.2, inplace=True)
# 4x4 to 8x8
self.tconv6 = nn.ConvTranspose2d(64 * 8, 64 * 8, 4, 2, 1, bias=False)
self.tbn6 = nn.BatchNorm2d(64 * 8)
self.trelu6 = nn.ReLU(True)
# 8x8 to 16x16
self.tconv5 = nn.ConvTranspose2d(64 * 8, 64 * 8, 4, 2, 1, bias=False)
self.tbn5 = nn.BatchNorm2d(64 * 8)
self.trelu5 = nn.ReLU(True)
# 16x16 to 32x32
self.tconv4 = nn.ConvTranspose2d(64 * 8, 64 * 4, 4, 2, 1, bias=False)
self.tbn4 = nn.BatchNorm2d(64 * 4)
self.trelu4 = nn.ReLU(True)
# 32x32 to 64X64
self.tconv3 = nn.ConvTranspose2d(64 * 4, 64 * 2, 4, 2, 1, bias=False)
self.tbn3 = nn.BatchNorm2d(64 * 2)
self.trelu3 = nn.ReLU(True)
# 64x64 to 128X128
self.tconv2 = nn.ConvTranspose2d(64 * 2, 64, 4, 2, 1, bias=False)
self.tbn2 = nn.BatchNorm2d(64)
self.trelu2 = nn.ReLU(True)
# 128x128 to 256X256
self.tconv1 = nn.ConvTranspose2d( 64, 1, 4, 2, 1, bias=False)
def forward(self, input):
conv1 = self.conv1( input )
relu1 = self.relu1( conv1 )
conv2 = self.conv2( relu1 )
bn2 = self.bn2( conv2 )
relu2 = self.relu2( bn2 )
conv3 = self.conv3( relu2 )
bn3 = self.bn3( conv3 )
relu3 = self.relu3( bn3 )
conv4 = self.conv4( relu3 )
bn4 = self.bn4( conv4 )
relu4 = self.relu4( bn4 )
conv5 = self.conv5( relu4 )
bn5 = self.bn5( conv5 )
relu5 = self.relu5( bn5 )
conv6 = self.conv6( relu5 )
bn6 = self.bn6( conv6 )
relu6 = self.relu6( bn6 )
## Transposed CNN
tconv6 = self.tconv6(relu6)
tbn6 = self.tbn6( tconv6 )
trelu6 = self.trelu6(tbn6)
tconv5 = self.tconv5(trelu6)
tbn5 = self.tbn5(tconv5)
trelu5 = self.trelu5(tbn5)
tconv4 = self.tconv4(trelu5)
tbn4 = self.tbn4(tconv4)
trelu4 = self.trelu4(tbn4)
tconv3 = self.tconv3(trelu4)
tbn3 = self.tbn3(tconv3)
trelu3 = self.trelu3(tbn3)
tconv2 = self.tconv2(trelu3)
tbn2 = self.tbn2(tconv2)
trelu2 = self.trelu2(tbn2)
tconv1 = self.tconv1(trelu2)
# pdb.set_trace()
return torch.sigmoid( tconv1 ), [relu1, relu2, relu3, relu4, relu5], [trelu2, trelu3, trelu4, trelu5, trelu6]