-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcgan.rb
166 lines (140 loc) · 3.6 KB
/
dcgan.rb
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
include DNN::Models
include DNN::Layers
class Generator < Model
def initialize(input_shape)
super()
@input_shape = input_shape
@l1 = Conv2D.new(32, 4, padding: true)
@l2 = Conv2D.new(32, 4, strides: 2, padding: true)
@l3 = Conv2D.new(64, 4, padding: true)
@l4 = Conv2D.new(64, 4, strides: 2, padding: true)
@l5 = Conv2D.new(128, 4, padding: true)
@l6 = Conv2DTranspose.new(64, 4, strides: 2, padding: true)
@l7 = Conv2D.new(64, 4, padding: true)
@l8 = Conv2DTranspose.new(32, 4, strides: 2, padding: true)
@l9 = Conv2D.new(32, 4, padding: true)
@l10 = Conv2D.new(32, 4, padding: true)
@l11 = Conv2D.new(3, 4, padding: true)
@bn1 = BatchNormalization.new
@bn2 = BatchNormalization.new
@bn3 = BatchNormalization.new
@bn4 = BatchNormalization.new
@bn5 = BatchNormalization.new
@bn6 = BatchNormalization.new
@bn7 = BatchNormalization.new
@bn8 = BatchNormalization.new
@bn9 = BatchNormalization.new
end
def forward(x)
input = InputLayer.new(@input_shape).(x)
x = @l1.(input)
x = @bn1.(x)
h1 = ReLU.(x)
x = @l2.(h1)
x = @bn2.(x)
x = ReLU.(x)
x = @l3.(x)
x = @bn3.(x)
h2 = ReLU.(x)
x = @l4.(x)
x = @bn4.(x)
x = ReLU.(x)
x = @l5.(x)
x = @bn5.(x)
x = ReLU.(x)
x = @l6.(x)
x = @bn6.(x)
x = ReLU.(x)
x = @l7.(x)
x = @bn7.(x)
x = ReLU.(x)
x = Concatenate.(x, h2, axis: 3)
x = @l8.(x)
x = @bn8.(x)
x = ReLU.(x)
x = @l9.(x)
x = @bn9.(x)
x = ReLU.(x)
x = Concatenate.(x, h1, axis: 3)
x = @l10.(x)
x = ReLU.(x)
x = @l11.(x)
x = Tanh.(x)
x
end
end
class Discriminator < Model
def initialize(gen_input_shape, gen_output_shape)
super()
@gen_input_shape = gen_input_shape
@gen_output_shape = gen_output_shape
@l1_1 = Conv2D.new(32, 4, padding: true)
@l1_2 = Conv2D.new(32, 4, padding: true)
@l2 = Conv2D.new(32, 4, strides: 2, padding: true)
@l3 = Conv2D.new(32, 4, padding: true)
@l4 = Conv2D.new(64, 4, strides: 2, padding: true)
@l5 = Conv2D.new(64, 4, padding: true)
@l6 = Dense.new(1024)
@l7 = Dense.new(1)
@bn1 = BatchNormalization.new
@bn2 = BatchNormalization.new
@bn3 = BatchNormalization.new
@bn4 = BatchNormalization.new
@bn5 = BatchNormalization.new
@bn6 = BatchNormalization.new
end
def forward(inputs)
input, images = *inputs
x = InputLayer.new(@gen_input_shape).(input)
x = @l1_1.(x)
x = @bn1.(x)
x1 = LeakyReLU.(x, 0.2)
x = InputLayer.new(@gen_output_shape).(images)
x = @l1_2.(x)
x = @bn2.(x)
x2 = LeakyReLU.(x, 0.2)
x = Concatenate.(x1, x2)
x = @l2.(x)
x = @bn3.(x)
x = LeakyReLU.(x, 0.2)
x = @l3.(x)
x = @bn4.(x)
x = LeakyReLU.(x, 0.2)
x = @l4.(x)
x = @bn5.(x)
x = LeakyReLU.(x, 0.2)
x = @l5.(x)
x = @bn6.(x)
x = LeakyReLU.(x, 0.2)
x = Flatten.(x)
x = @l6.(x)
x = LeakyReLU.(x, 0.2)
x = @l7.(x)
x
end
def enable_training
trainable_layers.each do |layer|
layer.trainable = true
end
end
def disable_training
trainable_layers.each do |layer|
layer.trainable = false
end
end
end
class DCGAN < Model
attr_reader :gen
attr_reader :dis
def initialize(gen, dis)
super()
@gen = gen
@dis = dis
end
def forward(input)
images = @gen.(input)
@dis.disable_training
out = @dis.([input, images])
[images, out]
end
end