-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp_mnist.jl
289 lines (241 loc) · 8.29 KB
/
exp_mnist.jl
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
include("data.jl")
include("model.jl")
include("train.jl")
include("exp.jl")
function MNIST_pretrain_fn(m)
function train_fn(model)
ds_fn = () -> load_MNIST_ds(batch_size=50)
ds, test_ds = ds_fn();
x, y = next_batch!(ds) |> gpu;
model(x)
opt = ADAM(1e-3);
train!(model, opt, ds, train_steps=2000, print_steps=100)
model
end
maybe_train("trained/pretrain-MNIST.bson", m, train_fn)
end
function MNIST_pretrained_fn()
m = get_Madry_model()
MNIST_pretrain_fn(m)
end
function MNIST_AE_pretrain_fn(m)
ds_fn = () -> load_MNIST_ds(batch_size=50)
function train_fn(model)
ds, test_ds = ds_fn();
x, y = next_batch!(ds) |> gpu;
model(x)
opt = ADAM(1e-3);
aetrain!(model, opt, ds, train_steps=2000, print_steps=100)
end
maybe_train("trained/pretrain-MNIST-ae.bson", m, train_fn)
end
function MNIST_exp_helper(expID, lr, total_steps, λ; pretrain=false)
# This put log and saved model into MNIST subfolder
expID = "MNIST/" * expID
model_fn = () -> get_Madry_model()
ds_fn = () -> load_MNIST_ds(batch_size=50)
adv_exp_helper(expID, lr, total_steps, λ,
model_fn, ds_fn,
if pretrain MNIST_pretrain_fn else (a)->a end,
print_steps=20, save_steps=40,
test_per_steps=100, test_run_steps=20,
attack_fn=attack_PGD_k(40))
end
function MNIST_free_exp_helper(expID, lr, total_steps)
expID = "MNIST-free/" * expID
model_fn = () -> get_Madry_model()
ds_fn = () -> load_MNIST_ds(batch_size=50)
free_exp_helper(expID, lr, total_steps, 0.3,
model_fn, ds_fn,
(a)->a,
print_steps=20, save_steps=40,
test_per_steps=100, test_run_steps=20,
test_attack_fn=attack_PGD_k(40))
end
function exp_free()
MNIST_free_exp_helper("test-$(now)", 1e-3, 1000)
end
function MNIST_dyattack_exp_helper(expID, lr, attack_fn, total_steps)
# This put log and saved model into MNIST subfolder
expID = "MNIST-dyattack/" * expID
model_fn = () -> get_Madry_model()
ds_fn = () -> load_MNIST_ds(batch_size=50)
adv_exp_helper(expID, lr, total_steps, 0,
model_fn, ds_fn,
(a)->a,
print_steps=20, save_steps=40,
test_per_steps=100, test_run_steps=20,
attack_fn=attack_fn,
test_attack_fn=attack_PGD_k(40))
end
function exp_dyattack(schedule)
expID = replace("$schedule", " "=>"")
@show expID
for m in schedule
@show m
# TODO lr schedule, reduce lr should increase acc further
# TODO pretrain? Probably not.
MNIST_dyattack_exp_helper(expID, 1e-3, attack_PGD_k(m[1]), m[2])
end
end
function exp_dymix(schedule)
expID = replace("$schedule", " "=>"")
@show expID
for m in schedule
@show m
steps = m[2]
λ = m[1]
MNIST_exp_helper(expID, 1e-3, steps, λ)
end
end
function tmp()
# testing recording test_attack_fn using PGD-40, this would be the default one
# TODO tune schedule hyper-parameters
exp_dyattack((5=>400, 10=>800, 15=>1200, 20=>1600, 30=>2000, 40=>3000))
# exp_dyattack("test-$(now())")
exp_dymix((5=>1000, 4=>2000, 3=>3000, 2=>4000, 1=>5000, 0=>6000))
end
function MNIST_advae_exp_helper(expID, lr, total_steps; λ=0, γ=0, β=1, pretrain=false)
# This put log and saved model into MNIST subfolder
expID = "MNIST-advae/" * expID
ae_model_fn = CNN_AE
ds_fn = () -> load_MNIST_ds(batch_size=50)
advae_exp_helper(expID, lr, total_steps,
ae_model_fn, ds_fn,
if pretrain MNIST_AE_pretrain_fn else (a)->a end,
MNIST_pretrained_fn,
λ=λ, γ=γ, β=β,
print_steps=20, save_steps=40,
test_per_steps=100, test_run_steps=20,
attack_fn=attack_PGD_k(40))
end
# CAUTION the function names are the same for CIFAR
function exp_itadv(lr, total_steps)
expID = "f0-$lr"
MNIST_exp_helper(expID, lr, total_steps, 0)
end
function exp_pretrain(lr, total_steps)
expID = "pretrain-$lr"
MNIST_exp_helper(expID, lr, total_steps, 0, pretrain=true)
end
function exp_f1(lr, total_steps)
expID = "f1-$lr"
MNIST_exp_helper(expID, lr, total_steps, 1)
end
function exp_itadv()
# FIXME should I use learning rate decay at the same time?
#
# NOTE: the steps must devide all metric steps, especially save steps,
# otherwise it won't be saved correctly.
exp_itadv(1e-1, 600)
exp_itadv(5e-2, 600)
exp_itadv(1e-2, 600)
exp_itadv(5e-3, 600)
exp_itadv(1e-3, 600)
exp_itadv(5e-4, 1000)
exp_itadv(4e-4, 1000)
# converging from 3e-4, (HEBI: this is the border line)
exp_itadv(3e-4, 6000)
exp_itadv(1e-4, 8000)
exp_itadv(5e-5, 4000)
exp_itadv(1e-5, 3000)
end
function exp_pretrain()
exp_pretrain(1e-2, 1000)
# This does not converge, and I would expect nat acc to graduallly reduce 0.1
exp_pretrain(1e-3, 1000)
exp_pretrain(8e-4, 2000)
# TODO and this is important because it is next to border-line. FIXME It
# also does work
exp_pretrain(7e-4, 2000)
# This converges, and (HEBI: this is the border line)
exp_pretrain(6e-4, 5000)
# it is working here, but struggled
exp_pretrain(5e-4, 5000)
# TODO and I want to show how the worked one perform with a pretrained start
exp_pretrain(3e-4, 3000)
end
function exp_f1()
# TODO what about starting from pretrained?
#
# (HEBI: I hope this not to reach high accuracy)
#
# FIXME what if we just use a simple lr decay? The key point should be, no
# matter how the lr change, the accuracy should still not reach the high
# value. This might make more sense on CIFAR10 than MNIST. I'll need to stop
# here and (HEBI: move to CIFAR NOW).
#
# this defnintely does not converge, this lr may not converge even for clean
# train, I didn't try though.
exp_f1(1e-2, 2000)
# this converges, but end acc is not high, as expected
exp_f1(8e-3, 3000)
exp_f1(5e-3, 3000)
# TODO what is the borderline of fast+acc
exp_f1(3e-3, 5000)
exp_f1(2e-3, 5000)
# this should be the most promising results for this exp setting
exp_f1(1e-3, 5000)
exp_f1(5e-4, 1000)
end
function exp()
# itadv train with different learning rate
exp_itadv()
# TODO pretrain CNN with different learning rate
exp_pretrain()
# TODO nat+acc 1:1 with different learning rate
exp_f1()
# TODO mixing data with schedule
# TODO dynamic attacking strength
# FIXME I will need to monitor the acc of which attack?
end
function main()
test()
exp()
end
##############################
## AdvAE experiments
##############################
function exp_advae_f0(lr, total_steps)
expID = "f0-$lr"
MNIST_advae_exp_helper(expID, lr, total_steps, λ=0)
end
function exp_advae_pretrain(lr, total_steps)
expID = "pretrain-$lr"
MNIST_advae_exp_helper(expID, lr, total_steps, λ=0, pretrain=true)
end
function exp_advae_f1(lr, total_steps)
expID = "f1-$lr"
MNIST_advae_exp_helper(expID, lr, total_steps, λ=1)
end
function exp_advae_f01(lr, total_steps)
expID = "f01-$lr"
MNIST_advae_exp_helper(expID, lr, total_steps, λ=0, γ=1)
end
function exp_advae_test(lr, total_steps; expID="test-$(now())")
@show expID
MNIST_advae_exp_helper(expID, lr, total_steps, λ=1, γ=10, β=1)
end
function test()
exp_advae_test(2e-3, 2000, expID="test-2019-11-17T22:46:27.503")
exp_advae_test(2e-3, 1000)
@load "trained/pretrain-MNIST.bson" model
# gpu(model)(x)
exp_advae_f0(1e-3, 2000)
exp_advae_f0(1e-4, 2000)
exp_advae_pretrain(1e-4, 2000)
# this works pretty good. Two things:
#
# 1. pretraining seems to be important here. I probably want to enable
# pretrain by default, that does not hurt.
#
# 2. TODO IMPORTANT lr decay might help
# 3. what is the performance gap (both acc and time) with itadv?
exp_advae_pretrain(1e-3, 3000)
# surprisingly this does not work smoothly, it struggled until a point clean
# acc jumps
exp_advae_f1(1e-3, 2000)
exp_advae_f1(2e-3, 2000)
# rec loss
exp_advae_f01(1e-3, 2000)
end