-
Notifications
You must be signed in to change notification settings - Fork 46
/
discriminator_model.py
177 lines (162 loc) · 7.64 KB
/
discriminator_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
175
176
177
import torch
import torch.nn as nn
from helper_functions.ret_image import downscale_16times
from helper_functions.Blocks import Block3x3_leakRelu, downBlock
import helper_functions.config as cfg
class D64(nn.Module):
def __init__(self, inn_channels):
super(D64, self).__init__()
self.inn_channels = inn_channels
self.df_dim = cfg.discriminatorDim
self.ef_dim = cfg.embeddingsDim
self.define_module()
def define_module(self):
ndf = self.df_dim
efg = self.ef_dim
self.img_code_s16 = downscale_16times(ndf, self.inn_channels)
self.logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
self.jointConv = Block3x3_leakRelu(ndf * 8 + efg, ndf * 8)
self.uncond_logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
def forward(self, x_var, c_code=None):
x_code = self.img_code_s16(x_var)
c_code = c_code.view(-1, self.ef_dim, 1, 1)
c_code = c_code.repeat(1, 1, 4, 4)
h_c_code = torch.cat((c_code, x_code), 1)
h_c_code = self.jointConv(h_c_code)
output = self.logits(h_c_code)
out_uncond = self.uncond_logits(x_code)
return [output.view(-1), out_uncond.view(-1)]
class D128(nn.Module):
def __init__(self, inn_channels):
super(D128, self).__init__()
self.inn_channels = inn_channels
self.df_dim = cfg.discriminatorDim
self.ef_dim = cfg.embeddingsDim
self.define_module()
def define_module(self):
ndf = self.df_dim
efg = self.ef_dim
self.img_code_s16 = downscale_16times(ndf, self.inn_channels)
self.img_code_s32 = downBlock(ndf * 8, ndf * 16)
self.img_code_s32_1 = Block3x3_leakRelu(ndf * 16, ndf * 8)
self.logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
self.jointConv = Block3x3_leakRelu(ndf * 8 + efg, ndf * 8)
self.uncond_logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
def forward(self, x_var, c_code=None):
x_code = self.img_code_s16(x_var)
x_code = self.img_code_s32(x_code)
x_code = self.img_code_s32_1(x_code)
c_code = c_code.view(-1, self.ef_dim, 1, 1)
c_code = c_code.repeat(1, 1, 4, 4)
h_c_code = torch.cat((c_code, x_code), 1)
h_c_code = self.jointConv(h_c_code)
output = self.logits(h_c_code)
out_uncond = self.uncond_logits(x_code)
return [output.view(-1), out_uncond.view(-1)]
class D256(nn.Module):
def __init__(self, inn_channels):
super(D256, self).__init__()
self.inn_channels = inn_channels
self.df_dim = cfg.discriminatorDim
self.ef_dim = cfg.embeddingsDim
self.define_module()
def define_module(self):
ndf = self.df_dim
efg = self.ef_dim
self.img_code_s16 = downscale_16times(ndf, self.inn_channels)
self.img_code_s32 = downBlock(ndf * 8, ndf * 16)
self.img_code_s64 = downBlock(ndf * 16, ndf * 32)
self.img_code_s64_1 = Block3x3_leakRelu(ndf * 32, ndf * 16)
self.img_code_s64_2 = Block3x3_leakRelu(ndf * 16, ndf * 8)
self.logits = nn.Sequential(
nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4),
nn.Sigmoid())
self.jointConv = Block3x3_leakRelu(ndf * 8 + efg, ndf * 8)
self.uncond_logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
def forward(self, x_var, c_code=None):
x_code = self.img_code_s16(x_var)
x_code = self.img_code_s32(x_code)
x_code = self.img_code_s64(x_code)
x_code = self.img_code_s64_1(x_code)
x_code = self.img_code_s64_2(x_code)
c_code = c_code.view(-1, self.ef_dim, 1, 1)
c_code = c_code.repeat(1, 1, 4, 4)
h_c_code = torch.cat((c_code, x_code), 1)
h_c_code = self.jointConv(h_c_code)
output = self.logits(h_c_code)
out_uncond = self.uncond_logits(x_code)
return [output.view(-1), out_uncond.view(-1)]
class D512(nn.Module):
def __init__(self, inn_channels):
super(D512, self).__init__()
self.inn_channels = inn_channels
self.df_dim = cfg.discriminatorDim
self.ef_dim = cfg.embeddingsDim
self.define_module()
def define_module(self):
ndf = self.df_dim
efg = self.ef_dim
self.img_code_s16 = downscale_16times(ndf, self.inn_channels)
self.img_code_s32 = downBlock(ndf * 8, ndf * 16)
self.img_code_s64 = downBlock(ndf * 16, ndf * 32)
self.img_code_s128 = downBlock(ndf * 32, ndf * 64)
self.img_code_s128_1 = Block3x3_leakRelu(ndf * 64, ndf * 32)
self.img_code_s128_2 = Block3x3_leakRelu(ndf * 32, ndf * 16)
self.img_code_s128_3 = Block3x3_leakRelu(ndf * 16, ndf * 8)
self.logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
self.jointConv = Block3x3_leakRelu(ndf * 8 + efg, ndf * 8)
self.uncond_logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
def forward(self, x_var, c_code=None):
x_code = self.img_code_s16(x_var)
x_code = self.img_code_s32(x_code)
x_code = self.img_code_s64(x_code)
x_code = self.img_code_s128(x_code)
x_code = self.img_code_s128_1(x_code)
x_code = self.img_code_s128_2(x_code)
x_code = self.img_code_s128_3(x_code)
c_code = c_code.view(-1, self.ef_dim, 1, 1)
c_code = c_code.repeat(1, 1, 4, 4)
h_c_code = torch.cat((c_code, x_code), 1)
h_c_code = self.jointConv(h_c_code)
output = self.logits(h_c_code)
out_uncond = self.uncond_logits(x_code)
return [output.view(-1), out_uncond.view(-1)]
class D1024(nn.Module):
def __init__(self, inn_channels):
super(D1024, self).__init__()
self.inn_channels = inn_channels
self.df_dim = cfg.discriminatorDim
self.ef_dim = cfg.embeddingsDim
self.define_module()
def define_module(self):
ndf = self.df_dim
efg = self.ef_dim
self.img_code_s16 = downscale_16times(ndf, self.inn_channels)
self.img_code_s32 = downBlock(ndf * 8, ndf * 16)
self.img_code_s64 = downBlock(ndf * 16, ndf * 32)
self.img_code_s128 = downBlock(ndf * 32, ndf * 64)
self.img_code_s256 = downBlock(ndf * 64, ndf * 128)
self.img_code_s256_1 = Block3x3_leakRelu(ndf * 128, ndf * 64)
self.img_code_s256_2 = Block3x3_leakRelu(ndf * 64, ndf * 32)
self.img_code_s256_3 = Block3x3_leakRelu(ndf * 32, ndf * 16)
self.img_code_s256_4 = Block3x3_leakRelu(ndf * 16, ndf * 8)
self.logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
self.jointConv = Block3x3_leakRelu(ndf * 8 + efg, ndf * 8)
self.uncond_logits = nn.Sequential(nn.Conv2d(ndf * 8, 1, kernel_size=4, stride=4), nn.Sigmoid())
def forward(self, x_var, c_code=None):
x_code = self.img_code_s16(x_var)
x_code = self.img_code_s32(x_code)
x_code = self.img_code_s64(x_code)
x_code = self.img_code_s128(x_code)
x_code = self.img_code_s256(x_code)
x_code = self.img_code_s256_1(x_code)
x_code = self.img_code_s256_2(x_code)
x_code = self.img_code_s256_3(x_code)
x_code = self.img_code_s256_4(x_code)
c_code = c_code.view(-1, self.ef_dim, 1, 1)
c_code = c_code.repeat(1, 1, 4, 4)
h_c_code = torch.cat((c_code, x_code), 1)
h_c_code = self.jointConv(h_c_code)
output = self.logits(h_c_code)
out_uncond = self.uncond_logits(x_code)
return [output.view(-1), out_uncond.view(-1)]