-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist.jl
More file actions
209 lines (162 loc) · 6.57 KB
/
mnist.jl
File metadata and controls
209 lines (162 loc) · 6.57 KB
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
module mnist
using Flux.Data.MNIST, Statistics
using Base.Iterators: repeated, partition
using Flux
using wgan: WGAN, trainWGAN, DCGANCritic, DCGANGenerator, MLPCritic, MLPGenerator, sweepLatentSpace
# Bundle images together with labels and group into minibatchess
function makeMinibatch(X, idxs)
X_batch = Array{Float32}(undef, size(X[1])..., 1, length(idxs))
for i in 1:length(idxs)
X_batch[:, :, :, i] = Float32.(X[idxs[i]])
end
return X_batch
end
function make_minibatch_mlp(X, idxs)
X_batch = Array{Float32}(undef, 28 * 28, length(idxs))
for i in 1:length(idxs)
X_batch[:, i] = reshape(Float32.(X[idxs[i]]), :)
end
return X_batch
end
function DCGANCritic()
model = Chain(x->reshape(x, 28, 28, 1, :),
Conv((3, 3), 1 => 16, pad = (1, 1)),
BatchNorm(16, relu),
x->maxpool(x, (2, 2)),
# Second convolution, operating upon a 14x14 image
Conv((3, 3), 16 => 32, pad = (1, 1)),
BatchNorm(32, relu),
x->maxpool(x, (2, 2)),
# Third convolution, operating upon a 7x7 image
Conv((3, 3), 32 => 32, pad = (1, 1)),
BatchNorm(32, relu),
x->maxpool(x, (2, 2)),
# Reshape 3d tensor into a 2d one, at this point it should be (3, 3, 32, N)
# which is where we get the 288 in the `Dense` layer below:
x->reshape(x, :, size(x, 4)),
Dense(288, 1),
)
return DCGANCritic(model)
end
function DCGANGenerator(;generatorInputSize = 10)
model = Chain(Dense(generatorInputSize, 288, relu),
BatchNorm(288),
x->reshape(x, 3, 3, 32, :),
# Second convolution, operating upon a 14x14 image
ConvTranspose((3, 3), 32 => 32, stride = (2, 2), pad = (2, 2)),
BatchNorm(32, relu),
# Third convolution, operating upon a 7x7 image
ConvTranspose((3, 3), 32 => 1, σ, stride = (2, 2), pad = (2, 2)),
x->reshape(x, 28, 28, :))
return DCGANGenerator(model)
end
function DCGANCritic2()
model = Chain(x->reshape(x, 28, 28, 1, :),
Conv((5, 5), 1 => 32),
BatchNorm(32, relu),
x->maxpool(x, (2, 2)),
# Second convolution, operating upon a 14x14 image
Conv((5, 5), 32 => 64),
BatchNorm(64, relu),
x->maxpool(x, (2, 2)),
# Reshape 3d tensor into a 2d one, at this point it should be (3, 3, 32, N)
# which is where we get the 288 in the `Dense` layer below:
x->reshape(x, 1024, :),
Dense(1024, 1),
)
return DCGANCritic(model)
end
function DCGANGenerator2(;generatorInputSize = 100)
model = Chain(Dense(generatorInputSize, 128 * 7 * 7, relu),
BatchNorm(128 * 7 * 7),
x->reshape(x, 7, 7, 128, :),
# Second convolution, operating upon a 14x14 image
ConvTranspose((4, 4), 128 => 64, stride = (2, 2), pad = (1, 1)),
BatchNorm(64, relu),
# Third convolution, operating upon a 7x7 image
ConvTranspose((4, 4), 64 => 1, σ, stride = (2, 2), pad = (1, 1)),
x->reshape(x, 28, 28, :))
return DCGANGenerator(model)
end
function MLPCritic()
model = Chain(x->reshape(x, 28^2, :),
Dense(28^2, 128, relu),
Dense(128, 1))
return MLPCritic(model)
end
function MLPGenerator(;generatorInputSize = 10)
model = Chain(Dense(generatorInputSize, 128, relu),
Dense(128, 28^2, σ),
x->reshape(x, 28, 28, :))
return MLPGenerator(model)
end
function MLPGenerator2(;generatorInputSize = 10)
model = Chain(Dense(generatorInputSize, 256, relu),
Dense(256, 28^2, σ),
x->reshape(x, 28, 28, :))
return MLPGenerator(model)
end
function trainMNISTMLP()
# Load labels and images from Flux.Data.MNIST
@info("Loading data set")
train_imgs = MNIST.images()
batch_size = 32
mb_idxs = partition(1:length(train_imgs), batch_size)
train_set = [make_minibatch_mlp(train_imgs, i) for i in mb_idxs]
# Prepare test set as one giant minibatch:
test_imgs = MNIST.images(:test)
test_set = make_minibatch_mlp(test_imgs, 1:length(test_imgs))
@info("Constructing model...")
wgan = WGAN(MLPCritic(), MLPGenerator())
trainWGAN(wgan, train_set, test_set; modelName = "mnist_mlp", numSamplesToSave = 40)
end
function trainMNISTMLPCriticDCGANCritic()
# Load labels and images from Flux.Data.MNIST
@info("Loading data set")
train_imgs = MNIST.images()
batch_size = 32
mb_idxs = partition(1:length(train_imgs), batch_size)
train_set = [make_minibatch_mlp(train_imgs, i) for i in mb_idxs]
# Prepare test set as one giant minibatch:
test_imgs = MNIST.images(:test)
test_set = make_minibatch_mlp(test_imgs, 1:length(test_imgs))
generatorInputSize = 20
@info("Constructing model...")
wgan = WGAN(DCGANCritic(), MLPGenerator2(generatorInputSize = generatorInputSize); generatorInputSize = generatorInputSize, batchSize = batch_size)
trainWGAN(wgan, train_set, test_set; modelName = "mnist_mlp_dcgan", numSamplesToSave = 40)
end
function trainMNISTDCGAN()
# Load labels and images from Flux.Data.MNIST
@info("Loading data set")
train_imgs = MNIST.images()
batch_size = 32
mb_idxs = partition(1:length(train_imgs), batch_size)
train_set = [make_minibatch_mlp(train_imgs, i) for i in mb_idxs]
# Prepare test set as one giant minibatch:
test_imgs = MNIST.images(:test)
test_set = make_minibatch_mlp(test_imgs, 1:length(test_imgs))
generatorInputSize = 20
@info("Constructing model...")
wgan = WGAN(DCGANCritic2(), DCGANGenerator2(generatorInputSize = generatorInputSize); generatorInputSize = generatorInputSize, batchSize = batch_size)
trainWGAN(wgan, train_set, test_set; modelName = "mnist_dcgan_dcgan", numSamplesToSave = 40)
end
function sweepMNISTMLPCriticDCGANCritic()
@info("Constructing model...")
generatorInputSize = 20
batch_size = 1
wgan = WGAN(DCGANCritic(), MLPGenerator2(generatorInputSize = generatorInputSize); generatorInputSize = generatorInputSize, batchSize = batch_size)
sweepLatentSpace(wgan; modelName = "mnist_mlp_dcgan", stepSize = .5, imageSize = 28, epoch_idx = 100)
end
function sweepDCGANDCGANCritic()
@info("Constructing model...")
generatorInputSize = 20
batch_size = 1
wgan = WGAN(DCGANCritic(), DCGANGenerator2(generatorInputSize = generatorInputSize); generatorInputSize = generatorInputSize, batchSize = batch_size)
sweepLatentSpace(wgan; modelName = "mnist_dcgan_dcgan", stepSize = .5, imageSize = 28, epoch_idx = 100)
end
#trainMNISTMLP()
#trainMNISTDCGAN()
#trainMNISTMLPCriticDCGANCritic()
sweepMNISTMLPCriticDCGANCritic()
#sweepDCGANDCGANCritic()
end # module main