Skip to content

Commit d63b5f9

Browse files
Code cleanup
1 parent 4d444c8 commit d63b5f9

File tree

6 files changed

+41
-44
lines changed

6 files changed

+41
-44
lines changed

figures/discogan.png

460 KB
Loading

implementations/cyclegan/models.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ def __init__(self, in_channels=3, out_channels=3, res_blocks=9):
146146
def forward(self, x):
147147
return self.model(x)
148148

149+
##############################
150+
# Discriminator
151+
##############################
152+
149153
class Discriminator(nn.Module):
150154
def __init__(self, in_channels=3):
151155
super(Discriminator, self).__init__()
@@ -158,18 +162,13 @@ def discriminator_block(in_filters, out_filters, stride, normalize):
158162
layers.append(nn.LeakyReLU(0.2, inplace=True))
159163
return layers
160164

161-
layers = []
162-
in_filters = in_channels
163-
for out_filters, stride, normalize in [ (64, 2, False),
164-
(128, 2, True),
165-
(256, 2, True),
166-
(512, 1, True)]:
167-
layers.extend(discriminator_block(in_filters, out_filters, stride, normalize))
168-
in_filters = out_filters
169-
170-
layers.append(nn.Conv2d(out_filters, 1, 3, 1, 1))
171-
172-
self.model = nn.Sequential(*layers)
165+
self.model = nn.Sequential(
166+
*discriminator_block(in_channels, 64, 2, False),
167+
*discriminator_block(64, 128, 2, True),
168+
*discriminator_block(128, 256, 2, True),
169+
*discriminator_block(256, 512, 1, True),
170+
nn.Conv2d(512, 1, 3, 1, 1)
171+
)
173172

174173
def forward(self, img):
175174
return self.model(img)

implementations/dcgan/dcgan.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,4 @@ def forward(self, img):
185185

186186
batches_done = epoch * len(dataloader) + i
187187
if batches_done % opt.sample_interval == 0:
188-
save_image(gen_imgs.data[:25], 'images/%d.png' % batches_done, nrow=5, normalize=True)
188+
save_image(gen_imgs.data[:100], 'images/%d.png' % batches_done, nrow=10, normalize=True)

implementations/dualgan/models.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ def __init__(self, in_channels=3, out_channels=3):
5454
self.down4 = UNetDown(256, 512, dropout=0.5)
5555
self.down5 = UNetDown(512, 512, dropout=0.5)
5656
self.down6 = UNetDown(512, 512, dropout=0.5)
57+
self.down7 = UNetDown(512, 512, dropout=0.5)
5758

58-
self.up1 = UNetUp(512, 512, dropout=0.5)
59+
self.up1 = UNetUp(512, 512, dropout=0.8)
5960
self.up2 = UNetUp(1024, 512, dropout=0.5)
60-
self.up3 = UNetUp(1024, 256, dropout=0.5)
61-
self.up4 = UNetUp(512, 128)
62-
self.up5 = UNetUp(256, 64)
61+
self.up3 = UNetUp(1024, 512, dropout=0.5)
62+
self.up4 = UNetUp(1024, 256, dropout=0.5)
63+
self.up5 = UNetUp(512, 128)
64+
self.up6 = UNetUp(256, 64)
6365

6466

6567
final = [ nn.Upsample(scale_factor=2),
@@ -75,14 +77,19 @@ def forward(self, x):
7577
d4 = self.down4(d3)
7678
d5 = self.down5(d4)
7779
d6 = self.down6(d5)
78-
u1 = self.up1(d6, d5)
79-
u2 = self.up2(u1, d4)
80-
u3 = self.up3(u2, d3)
81-
u4 = self.up4(u3, d2)
82-
u5 = self.up5(u4, d1)
80+
d7 = self.down7(d6)
81+
u1 = self.up1(d7, d6)
82+
u2 = self.up2(u1, d5)
83+
u3 = self.up3(u2, d4)
84+
u4 = self.up4(u3, d3)
85+
u5 = self.up5(u4, d2)
86+
u6 = self.up6(u5, d1)
8387

84-
return self.final(u5)
88+
return self.final(u6)
8589

90+
##############################
91+
# Discriminator
92+
##############################
8693

8794
class Discriminator(nn.Module):
8895
def __init__(self, img_shape):
@@ -104,12 +111,5 @@ def block(in_features, out_features, normalization=True):
104111
nn.Conv2d(512, 1, 3, 1, 1)
105112
)
106113

107-
#in_size = img_shape[1] // 2**4
108-
#self.output_layer = nn.Sequential(nn.Linear(512*in_size**2, 1))
109-
110114
def forward(self, img):
111-
# feature_repr = self.model(img)
112-
# feature_repr = feature_repr.view(feature_repr.size(0), -1)
113-
# validity = self.output_layer(feature_repr)
114-
115115
return self.model(img)

implementations/lsgan/lsgan.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def forward(self, img):
9898

9999
return validity
100100

101-
# Use binary cross-entropy loss
101+
# !!! Minimizes MSE instead of BCE
102102
adversarial_loss = torch.nn.MSELoss()
103103

104104
# Initialize generator and discriminator

implementations/pix2pix/models.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ def __init__(self, in_channels=3, out_channels=3, resblocks=9):
150150
def forward(self, x):
151151
return self.model(x)
152152

153+
##############################
154+
# Discriminator
155+
##############################
156+
153157
class Discriminator(nn.Module):
154158
def __init__(self, in_channels=3):
155159
super(Discriminator, self).__init__()
@@ -162,19 +166,13 @@ def discriminator_block(in_filters, out_filters, stride, normalize):
162166
layers.append(nn.LeakyReLU(0.2, inplace=True))
163167
return layers
164168

165-
layers = []
166-
in_filters = in_channels*2
167-
for out_filters, stride, normalize in [ (64, 2, False),
168-
(128, 2, True),
169-
(256, 2, True),
170-
(512, 2, True)]:
171-
layers.extend(discriminator_block(in_filters, out_filters, stride, normalize))
172-
in_filters = out_filters
173-
174-
# Output layer
175-
layers.append(nn.Conv2d(out_filters, 1, 3, 1, 1))
176-
177-
self.model = nn.Sequential(*layers)
169+
self.model = nn.Sequential(
170+
*discriminator_block(in_channels, 64, 2, False),
171+
*discriminator_block(64, 128, 2, True),
172+
*discriminator_block(128, 256, 2, True),
173+
*discriminator_block(256, 512, 2, True),
174+
nn.Conv2d(512, 1, 3, 1, 1)
175+
)
178176

179177
def forward(self, img_A, img_B):
180178
# Concatenate image and condition image by channels to produce input

0 commit comments

Comments
 (0)